Added protected OnEvent functions for inheritance

This commit is contained in:
Robert 2020-05-16 19:38:57 +02:00
parent 0611493afd
commit 10d19f7f58
6 changed files with 98 additions and 18 deletions

View file

@ -1,3 +1,5 @@
#pragma once
#include "graphics/RenderWindow.hpp"
#include "graphics/RenderWindow.hpp"
#include "exceptions/Exceptions.hpp"

View file

@ -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 )
#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) )

View file

@ -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);
}
}

View file

@ -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();
};
}

View file

@ -1,2 +1,30 @@
#pragma once
#include "SDLU.hpp"
#include "SDLU.hpp"
#include <iostream>
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;
}

View file

@ -1,7 +1,5 @@
#include "header.hpp"
#include <iostream>
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;
}