From 8a0f6d6eee6162d1a10b20512e2f2c86a0278a34 Mon Sep 17 00:00:00 2001
From: Nao Pross <naopross@thearcway.org>
Date: Mon, 21 Jan 2019 20:11:29 +0100
Subject: Add events wrapper

---
 test/makefile        |  4 ++++
 test/window_test.cpp | 56 +++++++++++++++++++++++++++++++---------------------
 2 files changed, 38 insertions(+), 22 deletions(-)

(limited to 'test')

diff --git a/test/makefile b/test/makefile
index ee8569a..a2d6800 100644
--- a/test/makefile
+++ b/test/makefile
@@ -9,6 +9,10 @@ OBJS := $(patsubst %.cpp,build/%.o,$(SRCS))
 # Recipes
 all: build/window_test
 
+.PHONY: run
+run: build/window_test
+	./build/window_test
+
 build/window_test: build/window_test.o
 	$(CPP) $(CFLAGS) -o $@ $< $(LFLAGS)
 
diff --git a/test/window_test.cpp b/test/window_test.cpp
index 2136488..c473a34 100644
--- a/test/window_test.cpp
+++ b/test/window_test.cpp
@@ -1,39 +1,51 @@
+#include "../debug.hpp"
+
 #include "wrapsdl2.hpp"
 #include "video.hpp"
+#include "event.hpp"
 
 #include <iostream>
 #include <thread>
 #include <mutex>
 
 int main(int argc, char *argv[]) {
-    wrapsdl2::initialize();
-
-    {
-        using namespace wrapsdl2;
-
-        window win("Window Test", 800, 600);
-        std::mutex win_mutex;
 
-        std::cout << "press ENTER to show the window" << std::endl;
-        std::cin.get();
-
-        win.show();
-
-        std::thread win_update([&]() {
-            std::lock_guard<std::mutex> lock(win_mutex);
-            while (win.is_open()) {
-                win.update();
-                wrapsdl2::delay(200);
+    using namespace wrapsdl2;
 
+    wrapsdl2::initialize();
 
+    window win("Window Test", 800, 600);
+    std::mutex win_mutex;
+
+    win.open();
+
+    std::thread win_update([&]() {
+        std::lock_guard<std::mutex> lock(win_mutex);
+        do {
+            std::optional<event> ev = poll_event();
+            if (ev.has_value()) {
+                event& event = ev.value();
+                npdebug("received event", event.sdl().type);
+
+                // TODO: remove this sdl code
+                if (event.sdl().type == SDL_WINDOWEVENT) {
+                    if (event.sdl().window.event == SDL_WINDOWEVENT_CLOSE) {
+                        npdebug("SDL_WINDOWEVENT_CLOSE")
+                        win.close();
+                    }
+                }
+
+                if (event.sdl().type == SDL_QUIT) {
+                    npdebug("SDL_QUIT");
+                    win.close();
+                }
             }
-        });
 
-        std::cout << "press ENTER to quit" << std::endl;
-        std::cin.get();
+            win.update();
+        } while (win.is_open());
+    });
 
-        win_update.join();
-    }
+    win_update.join();
 
     wrapsdl2::quit();
     return 0;
-- 
cgit v1.2.1