Yield in a Cocoa while loop

⌘-R in Chief
Posts: 1,181
Joined: 2002.05
Post: #4
If you lock everything to say.... 60fps, you can easily add a bit of timing and a usleep for the extra time.


Code:
while (playing) {
    
    NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
    if (currentTime - _timeOfLastFrame >= 1.0 / _maxNumberOfFramesPerSecond) {
        
        run a frame
    
    } else {
        double timeLeft = ((1.0 / _maxNumberOfFramesPerSecond) - (currentTime - _timeOfLastFrame));
        usleep(1000000 * timeLeft);
    }
}
Quote this message in a reply
Post Reply 


Messages In This Thread
Yield in a Cocoa while loop - Fenris - Dec 27, 2007, 08:48 AM
Yield in a Cocoa while loop - ThemsAllTook - Dec 27, 2007, 10:27 AM
Yield in a Cocoa while loop - mattz - Dec 27, 2007, 11:38 AM
Yield in a Cocoa while loop - SethWillits - Dec 27, 2007 01:21 PM
Yield in a Cocoa while loop - OneSadCookie - Dec 27, 2007, 01:55 PM
Yield in a Cocoa while loop - Fenris - Dec 27, 2007, 02:41 PM
Yield in a Cocoa while loop - OneSadCookie - Dec 27, 2007, 10:09 PM
Yield in a Cocoa while loop - Fenris - Dec 28, 2007, 02:00 AM
Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Proper game update loop in Cocoa? LoneIgadzra 13 5,742 May 30, 2008 05:49 AM
Last Post: ThemsAllTook
  Cocoa Event Loop/NSTimer revisited Fenris 6 4,633 Oct 29, 2005 11:27 PM
Last Post: maaaaark
  Game Loop in Cocoa robmcq 9 5,406 Sep 27, 2005 10:49 AM
Last Post: akb825