Muto 0.0.1
Cross-Platform Application Template
GitHub Repo | Muto Updates | Muto's Author
Loading...
Searching...
No Matches
Muto::Scene Class Reference

Represents a scene in the application and manages entities and their components. More...

#include <Scene.h>

Public Member Functions

Entity CreateEntity (const std::string &name, const std::string &uuid)
 
Entity CreateEntity (const std::string &name=std::string())
 
void DestroyEntity (Entity entity)
 
Entity GetPrimaryCameraEntity ()
 
void OnUpdateEditor (Timestep ts, EditorCamera &camera)
 
void OnUpdateRuntime (Timestep ts)
 
void OnViewportResize (uint32_t width, uint32_t height)
 
 Scene ()
 
 Scene (const std::string &name)
 
 ~Scene ()
 

Private Member Functions

const std::string & GetName () const
 
template<>
void OnComponentAdded (Entity entity, CameraComponent &component)
 
template<>
void OnComponentAdded (Entity entity, CameraComponent &component)
 
template<>
void OnComponentAdded (Entity entity, NameComponent &component)
 
template<>
void OnComponentAdded (Entity entity, NameComponent &component)
 
template<>
void OnComponentAdded (Entity entity, NativeScriptComponent &component)
 
template<>
void OnComponentAdded (Entity entity, NativeScriptComponent &component)
 
template<>
void OnComponentAdded (Entity entity, ParticleSystemComponent &component)
 
template<>
void OnComponentAdded (Entity entity, ParticleSystemComponent &component)
 
template<>
void OnComponentAdded (Entity entity, SpriteRendererComponent &component)
 
template<>
void OnComponentAdded (Entity entity, SpriteRendererComponent &component)
 
template<>
void OnComponentAdded (Entity entity, SubTextureComponent &component)
 
template<>
void OnComponentAdded (Entity entity, SubTextureComponent &component)
 
template<typename T >
void OnComponentAdded (Entity entity, T &component)
 
template<>
void OnComponentAdded (Entity entity, TextureAnimationComponent &component)
 
template<>
void OnComponentAdded (Entity entity, TextureAnimationComponent &component)
 
template<>
void OnComponentAdded (Entity entity, TransformComponent &component)
 
template<>
void OnComponentAdded (Entity entity, TransformComponent &component)
 
template<>
void OnComponentAdded (Entity entity, UUIDComponent &component)
 
template<>
void OnComponentAdded (Entity entity, UUIDComponent &component)
 
void SetName (const std::string &name)
 

Private Attributes

std::string m_Name
 
entt::registry m_Registry
 
uint32_t m_ViewportHeight = 90
 
uint32_t m_ViewportWidth = 160
 

Friends

class Entity
 
class SceneCamera
 
class SceneHierarchyPanel
 
class SceneSerializer
 

Detailed Description

Represents a scene in the application and manages entities and their components.

Todo:
Create a friend class SceneRenderer;

Constructor & Destructor Documentation

◆ Scene() [1/2]

Muto::Scene::Scene ( )
15 : m_Name("Untitled Scene")
16 {
18
19 }
#define MU_PROFILE_FUNCTION()
Definition Instrumentor.h:240
std::string m_Name
Definition Scene.h:40

References m_Name.

◆ Scene() [2/2]

Muto::Scene::Scene ( const std::string &  name)
22 : m_Name(name)
23 {
24 }
constexpr Scope< T > CreateScope(Args &&... args)
Creates a Scope (unique_ptr) for the given type and constructor arguments.
Definition Defines_Macros.h:51

References m_Name.

◆ ~Scene()

Muto::Scene::~Scene ( )
27 {
28 }

Member Function Documentation

◆ CreateEntity() [1/2]

Entity Muto::Scene::CreateEntity ( const std::string &  name,
const std::string &  uuid 
)
42 {
43 Entity entity = { m_Registry.create(), this };
44 entity.AddComponent<TransformComponent>();
45 auto& nameTag = entity.AddComponent<NameComponent>();
46 nameTag.Name = name.empty() ? "Entity" + std::to_string(static_cast<std::uint32_t>(entity)) : name;
47 entity.AddComponent<UUIDComponent>(uuid);
48 return entity;
49
50 }
entt::registry m_Registry
Definition Scene.h:41
friend class Entity
Definition Scene.h:43

