summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2019-01-23 01:12:09 +0100
committerNao Pross <naopross@thearcway.org>2019-01-23 01:12:09 +0100
commit467a6990b0ee9ba568008697ba2f31b26951dbeb (patch)
tree298c21e7c9da10c74023d0479139f2fc995fd06b
parentRemove test makefile (diff)
downloadlibwsdl2-467a6990b0ee9ba568008697ba2f31b26951dbeb.tar.gz
libwsdl2-467a6990b0ee9ba568008697ba2f31b26951dbeb.zip
Separate renderer from window
Diffstat (limited to '')
-rw-r--r--include/video.hpp34
-rw-r--r--test/window_test.cpp1
-rw-r--r--video.cpp53
-rw-r--r--wrapsdl2.cpp12
4 files changed, 78 insertions, 22 deletions
diff --git a/include/video.hpp b/include/video.hpp
index a00f77e..23c1c6e 100644
--- a/include/video.hpp
+++ b/include/video.hpp
@@ -12,13 +12,37 @@ namespace wsdl2 {
typedef SDL_Point point;
typedef SDL_Rect rect;
+ class window;
+
+ class renderer {
+ public:
+ friend class window;
+
+ renderer(window& w);
+ virtual ~renderer();
+
+ inline void clear() { SDL_RenderClear(safe()); }
+ inline void present() { SDL_RenderPresent(safe()); }
+
+ // dirty C code
+ inline SDL_Renderer* sdl() { return m_renderer; }
+
+ private:
+ renderer();
+
+ SDL_Renderer* safe();
+ void create_sdl_renderer(SDL_Window *win);
+
+ SDL_Renderer *m_renderer;
+ };
+
class window {
public:
window() = delete;
window(const window& other) = delete;
window(const std::string& title, std::size_t width, std::size_t height);
- ~window();
+ virtual ~window();
// setters
void open();
@@ -35,9 +59,15 @@ namespace wsdl2 {
// rendering
void update();
+ // dirty C code
+ inline SDL_Window* sdl() { return m_window; }
+
private:
bool m_open;
+ renderer m_renderer;
SDL_Window *m_window;
- SDL_Renderer *m_renderer;
};
+
+
+
} \ No newline at end of file
diff --git a/test/window_test.cpp b/test/window_test.cpp
index 17c7cf4..3971645 100644
--- a/test/window_test.cpp
+++ b/test/window_test.cpp
@@ -25,7 +25,6 @@ int main(int argc, char *argv[]) {
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) {
diff --git a/video.cpp b/video.cpp
index 68beff7..db153c7 100644
--- a/video.cpp
+++ b/video.cpp
@@ -27,21 +27,13 @@ window::window(const std::string& title, std::size_t width, std::size_t height)
throw std::runtime_error("failed to create SDL window");
}
- // create a rendering contest
- m_renderer = SDL_CreateRenderer(
- m_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
- );
+ m_renderer.create_sdl_renderer(m_window);
// other attributes
m_open = false;
}
window::~window() {
- if (m_renderer != NULL)
- SDL_DestroyRenderer(m_renderer);
- else
- npdebug("warning: m_renderer is NULL")
-
if (m_window != NULL)
SDL_DestroyWindow(m_window);
else
@@ -68,6 +60,45 @@ bool window::is_visible() {
}
void window::update() {
- SDL_RenderClear(m_renderer);
- SDL_RenderPresent(m_renderer);
+ m_renderer.clear();
+ m_renderer.present();
+}
+
+
+/* class renderer */
+
+renderer::renderer() {
+ npdebug("warning: created uninitialized renderer object");
+}
+
+SDL_Renderer * renderer::safe() {
+#ifndef WRAPSDL2_UNSAFE
+ if (m_renderer == NULL) {
+ throw std::runtime_error("attempted to call safe() when m_renderer is NULL");
+ }
+#endif
+
+ return m_renderer;
+}
+
+void renderer::create_sdl_renderer(SDL_Window *win) {
+ // create a rendering contest
+ m_renderer = SDL_CreateRenderer(
+ win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
+ );
+
+ if (m_renderer == NULL) {
+ throw std::runtime_error("failed to create SDL renderer");
+ }
+}
+
+renderer::renderer(window& w) {
+ create_sdl_renderer(w.sdl());
+}
+
+renderer::~renderer() {
+ if (m_renderer != NULL)
+ SDL_DestroyRenderer(m_renderer);
+ else
+ npdebug("warning: m_renderer is NULL")
} \ No newline at end of file
diff --git a/wrapsdl2.cpp b/wrapsdl2.cpp
index a0fa624..393bff6 100644
--- a/wrapsdl2.cpp
+++ b/wrapsdl2.cpp
@@ -11,20 +11,16 @@ extern "C" {
bool wsdl2::initialize(void) {
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
-#ifdef WRAPSDL2_EXCEPTIONS
- throw std::runtime_error("failed to initialize sdl video subsystem");
-#endif
+ throw std::runtime_error("failed to initialize SDL video subsystem");
return false;
}
if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
-#ifdef WRAPSDL2_EXCEPTIONS
- throw std::runtime_error("failed to initialzie sdl events subsystem");
-#endif
+ throw std::runtime_error("failed to initialzie SDL events subsystem");
return false;
}
- npdebug("initialized sdl2");
+ npdebug("initialized SDL2");
return true;
}
@@ -38,7 +34,7 @@ void wsdl2::quit(void) {
SDL_Quit();
- npdebug("deinitialized (quit) sdl2");
+ npdebug("deinitialized (quit) SDL2");
}