250 lines
7.2 KiB
C++
250 lines
7.2 KiB
C++
/**
|
|
* @file RenderWindow.hpp
|
|
* @brief A wrapper around SDL_Window and SDL_Renderer
|
|
* @author Lauchmelder23
|
|
* @date 16.05.2020
|
|
*/
|
|
#pragma once
|
|
#include <string>
|
|
#include <SDL.h>
|
|
|
|
#include <structures/Vector2.hpp>
|
|
#include <structures/Color.hpp>
|
|
|
|
namespace sdlu
|
|
{
|
|
/**
|
|
* @brief A class that handles window related functionality
|
|
*
|
|
* A class that combines the SDL_Window and SDL_Renderer and
|
|
* behaves similar to the sf::RenderWindow from SFML. It provides
|
|
* utility and wrappers for common operations on those objects.
|
|
*/
|
|
class RenderWindow
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Default Constructor. No window or renderer is created.
|
|
*/
|
|
RenderWindow();
|
|
|
|
/**
|
|
* @brief Creates a window and renderer with the given parameters
|
|
*
|
|
* @param[in] dimension A vector containing the width and height
|
|
* @param[in] 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;
|
|
|
|
virtual ~RenderWindow();
|
|
|
|
/**
|
|
* @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[in] dimension A vector containing the width and height
|
|
* @param[in] title The title of the create window
|
|
*/
|
|
void Create(Vector2u dimension, const std::string& title,
|
|
Uint32 windowFlags, Uint32 rendererFlags);
|
|
|
|
/**
|
|
* @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();
|
|
|
|
/**
|
|
* @brief A non-blocking event polling function
|
|
*
|
|
* @param[out] event An object to write the latest event to
|
|
* @return True if there was an event, False if there wasn't
|
|
*/
|
|
bool PollEvent(SDL_Event* event);
|
|
|
|
/**
|
|
* @brief A blocking event polling function
|
|
*
|
|
* @param[out] event An object to write the latest event to
|
|
* @return True if an event was polled
|
|
*/
|
|
bool WaitEvent(SDL_Event* event);
|
|
|
|
|
|
/**
|
|
* @brief Returns the current position of the window
|
|
*
|
|
* @return A vector with the current position relative to the top left corner of the display
|
|
*/
|
|
Vector2i GetPosition();
|
|
|
|
/**
|
|
* @brief Sets a new window position
|
|
*
|
|
* @param[in] position A vector with the new position
|
|
*/
|
|
void SetPosition(Vector2i position);
|
|
|
|
/**
|
|
* @brief Sets a new window position
|
|
*
|
|
* @param[in] x The new x position
|
|
* @param[in] y The new y position
|
|
*/
|
|
void SetPosition(int x, int y);
|
|
|
|
|
|
/**
|
|
* @brief Gets the current window size
|
|
*
|
|
* @return A vector with the windows size
|
|
*/
|
|
Vector2u GetSize();
|
|
|
|
/**
|
|
* @brief Sets a new window size
|
|
*
|
|
* @param[in] size A vector with the new size
|
|
*/
|
|
void SetSize(Vector2u size);
|
|
|
|
/**
|
|
* @brief Sets a new window size
|
|
*
|
|
* @param[in] width The new width of the window
|
|
* @param[in] height The new height of the window
|
|
*/
|
|
void SetSize(unsigned int width, unsigned int height);
|
|
|
|
|
|
/**
|
|
* @brief Gets the current window title
|
|
*
|
|
* @return The title of the widnow
|
|
*/
|
|
std::string GetTitle();
|
|
|
|
/**
|
|
* @brief Sets a new window title
|
|
*
|
|
* @param[in] title The new window title
|
|
*/
|
|
void SetTitle(std::string title);
|
|
|
|
/**
|
|
* @brief Returns a constant pointer to the SDL_Window
|
|
*
|
|
* @return A constant pointer to SDL_Window
|
|
*/
|
|
SDL_Window* const GetWindow();
|
|
|
|
/**
|
|
* @brief Returns a constant pointer to the SDL_Renderer
|
|
*
|
|
* @return A constant pointer to SDL_Renderer
|
|
*/
|
|
SDL_Renderer* const GetRenderer();
|
|
|
|
/**
|
|
* @brief Clears the display
|
|
*
|
|
* @param[in] color The color to clear the display with
|
|
*/
|
|
void Clear(const Color& color = Color::Black);
|
|
|
|
/**
|
|
* @brief Display the current state of the renderer to the screen
|
|
*/
|
|
void Display();
|
|
|
|
/**
|
|
* @brief Set the windows visibility
|
|
*
|
|
* @param[in] visible The new visibility setting
|
|
*/
|
|
void SetVisible(bool visible);
|
|
|
|
/**
|
|
* @brief (De)activates VSync !globally!
|
|
*
|
|
* @param[in] vsync Wether to enable or disable vsync
|
|
*/
|
|
void SetVsync(bool vsync);
|
|
|
|
/**
|
|
* @brief Hides/Shows the mouse cursor inside the windos
|
|
*
|
|
* @param[in] visible The new visibility of the cursor
|
|
*/
|
|
void SetMouseCursorVisible(bool visible);
|
|
|
|
/**
|
|
* @brief Traps the mouse cursor inside the window
|
|
*
|
|
* @param[in] grabbed Wether to (un)trap the cursor
|
|
*/
|
|
void SetMouseCursorGrabbed(bool grabbed);
|
|
|
|
/**
|
|
* @brief Sets the window icon to an array of RGBA values
|
|
*
|
|
* @param[in] width Width of the icon (in px)
|
|
* @param[in] height Height of the icon (in px)
|
|
* @param[in] pixels Array of color data (RGBA as seperate 8-Bit integer values)
|
|
*/
|
|
void SetIcon(Uint32 width, Uint32 height, const Uint8* pixels);
|
|
|
|
/**
|
|
* @brief Sets the window icon to an array of RGBA values
|
|
*
|
|
* @param[in] width Width of the icon (in px)
|
|
* @param[in] height Height of the icon (in px)
|
|
* @param[in] pixels Array of color data (RGBA as one 32-Bit integer value)
|
|
*/
|
|
void SetIcon(Uint32 width, Uint32 height, const Uint32* pixels);
|
|
|
|
/**
|
|
* @brief Sets the window icon to a SDL_Surface
|
|
*
|
|
* @param[in] icon A SDL_Surface* holding the icon data
|
|
*/
|
|
void SetIcon(SDL_Surface* icon);
|
|
|
|
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();
|
|
};
|
|
} |