00001 #ifndef H_SDLPP_INPUT
00002 #define H_SDLPP_INPUT
00003
00004 namespace SDLPP {
00005
00006 class EventListener
00007 {
00008 public:
00009 virtual ~EventListener() {}
00012 virtual bool handle_event(const SDL_Event&) = 0;
00013 };
00014
00015 struct Joystick
00016 {
00017 public:
00018 std::vector<double> axes;
00019 std::vector<Uint8> hats;
00020 std::vector<bool> buttons;
00021 std::vector<iVec2> balls;
00022
00023 void update();
00024
00027 Joystick(int i=-1);
00028 ~Joystick();
00029 void init(int i);
00030 private:
00031 SDL_Joystick* joystick;
00032 };
00033
00034 class EventManager : public Singleton
00035 {
00036 public:
00037 static EventManager* instance()
00038 {
00039 static std::auto_ptr<EventManager> ptr(new EventManager);
00040 return ptr.get();
00041 }
00042
00043 void register_listener(Uint8 event_type, EventListener* listener);
00044 void remove_listener(Uint8 event_type, EventListener* listener);
00045 void poll();
00046 bool is_pressed(SDLKey key);
00047 iVec2 get_mouse_position();
00048 bool is_mouse_button_pressed(int button);
00049 bool is_key_available() const { return !m_KeyQueue.empty(); }
00050 Uint16 get_key() { Uint16 k=m_KeyQueue.front(); m_KeyQueue.pop_front(); return k; }
00051 void clear_keys() { m_KeyQueue.clear(); }
00052
00053 int get_joystick_count() const { return m_Joysticks.size(); }
00054 Joystick& get_joystick(int i)
00055 {
00056 static Joystick Null;
00057 if (i<0 || i>=get_joystick_count()) return Null;
00058 return m_Joysticks[i];
00059 }
00060
00061 void shutdown();
00062 private:
00063 friend class std::auto_ptr<EventManager>;
00064 EventManager() : m_KeysState(SDLK_LAST,0), m_MouseButtons(32,false)
00065 {
00066 init_joysticks();
00067 }
00068 ~EventManager() {}
00069 EventManager(const EventManager&) {}
00070 void init_joysticks();
00071
00072 typedef std::multimap<Uint8,EventListener*> listener_map;
00073 typedef std::vector<char> key_vec;
00074 iVec2 m_MousePosition;
00075 key_vec m_KeysState;
00076 listener_map m_Listeners;
00077 std::vector<bool> m_MouseButtons;
00078 std::vector<Joystick> m_Joysticks;
00079 std::list<Uint16> m_KeyQueue;
00080 };
00081
00082 #define LISTEN_FOR_EVENT(x) EventManager::instance()->register_listener(x,this)
00083
00084 class QuitListener : public EventListener
00085 {
00086 bool m_Quit;
00087 public:
00088 QuitListener()
00089 : m_Quit(false)
00090 {
00091 EventManager::instance()->register_listener(SDL_QUIT,this);
00092 }
00093 virtual bool handle_event(const SDL_Event&)
00094 {
00095 m_Quit=true;
00096 return false;
00097 }
00098 bool quit() const { return m_Quit; }
00099 };
00100
00101
00102
00103 inline void poll() { EventManager::instance()->poll(); }
00104 inline bool is_pressed(SDLKey key) { return EventManager::instance()->is_pressed(key); }
00105 inline iVec2 get_mouse_position() { return EventManager::instance()->get_mouse_position(); }
00106 inline bool is_mouse_button_pressed(int button) { return EventManager::instance()->is_mouse_button_pressed(button); }
00107 inline void clear_key(SDLKey key) { while (is_pressed(key)) { poll(); SDL_Delay(100); } }
00108 inline void clear_button(int button) { while (is_mouse_button_pressed(button)) { poll(); SDL_Delay(100); } }
00109 }
00110
00111
00112 #endif // H_SDLPP_INPUT
00113
00114