1 ///////////////////////////////////////// 2 // 3 // OpenLieroX 4 // 5 // event queue 6 // 7 // based on the work of JasonB 8 // enhanced by Dark Charlie and Albert Zeyer 9 // 10 // code under LGPL 11 // 12 ///////////////////////////////////////// 13 14 #ifndef __EVENTQUEUE_H__ 15 #define __EVENTQUEUE_H__ 16 17 #include <cassert> 18 #include "ThreadPool.h" // for Action 19 20 enum SDLUserEvent { 21 UE_CustomEventHandler = 0, 22 UE_QuitEventThread = 1, 23 UE_DoVideoFrame = 2, 24 UE_DoSetVideoMode = 3, 25 UE_DoActionInMainThread = 4 26 }; 27 28 29 // bDedicated must be set before we can call this 30 void InitEventQueue(); 31 void ShutdownEventQueue(); 32 33 34 35 union SDL_Event; 36 typedef SDL_Event EventItem; // for now, we can change that later 37 38 class _Event; 39 template< typename _Data > class Event; 40 41 class CustomEventHandler : public Action { 42 public: 43 virtual int handle() = 0; 44 virtual const _Event* owner() const = 0; 45 virtual CustomEventHandler* copy(_Event* newOwner = NULL) const = 0; ~CustomEventHandler()46 virtual ~CustomEventHandler() {} 47 }; 48 49 template< typename _Data > 50 class EventThrower : public CustomEventHandler { 51 public: 52 Event<_Data>* m_event; 53 _Data m_data; EventThrower(Event<_Data> * e,_Data d)54 EventThrower(Event<_Data>* e, _Data d) : m_event(e), m_data(d) {} handle()55 virtual int handle() { 56 m_event->occurred( m_data ); 57 return 0; 58 } owner()59 virtual const _Event* owner() const { return m_event; } copy(_Event * newOwner)60 virtual CustomEventHandler* copy(_Event* newOwner) const { 61 EventThrower* th = new EventThrower(*this); 62 if(newOwner) th->m_event = (Event<_Data>*) newOwner; 63 return th; 64 } 65 }; 66 67 68 struct EventQueueIntern; 69 70 class EventQueue { 71 private: 72 EventQueueIntern* data; 73 public: 74 EventQueue(); 75 ~EventQueue(); 76 77 // Polls for currently pending events. 78 bool poll(EventItem& e); 79 80 // Waits indefinitely for the next available event. 81 bool wait(EventItem& e); 82 83 /* Add an event to the event queue. 84 * This function returns true on success 85 * or false if there was some error. 86 */ 87 bool push(const EventItem& e); 88 bool push(Action* eh); 89 90 // goes through all CustomEventHandler and copies them if oldOwner is matching 91 void copyCustomEvents(const _Event* oldOwner, _Event* newOwner); 92 93 // removes all CustomEventHandler with owner 94 void removeCustomEvents(const _Event* owner); 95 }; 96 97 extern EventQueue* mainQueue; 98 99 100 101 #endif // __EVENTQUEUE_H__ 102