From 46a28a711fd730828ae5596f68ef3510a20b7b31 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 21 Jan 2019 15:26:43 +0100 Subject: Make window_test threaded --- debug.hpp | 2 -- include/input.hpp | 0 include/video.hpp | 14 +++++++++++--- include/wrapsdl2.hpp | 16 +++++++++++++++- test/makefile | 2 +- test/window_test.cpp | 20 ++++++++++++++++++-- video.cpp | 12 +++++++++--- wrapsdl2.cpp | 9 +++++++++ 8 files changed, 63 insertions(+), 12 deletions(-) delete mode 100644 include/input.hpp diff --git a/debug.hpp b/debug.hpp index 16dc5a8..c53fc45 100644 --- a/debug.hpp +++ b/debug.hpp @@ -4,8 +4,6 @@ #include #ifdef DEBUG - - #warning building in debug mode! #ifdef __builtin_strrchr #define __FILENAME__ (\ __builtin_strrchr(__FILE__, '/') ? \ diff --git a/include/input.hpp b/include/input.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/include/video.hpp b/include/video.hpp index 1b6e022..45b1fc5 100644 --- a/include/video.hpp +++ b/include/video.hpp @@ -1,11 +1,16 @@ #pragma once #include +#include -class SDL_Window; -class SDL_Surface; +extern "C" { +#include +} namespace wrapsdl2 { + typedef SDL_Point point; + typedef SDL_Rect rect; + class window { public: window() = delete; @@ -14,11 +19,14 @@ namespace wrapsdl2 { window(const std::string& title, std::size_t width, std::size_t height); ~window(); - // manipulation + // setters void show(); void hide(); void raise(); + // getters + bool visible(); + // rendering void update(); diff --git a/include/wrapsdl2.hpp b/include/wrapsdl2.hpp index 993a704..847ed99 100644 --- a/include/wrapsdl2.hpp +++ b/include/wrapsdl2.hpp @@ -1,9 +1,23 @@ #pragma once +extern "C" { +#include +} + namespace wrapsdl2 { bool initialize(void); void quit(void); + + namespace util { + constexpr bool sdl_bool(SDL_bool b) { + return b == SDL_TRUE; + } + + constexpr SDL_bool sdl_bool(bool b) { + return (b) ? SDL_TRUE : SDL_FALSE; + } + } // tool functions - void delay() + void delay(unsigned ms); } \ No newline at end of file diff --git a/test/makefile b/test/makefile index 11df431..ee8569a 100644 --- a/test/makefile +++ b/test/makefile @@ -1,7 +1,7 @@ # Compiler CPP := c++ CFLAGS := -Wall -pedantic -std=c++17 -fPIC -I ../include -DDEBUG -LFLAGS := ../build/libwrapsdl2.so -lSDL2 +LFLAGS := ../build/libwrapsdl2.so -lSDL2 -lpthread SRCS := $(wildcard *.cpp) OBJS := $(patsubst %.cpp,build/%.o,$(SRCS)) diff --git a/test/window_test.cpp b/test/window_test.cpp index 70fcb1a..2136488 100644 --- a/test/window_test.cpp +++ b/test/window_test.cpp @@ -2,21 +2,37 @@ #include "video.hpp" #include +#include +#include int main(int argc, char *argv[]) { wrapsdl2::initialize(); { - wrapsdl2::window win("Window Test", 800, 600); + 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(); - win.update(); + + std::thread win_update([&]() { + std::lock_guard lock(win_mutex); + while (win.is_open()) { + win.update(); + wrapsdl2::delay(200); + + + } + }); std::cout << "press ENTER to quit" << std::endl; std::cin.get(); + + win_update.join(); } wrapsdl2::quit(); diff --git a/video.cpp b/video.cpp index 8413cb4..d6ae6cb 100644 --- a/video.cpp +++ b/video.cpp @@ -1,13 +1,13 @@ #include "video.hpp" #include "debug.hpp" +#include +#include + extern "C" { #include } -#include - - using namespace wrapsdl2; window::window(const std::string& title, std::size_t width, std::size_t height) { @@ -39,6 +39,12 @@ void window::show() { SDL_ShowWindow(m_window); } void window::hide() { SDL_HideWindow(m_window); } void window::raise() { SDL_RaiseWindow(m_window); } +bool window::visible() { + std::uint32_t flags = SDL_GetWindowFlags(m_window); + + return flags & SDL_WINDOW_SHOWN; +} + void window::update() { SDL_UpdateWindowSurface(m_window); } \ No newline at end of file diff --git a/wrapsdl2.cpp b/wrapsdl2.cpp index 6c7896f..f9f346b 100644 --- a/wrapsdl2.cpp +++ b/wrapsdl2.cpp @@ -15,5 +15,14 @@ bool wrapsdl2::initialize(void) { } void wrapsdl2::quit(void) { + if (SDL_WasInit(SDL_INIT_VIDEO)) + SDL_QuitSubSystem(SDL_INIT_VIDEO); + SDL_Quit(); } + + + +void wrapsdl2::delay(unsigned ms) { + SDL_Delay(ms); +} \ No newline at end of file -- cgit v1.2.1