Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
rectangle-shape.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4
5#include "flint/core/api.h"
6#include "flint/graphics/shape.h"
7#include "flint/graphics/types/color.h"
8#include "flint/math/types.h"
9
10#include <algorithm>
11#include <cmath>
12
13namespace Flint {
14
21 class RectangleShape : public Shape {
22 public:
23 RectangleShape() = default;
24
31 FLINT_API RectangleShape(Vec2f size, Color fill = {255, 255, 255, 255});
32
38 FLINT_API void setSize(Vec2f newSize);
39
45 void setOrigin(Vec2f newOrigin) {
46 m_origin = newOrigin;
47 }
48
54 void setScale(Vec2f newScale) {
55 m_scale = newScale;
56 }
57
63 void setRotation(float newRotation) {
64 m_rotation = newRotation;
65 }
66
68 [[nodiscard]] Vec2f getSize() const {
69 return m_size;
70 }
71
73 [[nodiscard]] const Vec2f &getOrigin() const {
74 return m_origin;
75 }
76
78 [[nodiscard]] const Vec2f &getScale() const {
79 return m_scale;
80 }
81
83 [[nodiscard]] float getRotation() const {
84 return m_rotation;
85 }
86
91 [[nodiscard]] FloatRect getGlobalBounds() const override {
92 const Vec2f scaledSize{m_size.x * m_scale.x, m_size.y * m_scale.y};
93 const Vec2f scaledOrigin{m_origin.x * m_scale.x, m_origin.y * m_scale.y};
94 const Vec2f topLeft = m_position - scaledOrigin;
95
96 if (m_rotation == 0.f) {
97 return FloatRect(topLeft, scaledSize);
98 }
99
100 const float c = std::cos(m_rotation);
101 const float s = std::sin(m_rotation);
102
103 const auto rotate = [&](const Vec2f &point) {
104 const Vec2f local = point - m_position;
105
106 return Vec2f{m_position.x + local.x * c - local.y * s,
107 m_position.y + local.x * s + local.y * c};
108 };
109
110 const Vec2f p0 = rotate(topLeft);
111 const Vec2f p1 = rotate({topLeft.x + scaledSize.x, topLeft.y});
112 const Vec2f p2 = rotate({topLeft.x + scaledSize.x, topLeft.y + scaledSize.y});
113 const Vec2f p3 = rotate({topLeft.x, topLeft.y + scaledSize.y});
114
115 const float minX = std::min({p0.x, p1.x, p2.x, p3.x});
116 const float minY = std::min({p0.y, p1.y, p2.y, p3.y});
117 const float maxX = std::max({p0.x, p1.x, p2.x, p3.x});
118 const float maxY = std::max({p0.y, p1.y, p2.y, p3.y});
119
120 return FloatRect({minX, minY}, {maxX - minX, maxY - minY});
121 }
122
126 FLINT_API void emit(RenderQueue &queue) const override;
127
128 private:
129 Vec2f m_size{0.f, 0.f};
130 Vec2f m_origin{0.f, 0.f};
131 Vec2f m_scale{1.f, 1.f};
132 float m_rotation = 0.f;
133 };
134
135}
A 2D axis-aligned rectangle rendered as a textured quad.
void setRotation(float newRotation)
Set the rotation angle.
float getRotation() const
Get the current rotation angle in radians.
FLINT_API RectangleShape(Vec2f size, Color fill={255, 255, 255, 255})
Construct a rectangle with the given dimensions and fill color.
const Vec2f & getOrigin() const
Get the current local origin pivot point.
void setOrigin(Vec2f newOrigin)
Set the local pivot point used for rotation and positioning.
void setScale(Vec2f newScale)
Apply a per-axis scale factor.
FloatRect getGlobalBounds() const override
Get the axis-aligned bounding box of the rectangle in world space.
FLINT_API void emit(RenderQueue &queue) const override
Emit this drawable's rendering work for the current frame.
const Vec2f & getScale() const
Get the current scale factors.
FLINT_API void setSize(Vec2f newSize)
Set the rectangle dimensions.
Vec2f getSize() const
Get the current dimensions in world-space units.
Batches drawables into sorted geometry, primitive, and custom execution passes for GPURenderer.
Abstract base class for 2-D geometric shapes.
Definition shape.h:20
RGBA color with 8-bit unsigned components.
Definition color.h:13