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