Sign in to follow this  

Help with Timing

This topic is 4584 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

I am currently doing a space invaders clone. I have the framerate locked at 30 frames per sec. I do this basically by retrieving the current starting clock count at the beginnig of the game loop using GetTickcount(), then I wait until 33 miliseconds have past before moving onto the next frame. However I want the lasers in my ship to recharge 5 seconds after firing. How can I test to see when 5 seconds have been elapse even though I have the framerate locked. Thanx for any assistance

Share this post


Link to post
Share on other sites
A very crude way would be to store the GetTickCount() when the lasers were shot, and only allow them to reshoot when GetTickCount() of the current frame is > 5000 over the frame when they were shot. At this point, you update the GetTickCount() value for the frame they were shot in.

Pretty crude though...

Share this post


Link to post
Share on other sites
It doesn't seem as though the frame lock should have any effect on the 5 second timer. 5 seconds is 5 seconds, no matter how long you lock the frame. The only problem you may run into is with accuracy, since there is a lot of time (relativily speaking) where nothing is happening.

You could solve that with multi-threading, but that seems a bit overkill to me. I would remove the lock before I did that.

So to creat the timer, you will want to create a singleton, or a global class to keep track of time. The class will need a minimum of 3 variables; one to keep track of the current time (CT), one to keep track of the previous time (PT), and one to keep track of the time between last frame and this frame (ET).

So, every frame, you call a function that will update the timer. 3 steps:

1) CT = QueryPerformance... (or what ever function you use to get the current time)
2) ET = CT - PT
3) PT = CT

You can then keep added up the elapsed time (ET) and when it reaches > 5 seconds, fire a laser.

I know that may all be a little hard to understand. Let me know if you have any problems.

Matt Hughson

Share this post


Link to post
Share on other sites

This topic is 4584 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this