restructured everything

This commit is contained in:
Robert 2021-04-23 15:08:51 +02:00
parent 4225a524b9
commit c66cae17f2
61 changed files with 18406 additions and 1710 deletions

162
src/structures/Color.cpp Normal file
View file

@ -0,0 +1,162 @@
#include "structures/Color.hpp"
#include <math.h>
#include <cmath>
SDLU_BEGIN
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;
}
Color Color::FromHSV(Uint16 h, Uint8 s, Uint8 v)
{
// Normalize parameters
// H : [0, 360)
// S : [0, 1]
// V : [0, 1]
h -= std::floor(h / 360) * 360;
s = (s > 1) ? 1 : s;
v = (v > 1) ? 1 : v;
// Convert to RGBA
Uint16 H = std::floor(h / 60.f);
float f = (h / 60.f) - H;
Uint8 p = static_cast<Uint8>((v * (1 - s)) * 255);
Uint8 q = static_cast<Uint8>((v * (1 - s * f)) * 255);
Uint8 t = static_cast<Uint8>((v * (1 - s * (1 - f))) * 255);
v *= 255;
Color output;
switch (H)
{
case 0:
case 6:
output = Color(v, t, p);
break;
case 1:
output = Color(q, v, p);
break;
case 2:
output = Color(p, v, t);
break;
case 3:
output = Color(p, q, v);
break;
case 4:
output = Color(t, p, v);
break;
case 5:
output = Color(v, p, q);
break;
default:
break;
}
return output;
}
Color operator+(const Color& left, const Color& right)
{
return Color((UINT8_MAX - left.r) < right.r ? 255 : left.r + right.r,
(UINT8_MAX - left.g) < right.g ? 255 : left.g + right.g,
(UINT8_MAX - left.b) < right.b ? 255 : left.b + right.b,
(UINT8_MAX - left.a) < right.a ? 255 : left.a + right.a);
}
Color operator-(const Color& left, const Color& right)
{
return Color(left.r < right.r ? 0 : left.r - right.r,
left.g < right.g ? 0 : left.g - right.g,
left.b < right.b ? 0 : left.b - right.b,
left.a < right.a ? 0 : left.a - right.a);
}
Color operator*(const Color& left, const Color& right)
{
return Color((UINT8_MAX / left.r) < right.r ? 255 : left.r * right.r,
(UINT8_MAX / left.g) < right.g ? 255 : left.g * right.g,
(UINT8_MAX / left.b) < right.b ? 255 : left.b * right.b,
(UINT8_MAX / left.a) < right.a ? 255 : left.a * right.a);
}
Color operator/(const Color& left, const Color& right)
{
return Color(left.r / right.r,
left.g / right.g,
left.b / right.b,
left.a / right.a);
}
Color& operator+=(Color& left, const Color& right)
{
left = left + right;
return left;
}
Color& operator-=(Color& left, const Color& right)
{
left = left - right;
return left;
}
Color& operator*=(Color& left, const Color& right)
{
left = left * right;
return left;
}
Color& operator/=(Color& left, const Color& right)
{
left = left / right;
return left;
}
bool operator==(const Color& left, const Color& right)
{
return ((left.r == right.r) && (left.g == right.g) && (left.b == right.b) && (left.a == right.a));
}
bool operator!=(const Color& left, const Color& right)
{
return !(left == right);
}
SDLU_END

37
src/structures/Mouse.cpp Normal file
View file

@ -0,0 +1,37 @@
#include <structures/Mouse.hpp>
#include <SDL_mouse.h>
SDLU_BEGIN
Uint32 Mouse::GetButtonState()
{
return SDL_GetMouseState(NULL, NULL);
}
bool Mouse::IsButtonDown(Button button)
{
return (GetButtonState() & SDL_BUTTON(static_cast<int>(button)));
}
Vector2i Mouse::GetPosition()
{
int x = 0, y = 0;
SDL_GetGlobalMouseState(&x, &y);
return Vector2i(x, y);
}
Vector2i Mouse::GetPosition(const RenderWindow& relativeTo)
{
return GetPosition() - relativeTo.GetPosition();
}
void Mouse::SetPosition(const Vector2i& position)
{
SDL_WarpMouseGlobal(position.x, position.y);
}
void Mouse::SetPosition(const Vector2i& position, const RenderWindow& relativeTo)
{
SDL_WarpMouseInWindow(relativeTo.GetWindow(), position.x, position.y);
}
SDLU_END

245
src/structures/Window.cpp Normal file
View file

