Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a better way to create a game loop on the iPhone other than using NSTimer?

I am programming a game on the iPhone. I am currently using NSTimer to trigger my game update/render. The problem with this is that (after profiling) I appear to lose a lot of time between updates/renders and this seems to be mostly to do with the time interval that I plug into NSTimer.

So my question is what is the best alternative to using NSTimer?

One alternative per answer please.

like image 868
Ashley Davis Avatar asked Sep 18 '08 19:09

Ashley Davis


People also ask

Do all games have a game loop?

Game loop is the main process of all the game rendering threads. It's present in all modern games. It drives input process, internal status update, rendering, AI and all the other processes. Game Loop pattern ensures that game time progresses in equal speed in all different hardware setups.

Why do you need a game loop?

If you were to only update on user input, the game would only react when the user was providing input. Other game components such as A.I game objects would not react on their own. A loop is therefore the easiest and best way of updating a game.


2 Answers

You can get a better performance with threads, try something like this:

- (void) gameLoop {     while (running)     {         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];         [self renderFrame];         [pool release];     } }  - (void) startLoop {     running = YES; #ifdef THREADED_ANIMATION     [NSThread detachNewThreadSelector:@selector(gameLoop)     toTarget:self withObject:nil]; #else     timer = [NSTimer scheduledTimerWithTimeInterval:1.0f/60     target:self selector:@selector(renderFrame) userInfo:nil repeats:YES]; #endif }  - (void) stopLoop {     [timer invalidate];     running = NO; } 

In the renderFrame method You prepare the framebuffer, draw frame and present the framebuffer on screen. (P.S. There is a great article on various types of game loops and their pros and cons.)

like image 107
zoul Avatar answered Oct 10 '22 19:10

zoul


I don't know about the iPhone in particular, but I may still be able to help: Instead of simply plugging in a fixed delay at the end of the loop, use the following:

  • Determine a refresh interval that you would be happy with and that is larger than a single pass through your main loop.
  • At the start of the loop, take a current timestamp of whatever resolution you have available and store it.
  • At the end of the loop, take another timestamp, and determine the elapsed time since the last timestamp (initialize this before the loop).
  • sleep/delay for the difference between your ideal frame time and the already elapsed time this for the frame.
  • At the next frame, you can even try to compensate for inaccuracies in the sleep interval by comparing to the timestamp at the start of the previous loop. Store the difference and add/subtract it from the sleep interval at the end of this loop (sleep/delay can go too long OR too short).

You might want to have an alert mechanism that lets you know if you're timing is too tight(i,e, if your sleep time after all the compensating is less than 0, which would mean you're taking more time to process than your frame rate allows). The effect will be that your game slows down. For extra points, you may want to simplify rendering for a while, if you detect this happening, until you have enough spare capacity again.

like image 41
Galghamon Avatar answered Oct 10 '22 18:10

Galghamon