To be able to detect when the window goes out of focus, we need to tell Cocoa that the to the gameview outlet we created. Now when the window goes out of focus, the application will send that event to the view, and our timer will stop and the game will enter paused state.I like it when games can toggle between windowed and full screen mode.Cocoa is OS X-only so we break portability with other platforms by choosing NSOpen GLView.
In code, toggling screen mode is always a bit of a hassle.
In Cocoa, the timer fires just like you specified (its accuracy is good enough) but rather than having a frame rate, it’s the rate at which you drive the game. You can’t really control the frame rate anyway because OS X decides what happens on the display—and it does a nice job too, no need to worry about flicker or tearing whatsoever.
Setting up a timer sounds easy enough, but there is more to it.
As you can see, some things are a little different on the Mac, like having to enable swapping for double buffering. But rather than just that, the frame rate timer has to drive the entire game: do animations, do game mechanics, and finally tell Cocoa to redraw the screen.
If you don’t do this, you won’t see anything being displayed. To get a super consistent frame rate in SDL I would actually take into account the time passed since the last loop.