History: Assaf Muller's Timer Manager
Source of version: 3 (current)
Copy to clipboard
Timer_Manager from Assaf Muller on the 17th of June, 2007.
{maketoc}
This wiki page will show a Timer class & Timer Factory class duo to help you with all things timing!
Notice that this manager is under no license. Use it however you wish.
!!Timer Class Header
{CODE(wrap="1", colors="c++")}#ifndef TIMER_H
#define TIMER_H
#include <string>
#include "Ogre.h"
using namespace std;
using namespace Ogre;
class TimerObject {
private:
Real mTime;
bool mDone;
public:
TimerObject();
TimerObject(Real time);
~TimerObject();
Real elpased();
bool done();
void reset();
void reset(Real time);
Timer mTimer;
};
#endif{CODE}
!!Timer Class Implementation
{CODE(wrap="1", colors="c++")}
#include "Timer.h"
TimerObject::TimerObject()
{
TimerObject(0.0);
}
TimerObject::TimerObject (Real time = 0) : mTime(time)
{
mDone = false;
mTimer.reset();
}
TimerObject::~TimerObject()
{
}
Real TimerObject::elpased()
{
return mTimer.getMilliseconds() * 0.001;
}
bool TimerObject::done()
{
if(!mDone) // If not done
if(mTimer.getMilliseconds() * 0.001 > mTime)
mDone = true;
return mDone;
}
void TimerObject::reset()
{
mDone = false;
mTimer.reset();
}
void TimerObject::reset(Real time)
{
mTime = time;
mDone = false;
mTimer.reset();
}
{CODE}
!!Timer Factory Header
{CODE(wrap="1", colors="c++")}
#ifndef TIMER_FACTORY_H
#define TIMER_FACTORY_H
#include <OgreSingleton.h>
#include "Timer.h"
#include <map>
using namespace std;
class TimerFactory : public Ogre::Singleton<TimerFactory> {
private:
map<string, TimerObject> mTimers;
public:
void create(string name, Real time = 0);
void destroy(string name);
bool exists(string name);
TimerObject& operator [] (string name);
~TimerFactory();
};
template <> TimerFactory* Ogre::Singleton<TimerFactory>::ms_Singleton = 0;
#endif
{CODE}
!!Timer Factory Implementation
{CODE(wrap="1", colors="c++")}
#include "TimerFactory.h"
void TimerFactory::create(string name, Real time)
{
if(mTimers.find(name) == mTimers.end())
mTimers[name] = TimerObject(time);
}
void TimerFactory::destroy(string name)
{
map<string, TimerObject>::iterator i = mTimers.find(name);
if(i != mTimers.end())
mTimers.erase(i);
}
bool TimerFactory::exists(string name)
{
map<string, TimerObject>::iterator i = mTimers.find(name);
if(i != mTimers.end())
return true;
return false;
}
TimerObject& TimerFactory::operator [] (string name)
{
if(mTimers.find(name) != mTimers.end())
return mTimers[name];
else
throw Ogre::Exception(Ogre::Exception::ERR_INVALIDPARAMS,
"Tried to access a non-existant timer!", "TimerFactory::operator []");
}
TimerFactory::~TimerFactory()
{
map<string, TimerObject>::iterator i = mTimers.begin();
while(!mTimers.empty())
{
mTimers.erase(mTimers.begin());
}
}
{CODE}