Added basic Rectangle
This commit is contained in:
parent
8dd9035ff8
commit
af64706ba7
|
@ -2,7 +2,7 @@ set(PNAME SDLU)
|
|||
|
||||
add_library(${PNAME}
|
||||
alibi.cpp SDLU.hpp Util.hpp
|
||||
"structures/Color.cpp" "structures/Mouse.cpp" "structures/Window.cpp" "graphics/RenderTarget.cpp" "graphics/drawable/Transformable.cpp")
|
||||
"structures/Color.cpp" "structures/Mouse.cpp" "structures/Window.cpp" "graphics/RenderTarget.cpp" "graphics/drawable/Transformable.cpp" "graphics/drawable/shapes/Rectangle.cpp")
|
||||
|
||||
set_property(TARGET ${PNAME} PROPERTY CXX_STANDARD 17)
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "graphics/RenderWindow.hpp"
|
||||
#include "graphics/Graphics.hpp"
|
||||
|
||||
#include "structures/Mouse.hpp"
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
#pragma once
|
||||
|
||||
#include <RenderTarget.hpp>
|
||||
#include <RenderWindow.hpp>
|
||||
#include <drawable/Drawable.hpp>
|
||||
#include "RenderWindow.hpp"
|
||||
#include "drawable/shapes/Rectangle.hpp"
|
|
@ -24,7 +24,9 @@ namespace sdlu
|
|||
|
||||
void RenderTarget::Draw(const Drawable& drawable)
|
||||
{
|
||||
drawable.Draw(*this);
|
||||
RETURN_IF_NULLPTR(renderer);
|
||||
|
||||
drawable.Draw(renderer);
|
||||
}
|
||||
|
||||
void RenderTarget::Display()
|
||||
|
|
|
@ -2,4 +2,6 @@ target_sources(${PNAME} PRIVATE
|
|||
${CMAKE_CURRENT_SOURCE_DIR}/Drawable.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Transformable.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Transformable.cpp
|
||||
)
|
||||
)
|
||||
|
||||
add_subdirectory( shapes )
|
|
@ -6,7 +6,7 @@
|
|||
*/
|
||||
#pragma once
|
||||
|
||||
#include <graphics/RenderTarget.hpp>
|
||||
#include <SDL.h>
|
||||
|
||||
namespace sdlu
|
||||
{
|
||||
|
@ -16,7 +16,6 @@ namespace sdlu
|
|||
class Drawable
|
||||
{
|
||||
public:
|
||||
Drawable() = delete;
|
||||
Drawable(const Drawable& other) = delete;
|
||||
Drawable(Drawable&& other) = delete;
|
||||
Drawable& operator=(const Drawable& other) = delete;
|
||||
|
@ -24,6 +23,7 @@ namespace sdlu
|
|||
friend class RenderTarget;
|
||||
|
||||
protected:
|
||||
virtual void Draw(RenderTarget& target) const = 0;
|
||||
Drawable() { }
|
||||
virtual void Draw(SDL_Renderer* const target) const = 0;
|
||||
};
|
||||
}
|
|
@ -3,8 +3,8 @@
|
|||
namespace sdlu
|
||||
{
|
||||
Transformable::Transformable() :
|
||||
m_oPosition(0, 0), m_oOrigin(0, 0),
|
||||
m_oScale(1.f, 1.f), m_fRotation(0.f)
|
||||
position(0, 0), origin(0, 0),
|
||||
scale(1.f, 1.f), rotation(0.f)
|
||||
{
|
||||
// Empty
|
||||
}
|
||||
|
@ -16,80 +16,80 @@ namespace sdlu
|
|||
|
||||
Vector2f Transformable::GetPosition()
|
||||
{
|
||||
return m_oPosition;
|
||||
return position;
|
||||
}
|
||||
|
||||
void Transformable::SetPosition(const Vector2f& position)
|
||||
{
|
||||
m_oPosition = position;
|
||||
this->position = position;
|
||||
}
|
||||
|
||||
void Transformable::SetPosition(float x, float y)
|
||||
{
|
||||
m_oPosition = Vector2f(x, y);
|
||||
position = Vector2f(x, y);
|
||||
}
|
||||
|
||||
void Transformable::Move(const Vector2f& position)
|
||||
{
|
||||
m_oPosition += position;
|
||||
this->position += position;
|
||||
}
|
||||
|
||||
void Transformable::Move(float x, float y)
|
||||
{
|
||||
m_oPosition += Vector2f(x, y);
|
||||
position += Vector2f(x, y);
|
||||
}
|
||||
Vector2f Transformable::GetOrigin()
|
||||
{
|
||||
return m_oOrigin;
|
||||
return origin;
|
||||
}
|
||||
|
||||
void Transformable::SetOrigin(const Vector2f& origin)
|
||||
{
|
||||
m_oOrigin = origin;
|
||||
this->origin = origin;
|
||||
}
|
||||
|
||||
void Transformable::SetOrigin(float x, float y)
|
||||
{
|
||||
m_oOrigin = Vector2f(x, y);
|
||||
origin = Vector2f(x, y);
|
||||
}
|
||||
|
||||
Vector2f Transformable::GetScale()
|
||||
{
|
||||
return m_oScale;
|
||||
return scale;
|
||||
}
|
||||
|
||||
void Transformable::SetScale(const Vector2f& scale)
|
||||
{
|
||||
m_oScale = scale;
|
||||
this->scale = scale;
|
||||
}
|
||||
|
||||
void Transformable::SetScale(float x, float y)
|
||||
{
|
||||
m_oScale = Vector2f(x, y);
|
||||
scale = Vector2f(x, y);
|
||||
}
|
||||
|
||||
void Transformable::Scale(const Vector2f& scale)
|
||||
{
|
||||
m_oScale += scale;
|
||||
this->scale += scale;
|
||||
}
|
||||
|
||||
void Transformable::Scale(float x, float y)
|
||||
{
|
||||
m_oScale += Vector2f(x, y);
|
||||
scale += Vector2f(x, y);
|
||||
}
|
||||
|
||||
float Transformable::GetRotation()
|
||||
{
|
||||
return m_fRotation;
|
||||
return rotation;
|
||||
}
|
||||
|
||||
void Transformable::SetRotation(float angle)
|
||||
{
|
||||
m_fRotation = angle;
|
||||
rotation = angle;
|
||||
}
|
||||
|
||||
void Transformable::Rotate(float angle)
|
||||
{
|
||||
m_fRotation += angle;
|
||||
rotation += angle;
|
||||
}
|
||||
}
|
|
@ -149,10 +149,10 @@ namespace sdlu
|
|||
*/
|
||||
void Rotate(float angle);
|
||||
|
||||
private:
|
||||
Vector2f m_oPosition;
|
||||
Vector2f m_oOrigin;
|
||||
Vector2f m_oScale;
|
||||
float m_fRotation;
|
||||
protected:
|
||||
Vector2f position; ///< Position of the object
|
||||
Vector2f origin; ///< Offset of the top-left corner from the position
|
||||
Vector2f scale; ///< Scale of the object
|
||||
float rotation; ///< Rotation of the object (in degrees)
|
||||
};
|
||||
}
|
6
SDLU/graphics/drawable/shapes/CMakeLists.txt
Normal file
6
SDLU/graphics/drawable/shapes/CMakeLists.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
target_sources(${PNAME} PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Shape.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Shape.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Rectangle.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Rectangle.cpp
|
||||
)
|
44
SDLU/graphics/drawable/shapes/Rectangle.cpp
Normal file
44
SDLU/graphics/drawable/shapes/Rectangle.cpp
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include "Rectangle.hpp"
|
||||
|
||||
#include <graphics/RenderTarget.hpp>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
64
SDLU/graphics/drawable/shapes/Rectangle.hpp
Normal file
64
SDLU/graphics/drawable/shapes/Rectangle.hpp
Normal file
|
@ -0,0 +1,64 @@
|
|||
/**
|
||||
* @file Rectangle.hpp
|
||||
* @brief The primitive Rectangle shape
|
||||
* @author Lauchmelder23
|
||||
* @date 23.05.2020
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "Shape.hpp"
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
namespace sdlu
|
||||
{
|
||||
class Rectangle :
|
||||
public Shape
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Default constructor
|
||||
*/
|
||||
Rectangle();
|
||||
|
||||
/**
|
||||
* @brief Constructor with default parameters
|
||||
*
|
||||
* @param[in] position A 2D position vector
|
||||
* @param[in] size A 2D size vector
|
||||
*/
|
||||
Rectangle(const Vector2f& position, const Vector2f& size);
|
||||
|
||||
/**
|
||||
* @brief Gets the size of the rectangle
|
||||
*
|
||||
* @return A 2D vector with the size information
|
||||
*/
|
||||
Vector2f GetSize();
|
||||
|
||||
/**
|
||||
* @brief Sets a new size for the rectangle
|
||||
*
|
||||
* @param[in] size A 2D vector with the size information
|
||||
*/
|
||||
void SetSize(const Vector2f& size);
|
||||
|
||||
/**
|
||||
* @brief Sets a new size for the rectangle
|
||||
*
|
||||
* @param[in] x The new size in x direction
|
||||
* @param[in] y The new size in y direction
|
||||
*/
|
||||
void SetSize(float x, float y);
|
||||
|
||||
/**
|
||||
* @brief Draws the Rectangle to the target
|
||||
*
|
||||
* @param[in] target The RenderTarget to draw to
|
||||
*/
|
||||
virtual void Draw(SDL_Renderer* const target) const override;
|
||||
|
||||
private:
|
||||
Vector2f size; ///< Size of the rectangle
|
||||
};
|
||||
}
|
23
SDLU/graphics/drawable/shapes/Shape.cpp
Normal file
23
SDLU/graphics/drawable/shapes/Shape.cpp
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "Shape.hpp"
|
||||
|
||||
namespace sdlu
|
||||
{
|
||||
Shape::~Shape()
|
||||
{
|
||||
}
|
||||
|
||||
void Shape::SetColor(const Color& color)
|
||||
{
|
||||
this->color = color;
|
||||
}
|
||||
|
||||
Color Shape::GetColor()
|
||||
{
|
||||
return color;
|
||||
}
|
||||
|
||||
Shape::Shape() :
|
||||
Drawable(), Transformable()
|
||||
{
|
||||
}
|
||||
}
|
46
SDLU/graphics/drawable/shapes/Shape.hpp
Normal file
46
SDLU/graphics/drawable/shapes/Shape.hpp
Normal file
|
@ -0,0 +1,46 @@
|
|||
/**
|
||||
* @file Shape.hpp
|
||||
* @brief The base class for all native SDLU shapes
|
||||
* @author Lauchmelder23
|
||||
* @date 23.05.2020
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <graphics/drawable/Drawable.hpp>
|
||||
#include <graphics/drawable/Transformable.hpp>
|
||||
#include <structures/Color.hpp>
|
||||
|
||||
namespace sdlu
|
||||
{
|
||||
/**
|
||||
* @brief The non-instantiable base class for all SDLU shapes
|
||||
*/
|
||||
class Shape :
|
||||
public Drawable, public Transformable
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Deconstructor
|
||||
*/
|
||||
virtual ~Shape();
|
||||
|
||||
/**
|
||||
* @brief Sets the color of the shape
|
||||
*/
|
||||
void SetColor(const Color& color);
|
||||
|
||||
/**
|
||||
* @brief Gets the color of the shape
|
||||
*/
|
||||
Color GetColor();
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @brief Default constructor
|
||||
*/
|
||||
Shape();
|
||||
|
||||
protected:
|
||||
Color color;
|
||||
};
|
||||
}
|
|
@ -30,6 +30,9 @@ int main(int argc, char** argv)
|
|||
|
||||
SDL_Event event;
|
||||
|
||||
sdlu::Rectangle rect(sdlu::Vec2f(100, 100), sdlu::Vec2f(300, 200));
|
||||
rect.SetColor(sdlu::Color::Blue);
|
||||
|
||||
std::string title = "";
|
||||
while (window.IsOpen())
|
||||
{
|
||||
|
@ -53,6 +56,8 @@ int main(int argc, char** argv)
|
|||
window.Clear(sdlu::Color::FromHSV(std::atan2(mousePos.y, mousePos.x) / PI * 180 + 180,
|
||||
100, 100));
|
||||
|
||||
window.Draw(rect);
|
||||
|
||||
window.Display();
|
||||
|
||||
diff = std::chrono::duration_cast<std::chrono::microseconds>
|
||||
|
|
Loading…
Reference in a new issue