◆ CreateEntity() [2/2]

Entity Muto::Scene::CreateEntity ( const std::string &  name = std::string())
31 {
32 Entity entity = { m_Registry.create(), this };
33 entity.AddComponent<TransformComponent>();
34 auto& nameTag = entity.AddComponent<NameComponent>();
35 nameTag.Name = name.empty() ? "Entity" + std::to_string(static_cast<std::uint32_t>(entity)) : name;
36 entity.AddComponent<UUIDComponent>();
37
38 return entity;
39 }

◆ DestroyEntity()

void Muto::Scene::DestroyEntity ( Entity  entity)
53 {
54 m_Registry.destroy(entity);
55 }

◆ GetName()

const std::string & Muto::Scene::GetName ( ) const
inlineprivate
50{ return m_Name; }

References m_Name.

◆ GetPrimaryCameraEntity()

Entity Muto::Scene::GetPrimaryCameraEntity ( )
296 {
297 auto view = m_Registry.view<CameraComponent>();
298 for (auto entity : view) {
299
300 const auto& camera = view.get<CameraComponent>(entity);
301 if (camera.Primary)
302 return Entity{ entity, this };
303 }
304 return {};
305 }

◆ OnComponentAdded() [1/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
CameraComponent &  component 
)
private
331 {
332 component.Camera.SetViewportSize(m_ViewportWidth, m_ViewportHeight);
333 }
uint32_t m_ViewportHeight
Definition Scene.h:42
uint32_t m_ViewportWidth
Definition Scene.h:42

◆ OnComponentAdded() [2/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
CameraComponent &  component 
)
private
331 {
332 component.Camera.SetViewportSize(m_ViewportWidth, m_ViewportHeight);
333 }

References m_ViewportHeight, and m_ViewportWidth.

◆ OnComponentAdded() [3/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
NameComponent &  component 
)
private
323 {
324 }

◆ OnComponentAdded() [4/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
NameComponent &  component 
)
private
323 {
324 }

◆ OnComponentAdded() [5/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
NativeScriptComponent &  component 
)
private
357 {
358 }

◆ OnComponentAdded() [6/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
NativeScriptComponent &  component 
)
private
357 {
358 }

◆ OnComponentAdded() [7/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
ParticleSystemComponent &  component 
)
private
361 {
362 }

◆ OnComponentAdded() [8/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
ParticleSystemComponent &  component 
)
private
361 {
362 }

◆ OnComponentAdded() [9/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
SpriteRendererComponent &  component 
)
private
336 {
337 }

◆ OnComponentAdded() [10/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
SpriteRendererComponent &  component 
)
private
336 {
337 }

◆ OnComponentAdded() [11/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
SubTextureComponent &  component 
)
private
340 {
341 auto& src = entity.GetOrAddComponent<SpriteRendererComponent>();
342 if (!src.TextureEnabled) src.TextureEnabled = true;
343 component.SetTexture(src.Texture ? src.Texture : Renderer2D::GetWhiteTexture());
344 }

◆ OnComponentAdded() [12/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
SubTextureComponent &  component 
)
private
340 {
341 auto& src = entity.GetOrAddComponent<SpriteRendererComponent>();
342 if (!src.TextureEnabled) src.TextureEnabled = true;
343 component.SetTexture(src.Texture ? src.Texture : Renderer2D::GetWhiteTexture());
344 }

◆ OnComponentAdded() [13/19]

template<typename T >
void Muto::Scene::OnComponentAdded ( Entity  entity,
T &  component 
)
private
313 {
314 static_assert(false);
315 }

◆ OnComponentAdded() [14/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
TextureAnimationComponent &  component 
)
private
347 {
348 auto& src = entity.GetOrAddComponent<SpriteRendererComponent>();
349 if (!src.TextureEnabled) src.TextureEnabled = true;
350 auto& stc = entity.GetOrAddComponent<SubTextureComponent>();
351 stc.SetTexture(src.Texture ? src.Texture : Renderer2D::GetWhiteTexture());
352 component.SubTextures.clear();
353 component.SubTextures.push_back(SubTexture2D::CreateFromCoords(stc.SubTexture->GetTexture(), {1, 1}, {1, 1}));
354 }
static Ref< SubTexture2D > CreateFromCoords(const Ref< Texture2D > &texture, const glm::vec2 &coords, const glm::vec2 &cellSize, const glm::vec2 &spriteSize={1, 1})
Creates a SubTexture2D from a grid of cells within the given texture.
Definition SubTexture2D.cpp:17

