SDL++
C++wrapperfortheSDLlibrary.
|
00001 00021 #ifndef SDL_SUBSYSTEM_SUBSYSTEM_H 00022 #define SDL_SUBSYSTEM_SUBSYSTEM_H 00023 00024 #include <stdexcept> 00025 #include <string> 00026 00027 #include <SDL.h> 00028 00029 #include "sdlpp/Sdl.h" 00030 00031 namespace sdl { 00032 namespace subsystem { 00033 using namespace std; 00034 00042 template<int T, class Base> 00043 class Subsystem : public Base { 00044 public: 00050 static Subsystem& instance () { 00051 static Subsystem instance_; 00052 return instance_; 00053 }; 00054 00060 static bool isOpen () { return SDL_WasInit (T) == T; }; 00061 00067 Subsystem& open () { 00068 init (); 00069 return *this; 00070 }; 00071 00077 Subsystem& close () { 00078 quit (); 00079 return *this; 00080 }; 00081 00082 protected: 00090 Subsystem& init () { 00091 if (SDL_InitSubSystem (T) == -1) 00092 throw runtime_error (Base::name () + " subsystem failed to initialize."); 00093 return *this; 00094 }; 00095 00101 Subsystem& quit () { 00102 SDL_QuitSubSystem (T); 00103 return *this; 00104 }; 00105 00109 Subsystem () : Base () { init (); }; 00110 00116 Subsystem (const Subsystem& rhs) {}; 00117 00121 ~Subsystem () { quit (); }; 00122 00130 Subsystem& operator= (const Subsystem& rhs) {}; 00131 }; //Subsystem 00132 00136 struct AudioBase { 00137 protected: 00143 static const string name () { return "Audio"; }; 00144 }; //AudioBase 00145 00150 typedef Subsystem<SDL_INIT_AUDIO, AudioBase> Audio; 00151 00156 struct CdRomBase { 00162 int numDrives () { return SDL_CDNumDrives (); }; 00163 00164 protected: 00170 static const string name () { return "Cd-Rom"; }; 00171 }; //CdRomBase 00172 00177 typedef Subsystem<SDL_INIT_CDROM, CdRomBase> CdRom; 00178 00183 struct EverythingBase { 00184 protected: 00190 static const string name () { return "Everything"; }; 00191 }; //EverythingBase 00192 00197 typedef Subsystem<SDL_INIT_EVERYTHING, EverythingBase> Everything; 00198 00203 struct EventThreadBase { 00204 protected: 00210 static const string name () { return "EventThread"; }; 00211 }; //EventThreadBase 00212 00217 typedef Subsystem<SDL_INIT_EVENTTHREAD, EventThreadBase> EventThread; 00218 00223 struct JoystickBase { 00227 void update () { SDL_JoystickUpdate (); }; 00228 00232 int numJoysticks () { return SDL_NumJoysticks (); }; 00233 00240 bool enableEvents () { return SDL_JoystickEventState (SDL_ENABLE) == SDL_ENABLE; }; 00241 00248 bool disableEvents () { return SDL_JoystickEventState (SDL_IGNORE) == SDL_IGNORE; }; 00249 00256 bool areEventsEnabled () { return SDL_JoystickEventState (SDL_QUERY) == SDL_ENABLE; }; 00257 00258 protected: 00264 static const string name () { return "Joystick"; }; 00265 }; //JoystickBase 00266 00271 typedef Subsystem<SDL_INIT_JOYSTICK, JoystickBase> Joystick; 00272 00277 struct NoParachuteBase { 00278 protected: 00284 static const string name () { return "NoParachute"; }; 00285 }; //NoParachuteBase 00286 00291 typedef Subsystem<SDL_INIT_NOPARACHUTE, NoParachuteBase> NoParachute; 00292 00297 struct TimerBase { 00301 unsigned int ticks () { return SDL_GetTicks (); }; 00302 00308 TimerBase& delay (unsigned int interval) { 00309 SDL_Delay (interval); 00310 return *this; 00311 }; 00312 00313 protected: 00319 static const string name () { return "Timer"; }; 00320 }; //TimerBase 00321 00326 typedef Subsystem<SDL_INIT_TIMER, TimerBase> Timer; 00327 00331 struct VideoBase { 00335 void swapBuffers () { SDL_GL_SwapBuffers (); }; 00336 00337 protected: 00343 static const string name () { return "Video"; }; 00344 }; //VideoBase 00345 00350 typedef Subsystem<SDL_INIT_VIDEO, VideoBase> Video; 00351 }; //subsystem 00352 }; //sdl 00353 00354 #endif //SDL_SUBSYSTEM_SUBSYSTEM_H 00355