Rehti MMORPG  1.0.0
Rehti MMORPG is a free and open source MMORPG game.
TimerCallbackSystem.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <functional>
3 #include <map>
4 #include <mutex>
5 
6 constexpr float EPSILON = 1e-6f;
7 
12 {
13  float time; // time in seconds
14  float factor; // factor to multiply the delta time with
15  std::function<void(float dt)> callback;
16 };
17 
22 {
23 public:
28  void elapseTime(float dt);
29 
36  void addTimerCallback(int id, float time, std::function<void(float dt)> callback, float factor = 1.f);
37 
43  bool forceQuitCallback(int id);
44 
50  bool finishCallback(int id);
51 
52 private:
53  // TODO use a custom data structure with the following properties:
54  // 1. Fast iteration
55  // 2. Fast removal while iterating
56  // 3. Semi fast removal while not iterating
57  // 4. Fast insertion to the back of the structure
58  std::mutex mutexM;
59  std::map<int, CallBackTimer> timersM;
60 };
constexpr float EPSILON
Definition: TimerCallbackSystem.hpp:6
Class for handling smooth interpolation of events. A callback can be registered with a given id and t...
Definition: TimerCallbackSystem.hpp:22
std::mutex mutexM
Definition: TimerCallbackSystem.hpp:58
void elapseTime(float dt)
Elapses time by the given delta time, meaning calling the callbacks with the given delta time.
Definition: TimerCallbackSystem.cpp:3
bool forceQuitCallback(int id)
Immediately forces the removal of the callback with the given id, without finishing the remaining tim...
Definition: TimerCallbackSystem.cpp:31
std::map< int, CallBackTimer > timersM
Definition: TimerCallbackSystem.hpp:59
bool finishCallback(int id)
Forces the callback to be completed with the time left in the timer and removes it from the system.
Definition: TimerCallbackSystem.cpp:38
void addTimerCallback(int id, float time, std::function< void(float dt)> callback, float factor=1.f)
Adds a new timer callback for the given id.
Definition: TimerCallbackSystem.cpp:25
Struct representing a callback with a timer. Timer is decremented with the given deltatime.
Definition: TimerCallbackSystem.hpp:12
std::function< void(float dt)> callback
Definition: TimerCallbackSystem.hpp:15
float time
Definition: TimerCallbackSystem.hpp:13
float factor
Definition: TimerCallbackSystem.hpp:14