From 8a0f6d6eee6162d1a10b20512e2f2c86a0278a34 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 21 Jan 2019 20:11:29 +0100 Subject: Add events wrapper --- event.cpp | 21 ++++++++++++++++++++ include/event.hpp | 24 ++++++++++++++++++++++ makefile | 10 +++++++++- test/makefile | 4 ++++ test/window_test.cpp | 56 +++++++++++++++++++++++++++++++--------------------- wrapsdl2.cpp | 24 ++++++++++++++++++++-- 6 files changed, 114 insertions(+), 25 deletions(-) create mode 100644 event.cpp create mode 100644 include/event.hpp diff --git a/event.cpp b/event.cpp new file mode 100644 index 0000000..72de51c --- /dev/null +++ b/event.cpp @@ -0,0 +1,21 @@ +#include "event.hpp" + +#include + +extern "C" { +#include +} + +wrapsdl2::event::event(const SDL_Event& e) { + m_event = e; +} + +std::optional wrapsdl2::poll_event() { + SDL_Event ev; + + if (SDL_PollEvent(&ev) != 0) { + return event(ev); + } + + return std::nullopt; +} \ No newline at end of file diff --git a/include/event.hpp b/include/event.hpp new file mode 100644 index 0000000..b992852 --- /dev/null +++ b/include/event.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include + +extern "C" { +#include +} + +namespace wrapsdl2 { + class event { + public: + using type = SDL_EventType; + + event(const SDL_Event& e); + + SDL_Event& sdl() { return m_event; } + + private: + SDL_Event m_event; + }; + + + std::optional poll_event(); +} \ No newline at end of file diff --git a/makefile b/makefile index 2c13834..ed5420a 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,6 @@ # Compiler CPP := c++ -CFLAGS := -Wall -pedantic -std=c++17 -fPIC -shared -I include -DDEBUG +CFLAGS := -Wall -pedantic -std=c++17 -fPIC -shared -I include -DDEBUG -DWRAPSDL2_EXCEPTIONS LFLAGS := -lSDL2 SRCS := $(wildcard *.cpp) @@ -8,6 +8,14 @@ OBJS := $(patsubst %.cpp,build/%.o,$(SRCS)) # Recipes all: build/libwrapsdl2.so + $(MAKE) -C test + +.PHONY: test-all +.ONESHELL: +test-all: + cd test + ./build/window_test + build/libwrapsdl2.so: $(OBJS) $(CPP) $(CFLAGS) -o $@ $(LFLAGS) $(OBJS) 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 #include #include 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 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 lock(win_mutex); + do { + std::optional 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; diff --git a/wrapsdl2.cpp b/wrapsdl2.cpp index f9f346b..3828abc 100644 --- a/wrapsdl2.cpp +++ b/wrapsdl2.cpp @@ -1,24 +1,44 @@ #include "wrapsdl2.hpp" #include "debug.hpp" +#ifdef WRAPSDL2_EXCEPTIONS +#include +#endif + extern "C" { #include } bool wrapsdl2::initialize(void) { if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { - npdebug("failed to initialize sdl video subsystem"); +#ifdef WRAPSDL2_EXCEPTIONS + throw std::runtime_error("failed to initialize sdl video subsystem"); +#endif + return false; + } + + if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) { +#ifdef WRAPSDL2_EXCEPTIONS + throw std::runtime_error("failed to initialzie sdl events subsystem"); +#endif return false; } + npdebug("initialized sdl2"); + return true; } void wrapsdl2::quit(void) { if (SDL_WasInit(SDL_INIT_VIDEO)) SDL_QuitSubSystem(SDL_INIT_VIDEO); - + + if (SDL_WasInit(SDL_INIT_EVENTS)) + SDL_QuitSubSystem(SDL_INIT_EVENTS); + SDL_Quit(); + + npdebug("deinitialized (quit) sdl2"); } -- cgit v1.2.1