How do i Set up an accurate timer that allows pausing?
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?
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 returnstime_type Timer::getTime(){ return globalTime() - mTotalTimePaused;}
Uhm, _Phalanx_, what's not efficient about it? What else can you do? You can't really stop the system timer now, can you? ;-)
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:
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; }
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement