00001 #include "cevent.h" 00002 00003 #include <SDL.h> 00004 00014 namespace SDLDriver { 00015 00018 class CEventSDL : public CEvent { 00019 private: 00020 /* The wrapped event structure. */ 00021 SDL_Event *event; 00022 00023 public: 00028 CEventSDL(SDL_Event *e) { event = e; } 00029 virtual ~CEventSDL() { delete event; } 00030 int is_ctrl_pressed() { 00031 return event->key.keysym.mod & KMOD_CTRL; 00032 } 00033 char get_key_ascii_value() { 00034 return event->key.keysym.sym; 00035 } 00036 int get_type() { 00037 switch (event->type) { 00038 case SDL_KEYDOWN: 00039 return CEvent::TYPE_KEY_DOWN; 00040 case SDL_QUIT: 00041 return CEvent::TYPE_APPLICATION_EXIT; 00042 } 00043 return 0; 00044 } 00045 }; 00046 00048 class CEventSystemSDL : public CEventSystem { 00049 private: 00050 /* This is an SDL_EventFilter function that filters out all events except the ones 00051 that we're interested in. SDL calls this function, and its documentation is at 00052 http://sdldoc.csn.ul.ie/sdlseteventfilter.php . */ 00053 static int sdl_event_filter(const SDL_Event *event) { 00054 switch (event->type) { 00055 case SDL_KEYDOWN: 00056 case SDL_QUIT: 00057 /* Yes, we want these kinds of events, so return true. */ 00058 return 1; 00059 default: 00060 /* We don't want any other kinds of events, so return false. */ 00061 return 0; 00062 } 00063 } 00064 00065 CEventSystemSDL() { 00066 /* Set our event filter so we only receive the events we want. */ 00067 SDL_SetEventFilter(sdl_event_filter); 00068 } 00069 00070 public: 00071 friend class CEventSystemFactorySDL; 00072 00078 virtual CEvent *get_next_event() { 00079 if (SDL_PollEvent(NULL)) { 00080 SDL_Event *e = new SDL_Event; 00081 SDL_PollEvent(e); 00082 return new CEventSDL(e); 00083 } 00084 return 0; 00085 } 00086 }; 00087 00090 class CEventSystemFactorySDL : public CEventSystemFactory { 00091 virtual CEventSystem *make_singleton() { 00092 return new CEventSystemSDL(); 00093 } 00094 }; 00095 00096 /* Add the factory retriever to the SDLDriver namespace's repertoire. */ 00097 CEventSystemFactory *make_event_system_factory() { 00098 return new CEventSystemFactorySDL(); 00099 } 00100 }
1.3.3