How to create a game cycle on iPhone without using NSTimer

To cleanly port my game to iPhone, I am trying to create a game loop that does not use NSTimer.

In some code examples, I noticed that if you use NSTimer, you should install it at the beginning with something like

    self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES];

where drawView will look something like this:


- (void)drawView 
{
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    mFooModel->render();
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

Using this method, mFooModel displays fine, but instead I want to create my own game loop that calls drawView instead of NSTimer calling drawView 60 times per second. I would like something like:


while(gGameState != kShutDown)
{
    [self drawView]
}

Unfortunately, when I do this, all I get is a black screen. Why is this happening? Anyway, can I implement what I am describing here?

, NSTimer, , AI- . /, , , . . ,

(, , , , )

.

+5
5

NSTimer, NSRunLoop:

static BOOL shouldContinueGameLoop;
static void RunGameLoop() {
    NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
    NSDate *destDate = [[NSDate alloc] init];
    do {
        // Create an autorelease pool
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        // Run the runloop to process OS events
        [currentRunLoop runUntilDate:destDate];
        // Your logic/draw code goes here
        // Drain the pool
        [pool drain];
        // Calculate the new date
        NSDate *newDate = [[NSDate alloc] initWithTimeInterval:1.0f/45 sinceDate:destDate];
        [destDate release];
        destDate = newDate;
    } while(shouldContinueGameLoop);
    [destDate release];
}
+2

iPhoneOS 3.1 CADisplayLink api. , .

displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(renderAndUpdate)];
[displayLink setFrameInterval:2];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

OpenGL XCode CADisplayLink, .

+21

CADisplayLink 3.1,
, "", - .

" ", .

Fabien Doom Iphone:
http://fabiensanglard.net/doomIphone/

+2

, self displayLinkWithTarget, : " ".

+2

CADisplayLink Doom for iPhone Fabien, Fabien, , . DisplayLink , DisplayLink > OS 3.1. .

0

All Articles