Multiple NSTimers

Discussion in 'iOS Programming' started by Gandolfmatt, May 10, 2010.

  1. Gandolfmatt macrumors member

    Joined:
    Jul 18, 2008
    #1
    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?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #3
    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
     
  4. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #4
    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
     

Share This Page