diff --git a/SDLU/CMakeLists.txt b/SDLU/CMakeLists.txt index b7a915e..6b2499d 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" "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) diff --git a/SDLU/SDLU.hpp b/SDLU/SDLU.hpp index 89acf36..0a5c61f 100644 --- a/SDLU/SDLU.hpp +++ b/SDLU/SDLU.hpp @@ -1,6 +1,6 @@ #pragma once -#include "graphics/RenderWindow.hpp" +#include "graphics/Graphics.hpp" #include "structures/Mouse.hpp" diff --git a/SDLU/graphics/Graphics.hpp b/SDLU/graphics/Graphics.hpp index 21f3710..9cd7b34 100644 --- a/SDLU/graphics/Graphics.hpp +++ b/SDLU/graphics/Graphics.hpp @@ -1,5 +1,4 @@ #pragma once -#include -#include -#include \ No newline at end of file +#include "RenderWindow.hpp" +#include "drawable/shapes/Rectangle.hpp" \ No newline at end of file diff --git a/SDLU/graphics/RenderTarget.cpp b/SDLU/graphics/RenderTarget.cpp index 929311a..fe475c6 100644 --- a/SDLU/graphics/RenderTarget.cpp +++ b/SDLU/graphics/RenderTarget.cpp @@ -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() diff --git a/SDLU/graphics/drawable/CMakeLists.txt b/SDLU/graphics/drawable/CMakeLists.txt index 38e19d0..de69ad1 100644 --- a/SDLU/graphics/drawable/CMakeLists.txt +++ b/SDLU/graphics/drawable/CMakeLists.txt @@ -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 -) \ No newline at end of file +) + +add_subdirectory( shapes ) \ No newline at end of file diff --git a/SDLU/graphics/drawable/Drawable.hpp b/SDLU/graphics/drawable/Drawable.hpp index 6288305..1cbe97e 100644 --- a/SDLU/graphics/drawable/Drawable.hpp +++ b/SDLU/graphics/drawable/Drawable.hpp @@ -6,7 +6,7 @@ */ #pragma once -#include +#include 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; }; } \ No newline at end of file diff --git a/SDLU/graphics/drawable/Transformable.cpp b/SDLU/graphics/drawable/Transformable.cpp index 3390f21..965c61a 100644 --- a/SDLU/graphics/drawable/Transformable.cpp +++ b/SDLU/graphics/drawable/Transformable.cpp @@ -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; } } \ No newline at end of file diff --git a/SDLU/graphics/drawable/Transformable.hpp b/SDLU/graphics/drawable/Transformable.hpp index 4317456..cd9cd4b 100644 --- a/SDLU/graphics/drawable/Transformable.hpp +++ b/SDLU/graphics/drawable/Transformable.hpp @@ -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) }; } \ No newline at end of file diff --git a/SDLU/graphics/drawable/shapes/CMakeLists.txt b/SDLU/graphics/drawable/shapes/CMakeLists.txt new file mode 100644 index 0000000..bb0acb3 --- /dev/null +++ b/SDLU/graphics/drawable/shapes/CMakeLists.txt @@ -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 +) \ No newline at end of file diff --git a/SDLU/graphics/drawable/shapes/Rectangle.cpp b/SDLU/graphics/drawable/shapes/Rectangle.cpp new file mode 100644 index 0000000..8c3354f --- /dev/null +++ b/SDLU/graphics/drawable/shapes/Rectangle.cpp @@ -0,0 +1,44 @@ +#include "Rectangle.hpp" + +#include + +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); + } +} \ No newline at end of file diff --git a/SDLU/graphics/drawable/shapes/Rectangle.hpp b/SDLU/graphics/drawable/shapes/Rectangle.hpp new file mode 100644 index 0000000..fea0612 --- /dev/null +++ b/SDLU/graphics/drawable/shapes/Rectangle.hpp @@ -0,0 +1,64 @@ +/** + * @file Rectangle.hpp + * @brief The primitive Rectangle shape + * @author Lauchmelder23 + * @date 23.05.2020 + */ +#pragma once + +#include "Shape.hpp" + +#include + +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 + }; +} \ No newline at end of file diff --git a/SDLU/graphics/drawable/shapes/Shape.cpp b/SDLU/graphics/drawable/shapes/Shape.cpp new file mode 100644 index 0000000..5cb6a50 --- /dev/null +++ b/SDLU/graphics/drawable/shapes/Shape.cpp @@ -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() + { + } +} \ No newline at end of file diff --git a/SDLU/graphics/drawable/shapes/Shape.hpp b/SDLU/graphics/drawable/shapes/Shape.hpp new file mode 100644 index 0000000..f8e03e6 --- /dev/null +++ b/SDLU/graphics/drawable/shapes/Shape.hpp @@ -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 +#include +#include + +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; + }; +} \ No newline at end of file diff --git a/SDLU_Example/main.cpp b/SDLU_Example/main.cpp index e23b71a..2844e0d 100644 --- a/SDLU_Example/main.cpp +++ b/SDLU_Example/main.cpp @@ -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