@ -0,0 +1,245 @@
#include "structures/Window.hpp"
#include <SDL.h>
#include <cstring>
SDLU_BEGIN
Window::Window() :
window(nullptr)
{
// Empty
}
Window::Window(Vector2u dimension, const std::string& title, Uint32 windowFlags) :
Window()
{
Create(dimension, title, windowFlags);
}
Window::~Window()
{
Close();
}
void Window::Create(Vector2u dimension, const std::string& title, Uint32 windowFlags)
{
// Don't create a window when it already exists
RETURN_IF_NOT_NULLPTR(window);
window = SDL_CreateWindow(title.c_str(),
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
dimension.x, dimension.y,
windowFlags);
THROW_IF(IS_NULLPTR(window),
std::runtime_error("Failed to create SDL_Window. \nSDL_GetError(): " + std::string(SDL_GetError())));
OnCreate();
}
void Window::Close()
{
// Don't destroy a window that doesn't exist
RETURN_IF_NULLPTR(window);
SDL_DestroyWindow(window);
window = nullptr;
OnClose();
}
bool Window::IsOpen() const
{
RETURN_IF_NULLPTR(window, false);
return (!SDL_GetWindowID(window) ? false : true);
}
bool Window::PollEvent(SDL_Event* event)
{
RETURN_IF_NULLPTR(window, false);
// Handle events before the user in case a derived
// class decides to block the event.
while (SDL_PollEvent(event))
{
switch (event->window.event)
{
case SDL_WINDOWEVENT_RESIZED: if (!OnResize()) return true; break;
default: return true;
}
}
event = NULL;
return false;
}
bool Window::WaitEvent(SDL_Event* event)
{
while (!PollEvent(event)) continue;
return true;
}
Vector2i Window::GetPosition() const
{
RETURN_IF_NULLPTR(window, Vector2i());
int x = 0, y = 0;
SDL_GetWindowPosition(window, &x, &y);
return Vector2i(x, y);
}
void Window::SetPosition(Vector2i position)
{
RETURN_IF_NULLPTR(window);
SDL_SetWindowPosition(window, position.x, position.y);
}
void Window::SetPosition(int x, int y)
{
RETURN_IF_NULLPTR(window);
SDL_SetWindowPosition(window, x, y);
}
Vector2u Window::GetSize() const
{
RETURN_IF_NULLPTR(window, Vector2u());
int x = 0, y = 0;
SDL_GetWindowSize(window, &x, &y);
return Vector2u(x, y);
}
void Window::SetSize(Vector2u size)
{
RETURN_IF_NULLPTR(window);
SDL_SetWindowSize(window, size.x, size.y);
}
void Window::SetSize(unsigned int width, unsigned int height)
{
RETURN_IF_NULLPTR(window);
SDL_SetWindowSize(window, width, height);
}
std::string Window::GetTitle() const
{
RETURN_IF_NULLPTR(window, "");
return SDL_GetWindowTitle(window);
}
void Window::SetTitle(std::string title)
{
RETURN_IF_NULLPTR(window);
SDL_SetWindowTitle(window, title.c_str());
}
SDL_Window* const Window::GetWindow() const
{
return window;
}
void Window::SetVisible(bool visible)
{
RETURN_IF_NULLPTR(window);
if (visible)
SDL_ShowWindow(window);
else
SDL_HideWindow(window);
}
void Window::SetVsync(bool vsync)
{
// SDL actually doesn't allow you to change the VSync
// flag of a Renderer after it's been created. This
// Changes it globally for all other windows
SDL_GL_SetSwapInterval(vsync);
}
void Window::SetMouseCursorVisible(bool visible)
{
SDL_ShowCursor(visible);
}
void Window::SetMouseCursorGrabbed(bool grabbed)
{
SDL_SetWindowGrab(window, grabbed ? SDL_TRUE : SDL_FALSE);
}
void Window::SetIcon(Uint32 width, Uint32 height, const Uint8* pixels)
{
size_t size = static_cast<size_t>(width) * static_cast<size_t>(height) * 4;
void* _pixels = malloc(size);
memcpy(_pixels, pixels, size);
SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(_pixels,
width, height, 32, 32 * width,
SDL_PIXELFORMAT_RGBA8888);
SDL_SetWindowIcon(window, surface);
}
void Window::SetIcon(Uint32 width, Uint32 height, const Uint32* pixels)
{
size_t size = static_cast<size_t>(width) * static_cast<size_t>(height) * 4;
void* _pixels = malloc(size);
memcpy(_pixels, pixels, size);
SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(_pixels,
width, height, 32, 4 * width,
SDL_PIXELFORMAT_RGBA8888);
SDL_SetWindowIcon(window, surface);
}
void Window::SetIcon(SDL_Surface* icon)
{
SDL_SetWindowIcon(window, icon);
}
void Window::SetMouseCursor(SDL_Cursor* cursor)
{
SDL_SetCursor(cursor);
}
void Window::SetMouseCursor(SDL_Surface* surface, Vector2u clickspot)
{
SDL_Cursor* _cursor = SDL_CreateColorCursor(surface, clickspot.x, clickspot.y);
SDL_SetCursor(_cursor);
}
void Window::SetMouseCursor(const Uint8* pixels, Vector2u size, Vector2u clickspot)
{
size_t _size = static_cast<size_t>(size.x) * static_cast<size_t>(size.y) * 4;
void* _pixels = malloc(_size);
memcpy(_pixels, pixels, _size);
SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(_pixels,
size.x, size.y, 32, 8 * size.x, SDL_PIXELFORMAT_RGBA8888);
this->SetMouseCursor(surface, clickspot);
}
void Window::SetMouseCursor(const Uint32* pixels, Vector2u size, Vector2u clickspot)
{
size_t _size = static_cast<size_t>(size.x) * static_cast<size_t>(size.y) * 4;
void* _pixels = malloc(_size);
memcpy(_pixels, pixels, _size);
SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(_pixels,
size.x, size.y, 32, 8 * size.x, SDL_PIXELFORMAT_RGBA32);
this->SetMouseCursor(surface, clickspot);
}
void Window::OnCreate()
{
}
bool Window::OnResize()
{
return false;
}
void Window::OnClose()
{
}
SDLU_END