21 #include <SDL2/SDL_timer.h>
33 std::function<void(std::size_t
id)>
callback{};
42 static std::map<std::size_t, timer>* ptimers =
new std::map<std::size_t, timer>();
95 DBG_GUI_E <<
"Pushing timer event in queue.";
102 auto itor =
get_timers().find(
reinterpret_cast<std::size_t
>(
id));
106 result = itor->second.interval.count();
113 event.user.data1 =
id;
114 event.user.data2 =
nullptr;
116 SDL_PushEvent(&event);
123 std::size_t
add_timer(
const std::chrono::milliseconds& interval,
124 const std::function<
void(std::size_t
id)>& callback,
127 static_assert(
sizeof(std::size_t) ==
sizeof(
void*),
"Pointer and std::size_t are not the same size");
144 WRN_GUI_E <<
"Failed to create an sdl timer.";
166 DBG_GUI_E <<
"Removing timer " <<
id <<
".";
172 LOG_GUI_E <<
"Can't remove timer since it no longer exists.";
181 if(!SDL_RemoveTimer(itor->second.sdl_id)) {
191 DBG_GUI_E <<
"The timer is already out of the SDL timer list.";
199 DBG_GUI_E <<
"Executing timer " <<
id <<
".";
201 std::function<void(
size_t)> callback =
nullptr;
207 LOG_GUI_E <<
"Can't execute timer since it no longer exists.";
211 callback = itor->second.callback;
213 if(itor->second.interval == std::chrono::milliseconds{0}) {
Helper to make removing a timer in a callback safe.
Define the common log macros for the gui toolkit.
std::string id
Text to match against addon_info.tags()
static std::size_t executing_id
The id of the event being executed, 0 if none.
static bool executing_id_removed
Did somebody try to remove the timer during its execution?
std::size_t add_timer(const std::chrono::milliseconds &interval, const std::function< void(std::size_t id)> &callback, const bool repeat)
Adds a new timer.
static std::size_t next_timer_id
Ids for the timers.
static uint32_t timer_callback(uint32_t, void *id)
bool remove_timer(const std::size_t id)
Removes a timer.
bool execute_timer(const std::size_t id)
Executes a timer.
static std::map< std::size_t, timer > & get_timers()
The active timers.
std::function< void(std::size_t id)> callback
std::chrono::milliseconds interval
Contains the gui2 timer routines.