• Advertisement
Sign in to follow this  

How do i Set up an accurate timer that allows pausing?

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

hey, i am currently writing a game where enemys are activated and come after you after a certain amount of time. I am currently using clock() in time.h to determine the amount of time elapsed but i have a problem, i dont want time spent in the menu to count towards game time, what sort of timing mechanism is fairly accurate but can be paused?

Share this post


Link to post
Share on other sites
Advertisement
Generally what I do is keep track of the time which the timer has been paused.. basically:

void Timer::pause()
{
mPauseTime = globalTime();
}

void Timer::unpause()
{
mTotalTimePaused += globalTime()-mPauseTime;
}

//globalTime() should return the time you get from the OS

//you should then provide a getTime, typetype is whatever type your OS globalTime function returns
time_type Timer::getTime()
{
return globalTime() - mTotalTimePaused;
}




Share this post


Link to post
Share on other sites
Uhm, _Phalanx_, what's not efficient about it? What else can you do? You can't really stop the system timer now, can you? ;-)

Share this post


Link to post
Share on other sites
I generally keep a game timer that is paused when the game isn't active, or when the game is paused, in a menu or otherwise not running the game logic.

It's fairly easy to do, just make the game call update() on the timer each frame it's needed. The timer will then update itself and not at all if paused.

My game loop looks something like this:


(Pseudocode)

if !game.isPaused or !game.inMenu then
leveltimer->update();
level->update( leveltimer->dT )l
end if

renderlevel();





onGamePause()
{
leveltimer->pauseTimer();
game.isPaused = true;
}

onGameUnpause()
{
leveltimer->startTimer()
game.isPaused = false;
}


Share this post


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

  • Advertisement