OpenGTA/util/timer.h

79 lines
3.1 KiB
C
Raw Permalink Normal View History

2015-12-03 00:37:37 +00:00
/************************************************************************
* Copyright (c) 2005-2007 tok@openlinux.org.uk *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
2015-12-03 00:37:02 +00:00
#ifndef OGTA_TIMER_H
#define OGTA_TIMER_H
#include <map>
#include "Singleton.h"
2018-09-23 05:57:34 +00:00
#include <functional>
2015-12-03 00:37:02 +00:00
2015-12-03 00:37:37 +00:00
// instead of SDL_GetTicks()
#define TIMER_OPENSTEER_CLOCK
#ifdef TIMER_OPENSTEER_CLOCK
#include "OpenSteer/Clock.h"
#endif
2015-12-03 00:37:02 +00:00
class Timer {
public:
2018-09-23 05:57:34 +00:00
typedef std::function<void(float)> CallbackType;
2015-12-03 00:37:02 +00:00
struct TimeEvent {
TimeEvent(const uint32_t & b, const uint32_t e, CallbackType & c);
TimeEvent(const uint32_t & b, CallbackType & c);
TimeEvent(const TimeEvent & o);
const uint32_t begin;
const uint32_t end;
CallbackType callback;
};
Timer();
~Timer();
// simulation time in ticks
inline const uint32_t & getTime() const { return simTicks; }
// time since SDL_Init
inline const uint32_t & getRealTime() const { return sdlTicks; }
void update();
2015-12-03 00:37:37 +00:00
void setSimulationRunning(bool yes);
2015-12-03 00:37:02 +00:00
inline bool getSimulationRunning() const { return simIsRunning; }
void registerCallback(bool simTime, CallbackType & c, const uint32_t & b, const uint32_t & e);
void registerCallback(bool simTime, CallbackType & c, const uint32_t & b);
void clearAllEvents();
2015-12-03 00:37:37 +00:00
#ifdef TIMER_OPENSTEER_CLOCK
OpenSteer::Clock clock;
#endif
2015-12-03 00:37:02 +00:00
private:
void checkRTEvents();
void checkSimEvents();
uint32_t sdlTicks;
uint32_t simTicks;
uint32_t delta;
bool simIsRunning;
typedef std::multimap<uint32_t, TimeEvent> RealTimeMap;
RealTimeMap realTimeEvents;
typedef RealTimeMap SimTimeMap;
SimTimeMap simTimeEvents;
};
2018-09-23 05:57:34 +00:00
typedef Loki::SingletonHolder<Timer> TimerHolder;
2015-12-03 00:37:02 +00:00
#endif