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

View file

@ -0,0 +1,79 @@
#include "graphics/RenderTarget.hpp"
#include <SDL.h>
#include <Util.hpp>
SDLU_BEGIN
RenderTarget::~RenderTarget()
{
RETURN_IF_NULLPTR(renderer);
SDL_DestroyRenderer(renderer);
}
void RenderTarget::Clear(const Color& color)
{
RETURN_IF_NULLPTR(renderer);
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
SDL_RenderClear(renderer);
}
void RenderTarget::Draw(const Drawable& drawable)
{
RETURN_IF_NULLPTR(renderer);
drawable.Draw(renderer);
}
void RenderTarget::Display()
{
RETURN_IF_NULLPTR(renderer);
SDL_RenderPresent(renderer);
if (m_oFramerate != 0)
{
Uint64 diff = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - m_oTimeSinceLastDisplay).count();
if (diff < 1000 / m_oFramerate)
{
SDL_Delay(static_cast<Uint32>(1000 / m_oFramerate - diff));
}
}
m_oTimeSinceLastDisplay = std::chrono::steady_clock::now();
}
void RenderTarget::SetMaxFramerate(Uint32 max)
{
m_oFramerate = max;
}
RenderTarget::RenderTarget(SDL_Window* target) :
renderer(nullptr), m_oFramerate(0)
{
RETURN_IF_NOT_NULLPTR(renderer);
renderer = SDL_CreateRenderer(target, -1, SDL_RENDERER_ACCELERATED);
THROW_IF(IS_NULLPTR(renderer),
std::runtime_error("Failed to create SDL_Renderer* from SDL_Window*: " + std::string(SDL_GetError())));
m_oTimeSinceLastDisplay = std::chrono::steady_clock::now();
}
RenderTarget::RenderTarget(SDL_Surface* target) :
renderer(nullptr), m_oFramerate(0)
{
m_oFramerate = 0;
RETURN_IF_NOT_NULLPTR(renderer);
renderer = SDL_CreateSoftwareRenderer(target);
THROW_IF(IS_NULLPTR(renderer),
std::runtime_error("Failed to create SDL_Renderer* from SDL_Surface*: " + std::string(SDL_GetError())));
m_oTimeSinceLastDisplay = std::chrono::steady_clock::now();
}
SDLU_END

View file

@ -0,0 +1,37 @@
#include "graphics/RenderWindow.hpp"
#include <cstring>
#include <Util.hpp>
SDLU_BEGIN
RenderWindow::RenderWindow() :
Window(), RenderTarget(window)
{
// Empty
}
RenderWindow::RenderWindow(Vector2u dimension, const std::string& title,
Uint32 windowFlags) :
Window(dimension, title, windowFlags), RenderTarget(window)
{
// Empty
}
RenderWindow::~RenderWindow()
{
// Empty
}
void RenderWindow::OnCreate()
{
}
bool RenderWindow::OnResize()
{
return false;
}
void RenderWindow::OnClose()
{
}
SDLU_END

View file

@ -0,0 +1,94 @@
#include "graphics/drawable/Transformable.hpp"
SDLU_BEGIN
Transformable::Transformable() :
position(0, 0), origin(0, 0),
scale(1.f, 1.f), rotation(0.f)
{
// Empty
}
Transformable::~Transformable()
{
// Empty
}
Vector2f Transformable::GetPosition()
{
return position;
}
void Transformable::SetPosition(const Vector2f& position)
{
this->position = position;
}
void Transformable::SetPosition(float x, float y)
{
position = Vector2f(x, y);
}
void Transformable::Move(const Vector2f& position)
{
this->position += position;
}
void Transformable::Move(float x, float y)
{
position += Vector2f(x, y);
}
Vector2f Transformable::GetOrigin()
{
return origin;
}
void Transformable::SetOrigin(const Vector2f& origin)
{
this->origin = origin;
}
void Transformable::SetOrigin(float x, float y)
{
origin = Vector2f(x, y);
}
Vector2f Transformable::GetScale()
{
return scale;
}
void Transformable::SetScale(const Vector2f& scale)
{
this->scale = scale;
}
void Transformable::SetScale(float x, float y)
{
scale = Vector2f(x, y);
}
void Transformable::Scale(const Vector2f& scale)
{
this->scale += scale;
}
void Transformable::Scale(float x, float y)
{
scale += Vector2f(x, y);
}
float Transformable::GetRotation()
{
return rotation;
}
void Transformable::SetRotation(float angle)
{
rotation = angle;
}
void Transformable::Rotate(float angle)
{
rotation += angle;
}
SDLU_END

View file

@ -0,0 +1,45 @@
#include <graphics/drawable/shapes/Rectangle.hpp>
#include <graphics/RenderTarget.hpp>
#include <SDL.h>
namespace sdlu
{
Rectangle::Rectangle() :
Shape()
{
}
Rectangle::Rectangle(const Vector2f& position, const Vector2f& size) :
Shape()
{
this->position = position;
this->size = size;
}
Vector2f Rectangle::GetSize()
{
return this->size;
}
void Rectangle::SetSize(const Vector2f& size)
{
this->size = size;
}
void Rectangle::SetSize(float x, float y)
{
this->size = Vector2f(x, y);
}
void Rectangle::Draw(SDL_Renderer* const renderer) const
{
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
SDL_Rect rect;
rect.x = position.x;
rect.y = position.y;
rect.w = size.x;
rect.h = size.y;
SDL_RenderFillRect(renderer, &rect);
}
}

View file

@ -0,0 +1,23 @@
#include <graphics/drawable/shapes/Shape.hpp>
namespace sdlu
{
Shape::~Shape()
{
}
void Shape::SetColor(const Color& color)
{
this->color = color;
}
Color Shape::GetColor()
{
return color;
}
Shape::Shape() :
Drawable(), Transformable()
{
}
}