From 8dd9035ff8ef778afd64a9e363b1da1bd1c4b0e3 Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 23 May 2020 15:20:19 +0200 Subject: [PATCH] Added Transformable class --- SDLU/CMakeLists.txt | 2 +- SDLU/graphics/CMakeLists.txt | 5 +- SDLU/graphics/Graphics.hpp | 5 + SDLU/graphics/RenderTarget.cpp | 5 + SDLU/graphics/RenderTarget.hpp | 8 ++ SDLU/graphics/drawable/CMakeLists.txt | 5 + SDLU/graphics/drawable/Drawable.hpp | 29 +++++ SDLU/graphics/drawable/Transformable.cpp | 95 ++++++++++++++ SDLU/graphics/drawable/Transformable.hpp | 158 +++++++++++++++++++++++ 9 files changed, 310 insertions(+), 2 deletions(-) create mode 100644 SDLU/graphics/Graphics.hpp create mode 100644 SDLU/graphics/drawable/CMakeLists.txt create mode 100644 SDLU/graphics/drawable/Drawable.hpp create mode 100644 SDLU/graphics/drawable/Transformable.cpp create mode 100644 SDLU/graphics/drawable/Transformable.hpp diff --git a/SDLU/CMakeLists.txt b/SDLU/CMakeLists.txt index 1e4fe4d..b7a915e 100644 --- a/SDLU/CMakeLists.txt +++ b/SDLU/CMakeLists.txt @@ -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") + "structures/Color.cpp" "structures/Mouse.cpp" "structures/Window.cpp" "graphics/RenderTarget.cpp" "graphics/drawable/Transformable.cpp") set_property(TARGET ${PNAME} PROPERTY CXX_STANDARD 17) diff --git a/SDLU/graphics/CMakeLists.txt b/SDLU/graphics/CMakeLists.txt index 13a58eb..f67eed2 100644 --- a/SDLU/graphics/CMakeLists.txt +++ b/SDLU/graphics/CMakeLists.txt @@ -1,6 +1,9 @@ target_sources(${PNAME} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/Graphics.hpp ${CMAKE_CURRENT_SOURCE_DIR}/RenderWindow.hpp ${CMAKE_CURRENT_SOURCE_DIR}/RenderWindow.cpp ${CMAKE_CURRENT_SOURCE_DIR}/RenderTarget.hpp ${CMAKE_CURRENT_SOURCE_DIR}/RenderTarget.cpp -) \ No newline at end of file +) + +add_subdirectory(drawable) \ No newline at end of file diff --git a/SDLU/graphics/Graphics.hpp b/SDLU/graphics/Graphics.hpp new file mode 100644 index 0000000..21f3710 --- /dev/null +++ b/SDLU/graphics/Graphics.hpp @@ -0,0 +1,5 @@ +#pragma once + +#include +#include +#include \ No newline at end of file diff --git a/SDLU/graphics/RenderTarget.cpp b/SDLU/graphics/RenderTarget.cpp index f3e615c..929311a 100644 --- a/SDLU/graphics/RenderTarget.cpp +++ b/SDLU/graphics/RenderTarget.cpp @@ -22,6 +22,11 @@ namespace sdlu SDL_RenderClear(renderer); } + void RenderTarget::Draw(const Drawable& drawable) + { + drawable.Draw(*this); + } + void RenderTarget::Display() { RETURN_IF_NULLPTR(renderer); diff --git a/SDLU/graphics/RenderTarget.hpp b/SDLU/graphics/RenderTarget.hpp index 30f03f2..d4531f4 100644 --- a/SDLU/graphics/RenderTarget.hpp +++ b/SDLU/graphics/RenderTarget.hpp @@ -10,6 +10,7 @@ #include #include +#include namespace sdlu { @@ -29,6 +30,13 @@ namespace sdlu */ void Clear(const Color& color = Color::Black); + /** + * @brief Draws a sdlu::Drawable to the SDL_Renderer + * + * @param[in] drawable A reference to a derived class of Drawable + */ + void Draw(const Drawable& drawable); + /** * @brief Display the current state of the renderer to the screen */ diff --git a/SDLU/graphics/drawable/CMakeLists.txt b/SDLU/graphics/drawable/CMakeLists.txt new file mode 100644 index 0000000..38e19d0 --- /dev/null +++ b/SDLU/graphics/drawable/CMakeLists.txt @@ -0,0 +1,5 @@ +target_sources(${PNAME} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/Drawable.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/Transformable.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/Transformable.cpp +) \ No newline at end of file diff --git a/SDLU/graphics/drawable/Drawable.hpp b/SDLU/graphics/drawable/Drawable.hpp new file mode 100644 index 0000000..6288305 --- /dev/null +++ b/SDLU/graphics/drawable/Drawable.hpp @@ -0,0 +1,29 @@ +/** + * @file Drawable.hpp + * @brief The base class of everything renderable by RenderTarget + * @author Lauchmelder23 + * @date 20.05.2020 + */ +#pragma once + +#include + +namespace sdlu +{ + /** + * @brief Everything that can be rendered derives from this class. + */ + class Drawable + { + public: + Drawable() = delete; + Drawable(const Drawable& other) = delete; + Drawable(Drawable&& other) = delete; + Drawable& operator=(const Drawable& other) = delete; + + friend class RenderTarget; + + protected: + virtual void Draw(RenderTarget& target) const = 0; + }; +} \ No newline at end of file diff --git a/SDLU/graphics/drawable/Transformable.cpp b/SDLU/graphics/drawable/Transformable.cpp new file mode 100644 index 0000000..3390f21 --- /dev/null +++ b/SDLU/graphics/drawable/Transformable.cpp @@ -0,0 +1,95 @@ +#include "Transformable.hpp" + +namespace sdlu +{ + Transformable::Transformable() : + m_oPosition(0, 0), m_oOrigin(0, 0), + m_oScale(1.f, 1.f), m_fRotation(0.f) + { + // Empty + } + + Transformable::~Transformable() + { + // Empty + } + + Vector2f Transformable::GetPosition() + { + return m_oPosition; + } + + void Transformable::SetPosition(const Vector2f& position) + { + m_oPosition = position; + } + + void Transformable::SetPosition(float x, float y) + { + m_oPosition = Vector2f(x, y); + } + + void Transformable::Move(const Vector2f& position) + { + m_oPosition += position; + } + + void Transformable::Move(float x, float y) + { + m_oPosition += Vector2f(x, y); + } + Vector2f Transformable::GetOrigin() + { + return m_oOrigin; + } + + void Transformable::SetOrigin(const Vector2f& origin) + { + m_oOrigin = origin; + } + + void Transformable::SetOrigin(float x, float y) + { + m_oOrigin = Vector2f(x, y); + } + + Vector2f Transformable::GetScale() + { + return m_oScale; + } + + void Transformable::SetScale(const Vector2f& scale) + { + m_oScale = scale; + } + + void Transformable::SetScale(float x, float y) + { + m_oScale = Vector2f(x, y); + } + + void Transformable::Scale(const Vector2f& scale) + { + m_oScale += scale; + } + + void Transformable::Scale(float x, float y) + { + m_oScale += Vector2f(x, y); + } + + float Transformable::GetRotation() + { + return m_fRotation; + } + + void Transformable::SetRotation(float angle) + { + m_fRotation = angle; + } + + void Transformable::Rotate(float angle) + { + m_fRotation += angle; + } +} \ No newline at end of file diff --git a/SDLU/graphics/drawable/Transformable.hpp b/SDLU/graphics/drawable/Transformable.hpp new file mode 100644 index 0000000..4317456 --- /dev/null +++ b/SDLU/graphics/drawable/Transformable.hpp @@ -0,0 +1,158 @@ +/** + * @file Transformable.hpp + * @brief Contains information for transformable objects + * @author Lauchmelder23 + * @date 23.05.2020 + */ +#pragma once + +#include + +namespace sdlu +{ + /** + * @brief A class that stores locational information + * + * Stores position, rotation, scale and the origin of an + * object, and provides functions to get/set those values. + * "Origin" is the offset between the position and the top-left + * corner of the object. + */ + class Transformable + { + public: + /** + * @brief Default constructur + */ + Transformable(); + + /** + * @brief Deconstructor + */ + virtual ~Transformable(); + + /** + * @brief Returns the position of the object + * + * @return A 2D vector of the position + */ + Vector2f GetPosition(); + + /** + * @brief Sets a new position + * + * @param[in] position A 2D vector with the new position + */ + void SetPosition(const Vector2f& position); + + /** + * @brief Sets a new position + * + * @param[in] x The new x position + * @param[in] y The new y position + */ + void SetPosition(float x, float y); + + /** + * @brief Adds to the current position + * + * @param[in] position A 2D movement vector + */ + void Move(const Vector2f& position); + + /** + * @brief Adds to the current position + * + * @param[in] x The offset in x direction + * @param[in] y The offset in y direction + */ + void Move(float x, float y); + + + /** + * @brief Gets the current local origin + * + * @return A 2D vector with the offset + */ + Vector2f GetOrigin(); + + /** + * @brief Sets a new local origin + * + * @param[in] origin A 2D vector with the new origin + */ + void SetOrigin(const Vector2f& origin); + + /** + * @brief Sets a new local origin + * + * @param[in] x The new x component of the origin + * @param[in] y The new y component of the origin + */ + void SetOrigin(float x, float y); + + /** + * @brief Gets the current scale of the object + * + * @return A 2D vector with the scale in x- and y-direction + */ + Vector2f GetScale(); + + /** + * @brief Sets a new scale + * + * @param[in] scale A 2D vector with the new scale + */ + void SetScale(const Vector2f& scale); + + /** + * @brief Sets a new scale + * + * @param[in] x The new scale in x direction + * @param[in] y The new scale in y direction + */ + void SetScale(float x, float y); + + /** + * @brief Scales the object by some amount + * + * @param[in] scale The amount to scale by in x- and y-direction + */ + void Scale(const Vector2f& scale); + + /** + * @brief Scales the object by some amount + * + * @param[in] x The amount to scale by in x direction + * @param[in] y The amount to scale by in y direction + */ + void Scale(float x, float y); + + /** + * @brief Gets the current rotation + * + * @return The rotation in degrees + */ + float GetRotation(); + + /** + * @brief Sets a new rotation + * + * @param[in] angle The new rotation in degrees + */ + void SetRotation(float angle); + + /** + * @brief Rotates by some amount + * + * @param[in] angle The angle to rotate by in degrees + */ + void Rotate(float angle); + + private: + Vector2f m_oPosition; + Vector2f m_oOrigin; + Vector2f m_oScale; + float m_fRotation; + }; +} \ No newline at end of file