diff options
Diffstat (limited to '')
-rw-r--r-- | event.cpp | 21 | ||||
-rw-r--r-- | include/event.hpp | 24 | ||||
-rw-r--r-- | makefile | 10 | ||||
-rw-r--r-- | test/makefile | 4 | ||||
-rw-r--r-- | test/window_test.cpp | 56 | ||||
-rw-r--r-- | wrapsdl2.cpp | 24 |
6 files changed, 114 insertions, 25 deletions
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 <optional> + +extern "C" { +#include <SDL2/SDL.h> +} + +wrapsdl2::event::event(const SDL_Event& e) { + m_event = e; +} + +std::optional<wrapsdl2::event> 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 <optional> + +extern "C" { +#include <SDL2/SDL_events.h> +} + +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<event> poll_event(); +}
\ No newline at end of file @@ -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 <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; 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 <exception> +#endif + extern "C" { #include <SDL2/SDL.h> } 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"); } |