C++ best way to create a timer for my game
#1 Members - Reputation: 187
Posted 01 August 2012 - 02:27 PM
I'm thinking something along the lines of (roughly):
m_gameSpeed = 1000;
m_gameTime = 0;
bool secondsUp = Some code based on seconds - pulled from system time!
if(secondsUp)
m_gameTime += gameSpeed;
#3 Moderators - Reputation: 5309
Posted 01 August 2012 - 03:44 PM
One idea for correctness and performance is to only measure the elapsed time once per frame. If you call GetTickCount() all over the place, you could end up measuring the time between two different timestamps by accident. GetTickCount() called at the start of the frame will likely return a different value then GetTickCount() called just before updating the screen.
One way of writing your game is so that your game logic always uses a fixed time step. So you run a game logic step every time an accumulator exceeds a given value:
# 60 updates a second
GAME_STEP_TIME_MSS = 1000.0 / 60.0
running = true
accumulator = 0
previous = currentTimeMilliseconds()
while running
now = currentTimeMilliseconds()
elapsed = now - previous
accumulator += elapsed
# while loop here to "catch up" if we fall multiple game steps behind
while accumulator >= GAME_STEP_TIME_MS
gameLogic()
accumulator -= GAME_STEP_TIME_MS
drawGame(elapsed)
The animation etc can use an interpolated value between two game logic steps.
#6 Members - Reputation: 1907
Posted 01 August 2012 - 04:48 PM
It's also missing semicolons, parenthesis, and is using pound and not double-slash for comments -- in other words, it's psuedocode.Did you just negate the {} from here or is that valid code?
#7 Crossbones+ - Reputation: 1243
Posted 02 August 2012 - 01:53 AM
If you need more precise timing you will have to go with QueryPerformanceTimer, it's resolution is the same as that of the CPU speed.
#8 Members - Reputation: 113
Posted 02 August 2012 - 11:12 AM
[source lang="cpp"]// -- timer.h /** * A high resolution timer class. */ class Timer { private: LARGE_INTEGER mFreq; LARGE_INTEGER mStart; LARGE_INTEGER mEnd; double mElapsedTime; public: /** * Constructor. */ Timer(); /** * Destructor. */ virtual ~Timer(); /** * Marks the timer. */ void StartTimer(); /** * Gets the elapsed time in milliseconds. * @return Time in milliseconds. */ double GetTimeInMillis(); };// -- timer.cpp Timer::Timer() { QueryPerformanceFrequency ( &mFreq ); } Timer::~Timer() { } void Timer::StartTimer() { QueryPerformanceCounter ( &mStart ); } double Timer::GetTimeInMillis() { QueryPerformanceCounter ( &mEnd ); mElapsedTime = ((mEnd.QuadPart - mStart.QuadPart) * 1000.0f / mFreq.QuadPart); return mElapsedTime; }[/source]
#9 Crossbones+ - Reputation: 1243
Posted 02 August 2012 - 03:21 PM
Be aware that on multithreaded CPU's you have to bind this timer to a thread otherwise you can get inconsistancies in the sampling as it might happen on another physical core.GetTickCount() is a low resolution timer. Here, I have a class that uses HIGH RESOLUTION TIMER.
[source lang="cpp"]// -- timer.h /** * A high resolution timer class. */ class Timer { private: LARGE_INTEGER mFreq; LARGE_INTEGER mStart; LARGE_INTEGER mEnd; double mElapsedTime; public: /** * Constructor. */ Timer(); /** * Destructor. */ virtual ~Timer(); /** * Marks the timer. */ void StartTimer(); /** * Gets the elapsed time in milliseconds. * @return Time in milliseconds. */ double GetTimeInMillis(); };// -- timer.cpp Timer::Timer() { QueryPerformanceFrequency ( &mFreq ); } Timer::~Timer() { } void Timer::StartTimer() { QueryPerformanceCounter ( &mStart ); } double Timer::GetTimeInMillis() { QueryPerformanceCounter ( &mEnd ); mElapsedTime = ((mEnd.QuadPart - mStart.QuadPart) * 1000.0f / mFreq.QuadPart); return mElapsedTime; }[/source]






