How make a game loop on the iPhone without using NSTimer

前端 未结 5 1545
挽巷
挽巷 2021-01-31 23:53

In order to cleanly port my game to the iPhone, I\'m trying to make a game loop that doesn\'t use NSTimer.

I noticed in some sample code that, if using NSTimer, you\'d s

相关标签:
5条回答
  • 2021-02-01 00:13

    Regarding CADisplayLink and the Doom for iPhone article by Fabien, I emailed Fabien and I think the best option is subjective. Performance-wise DisplayLink and triple buffering should be the same, but DisplayLink is only available on > OS 3.1. So that should be your determining factor.

    0 讨论(0)
  • 2021-02-01 00:20

    Another option with iPhoneOS 3.1 is to use the new CADisplayLink api. This will call the selector you specify when the screen contents needs to be updated.

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

    The new OpenGL project template in XCode also use the CADisplayLink if you need some more example code.

    0 讨论(0)
  • 2021-02-01 00:21

    If you don't wish to use NSTimer you can try running the NSRunLoop manually:

    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];
    }
    
    0 讨论(0)
  • 2021-02-01 00:27

    While using CADisplayLink is a really good alternative for 3.1 based games,
    anything using "Timer" is a really bad idea.

    The best approach is the good old "tripple buffering" to decouple the GPU work.

    Fabien has a very good explanation in his Doom Iphone review:
    http://fabiensanglard.net/doomIphone/

    0 讨论(0)
  • 2021-02-01 00:33

    Take care using self as a target for displayLinkWithTarget, the manual says "The newly constructed display link retains the target". Martin

    0 讨论(0)
提交回复
热议问题