Added protected OnEvent functions for inheritance
This commit is contained in:
parent
0611493afd
commit
10d19f7f58
|
@ -1,3 +1,5 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "graphics/RenderWindow.hpp"
|
#include "graphics/RenderWindow.hpp"
|
||||||
|
|
||||||
|
#include "exceptions/Exceptions.hpp"
|
|
@ -8,5 +8,7 @@
|
||||||
|
|
||||||
#include "ObjectCreationException.hpp"
|
#include "ObjectCreationException.hpp"
|
||||||
|
|
||||||
#define THROW_IF_NOT( condition, exception ) ( condition ? throw exception : false )
|
#define IS_NULLPTR( x ) (x == nullptr)
|
||||||
#define THROW_IF( condition, exception ) ( condition ? false : throw exception )
|
|
||||||
|
#define THROW_IF( condition, exception ) ( condition ? throw exception : false)
|
||||||
|
#define THROW_IF_NOT( condition, exception ) ( THROW_IF(!condition, exception) )
|
|
@ -1,8 +1,6 @@
|
||||||
#include "RenderWindow.hpp"
|
#include "RenderWindow.hpp"
|
||||||
#include "../exceptions/Exceptions.hpp"
|
#include "../exceptions/Exceptions.hpp"
|
||||||
|
|
||||||
#define IS_NULLPTR( x ) (x == nullptr)
|
|
||||||
|
|
||||||
namespace sdlu
|
namespace sdlu
|
||||||
{
|
{
|
||||||
RenderWindow::RenderWindow()
|
RenderWindow::RenderWindow()
|
||||||
|
@ -15,16 +13,14 @@ namespace sdlu
|
||||||
Create(dimension, title, windowFlags, rendererFlags);
|
Create(dimension, title, windowFlags, rendererFlags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RenderWindow::~RenderWindow()
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
|
||||||
void RenderWindow::Create(Vector2u dimension, const std::string& title,
|
void RenderWindow::Create(Vector2u dimension, const std::string& title,
|
||||||
Uint32 windowFlags, Uint32 rendererFlags)
|
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(),
|
m_pWindow = SDL_CreateWindow(title.c_str(),
|
||||||
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||||
dimension.x, dimension.y,
|
dimension.x, dimension.y,
|
||||||
|
@ -35,5 +31,19 @@ namespace sdlu
|
||||||
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, rendererFlags);
|
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, rendererFlags);
|
||||||
THROW_IF(IS_NULLPTR(m_pRenderer),
|
THROW_IF(IS_NULLPTR(m_pRenderer),
|
||||||
ObjectCreationException("Failed to create SDL_Renderer."));
|
ObjectCreationException("Failed to create SDL_Renderer."));
|
||||||
|
|
||||||
|
OnCreate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void RenderWindow::Close()
|
||||||
|
{
|
||||||
|
SDL_DestroyWindow(m_pWindow);
|
||||||
|
|
||||||
|
OnClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RenderWindow::IsOpen()
|
||||||
|
{
|
||||||
|
return !IS_NULLPTR(m_pWindow);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -37,6 +37,8 @@ namespace sdlu
|
||||||
RenderWindow(const RenderWindow& other) = delete;
|
RenderWindow(const RenderWindow& other) = delete;
|
||||||
RenderWindow(const RenderWindow&& other) = delete;
|
RenderWindow(const RenderWindow&& other) = delete;
|
||||||
|
|
||||||
|
virtual ~RenderWindow();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Creates the window and renderer.
|
* @brief Creates the window and renderer.
|
||||||
*
|
*
|
||||||
|
@ -50,8 +52,40 @@ namespace sdlu
|
||||||
void Create(Vector2u dimension, const std::string& title,
|
void Create(Vector2u dimension, const std::string& title,
|
||||||
Uint32 windowFlags, Uint32 rendererFlags);
|
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_Window* m_pWindow; ///< A pointer to the window object
|
||||||
SDL_Renderer* m_pRenderer; ///< A pointer to the renderer 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();
|
||||||
};
|
};
|
||||||
}
|
}
|
|
@ -1,2 +1,30 @@
|
||||||
#pragma once
|
#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;
|
||||||
|
}
|
|
@ -1,7 +1,5 @@
|
||||||
#include "header.hpp"
|
#include "header.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
sdlu::Vector2f vecA(.4f, -2.3f);
|
sdlu::Vector2f vecA(.4f, -2.3f);
|
||||||
|
@ -13,7 +11,13 @@ int main(int argc, char** argv)
|
||||||
vec *= 1.8f;
|
vec *= 1.8f;
|
||||||
std::cout << "Vector2f: " << vec.x << ", " << vec.y << std::endl;
|
std::cout << "Vector2f: " << vec.x << ", " << vec.y << std::endl;
|
||||||
|
|
||||||
sdlu::RenderWindow window;
|
try {
|
||||||
window.Create(sdlu::Vec2u(800, 800), "First test window", NULL, NULL);
|
MyWindow window(800, 800, "Test");
|
||||||
|
}
|
||||||
|
catch (sdlu::ObjectCreationException e)
|
||||||
|
{
|
||||||
|
std::cerr << e.what() << std::endl;
|
||||||
|
std::cerr << SDL_GetError() << std::endl;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in a new issue