blocking function for timing purposes

Sage
Posts: 1,403
Joined: 2005.07
Post: #1
Some code I am dealing with has a function from the classic mac OS (toolbox I think?), that pauses for a short time..

Code:
WaitTick();            /* Wait for a system tick to go by */

so things like this are done:

Code:
    BlastScreen();
    StartSong(SongListPtr[0]);    /* Play the song */
    FadeTo(rMacPlayPal);    /* Fade in the picture */
    WaitTicksEvent(240);        /* Wait for event */
    FadeTo(rIdLogoPal);

instead of somthing like being in a main loop and updating when a specific time goes past, there too much use of tick's and tick events for me to recode it all in a game loop, so I need to find some replacement.

I want a function that I can call that will block for a set amount of time (actually until a specific date/time) but will not affect background events or cause things to slow down or any problems like that.. would usleep cause problems or be suitable?

Using Cocoa Obj-C, and C together.
Thanks in advance..

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 114
Joined: 2005.03
Post: #2
I think runUntilDate might be what you want.
Code:
[[NSRunLoop currentRunLoop] runUntilDate:<some date>];

This will process events and timers and return once the date you specified has passed. usleep will not process events. If you use timers at some point and mix it with the code above, you'll probably get into trouble, though, and event handling routines can and will be called while the calling function blocks at the line above.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #3
Would nanosleep() fit your bill? You have to calculate the delay, but it would do what you want.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #4
nanosleep() or sleep() is what I would use myself.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #5
ok Im sure nanosleep is what I require, thanks guys.
I figure I'll have to make a c function in my NSOpenGLView subclass that will call [[self openGLContext] flushBuffer], so that my other code © can call it, or is there a better way?

also would it be best to call the sleep function with the same argument every time (the one meaning 1/60th of a sec), or should I be sleeping for a variable amount depending on how long it is until the next tick would end.. counting from the start of the game?

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #6
Sleeping for a variable amount of time should be more accurate to get the time you want.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #7
Yeah, sleep for the remaining time. Otherwise the speed won't be uniform.

Also, on OSX I don't think you can sleep for less than 10ms or so, and make sure you don't sleep until everything in that tick is done. If you have VBL sync on and try to sleep before swapping buffers, the frame rate get stuttery as the two wait times fight.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Key blocking Miglu 40 15,840 Sep 13, 2010 12:39 PM
Last Post: sealfin
  Non-blocking VBL sync? TomorrowPlusX 4 4,052 Feb 28, 2005 02:06 PM
Last Post: arekkusu
  Drawing fps/Event timing using Carbon Nickolei 11 6,339 Jul 8, 2002 01:02 AM
Last Post: Jens
  Game Timing (Cocoa) Fletch 7 4,437 Jun 26, 2002 08:44 AM
Last Post: Fletch