The right way to feed Main Loop in Cocoa

I am writing a game that currently works on both Windows and Mac OS X. My main game loop looks like this:

while(running) { ProcessOSMessages(); // Using Peek/Translate message in Win32 // and nextEventMatchingMask in Cocoa GameUpdate(); GameRender(); } 

This is clearly a little simplified, but that's the point. On Windows, where I have full control of the application, it works great. Unfortunately, Apple has its own way of doing something in Cocoa apps.

When I first tried to implement my main loop in Cocoa, I couldn't figure out where to put it, so I created my own NSApplication for this post . I threw my GameFrame() into my run function and everything worked correctly.

However, it does not seem to me that this is the โ€œrightโ€ way to do this. I would like to play well in the Apple ecosystem, rather than trying to hack into a solution that works.

This apple article describes an old way to do this using NSTimer and a โ€œnewโ€ way to do it using CVDisplayLink . I plugged in the CVDisplayLink version, but it just feels .... weird. I do not like that my game is controlled by the display, and not vice versa.

Am I using only two CVDisplayLink parameters or overwrite my own NSApplication ? None of these solutions seem completely correct.

+4
source share
2 answers

I am curious to see if anyone who really did this wants to weigh, but here is my understanding:

Apple is pushing the CVDisplayLink solution by CVDisplayLink in the main thread, which uses -nextEventMatchingMask:untilDate:inMode:dequeue: because I think it provides better responsiveness for user interface controls. This may not be true for full-screen games. (Note: you donโ€™t need to replace NSApplication with this form of the game loop.) I think the main potential problem with using CVDisplayLink is that it will only start one frame in advance, and it makes this definition early, which is even stronger. than vertical sync. On the plus side, this can improve latency.

Other solutions include untying rendering from the game logic and periodic game logic in the main thread and rendering in the CVDisplayLink stream. I would probably recommend this, however, if you run into problems with the game-driven paradigm.

+2
source

You do not have to create your own NSApplication-based class or use CVDisplayLink to get around the fact that the runloop application is hidden from you in Cocoa.

You can simply create a thread and start your loop cycle instead.

For what it's worth, I'm just using CVDisplayLink.

+1
source

Source: https://habr.com/ru/post/1403360/


All Articles