Added color structures

This commit is contained in:
Robert 2020-05-16 23:36:21 +02:00
parent 712e238ca0
commit 305963fe7e
10 changed files with 213 additions and 23 deletions

View file

@ -1,8 +1,10 @@
set(PNAME SDLU)
add_library(${PNAME}
alibi.cpp SDLU.hpp
)
alibi.cpp SDLU.hpp Util.hpp
"structures/Color.cpp")
set_property(TARGET ${PNAME} PROPERTY CXX_STANDARD 17)
target_include_directories(${PNAME} PRIVATE
${PROJECT_SOURCE_DIR}/3rdparty/SDL/include

25
SDLU/Util.hpp Normal file
View file

@ -0,0 +1,25 @@
/**
* @file Util.hpp
* @brief Basic utility macros, typedefs...
* @author Lauchmelder23
* @date 16.05.2020
*/
#pragma once
#include <cstdint>
#define IS_NULLPTR( x ) (x == nullptr)
#define RETURN_IF_NULLPTR( x , v ) { if(IS_NULLPTR(x)) return v; }
#define RETURN_IF_NOT_NULLPTR( x , v ) { if(!IS_NULLPTR(x)) return v; }
typedef uint8_t Uint8;
typedef int8_t Int8;
typedef uint16_t Uint16;
typedef int16_t Int16;
typedef uint32_t Uint32;
typedef int32_t Int32;
typedef uint64_t Uint64;
typedef int64_t Int64;

View file

@ -8,7 +8,5 @@
#include "ObjectCreationException.hpp"
#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,5 +1,6 @@
#include "RenderWindow.hpp"
#include "../exceptions/Exceptions.hpp"
#include <exceptions/Exceptions.hpp>
#include <Util.hpp>
namespace sdlu
{
@ -26,7 +27,7 @@ namespace sdlu
Uint32 windowFlags, Uint32 rendererFlags)
{
// Don't create a window when it already exists
if (!IS_NULLPTR(m_pWindow)) return;
RETURN_IF_NOT_NULLPTR(m_pWindow);
m_pWindow = SDL_CreateWindow(title.c_str(),
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
@ -45,7 +46,7 @@ namespace sdlu
void RenderWindow::Close()
{
// Don't destroy a window that doesn't exist
if (IS_NULLPTR(m_pWindow)) return;
RETURN_IF_NULLPTR(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
m_pRenderer = nullptr;
@ -58,11 +59,13 @@ namespace sdlu
bool RenderWindow::IsOpen()
{
RETURN_IF_NULLPTR(m_pWindow, false);
return (!SDL_GetWindowID(m_pWindow) ? false : true);
}
bool RenderWindow::PollEvent(SDL_Event* event)
{
RETURN_IF_NULLPTR(m_pWindow, false);
// Handle events before the user in case a derived
// class decides to block the event.
while (SDL_PollEvent(event))
@ -86,6 +89,8 @@ namespace sdlu
Vector2i RenderWindow::GetPosition()
{
RETURN_IF_NULLPTR(m_pWindow, Vector2i());
int x = 0, y = 0;
SDL_GetWindowPosition(m_pWindow, &x, &y);
return Vector2i(x, y);
@ -93,16 +98,22 @@ namespace sdlu
void RenderWindow::SetPosition(Vector2i position)
{
RETURN_IF_NULLPTR(m_pWindow);
SDL_SetWindowPosition(m_pWindow, position.x, position.y);
}
void RenderWindow::SetPosition(int x, int y)
{
RETURN_IF_NULLPTR(m_pWindow);
SDL_SetWindowPosition(m_pWindow, x, y);
}
Vector2u RenderWindow::GetSize()
{
RETURN_IF_NULLPTR(m_pWindow, Vector2u());
int x = 0, y = 0;
SDL_GetWindowSize(m_pWindow, &x, &y);
return Vector2u(x, y);
@ -110,21 +121,29 @@ namespace sdlu
void RenderWindow::SetSize(Vector2u size)
{
RETURN_IF_NULLPTR(m_pWindow);
SDL_SetWindowSize(m_pWindow, size.x, size.y);
}
void RenderWindow::SetSize(unsigned int width, unsigned int height)
{
RETURN_IF_NULLPTR(m_pWindow);
SDL_SetWindowSize(m_pWindow, width, height);
}
std::string RenderWindow::GetTitle()
{
RETURN_IF_NULLPTR(m_pWindow, "");
return SDL_GetWindowTitle(m_pWindow);
}
void RenderWindow::SetTitle(std::string title)
{
RETURN_IF_NULLPTR(m_pWindow);
SDL_SetWindowTitle(m_pWindow, title.c_str());
}
@ -137,4 +156,19 @@ namespace sdlu
{
return m_pRenderer;
}
void RenderWindow::Clear(const Color& color)
{
RETURN_IF_NULLPTR(m_pWindow);
SDL_SetRenderDrawColor(m_pRenderer, color.r, color.g, color.b, color.a);
SDL_RenderClear(m_pRenderer);
}
void RenderWindow::Display()
{
RETURN_IF_NULLPTR(m_pWindow);
SDL_RenderPresent(m_pRenderer);
}
}

View file

@ -8,11 +8,14 @@
#include <string>
#include <SDL.h>
#include "../structures/Vector2.hpp"
#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.
@ -155,6 +158,18 @@ namespace sdlu
*/
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();
protected:
SDL_Window* m_pWindow; ///< A pointer to the window object
SDL_Renderer* m_pRenderer; ///< A pointer to the renderer object

View file

@ -1,3 +1,5 @@
target_sources(${PNAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/Vector2.hpp
${CMAKE_CURRENT_SOURCE_DIR}/Color.hpp
${CMAKE_CURRENT_SOURCE_DIR}/Color.cpp
)

46
SDLU/structures/Color.cpp Normal file
View file

@ -0,0 +1,46 @@
#include "Color.hpp"
namespace sdlu
{
const Color Color::Black = Color(0, 0, 0);
const Color Color::Red = Color(255, 0, 0);
const Color Color::Green = Color(0, 255, 0);
const Color Color::Blue = Color(0, 0, 255);
const Color Color::Yellow = Color(255, 255, 0);
const Color Color::Magenta = Color(255, 0, 255);
const Color Color::Cyan = Color(0, 255, 255);
const Color Color::White = Color(255, 255, 255);
const Color Color::Transparent = Color(0, 0, 0, 0);
Color::Color() :
r(0), g(0), b(0), a(0)
{
// Empty
}
Color::Color(Uint8 r, Uint8 g, Uint8 b, Uint8 a) :
r(r), g(g), b(b), a(a)
{
// Empty
}
Color::Color(Uint32 color) :
r((color & 0xFF000000) >> 24),
g((color & 0x00FF0000) >> 16),
b((color & 0x0000FF00) >> 8),
a((color & 0x000000FF))
{
// Empty
}
Uint32 Color::ToInt()
{
Uint32 color = 0;
color |= r << 24;
color |= g << 16;
color |= b << 8;
color |= a;
return color;
}
}

70
SDLU/structures/Color.hpp Normal file
View file

@ -0,0 +1,70 @@
/**
* @file Color.hpp
* @brief Provides utility for creating and handling colors
* @author Lauchmelder23
* @date 16.05.2020
*/
#pragma once
#include <Util.hpp>
namespace sdlu
{
/**
* @brief A structure holding color data
*
* This struct allows for the easy passing of color data between
* objects or functions. It holds RGBA values and allows for
* arithmetical operations.
*/
struct Color
{
Uint8 r; ///< Red component
Uint8 g; ///< Green component
Uint8 b; ///< Blue component
Uint8 a; ///< Alpha component
/**
* @brief Default constructor (Black)
*/
Color();
/**
* @brief Construct color from four 8-Bit integer values
*
* @param[in] r Red component
* @param[in] g Green component
* @param[in] b Blue component
* @param[in] a Alpha component
*/
Color(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255);
/**
* @brief Construct color from a 32-Bit integer value
*
* @param[in] color The color data structured as RGBA
*/
Color(Uint32 color);
/**
* @brief Returns color data as a 32-Bit integer
*
* @return A 32-Bit containing the RGBA values
*/
Uint32 ToInt();
/////////////////// DEFAULT COLORS ///////////////////
static const Color Black; ///< Default Color Black (#000000FF)
static const Color Red; ///< Default Color Red (#FF0000FF)
static const Color Green; ///< Default Color Green (#00FF00FF)
static const Color Blue; ///< Default Color Blue (#0000FFFF)
static const Color Yellow; ///< Default Color Yellow (#FFFF00FF)
static const Color Magenta; ///< Default Color Magenta (#FF00FFFF)
static const Color Cyan; ///< Default Color Cyan (#00FFFFFF)
static const Color White; ///< Default Color White (#FFFFFFFF)
static const Color Transparent; ///< Default Color Transparent (#00000000)
};
}

View file

@ -10,7 +10,7 @@
namespace sdlu
{
/**
* A struct to handle basic 2D vector operations.
* @brief A struct to handle basic 2D vector operations.
*
* @tparam T The (arithmetical) type of the vector components
*/

View file

@ -4,27 +4,25 @@ int main(int argc, char** argv)
{
SDL_Init(SDL_INIT_VIDEO);
sdlu::Vector2f vecA(.4f, -2.3f);
sdlu::Vector2f vecB(-8.9f, 0.003f);
sdlu::Vector2f vec = vecA + vecB;
std::cout << "Vector2f: " << vec.x << ", " << vec.y << std::endl;
vec *= 1.8f;
std::cout << "Vector2f: " << vec.x << ", " << vec.y << std::endl;
MyWindow window(800, 800, "Test");
SDL_SetWindowTitle(window.GetWindow(), "New Title");
SDL_Event event;
while (window.WaitEvent(&event))
while (window.IsOpen())
{
std::cout << event.type << std::endl;
if (event.window.event == SDL_WINDOWEVENT_CLOSE)
while (window.PollEvent(&event))
{
window.Close();
break;
switch (event.window.event)
{
case SDL_WINDOWEVENT_CLOSE:
window.Close();
break;
}
}
window.Clear(sdlu::Color::Magenta);
window.Display();
}
SDL_Quit();