SDL++
C++wrapperfortheSDLlibrary.
|
00001 00021 #ifndef SDL_DEVICES_TIMER_H 00022 #define SDL_DEVICES_TIMER_H 00023 00024 #include <stdexcept> 00025 #include <string> 00026 #include <iostream> 00027 00028 #include <SDL.h> 00029 00030 #include "sdlpp/subsystem/Subsystem.h" 00031 00032 namespace sdl { 00033 namespace devices { 00034 using namespace std; 00035 using namespace misc; 00036 00041 class Timer { 00042 public: 00052 typedef unsigned int (*Callback) (unsigned int interval, void* param); 00053 00061 Timer (unsigned int interval, Callback callback, void* param) 00062 : id_ (add (interval, callback, param)) { 00063 }; 00064 00068 ~Timer () { 00069 //log error if unable to remove timer. 00070 if (SDL_RemoveTimer (id_) == SDL_FALSE) 00071 cout << "Failed to remove timer " << id_ << endl; 00072 }; 00073 00074 private: 00080 Timer (const Timer& rhs); 00081 00089 Timer& operator= (const Timer& rhs); 00090 00102 SDL_TimerID add (unsigned int interval, Callback callback, void* param) { 00103 subsystem::Timer::instance (); 00104 SDL_TimerID id = SDL_AddTimer (interval, callback, param); 00105 if (id == NULL) 00106 throw runtime_error ("Failed to add timer."); 00107 return id; 00108 }; 00109 00113 SDL_TimerID id_; 00114 }; //Timer 00115 }; //devices 00116 }; //sdl 00117 00118 #endif //SDL_DEVICES_TIMER_H 00119