diff options
author | Eleni Maria Stea <elene.mst@gmail.com> | 2013-04-18 10:03:07 +0300 |
---|---|---|
committer | Eleni Maria Stea <elene.mst@gmail.com> | 2013-04-18 10:03:07 +0300 |
commit | ba5399dd00001b5abf21bd1b859bbfc39b8cbdbb (patch) | |
tree | 4afc27af0a009ed9390f222e2723723a81f455fa /libwinnie/src/sdl | |
parent | 1eb2a154bd2294c20b37ce67ab17e8a2e90f6ee1 (diff) |
new clients - license
Diffstat (limited to 'libwinnie/src/sdl')
-rw-r--r-- | libwinnie/src/sdl/event.cc | 51 | ||||
-rw-r--r-- | libwinnie/src/sdl/gfx.cc | 2 |
2 files changed, 52 insertions, 1 deletions
diff --git a/libwinnie/src/sdl/event.cc b/libwinnie/src/sdl/event.cc index 91b12b2..889e153 100644 --- a/libwinnie/src/sdl/event.cc +++ b/libwinnie/src/sdl/event.cc @@ -28,6 +28,10 @@ Author: Eleni Maria Stea <elene.mst@gmail.com> #include "mouse.h" #include "wm.h" +enum { + TIMER_EVENT = SDL_USEREVENT +}; + SDL_Event sdl_event; void process_events() { @@ -49,10 +53,57 @@ void process_events() break; case SDL_QUIT: exit(0); + + case TIMER_EVENT: + { + Window *win = (Window*)sdl_event.user.data1; + TimerFuncType func = win->get_timer_callback(); + if(func) { + func(win); + } else { + fprintf(stderr, "timer gone off but window has no timer callback!\n"); + } + } + break; + default: break; } } while(SDL_PollEvent(&sdl_event)); } +struct TimerData { + SDL_TimerID sdl_timer; + Window *win; + TimerMode mode; +}; + +static unsigned int timer_callback(unsigned int interval, void *cls) +{ + TimerData *td = (TimerData*)cls; + + SDL_Event ev; + ev.type = TIMER_EVENT; + ev.user.data1 = td->win; + SDL_PushEvent(&ev); + + if(td->mode == TIMER_ONESHOT) { + delete td; + return 0; + } + return interval; // repeat at same interval +} + +void set_window_timer(Window *win, unsigned int msec, TimerMode mode) +{ + if(!win->get_timer_callback()) { + fprintf(stderr, "trying to start a timer without having a timer callback!\n"); + return; + } + TimerData *td = new TimerData; + td->win = win; + td->mode = mode; + td->sdl_timer = SDL_AddTimer(msec, timer_callback, td); +} + #endif // WINNIE_SDL diff --git a/libwinnie/src/sdl/gfx.cc b/libwinnie/src/sdl/gfx.cc index c15cd78..edce8ba 100644 --- a/libwinnie/src/sdl/gfx.cc +++ b/libwinnie/src/sdl/gfx.cc @@ -41,7 +41,7 @@ static Graphics *gfx; bool init_gfx() { - if(SDL_Init(SDL_INIT_VIDEO) == -1) { + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { fprintf(stderr, "failed to initialize SDL\n"); return false; } |