◆ OnComponentAdded() [15/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
TextureAnimationComponent &  component 
)
private
347 {
348 auto& src = entity.GetOrAddComponent<SpriteRendererComponent>();
349 if (!src.TextureEnabled) src.TextureEnabled = true;
350 auto& stc = entity.GetOrAddComponent<SubTextureComponent>();
351 stc.SetTexture(src.Texture ? src.Texture : Renderer2D::GetWhiteTexture());
352 component.SubTextures.clear();
353 component.SubTextures.push_back(SubTexture2D::CreateFromCoords(stc.SubTexture->GetTexture(), {1, 1}, {1, 1}));
354 }

◆ OnComponentAdded() [16/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
TransformComponent &  component 
)
private
327 {
328 }

◆ OnComponentAdded() [17/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
TransformComponent &  component 
)
private
327 {
328 }

◆ OnComponentAdded() [18/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
UUIDComponent &  component 
)
private
318 {
319 // TODO: search registry to ensure unique UUID
320 }

◆ OnComponentAdded() [19/19]

template<>
void Muto::Scene::OnComponentAdded ( Entity  entity,
UUIDComponent &  component 
)
private
318 {
319 // TODO: search registry to ensure unique UUID
320 }

◆ OnUpdateEditor()

void Muto::Scene::OnUpdateEditor ( Timestep  ts,
EditorCamera &  camera 
)
189 {
192 auto view = m_Registry.group<SpriteRendererComponent>();
193 for (auto entity : view)
194 {
195 auto& transform = m_Registry.get<TransformComponent>(entity);
196 auto& sprite = m_Registry.get<SpriteRendererComponent>(entity);
197 // Do not render subtextures here
198 auto stc = m_Registry.try_get<SubTextureComponent>(entity);
199 if (stc) {
200 continue;
201 }
202 // Do not render texture animations here
203 auto texAnim = m_Registry.try_get<TextureAnimationComponent>(entity);
204 if (texAnim) {
205 continue;
206 }
207
208 if (sprite.TextureEnabled && !sprite.Texture)
209 Renderer2D::DrawQuad(transform.GetTransform(), MU_DEFAULT_TEXTURE, sprite.TilingFactor, sprite.Color);
210 else if (sprite.TextureEnabled && sprite.Texture)
211 Renderer2D::DrawQuad(transform.GetTransform(), sprite.Texture, sprite.TilingFactor, sprite.Color);
212 else
213 Renderer2D::DrawQuad(transform.GetTransform(), sprite.Color);
214 }
215
216 auto subTextureView = m_Registry.group<SubTextureComponent>();
217 for (auto entity : subTextureView) {
218 auto& transform = m_Registry.get<TransformComponent>(entity);
219 auto& sprite = m_Registry.get<SpriteRendererComponent>(entity);
220 auto& subTexture = m_Registry.get<SubTextureComponent>(entity);
221
222 // Do not render texture animations here
223 auto texAnim = m_Registry.try_get<TextureAnimationComponent>(entity);
224 if (texAnim) {
225 continue;
226 }
227
228 if (!sprite.TextureEnabled) sprite.TextureEnabled = true;
229 if (subTexture.SubTexture == nullptr)
230 {
231 subTexture.SetTexture(MU_DEFAULT_TEXTURE);
232 }
233 else {
234 // TODO: find way to avoid this check every frame
235 // Ensure the subtexture's texture matches the sprite's texture
236 if (sprite.Texture && subTexture.SubTexture->GetTexture() != sprite.Texture) {
237 subTexture.SetTexture(sprite.Texture);
238 }
239 }
240 Renderer2D::DrawQuad(transform.GetTransform(), subTexture.GetSubTexture(), sprite.TilingFactor, sprite.Color);
241 }
242
243 auto TextureAnimationView = m_Registry.group<TextureAnimationComponent>();
244 for (auto entity : TextureAnimationView)
245 {
246 auto& transform = m_Registry.get<TransformComponent>(entity);
247 auto& texAnim = m_Registry.get<TextureAnimationComponent>(entity);
248 auto& sprite = m_Registry.get<SpriteRendererComponent>(entity);
249 if (texAnim.IsLooping) {
250 texAnim.Update(ts.GetSeconds());
251 }
252 Renderer2D::DrawQuad(transform.GetTransform(), texAnim.SubTextures[texAnim.CurrentFrame], 1.0f, sprite.Color);
253 }
254
255 {
256 MU_PROFILE_SCOPE("Particle System(s) Update");
257
258 auto particleSystemView = m_Registry.group<ParticleSystemComponent>();
259 for (auto entity : particleSystemView) {
260 auto& transform = m_Registry.get<TransformComponent>(entity);
261 auto& particleSystem = m_Registry.get<ParticleSystemComponent>(entity);
262 auto& ps = particleSystem.System;
263 auto& props = ps.m_Props;
264 props.Position = transform.Translation;
265 ps.OnUpdate(ts);
266 if (ps.m_IsEmitting) {
267 for (int i = 0; i < ps.m_EmitRate; i++) {
268 ps.Emit(ps.m_Props);
269 }
270 ps.m_IsEmitting = false;
271 }
272
273 ps.OnRender();
274 }
275 }
276
278 }
#define MU_DEFAULT_TEXTURE
Editor Configurations.
Definition Config.h:13
#define MU_PROFILE_SCOPE(name)
Definition Instrumentor.h:239
static void EndScene()
Ends the current scene.
Definition Renderer2D.cpp:165
static void BeginScene(const Camera &camera, const glm::mat4 &transform)
Begins a new scene with the given camera and transform.
Definition Renderer2D.cpp:137
static void DrawQuad(const glm::mat4 &transform, const glm::vec4 &color)
Draws a colored quad with the given transform and color.
Definition Renderer2D.cpp:215

