Jump to content
  • Advertisement
Sign in to follow this  
NextEpisode1

Alternative to Sleep() ?

This topic is 2864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hy , i did a small application where a sprite (player) moves around the screen using keys.

The animation is done by successive blit of images, i use Sleep() function to limit the player's speed (if i dont to that , you press a key and u are at the end of the screen in less than a second). The problem is that Sleep() not only delays the animation but also the msg processing and the window is not processing anything until the animation loop is done executing (getting from point A to point B)

Any hints how can i fix that?

Share this post


Link to post
Share on other sites
Advertisement
You're using a very basic "frame limiter", which just wastes time (sleeps) so the frame rate can't get too high. These systems are fraught with a lot of problems.

It's much better to actually measure the amount of time that each frame has taken, and either:
*Use this time value to run your 'Update' code a variable number of times per frame.
and/or
*Use this time value as a multiplier on your movement speeds -- e.g. position += speed * elapsedTime

Share this post


Link to post
Share on other sites
As Hodgman said, use the elapsed time to calculate changes in your game objects. Here is a great article about time steps(and physics) if you are interested...

http://gafferongames.com/game-physics/fix-your-timestep/

Also, adding a small sleep call to your loop isn't necessarily bad. A sleep call small enough that it won't inhibit your run time performance will help reduce cpu usage and allow other tasks to get processor time on single processor systems.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
You're using a very basic "frame limiter", which just wastes time (sleeps) so the frame rate can't get too high. These systems are fraught with a lot of problems.

It's much better to actually measure the amount of time that each frame has taken, and either:
*Use this time value to run your 'Update' code a variable number of times per frame.
and/or
*Use this time value as a multiplier on your movement speeds -- e.g. position += speed * elapsedTime


thanks , i'v implemented that , works like clockwork.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chadwell
As Hodgman said, use the elapsed time to calculate changes in your game objects. Here is a great article about time steps(and physics) if you are interested...

http://gafferongames.com/game-physics/fix-your-timestep/

Also, adding a small sleep call to your loop isn't necessarily bad. A sleep call small enough that it won't inhibit your run time performance will help reduce cpu usage and allow other tasks to get processor time on single processor systems.


thanks for the reference

Share this post


Link to post
Share on other sites
Quote:
Original post by BTownTKD
P.S. NextEpisode1:

Your signature won't compile...
I don't see any errors in his signature. I haven't actually tried compiling it though so I could have missed something.

Share this post


Link to post
Share on other sites
Quote:
Original post by BTownTKD
P.S. NextEpisode1:

Your signature won't compile...


Yes, it will...

Share this post


Link to post
Share on other sites
Quote:
Original post by boogyman19946
^ I believe you're missing a return statement :D


A return statement in main is not mandatory. There is an implicit `return 0;' at the end.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!