1919#include " EventListener.h"
2020#include < SDL_syswm.h>
2121#include < cassert>
22+
23+ /*
24+ The main thread opens the window and handles events; the logic thread handles
25+ everything else. Every so often, the logic thread paints the screen. During
26+ resize, the event watch function will call paint() on every registered painter.
27+
28+ The logic thread is started in Glob2::run.
29+ */
2230namespace GAGCore {
2331std::deque<SDL_Event> events = std::deque<SDL_Event>();
2432std::mutex EventListener::queueMutex;
@@ -42,6 +50,7 @@ EventListener::EventListener(GraphicContext* gfx)
4250 quit = true ;
4351}
4452
53+ // ! End the event listening loop
4554void EventListener::stop ()
4655{
4756 quit = true ;
@@ -55,28 +64,32 @@ EventListener::~EventListener()
5564{
5665}
5766
58- // Create an OpenGL context or set existing context as current on this thread.
67+ // ! Create an OpenGL context or set existing context as current on this thread.
5968void EventListener::ensureContext ()
6069{
6170 instance ()->gfx ->createGLContext ();
6271}
6372
64- // deprecated; use addPainter/removePainter instead.
73+ // ! deprecated; use addPainter/removePainter instead.
6574void EventListener::setPainter (std::function<void ()> f)
6675{
6776 std::unique_lock<std::recursive_mutex> lock (renderMutex);
6877 painter = f;
6978}
7079
71- /* name should be the class name that the function is called on
72- * f is the function to call to draw the screen.
80+ /* * Add a painter to the list of painting functions to be called on window resize.
81+ * name should be the class name that the function is called on
82+ * f is the function to call to draw the screen.
7383 */
7484void EventListener::addPainter (const std::string& name, std::function<void ()> f)
7585{
7686 std::unique_lock<std::recursive_mutex> lock (renderMutex);
7787 painters.insert (std::pair<const std::string, std::function<void ()> >(name, f));
7888}
79- // Erase the latest painter added with the name `name` from the multimap
89+
90+ /* * Erase the latest painter added with the name `name` from the multimap
91+ * Removes the most recently added painter with that name.
92+ */
8093void EventListener::removePainter (const std::string& name)
8194{
8295 if (painters.empty ())
@@ -93,7 +106,8 @@ void EventListener::removePainter(const std::string& name)
93106 }
94107 }
95108}
96- // Draw all the registered painters in order
109+
110+ // ! Draw all the registered painters in order
97111void EventListener::paint ()
98112{
99113 depth++;
@@ -111,6 +125,9 @@ void EventListener::paint()
111125 }
112126 depth--;
113127}
128+
129+ // ! Handle user resizing the game window on Microsoft Windows.
130+ // TODO: Handle window resizing on macOS
114131// https://stackoverflow.com/a/51597338/8890345
115132#ifdef WINDOWS_OR_MINGW
116133bool sizeMoveTimerRunning = false ;
@@ -138,6 +155,10 @@ int eventWatch(void* self, SDL_Event* event) {
138155 return 0 ;
139156}
140157#endif
158+
159+ /* * Listens for events and adds them to a queue.
160+ * Call EventListener::poll to get an event from the queue.
161+ */
141162void EventListener::run ()
142163{
143164 {
@@ -190,6 +211,10 @@ void EventListener::run()
190211 }
191212 doneCond.notify_one ();
192213}
214+
215+ /* * Drop-in replacement for SDL_PollEvent.
216+ * Call this to get an event from the queue.
217+ */
193218int EventListener::poll (SDL_Event* e)
194219{
195220 std::lock_guard<std::mutex> lock (queueMutex);
@@ -200,10 +225,15 @@ int EventListener::poll(SDL_Event* e)
200225 }
201226 return 0 ;
202227}
228+
229+ /* * Gets the active EventListener instance.
230+ * Does not initialize the EventListener if it is nullptr.
231+ */
203232EventListener *EventListener::instance ()
204233{
205234 return el;
206235}
236+ // ! Checks if the EventListener is currently in the event handling loop.
207237bool EventListener::isRunning ()
208238{
209239 return !quit;
0 commit comments