Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
scene_component_2d.hpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin <alexanderobzherin@gmail.com>
3// Copyright (c) 2026 Alexander Obzherin
4// Distributed under the terms of the zlib License. See LICENSE.md for details.
5
6#pragma once
7
9
10#include <glm/glm.hpp>
11#include <glm/gtc/matrix_transform.hpp>
12
13namespace rendering_engine
14{
15/**
16 * @class SceneComponent2D
17 * @brief Represents a hierarchical 2D transform component.
18 *
19 * SceneComponent2D stores local 2D transformation state:
20 * - Position (x, y)
21 * - Rotation (degrees, counterclockwise around Z-axis)
22 * - Scale (x, y)
23 *
24 * It supports parent-child relationships, allowing hierarchical
25 * transform propagation. When attached to a parent component,
26 * its world transform is computed as:
27 *
28 * World = ParentWorld * Local
29 *
30 * The engine uses a left-handed coordinate system:
31 * - X+ : Right
32 * - Y+ : Up
33 *
34 * Transform order is:
35 *
36 * M = T * R * S
37 *
38 * which results in object-space vertices being scaled, then rotated,
39 * then translated when applied.
40 *
41 * The component uses a dirty-flag mechanism. World matrices are
42 * recomputed lazily when requested.
43 *
44 * @note
45 * - This component operates strictly in the 2D domain.
46 * - Rotation is always around the Z-axis.
47 * - Ownership and lifetime are managed externally (e.g., by Actor2D).
48 *
49 * @see Actor2D
50 */
52{
53public:
54 /**
55 * @brief Constructs a SceneComponent2D with identity transform.
56 *
57 * Initial state:
58 * - Position = (0, 0)
59 * - Rotation = 0 degrees
60 * - Scale = (1, 1)
61 * - No parent attached
62 */
64
65 /**
66 * @brief Destructor.
67 */
68 virtual ~SceneComponent2D() = default;
69
70 /**
71 * @brief Sets the local position of this component.
72 *
73 * Marks the component as dirty so that world transformation
74 * will be recalculated when needed.
75 *
76 * @param position Local position in 2D space.
77 */
78 void SetPosition(const glm::vec2& position);
79
80 /**
81 * @brief Sets the rotation angle in degrees.
82 * @param angleDegrees Angle in degrees (counterclockwise).
83 */
84 void SetRotation(float angleDegrees);
85
86 /**
87 * @brief Sets the scale in each dimension.
88 * @param scale New scale as glm::vec2.
89 */
90 void SetScale(const glm::vec2& scale);
91
92 /**
93 * @brief Gets the current position.
94 * @return The position as glm::vec2.
95 */
96 const glm::vec2& GetPosition() const { return mPosition; }
97
98 /**
99 * @brief Gets the current rotation angle (in degrees).
100 * @return The rotation angle (degrees).
101 */
102 float GetRotation() const { return mRotation; }
103
104 /**
105 * @brief Gets the current scale.
106 * @return The scale as glm::vec2.
107 */
108 const glm::vec2& GetScale() const { return mScale; }
109 /**
110 * @brief Returns the world-space position of this component.
111 *
112 * Extracted from the world transformation matrix.
113 *
114 * @return World position as glm::vec2.
115 */
116 glm::vec2 GetWorldPosition() const;
117 /**
118 * @brief Returns the accumulated world-space rotation.
119 *
120 * If attached to a parent, this includes parent rotation.
121 *
122 * @return World rotation angle in degrees.
123 */
124 float GetWorldRotation() const;
125 /**
126 * @brief Returns the accumulated world-space scale.
127 *
128 * If attached to a parent, this includes parent scaling.
129 *
130 * @return World scale as glm::vec2.
131 */
132 glm::vec2 GetWorldScale() const;
133 /**
134 * @brief Returns the world transformation matrix.
135 *
136 * If the component is marked dirty, the world matrix
137 * will be recomputed before returning.
138 *
139 * @return 4x4 world transformation matrix.
140 */
141 const glm::mat4& GetWorldMatrix();
142
143 /**
144 * @brief Recomputes the local transformation matrix.
145 *
146 * The local matrix is constructed as:
147 *
148 * Local = T * R * S
149 *
150 * This method does not compute the world matrix.
151 * Called internally by UpdateWorldMatrix().
152 */
153 void UpdateLocalMatrix();
154 /**
155 * @brief Recomputes the world transformation matrix.
156 *
157 * Combines parent world matrix (if any) with the local matrix:
158 *
159 * World = ParentWorld * Local
160 *
161 * Clears the dirty flag after update.
162 */
163 void UpdateWorldMatrix();
164
165 /**
166 * @brief Attaches this scene component to a parent scene component.
167 *
168 * After attachment, this component's transform becomes relative to the parent.
169 * The world transformation is computed by combining the parent's world transform
170 * with this component's local transform.
171 *
172 * Passing nullptr detaches the component and makes it a root-level component.
173 *
174 * @param parent Pointer to the parent SceneComponent2D, or nullptr to detach.
175 */
176 void AttachTo(SceneComponent2D* parent);
177
178 SceneComponent2D(const SceneComponent2D& rhs) = delete;
180
181protected:
182 glm::vec2 mPosition;
184 glm::vec2 mScale;
185
186 glm::mat4 mLocalMatrix;
187 glm::mat4 mWorldMatrix;
188
191};
192
193} // namespace rendering_engine
Represents a hierarchical 2D transform component.
virtual ~SceneComponent2D()=default
Destructor.
SceneComponent2D & operator=(const SceneComponent2D &rhs)=delete
SceneComponent2D(const SceneComponent2D &rhs)=delete
const glm::vec2 & GetScale() const
Gets the current scale.
const glm::vec2 & GetPosition() const
Gets the current position.
float GetRotation() const
Gets the current rotation angle (in degrees).
#define RE_API