SDL++
C++wrapperfortheSDLlibrary.

event/Queue.h

Go to the documentation of this file.
00001 
00021 #ifndef SDL_EVENT_QUEUE_H
00022 #define SDL_EVENT_QUEUE_H
00023 
00024 #include <SDL.h>
00025 
00026 namespace sdl {
00027 namespace event {
00028     using namespace std;
00029 
00034     class Queue {
00035         public:
00044             typedef int (*EventFilter) (const SDL_Event* event);
00045 
00046             /*
00047              * Provides access to the Queue.
00048              *
00049              * @return A reference to the Queue.
00050              */
00051             static Queue& instance () {
00052                 static Queue instance_;
00053                 return instance_;
00054             };
00055 
00061             void SetFilter (EventFilter filter) { SDL_SetEventFilter (filter); };
00062 
00068             EventFilter getFilter () { return SDL_GetEventFilter (); };
00069 
00077             int getEventState (int type) { return SDL_EventState (type, SDL_QUERY); };
00078 
00079             /*
00080              * Ignores a specific event.
00081              *
00082              * @param type The type of the event to ignore.
00083              *
00084              * @return no idea.
00085              */
00086             int ignoreEvent (int type) { return SDL_EventState (type, SDL_IGNORE); };
00087             
00095             int disableEvent (int type) { return SDL_EventState (type, SDL_DISABLE); };
00096 
00104             int enableEvent (int type) { return SDL_EventState (type, SDL_ENABLE); };
00105 
00106             /*
00107              * Determines whether or not the Queue is empty.
00108              *
00109              * @return True if the queue is empty, false otherwise.
00110              */
00111             bool empty () { return !SDL_PollEvent (0); };
00112 
00113             /*
00114              * Removes an event from the front of the Queue.
00115              *
00116              * @return The event at the front of the queue.
00117              *
00118              * @throw runtime_error Throws a runtime_error if no events in queue.
00119              */
00120             SDL_Event pop () {
00121                 SDL_Event event;
00122                 if (!SDL_PollEvent (&event)) throw runtime_error ("No events in queue.");
00123                 return event;
00124             };
00125 
00126             /*
00127              * Pushes an event onto the back of the Queue.
00128              *
00129              * @param event The event to push onto the Queue.
00130              *
00131              * @return True if the event was pushed onto the Queue, false otherwise.
00132              */
00133             bool push (SDL_Event& event) { return SDL_PushEvent (&event); };
00134 
00135             /*
00136              * Pumps the Queue.
00137              *
00138              * @return A reference to the Queue.
00139              */
00140             Queue& pump () {
00141                 SDL_PumpEvents ();
00142                 return *this;
00143             };
00144 
00145             /*
00146              * Waits on an event to become available in the Queue.
00147              *
00148              * @return The event returned at the end of the wait.
00149              *
00150              * @throw runtime_error Throws a runtime_error if failed waiting.
00151              */
00152             SDL_Event wait () {
00153                 SDL_Event event;
00154                 if (!SDL_WaitEvent (&event)) throw runtime_error ("An error occured while waiting for event.");
00155                 return event;
00156             };
00157 
00158         private:
00159             /*
00160              * Constructs a Queue.
00161              */
00162             Queue () {};
00163 
00164             /*
00165              * Copy constructs a Queue.
00166              *
00167              * @param rhs The Queue to copy.
00168              */
00169             Queue (const Queue& rhs);
00170 
00171             /*
00172              * Destroys a Queue.
00173              */
00174             ~Queue () {};
00175 
00176             /*
00177              * The assignment operator.
00178              *
00179              * @param rhs The Queue from which to assign.
00180              *
00181              * @return A reference to this Queue.
00182              */
00183             Queue& operator= (const Queue& rhs);
00184     }; //Queue
00185 }; //event
00186 }; //sdl
00187 
00188 #endif //SDL_EVENT_QUEUE_H
00189 
 All Classes Namespaces Files Functions Variables Typedefs