20 #include <SDL_timer.h>
42 static std::map<size_t, ttimer>
timers;
69 executing_id_removed =
false;
76 if(executing_id_removed) {
86 DBG_GUI_E <<
"Pushing timer event in queue.\n";
89 = timers.find(reinterpret_cast<size_t>(
id));
90 if(itor == timers.end()) {
100 data.data2 =
nullptr;
105 SDL_PushEvent(&event);
107 return itor->second.interval;
113 const std::function<
void(
size_t id)>& callback,
116 static_assert(
sizeof(
size_t) ==
sizeof(
void*),
"Pointer and size_t are not the same size");
122 }
while(
id == 0 || timers.find(
id) != timers.end());
125 timer.
sdl_id = SDL_AddTimer(
128 WRN_GUI_E <<
"Failed to create an sdl timer." << std::endl;
138 timers.insert(std::make_pair(
id, timer));
140 DBG_GUI_E <<
"Added timer " <<
id <<
".\n";
146 DBG_GUI_E <<
"Removing timer " <<
id <<
".\n";
149 if(itor == timers.end()) {
150 LOG_GUI_E <<
"Can't remove timer since it no longer exists.\n";
154 if(
id == executing_id) {
155 executing_id_removed =
true;
159 if(!SDL_RemoveTimer(itor->second.sdl_id)) {
169 DBG_GUI_E <<
"The timer is already out of the SDL timer list.\n";
177 DBG_GUI_E <<
"Executing timer " <<
id <<
".\n";
180 if(itor == timers.end()) {
181 LOG_GUI_E <<
"Can't execute timer since it no longer exists.\n";
187 itor->second.callback(
id);
190 if(!executing_id_removed && itor->second.interval == 0) {
Define the common log macros for the gui toolkit.
static bool executing_id_removed
Did somebody try to remove the timer during its execution?
bool remove_timer(const size_t id)
Removes a timer.
static std::map< size_t, ttimer > timers
The active timers.
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
A class inherited from ttext_box that displays its input as stars.
Helper to make removing a timer in a callback safe.
static size_t id
Ids for the timers.
std::map< std::string, tfilter >::iterator itor
Contains the gui2 timer routines.
static size_t executing_id
The id of the event being executed, 0 if none.
bool execute_timer(const size_t id)
Executes a timer.
std::function< void(size_t id)> callback
size_t add_timer(const Uint32 interval, const std::function< void(size_t id)> &callback, const bool repeat)
Adds a new timer.
static Uint32 timer_callback(Uint32, void *id)