You can wrap the Irrlicht Timer in an own class for which you can create as many timer instances as you need.
I can give you my code as example how i did it - maybe it helps:
timer.h:
Code: Select all
#ifndef TIMER_H
#define TIMER_H
class Timer
{
public:
Timer(irr::ITimer * irrlichtTimer_, bool startRunning_=false);
virtual ~Timer();
// in milliseconds
virtual u32 GetTime();
virtual void SetTime(u32 time_);
virtual u32 GetLastTickInMs();
virtual f32 GetLastTickInSeconds();
// unlike irr::ITimer there's no reference counting for start/stop!
virtual void Stop();
virtual void Start();
virtual bool IsStopped();
virtual void SetSpeed(f32 speed_ = 1.0f);
virtual f32 GetSpeed();
virtual void Tick();
private:
irr::ITimer * mIrrlichtTimer;
u32 mTime;
f32 mTimeRest;
u32 mLastRealTime;
f32 mSpeed;
u32 mLastTick;
int mIsRunning;
};
#endif // TIMER_H
timer.cpp:
Code: Select all
#include "timer.h"
Timer::Timer(irr::ITimer * irrlichtTimer_, bool startRunning_)
: mIrrlichtTimer(irrlichtTimer_)
, mTime(0)
, mTimeRest(0.f)
, mLastRealTime(0)
, mSpeed(1.f)
, mLastTick(0)
, mIsRunning(startRunning_)
{
}
Timer::~Timer()
{
}
u32 Timer::GetTime()
{
return mTime;
}
void Timer::SetTime(u32 time_)
{
mTime = time_;
mTimeRest = 0.f;
}
u32 Timer::GetLastTickInMs()
{
return mLastTick;
}
f32 Timer::GetLastTickInSeconds()
{
return static_cast<f32>(mLastTick) / 1000.f;
}
void Timer::Stop()
{
mIsRunning = 0;
mLastTick = 0;
}
void Timer::Start()
{
mIsRunning = 1;
if ( mIrrlichtTimer )
{
mLastRealTime = mIrrlichtTimer->getRealTime();
}
}
bool Timer::IsStopped()
{
return mIsRunning ? false : true;
}
void Timer::SetSpeed(f32 speed_)
{
mSpeed = speed_;
}
f32 Timer::GetSpeed()
{
return mSpeed;
}
void Timer::Tick()
{
if ( mLastRealTime == 0 )
{
if ( mIrrlichtTimer )
{
mLastRealTime = mIrrlichtTimer->getRealTime();
}
}
if ( mIsRunning )
{
u32 timeNow = 0;
if ( mIrrlichtTimer )
{
timeNow = mIrrlichtTimer->getRealTime();
f32 timeAdd = static_cast<float>(timeNow-mLastRealTime) * mSpeed + mTimeRest;
mLastTick = static_cast<int>(timeAdd);
mTime += mLastTick;
mTimeRest = timeAdd - mLastTick;
}
}
if ( mIrrlichtTimer )
{
mLastRealTime = mIrrlichtTimer->getRealTime();
}
}
Initialize it with the Irllicht timer and update it regularly with the Tick() function. Then you have a timer which can be independently be stopped, started, etc.
If you need to have something with fixed timesteps it can for example be done like following in the update (mGameTimer is an instance of Timer):
Code: Select all
mGameTimer->Tick();
f32 timeTickSec = mGameTimer->GetLastTickInSeconds();
static float timeRest = 0.f;
timeRest += timeTickSec;
int countSteps = 0;
while ( timeRest > 0.1f ) // 0.1 is the fixed stepsize
{
++countSteps;
timeRest -= 0.1; // stepsize
}