References Muto::Renderer2D::BeginScene(), and Muto::Renderer2D::EndScene().

◆ OnUpdateRuntime()

void Muto::Scene::OnUpdateRuntime ( Timestep  ts)

TODO: Move to Scene::OnScenePlay()

TODO: Move to Scene::OnScenePlay()

59 {
61
62 // Update scripts
63 {
64 m_Registry.view<NativeScriptComponent>().each([=](auto entity, NativeScriptComponent& nsc)
65 {
67 if (!nsc.Instance)
68 {
69 nsc.Instance = nsc.InstantiateScript();
70 nsc.Instance->m_Entity = Entity{ entity, this };
71 nsc.Instance->OnCreate();
72 }
73 nsc.Instance->OnUpdate(ts);
74 });
75 }
76
77 Camera* mainCamera = nullptr;
78 glm::mat4 camTransform;
79 {
80 auto view = m_Registry.view<TransformComponent, CameraComponent>();
81 for (auto entity : view)
82 {
83 auto [transform, camera] = view.get<TransformComponent, CameraComponent>(entity);
84 if (camera.Primary) {
85 mainCamera = &camera.Camera;
86 camTransform = transform.GetTransform();
87 break;
88 }
89 }
90 }
91 if (!mainCamera)
92 return;
93
95
96
97 auto view = m_Registry.group<SpriteRendererComponent>();
98 for (auto entity : view)
99 {
100 auto& transform = m_Registry.get<TransformComponent>(entity);
101 auto& sprite = m_Registry.get<SpriteRendererComponent>(entity);
102
103 // Do not render subtextures here
104 auto stc = m_Registry.try_get<SubTextureComponent>(entity);
105 if (stc) {
106 continue;
107 }
108
109 // Do not render texture animations here
110 auto texAnim = m_Registry.try_get<TextureAnimationComponent>(entity);
111 if (texAnim) {
112 continue;
113 }
114
115 if (sprite.TextureEnabled && !sprite.Texture)
116 Renderer2D::DrawQuad(transform.GetTransform(), MU_DEFAULT_TEXTURE, sprite.TilingFactor, sprite.Color);
117 else if (sprite.TextureEnabled && sprite.Texture)
118 Renderer2D::DrawQuad(transform.GetTransform(), sprite.Texture, sprite.TilingFactor, sprite.Color);
119 else
120 Renderer2D::DrawQuad(transform.GetTransform(), sprite.Color);
121 }
122
123 auto subTextureView = m_Registry.group<SubTextureComponent>();
124 for (auto entity : subTextureView) {
125 auto& transform = m_Registry.get<TransformComponent>(entity);
126 auto& sprite = m_Registry.get<SpriteRendererComponent>(entity);
127 auto& subTexture = m_Registry.get<SubTextureComponent>(entity);
128
129 // Do not render texture animations here
130 auto texAnim = m_Registry.try_get<TextureAnimationComponent>(entity);
131 if (texAnim) {
132 continue;
133 }
134
135 if (!sprite.TextureEnabled) sprite.TextureEnabled = true;
136 if (subTexture.SubTexture == nullptr)
137 {
138 subTexture.SetTexture(MU_DEFAULT_TEXTURE);
139 }
140 else {
141 // Ensure the subtexture's texture matches the sprite's texture
142 if (sprite.Texture && subTexture.SubTexture->GetTexture() != sprite.Texture) {
143 subTexture.SetTexture(sprite.Texture);
144 }
145 }
146
147 Renderer2D::DrawQuad(transform.GetTransform(), subTexture.GetSubTexture(), sprite.TilingFactor, sprite.Color);
148 }
149
150 auto TextureAnimationView = m_Registry.group<TextureAnimationComponent>();
151 for (auto entity : TextureAnimationView)
152 {
153 auto& transform = m_Registry.get<TransformComponent>(entity);
154 auto& texAnim = m_Registry.get<TextureAnimationComponent>(entity);
155 auto& sprite = m_Registry.get<SpriteRendererComponent>(entity);
156 texAnim.Update(ts.GetSeconds());
157 Renderer2D::DrawQuad(transform.GetTransform(), texAnim.SubTextures[texAnim.CurrentFrame], 1.0f, sprite.Color);
158 }
159
160
161 {
162 MU_PROFILE_SCOPE("Particle System(s) Update");
163
164 auto particleSystemView = m_Registry.group<ParticleSystemComponent>();
165 for (auto entity : particleSystemView) {
166 auto& transform = m_Registry.get<TransformComponent>(entity);
167 auto& particleSystem = m_Registry.get<ParticleSystemComponent>(entity);
168 auto& ps = particleSystem.System;
169 auto& props = ps.m_Props;
170 props.Position = transform.Translation;
171 ps.OnUpdate(ts);
172
173 if (ps.m_IsEmitting) {
174 for (int i = 0; i < ps.m_EmitRate; i++) {
175 ps.Emit(ps.m_Props);
176 }
177 ps.m_IsEmitting = false;
178 }
179
180 ps.OnRender();
181 }
182 }
183
185
186 }

