From 10d19f7f5874cf52ec9213995fdfcefaa7b5d216 Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 16 May 2020 19:38:57 +0200 Subject: [PATCH] Added protected OnEvent functions for inheritance --- SDLU/SDLU.hpp | 4 +++- SDLU/exceptions/Exceptions.hpp | 6 ++++-- SDLU/graphics/RenderWindow.cpp | 28 +++++++++++++++++--------- SDLU/graphics/RenderWindow.hpp | 36 +++++++++++++++++++++++++++++++++- SDLU_Example/header.hpp | 30 +++++++++++++++++++++++++++- SDLU_Example/main.cpp | 12 ++++++++---- 6 files changed, 98 insertions(+), 18 deletions(-) diff --git a/SDLU/SDLU.hpp b/SDLU/SDLU.hpp index 063db19..3e5b116 100644 --- a/SDLU/SDLU.hpp +++ b/SDLU/SDLU.hpp @@ -1,3 +1,5 @@ #pragma once -#include "graphics/RenderWindow.hpp" \ No newline at end of file +#include "graphics/RenderWindow.hpp" + +#include "exceptions/Exceptions.hpp" \ No newline at end of file diff --git a/SDLU/exceptions/Exceptions.hpp b/SDLU/exceptions/Exceptions.hpp index 46faf94..0759643 100644 --- a/SDLU/exceptions/Exceptions.hpp +++ b/SDLU/exceptions/Exceptions.hpp @@ -8,5 +8,7 @@ #include "ObjectCreationException.hpp" -#define THROW_IF_NOT( condition, exception ) ( condition ? throw exception : false ) -#define THROW_IF( condition, exception ) ( condition ? false : throw exception ) \ No newline at end of file +#define IS_NULLPTR( x ) (x == nullptr) + +#define THROW_IF( condition, exception ) ( condition ? throw exception : false) +#define THROW_IF_NOT( condition, exception ) ( THROW_IF(!condition, exception) ) \ No newline at end of file diff --git a/SDLU/graphics/RenderWindow.cpp b/SDLU/graphics/RenderWindow.cpp index 8a13654..59317c8 100644 --- a/SDLU/graphics/RenderWindow.cpp +++ b/SDLU/graphics/RenderWindow.cpp @@ -1,8 +1,6 @@ #include "RenderWindow.hpp" #include "../exceptions/Exceptions.hpp" -#define IS_NULLPTR( x ) (x == nullptr) - namespace sdlu { RenderWindow::RenderWindow() @@ -15,16 +13,14 @@ namespace sdlu Create(dimension, title, windowFlags, rendererFlags); } + RenderWindow::~RenderWindow() + { + Close(); + } + void RenderWindow::Create(Vector2u dimension, const std::string& title, Uint32 windowFlags, Uint32 rendererFlags) { - // Leave the function if the window or render already exist - if (!IS_NULLPTR(m_pWindow) || - !IS_NULLPTR(m_pRenderer)) - { - return; - } - m_pWindow = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, dimension.x, dimension.y, @@ -35,5 +31,19 @@ namespace sdlu m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, rendererFlags); THROW_IF(IS_NULLPTR(m_pRenderer), ObjectCreationException("Failed to create SDL_Renderer.")); + + OnCreate(); + } + + void RenderWindow::Close() + { + SDL_DestroyWindow(m_pWindow); + + OnClose(); + } + + bool RenderWindow::IsOpen() + { + return !IS_NULLPTR(m_pWindow); } } \ No newline at end of file diff --git a/SDLU/graphics/RenderWindow.hpp b/SDLU/graphics/RenderWindow.hpp index e204d27..6decc28 100644 --- a/SDLU/graphics/RenderWindow.hpp +++ b/SDLU/graphics/RenderWindow.hpp @@ -37,6 +37,8 @@ namespace sdlu RenderWindow(const RenderWindow& other) = delete; RenderWindow(const RenderWindow&& other) = delete; + virtual ~RenderWindow(); + /** * @brief Creates the window and renderer. * @@ -50,8 +52,40 @@ namespace sdlu void Create(Vector2u dimension, const std::string& title, Uint32 windowFlags, Uint32 rendererFlags); - private: + /** + * @brief Destroys the window and renderer. + */ + void Close(); + + /** + * @brief Wether or not the window object is created + * + * @return True if the window is open, False if not + */ + bool IsOpen(); + + protected: SDL_Window* m_pWindow; ///< A pointer to the window object SDL_Renderer* m_pRenderer; ///< A pointer to the renderer object + + protected: + /** + * @brief This function is called after Create() finishes + */ + virtual void OnCreate(); + + /** + * @brief This function is called after a SDL_WINDOWEVENT_RESIZED is polled. + * (PollEvent() must be called for this to work) + * + * @return True if the resize event should be popped from the event queue before + returning the polled event to the user + */ + virtual bool OnResize(); + + /** + * @brief This function is called after Close() finishes. + */ + virtual void OnClose(); }; } \ No newline at end of file diff --git a/SDLU_Example/header.hpp b/SDLU_Example/header.hpp index 5a1eea0..3a31173 100644 --- a/SDLU_Example/header.hpp +++ b/SDLU_Example/header.hpp @@ -1,2 +1,30 @@ #pragma once -#include "SDLU.hpp" \ No newline at end of file +#include "SDLU.hpp" +#include + +class MyWindow : + public sdlu::RenderWindow +{ +public: + MyWindow(Uint32 width, Uint32 height, const char* title) : + RenderWindow(sdlu::Vector2u(width, height), title, NULL, NULL) + { + // Empty + } +}; + +void sdlu::RenderWindow::OnCreate() +{ + std::cout << "Window was Created!" << std::endl; +} + +bool sdlu::RenderWindow::OnResize() +{ + std::cout << "Window was Resized!" << std::endl; + return false; +} + +void sdlu::RenderWindow::OnClose() +{ + std::cout << "Window was Closed!" << std::endl; +} \ No newline at end of file diff --git a/SDLU_Example/main.cpp b/SDLU_Example/main.cpp index 33480e4..95a0e48 100644 --- a/SDLU_Example/main.cpp +++ b/SDLU_Example/main.cpp @@ -1,7 +1,5 @@ #include "header.hpp" -#include - int main(int argc, char** argv) { sdlu::Vector2f vecA(.4f, -2.3f); @@ -13,7 +11,13 @@ int main(int argc, char** argv) vec *= 1.8f; std::cout << "Vector2f: " << vec.x << ", " << vec.y << std::endl; - sdlu::RenderWindow window; - window.Create(sdlu::Vec2u(800, 800), "First test window", NULL, NULL); + try { + MyWindow window(800, 800, "Test"); + } + catch (sdlu::ObjectCreationException e) + { + std::cerr << e.what() << std::endl; + std::cerr << SDL_GetError() << std::endl; + } return 0; } \ No newline at end of file