How do you manage events and game loop?

Member
Posts: 249
Joined: 2008.10
Post: #1
Hi friends!

I have some problems in my project and I hope you can help.
Thanks in advance.

Until now, my current project uses SDL, but likely and eventually, I will remove it.
I really like the way events are managed and game loop work, so I have some questions.
Note: I use C++/OpenGL/Box2D (which requires a constant update rate)

* Do you use a timer (the same as opengl cocoa template)?

* Otherwise, how do you avoid rendering as fast as you can to avoid wasting battery? (it seems using sleep has a poor granularity on the iPhone, that's my experience using it with SDL)

* Do you queue iPhone events to process them before updating and rendering your game? Cocoa OpenGL template process events when they are fired, but is not this a problem if in that moment I am rendering my scene or box2d updating the world? Can I queue events (multitouch and accelerometer)?

* Do you use a classical game loop like this one?:
while(1) {
process IO
update
render
maybe sleep to avoid wasting battery
}

* What kind of game loop and event processing use Rolando/Bio Surfer/iBlast Moki/Spy Bot... (I know they use BOX2D)

*Do they use a coupled rendering/updating loop?

Well, too much questions. I know there are some threads and some info on the net, but I really have these questions not clear.

Thank you very much for your help, I appreciate it a lot Smile
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
I moved away from timers because I could get significantly higher framerates from not using them.

My main loop in the base Gamestate class looks like so:
Code:
    [self sync];
    
    // pump events
    for(; CFRunLoopRunInMode(kCFRunLoopDefaultMode, runLoopDelay, TRUE) == kCFRunLoopRunHandledSource;){}
    
    while(self.needsUpdate)
        [self update];
    
    if(self.needsRedraw){
        [self draw];
        [EAGLView swapCurrentContext];
    }

sync - used to synchronize the game loop to the current time (implement waits or whatever)
pump the events - Use a for/while loop to pump events in the run loop's queue
update - run the update method as many times as needed. This allows you to implement a fixed step update.
draw - draw and sync to VBL

The runLoopDelay parameter is the sticking point. Normally you would just use 0, but on the iPhone you get very inconsistent results. I'm using a 3ms delay and that seems to make both OS 2.x and 3.x happy.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #3
Skorche Wrote:I moved away from timers because I could get significantly higher framerates from not using them.

My main loop in the base Gamestate class looks like so:
Code:
    [self sync];
    
    // pump events
    for(; CFRunLoopRunInMode(kCFRunLoopDefaultMode, runLoopDelay, TRUE) == kCFRunLoopRunHandledSource;){}
    
    while(self.needsUpdate)
        [self update];
    
    if(self.needsRedraw){
        [self draw];
        [EAGLView swapCurrentContext];
    }

sync - used to synchronize the game loop to the current time (implement waits or whatever)
pump the events - Use a for/while loop to pump events in the run loop's queue
update - run the update method as many times as needed. This allows you to implement a fixed step update.
draw - draw and sync to VBL

The runLoopDelay parameter is the sticking point. Normally you would just use 0, but on the iPhone you get very inconsistent results. I'm using a 3ms delay and that seems to make both OS 2.x and 3.x happy.

Skorche, how do you pump events using your squema? (btw, my obj-c skills are poor) and when you say sync, do you mean sleep or something like that?

Thanks a lot for your help and time.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Game loop and performance wonza 12 5,768 Dec 9, 2009 11:02 PM
Last Post: AnotherJake
  Where to put my game loop? davidB 2 2,315 Jan 11, 2009 08:04 AM
Last Post: davidB