References Muto::Renderer2D::EndScene().

◆ OnViewportResize()

void Muto::Scene::OnViewportResize ( uint32_t  width,
uint32_t  height 
)
281 {
284
285 // resize non fixed aspect ratio cameras
286 auto view = m_Registry.view<CameraComponent>();
287 for (auto entity : view)
288 {
289 auto& cameraComponent = view.get<CameraComponent>(entity);
290 if (!cameraComponent.FixedAspectRatio)
291 cameraComponent.Camera.SetViewportSize(width, height);
292 }
293 }

References m_ViewportHeight, and m_ViewportWidth.

◆ SetName()

void Muto::Scene::SetName ( const std::string &  name)
inlineprivate
49{ m_Name = name; }

References m_Name.

Friends And Related Symbol Documentation

◆ Entity

◆ SceneCamera

◆ SceneHierarchyPanel

◆ SceneSerializer

Member Data Documentation

◆ m_Name

std::string Muto::Scene::m_Name
private

Referenced by GetName(), Scene(), Scene(), and SetName().

◆ m_Registry

entt::registry Muto::Scene::m_Registry
private

◆ m_ViewportHeight

uint32_t Muto::Scene::m_ViewportHeight = 90
private

◆ m_ViewportWidth

uint32_t Muto::Scene::m_ViewportWidth = 160
private

The documentation for this class was generated from the following files: