Sign in to follow this  

Help with Timing

Recommended Posts

emperor_moore    122
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
choffstein    1090
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
matthughson    588
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

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