Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
actor_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
11
12namespace rendering_engine
13{
14
15class DrawableComponent;
16class Drawable2D;
17
18/**
19 * @class Actor2D
20 * @brief Base class representing a 2D entity within a Scene.
21 *
22 * Actor2D encapsulates:
23 * - A root SceneComponent2D defining its 2D transform.
24 * - A collection of attached 2D drawable subobjects.
25 * - Per-frame update logic.
26 * - Deferred destruction lifecycle integration with Scene.
27 *
28 * Actor2D operates strictly in the 2D domain:
29 * - Position is defined in (x, y).
30 * - Rotation is a single angle (degrees) around the Z-axis.
31 * - Scale is defined in (x, y).
32 *
33 * Transformations applied to the Actor2D propagate to all attached
34 * drawable subobjects via hierarchical SceneComponent2D attachment.
35 *
36 * Actors are owned and lifetime-managed by the Scene that spawns them.
37 * They must not be manually deleted.
38 *
39 * @note
40 * - Actor2D is conceptually separated from Actor (3D).
41 * - Destruction is deferred and handled safely by Scene.
42 *
43 * @see Scene, SceneComponent2D, Drawable2D, Actor
44 */
46{
47 friend class Scene;
48public:
49 /**
50 * @brief Constructs a 2D actor associated with a Scene.
51 *
52 * The Scene becomes the owner of this actor.
53 * The root SceneComponent2D is initialized with identity transform.
54 *
55 * @param scene Reference to the owning Scene.
56 */
57 Actor2D(Scene& scene);
58
59 virtual ~Actor2D();
60 /**
61 * @brief Initializes the actor after creation.
62 *
63 * Called automatically by Scene::SpawnActor2D().
64 * Derived classes should override this method to:
65 * - Create drawable subobjects
66 * - Attach components
67 * - Set initial transforms
68 */
69 virtual void Initialize();
70 /**
71 * @brief Sets the actor's position in world space.
72 * @param position New position vector (x, y).
73 */
74 void SetPosition(const glm::vec2& position);
75
76 /**
77 * @brief Sets the actor's rotation in degrees.
78 * @param angleDegrees Rotation angle in degrees (counterclockwise).
79 *
80 * Rotation is applied around the Z-axis.
81 */
82 void SetRotation(float angleDegrees);
83
84 /**
85 * @brief Sets the actor's scale along each axis.
86 * @param scale New scale vector (x, y).
87 */
88 void SetScale(const glm::vec2& scale);
89
90 /**
91 * @brief Gets the actor 2d position.
92 */
93 const glm::vec2& GetPosition() const;
94
95 /**
96 * @brief Gets the actor 2d rotation angle (degrees).
97 */
98 float GetRotation() const;
99
100 /**
101 * @brief Gets the actor 2d scale.
102 */
103 const glm::vec2& GetScale() const;
104
105 /**
106 * @brief Access to the underlying SceneComponent2D (transform).
107 *
108 * Allows direct manipulation of local or hierarchical transforms.
109 *
110 * @return Reference to the root 2D transform component.
111 */
112 SceneComponent2D& GetTransform();
113 const SceneComponent2D& GetTransform() const;
114 ///@}
115
116 /**
117 * @brief Updates actor logic and root transform state.
118 *
119 * Called once per frame by Scene::Update().
120 *
121 * The base implementation updates the root SceneComponent2D's
122 * world matrix if ticking is enabled.
123 *
124 * Derived classes should call Actor2D::Update(deltaTime)
125 * before or after custom logic as appropriate.
126 *
127 * @param deltaTime Time elapsed since last frame (seconds).
128 */
129 virtual void Update(float deltaTime);
130 /**
131 * @brief Returns the render resource context associated with this actor.
132 *
133 * Provides access to renderer-level resources
134 * required by attached 2D drawable components.
135 *
136 * @return RenderResourceContext for this actor.
137 */
138 RenderResourceContext GetRenderContext() const;
139 /**
140 * @brief Requests deferred destruction of this 2D actor.
141 *
142 * Marks the actor as pending destroy and schedules safe removal
143 * via the owning Scene.
144 *
145 * The actor is not deleted immediately. Instead:
146 * - Attached drawable subobjects are detached and scheduled for destruction.
147 * - The actor is added to the Scene's 2D pending destruction queue.
148 *
149 * @note
150 * - Do not manually delete actors.
151 * - After calling Destroy(), IsPendingDestroy() will return true.
152 * - Actual deletion occurs during Scene's flush phase.
153 */
154 void Destroy();
155 /**
156 * @brief Indicates whether this actor is scheduled for destruction.
157 *
158 * @return True if Destroy() has been called and the actor is awaiting removal.
159 */
160 bool IsPendingDestroy() const { return bPendingDestroy; }
161
162 Actor2D(const Actor2D&) = delete;
163 Actor2D& operator=(const Actor2D&) = delete;
164
165protected:
166 /**
167 * @brief Performs internal cleanup before destruction.
168 *
169 * Called by Scene during the destruction flush phase.
170 * Derived classes may override this to release resources,
171 * but must not delete the actor manually.
172 *
173 * @note Part of the deferred destruction pipeline.
174 */
175 virtual void Shutdown();
176 /**
177 * @brief Creates and attaches a 2D drawable subobject to this actor.
178 *
179 * This helper function delegates creation to the owning Scene,
180 * registers the drawable for lifetime management, and stores it
181 * as a ward (child drawable) of the actor.
182 *
183 * @tparam T Drawable type (must derive from Drawable2D).
184 * @tparam V Construction parameter type.
185 * @param arg Argument forwarded to Scene::Spawn().
186 *
187 * @return Pointer to the created drawable subobject.
188 *
189 * @note
190 * - The drawable is owned by the Scene.
191 * - The actor keeps a non-owning reference in mWards.
192 * - Destruction is automatically handled when the actor is destroyed.
193 */
194 template <typename T, typename V>
196
197 /**
198 * @brief Returns the owning Scene.
199 *
200 * Provides access to the scene that contains this actor.
201 *
202 * @return Reference to the parent Scene.
203 */
204 Scene& GetScene();
205
206protected:
209
210 bool bPendingDestroy = false;
211
212private:
213 Scene& mScene;
214 RenderResourceContext mRenderContext;
215 std::vector<Drawable2D*> mWards;
216};
217
218}// namespace rendering_engine
219
Base class representing a 2D entity within a Scene.
Definition: actor_2d.hpp:46
SceneComponent2D mRootComponent
Definition: actor_2d.hpp:207
Actor2D(const Actor2D &)=delete
T * CreateSubobject(V arg)
Creates and attaches a 2D drawable subobject to this actor.
Actor2D & operator=(const Actor2D &)=delete
bool IsPendingDestroy() const
Indicates whether this actor is scheduled for destruction.
Definition: actor_2d.hpp:160
Represents a hierarchical 2D transform component.
Base class representing a renderable scene.
Definition: scene.hpp:44
#define RE_API
Aggregates pointers to global rendering resource managers.