Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Gandolfmatt

macrumors member
Original poster
Jul 18, 2008
46
0
Hey, I have one NSTimer for my game's logic and the other for drawing.
The game logic is set to run at 20fps and the drawing timer is set for 40fps. I'm doing this to make it so that drawing is smooth even if the logic is processing something...

Yet it appears as if those two timers don't work separately. My drawing timer waits on the game logic timer. How come? Can I fix this?


Thanks,
-Gan
P.S. Should I be using an NSThread?
 
NSTimers are scheduled on the main NSRunloop (unless you set them on a runloop you own/control). So yes, they will wait for each other.

If you want the to be independent then yes, use NSThreads but they you will have to ensure safe access to shared data across the threads.
 
I've found some code on the net but it doesn't appear to be working:
Code:
- (void) RunGameLoop {
    NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
    NSDate *destDate = [[NSDate alloc] init];
    while (shouldContinueGameLoop) {
        // 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
		[self mainGameLoop];
		[self mainDrawLoop];
		while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01f, FALSE) == kCFRunLoopRunHandledSource);
        // Drain the pool
        [pool drain];
        // Calculate the new date
        NSDate *newDate = [[NSDate alloc] initWithTimeInterval:1.0f/45 sinceDate:destDate];
        [destDate release];
        destDate = newDate;
    }
    [destDate release];
}
Anyone know how to make a runloop that continually runs at 45 fps and keeps touch input going?


-Gan
 
Hey guys, found it:
Code:
- (void) runAnimation
{ 
  while (animating) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, (1.0/250.0), FALSE) == kCFRunLoopRunHandledSource);
    [self drawView:nil];
    [pool release];
  }
}

- (void) startAnimation
{
  animating = TRUE;
  [self performSelectorOnMainThread:@selector(runAnimation) withObject:nil waitUntilDone:NO];
}

- (void)stopAnimation
{
  animating = FALSE;
}
It's fantastic stuff. So much more reliable than NSTimers that tend to jump high then low to get an average of the correct fps.


-Gan
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.