From 740ddc2ca650ab014f04438a64a8d88624d05d09 Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 16 May 2020 15:40:57 +0200 Subject: [PATCH] Added creation routine --- SDLU/CMakeLists.txt | 1 + SDLU/SDLU.hpp | 2 +- SDLU/graphics/RenderWindow.cpp | 44 ++++++++++++++++++++++++++++ SDLU/graphics/RenderWindow.hpp | 53 +++++++++++++++++++++++++++++++++- 4 files changed, 98 insertions(+), 2 deletions(-) diff --git a/SDLU/CMakeLists.txt b/SDLU/CMakeLists.txt index 763596d..ad6bc11 100644 --- a/SDLU/CMakeLists.txt +++ b/SDLU/CMakeLists.txt @@ -6,6 +6,7 @@ add_library(${PNAME} target_include_directories(${PNAME} PRIVATE ${PROJECT_SOURCE_DIR}/3rdparty/SDL/include + ${CMAKE_CURRENT_LIST_DIR} ) target_link_libraries(${PNAME} PRIVATE diff --git a/SDLU/SDLU.hpp b/SDLU/SDLU.hpp index df523b9..063db19 100644 --- a/SDLU/SDLU.hpp +++ b/SDLU/SDLU.hpp @@ -1,3 +1,3 @@ #pragma once -#include "structures/Vector2.hpp" \ No newline at end of file +#include "graphics/RenderWindow.hpp" \ No newline at end of file diff --git a/SDLU/graphics/RenderWindow.cpp b/SDLU/graphics/RenderWindow.cpp index e69de29..9643077 100644 --- a/SDLU/graphics/RenderWindow.cpp +++ b/SDLU/graphics/RenderWindow.cpp @@ -0,0 +1,44 @@ +#include "RenderWindow.hpp" + +#define IS_NULLPTR( x ) (x == nullptr) + +namespace sdlu +{ + RenderWindow::RenderWindow() + { + } + + RenderWindow::RenderWindow(Vector2u dimension, const std::string& title, + Uint32 windowFlags, Uint32 rendererFlags) + { + Create(dimension, title, windowFlags, rendererFlags); + } + + 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, + windowFlags); + if (IS_NULLPTR(m_pWindow)) + { + // TODO: Implement exception + return + } + + m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, rendererFlags); + if (IS_NULLPTR(m_pRenderer)) + { + // TODO: Implement exception + return + } + } +} \ No newline at end of file diff --git a/SDLU/graphics/RenderWindow.hpp b/SDLU/graphics/RenderWindow.hpp index 7b9637e..e303002 100644 --- a/SDLU/graphics/RenderWindow.hpp +++ b/SDLU/graphics/RenderWindow.hpp @@ -1 +1,52 @@ -#pragma once \ No newline at end of file +/** + * @file RenderWindow.hpp + * @brief A wrapper around SDL_Window and SDL_Renderer + * @author Lauchmelder23 + * @date 16.05.2020 + */ +#pragma once +#include +#include + +#include "../structures/Vector2.hpp" + +namespace sdlu +{ + class RenderWindow + { + public: + /** + * @brief Default Constructor. No window or renderer is created. + */ + RenderWindow(); + + /** + * @brief Creates a window and renderer with the given parameters + * + * @param dimension A vector containing the width and height + * @param title The title of the create window + */ + RenderWindow(Vector2u dimension, const std::string& title, + Uint32 windowFlags, Uint32 rendererFlags); + + RenderWindow(const RenderWindow& other) = delete; + RenderWindow(const RenderWindow&& other) = delete; + + /** + * @brief Creates the window and renderer. + * + * This function creates the SDL_Window and SDL_Renderer objects. If + * they were already created the function does nothing and returns. + * If it fails to create either, an ObjectCreationException is thrown. + * + * @param dimension A vector containing the width and height + * @param title The title of the create window + */ + void Create(Vector2u dimension, const std::string& title, + Uint32 windowFlags, Uint32 rendererFlags); + + private: + SDL_Window* m_pWindow; + SDL_Renderer* m_pRenderer; + }; +} \ No newline at end of file