Skip to main content
gamedev.net
🔒 Locked

Commonly asked timer question

Started by Sponge99 May 26, 2001 at 9:18 AM 4 replies 400+ views
Original Post
Sponge99
Sponge99
Let''s see, I''m programming a game and am currently using the sleep function for all my delaying stuff, but it also freezes my WndProc from receiving/dealing with messages, such as closing the window or opening a menu. How else would I go about stopping an action without inhibiting other actions? -Sponge99
"Now watch as I run away in a womanly fashion." - Batman
Stoffel
Stoffel
Many, many, many solutions. The appropriate ones are impossible to tell from the brevity of your post, but here''s what you can look up:
WM_TIMER -- not appropriate for real-time stuff since it''s a low-priority message
multithreading - usually appropriate for real-time stuff, but a huge bag o worms.
Gillissie
Gillissie
I use the GetTickCount() function call to get the current time in milliseconds. It returns a DWORD value. In order to make sure each loop in your game takes at least 30 milliseconds, do this:

  
void GameLoop()
{
DWORD delayTime = GetTickCount();

// Do your code here


while (GetTickCount() - delayTime <= 30);
}


Beware, in the latest Windows versions, the resolution of GetTickCount() is only 10 ms, which means your delay will always fall on the 10 ms marks. You can't have a 5 ms delay for example, but usually you don't want to anyway.

Also, I think it is more common to let the main loop run as fast as possible (no delay), while delaying the particular parts of your code individually. This way your windows messages process as fast as possible also. Example, in code that would animate a sprite:

  
void Animate()
{
static DWORD animateDelay = GetTickCount();

if (GetTickCount() - animateDelay >= 30)
{
// Advance to the next frame of animation,

// then reset the timer.

animateDelay = GetTickCount();
}
}


Todd M. Gillissie

Edited by - Gillissie on May 27, 2001 11:18:05 PM

Edited by - Gillissie on May 27, 2001 11:19:00 PM
Todd M. GillissieGilligames
blakey
blakey
just like to say, ive looked how to do delays in all types of ways, but the above use of GetTickCount is the best ive used yet - very usefull
~~~~blakey~~~~
LilBudyWizer
LilBudyWizer
Peek for messages, rather than waiting, while looping to delay. If there is a message then process it, otherwise continue looping.
Keys to success: Ability, ambition and opportunity.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.