Vesper 0.5.1
Vesper is short form for the Latin word for "Bat", as Vesper is designed to be small, lightweight, and easily handle things like particles and flocking behaviors in accordance with the nature of bats. \n It is meant to be a particle simulation, VFX editor, and CAN be used secondarily as a small game engine.
GitHub | Vesper Updates | Creator
Loading...
Searching...
No Matches
Vesper::Scene Class Reference

#include <Scene.h>

Public Member Functions

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

Private Member Functions

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

Private Attributes

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

Friends

class Entity
class SceneSerializer
class SceneHierarchyPanel

Constructor & Destructor Documentation

◆ Scene() [1/2]

Vesper::Scene::Scene ( )
12 : m_Name("Untitled Scene")
13 {
15
16 }
#define VZ_PROFILE_FUNCTION()
Definition Instrumentor.h:240
std::string m_Name
Definition Scene.h:33

References Scene().

Referenced by Scene().

◆ Scene() [2/2]

Vesper::Scene::Scene ( const std::string & name)
19 : m_Name(name)
20 {
21 }

References Scene().

Referenced by Scene().

◆ ~Scene()

Vesper::Scene::~Scene ( )
24 {
25 }

Member Function Documentation

◆ CreateEntity() [1/2]

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

◆ CreateEntity() [2/2]

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

◆ DestroyEntity()

void Vesper::Scene::DestroyEntity ( Entity entity)
50 {
51 m_Registry.destroy(entity);
52 }

◆ GetName()

const std::string & Vesper::Scene::GetName ( ) const
inlineprivate
43{ return m_Name; }

◆ GetPrimaryCameraEntity()

Entity Vesper::Scene::GetPrimaryCameraEntity ( )
209 {
210 auto view = m_Registry.view<CameraComponent>();
211 for (auto entity : view) {
212
213 const auto& camera = view.get<CameraComponent>(entity);
214 if (camera.Primary)
215 return Entity{ entity, this };
216 }
217 return {};
218 }

◆ OnComponentAdded() [1/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
CameraComponent & component )
private
244 {
245 component.Camera.SetViewportSize(m_ViewportWidth, m_ViewportHeight);
246 }
uint32_t m_ViewportHeight
Definition Scene.h:35
uint32_t m_ViewportWidth
Definition Scene.h:35

References m_ViewportHeight, and m_ViewportWidth.

◆ OnComponentAdded() [2/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
CameraComponent & component )
private
244 {
245 component.Camera.SetViewportSize(m_ViewportWidth, m_ViewportHeight);
246 }

◆ OnComponentAdded() [3/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
NameComponent & component )
private
236 {
237 }

◆ OnComponentAdded() [4/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
NameComponent & component )
private
236 {
237 }

◆ OnComponentAdded() [5/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
NativeScriptComponent & component )
private
265 {
266 }

◆ OnComponentAdded() [6/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
NativeScriptComponent & component )
private
265 {
266 }

◆ OnComponentAdded() [7/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
SpriteRendererComponent & component )
private
249 {
250 }

◆ OnComponentAdded() [8/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
SpriteRendererComponent & component )
private
249 {
250 }

◆ OnComponentAdded() [9/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
SubTextureComponent & component )
private
253 {
254 auto& src = entity.GetOrAddComponent<SpriteRendererComponent>();
255 if (!src.TextureEnabled) src.TextureEnabled = true;
256 component.SetTexture(src.Texture ? src.Texture : VZ_DEFAULT_TEXTURE);
257 }
#define VZ_DEFAULT_TEXTURE
Editor Configurations.
Definition Config.h:14

◆ OnComponentAdded() [10/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
SubTextureComponent & component )
private
253 {
254 auto& src = entity.GetOrAddComponent<SpriteRendererComponent>();
255 if (!src.TextureEnabled) src.TextureEnabled = true;
256 component.SetTexture(src.Texture ? src.Texture : VZ_DEFAULT_TEXTURE);
257 }

◆ OnComponentAdded() [11/17]

template<typename T>
void Vesper::Scene::OnComponentAdded ( Entity entity,
T & component )
private
226 {
227 static_assert(false);
228 }

◆ OnComponentAdded() [12/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
TextureAnimationComponent & component )
private
260 {
261
262 }

◆ OnComponentAdded() [13/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
TextureAnimationComponent & component )
private
260 {
261
262 }

◆ OnComponentAdded() [14/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
TransformComponent & component )
private
240 {
241 }

◆ OnComponentAdded() [15/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
TransformComponent & component )
private
240 {
241 }

◆ OnComponentAdded() [16/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
UUIDComponent & component )
private
231 {
232 // TODO: search registry to ensure unique UUID
233 }

◆ OnComponentAdded() [17/17]

template<>
void Vesper::Scene::OnComponentAdded ( Entity entity,
UUIDComponent & component )
private
231 {
232 // TODO: search registry to ensure unique UUID
233 }

◆ OnUpdateEditor()

void Vesper::Scene::OnUpdateEditor ( Timestep ts,
EditorCamera & camera )
151 {
154 auto view = m_Registry.group<SpriteRendererComponent>();
155 for (auto entity : view)
156 {
157 auto& transform = m_Registry.get<TransformComponent>(entity);
158 auto& sprite = m_Registry.get<SpriteRendererComponent>(entity);
159 // Do not render subtextures here
160 auto stc = m_Registry.try_get<SubTextureComponent>(entity);
161 if (stc) {
162 continue;
163 }
164 if (sprite.TextureEnabled && !sprite.Texture)
165 Renderer2D::DrawQuadWithTexture(transform.GetTransform(), VZ_DEFAULT_TEXTURE, sprite.TilingFactor, sprite.Color);
166 else if (sprite.TextureEnabled && sprite.Texture)
167 Renderer2D::DrawQuadWithTexture(transform.GetTransform(), sprite.Texture, sprite.TilingFactor, sprite.Color);
168 else
169 Renderer2D::DrawQuad(transform.GetTransform(), sprite.Color);
170 }
171 auto subTextureView = m_Registry.group<SubTextureComponent>();
172 for (auto entity : subTextureView) {
173 auto& transform = m_Registry.get<TransformComponent>(entity);
174 auto& sprite = m_Registry.get<SpriteRendererComponent>(entity);
175 auto& subTexture = m_Registry.get<SubTextureComponent>(entity);
176 if (!sprite.TextureEnabled) sprite.TextureEnabled = true;
177 if (subTexture.SubTexture == nullptr)
178 {
179 subTexture.SetTexture(VZ_DEFAULT_TEXTURE);
180 }
181 else {
182 // TODO: find way to avoid this check every frame
183 // Ensure the subtexture's texture matches the sprite's texture
184 if (sprite.Texture && subTexture.SubTexture->GetTexture() != sprite.Texture) {
185 subTexture.SetTexture(sprite.Texture);
186 }
187 }
188 Renderer2D::DrawQuadWithTexture(transform.GetTransform(), subTexture.GetSubTexture(), sprite.TilingFactor, sprite.Color);
189 }
191 }
static void EndScene()
Ends the current scene.
Definition Renderer2D.cpp:160
static void BeginScene(const Camera &camera, const glm::mat4 &transform)
Begins a new scene with the given camera and transform.
Definition Renderer2D.cpp:123
static void DrawQuad(const glm::mat4 &transform, const glm::vec4 &color)
Draws a colored quad with the given transform and color.
Definition Renderer2D.cpp:180
static void DrawQuadWithTexture(const glm::mat4 &transform, const Ref< Texture2D > &texture, float tilingFactor, const glm::vec4 tintColor)
Draws a textured quad with the given transform, texture, tiling factor, and tint color.
Definition Renderer2D.cpp:218

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

◆ OnUpdateRuntime()

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

TODO: Move to Scene::OnScenePlay()

56 {
58
59 // Update scripts
60 {
61 m_Registry.view<NativeScriptComponent>().each([=](auto entity, NativeScriptComponent& nsc)
62 {
64 if (!nsc.Instance)
65 {
66 nsc.Instance = nsc.InstantiateScript();
67 nsc.Instance->m_Entity = Entity{ entity, this };
68 nsc.Instance->OnCreate();
69 }
70 nsc.Instance->OnUpdate(ts);
71 });
72 }
73
74 Camera* mainCamera = nullptr;
75 glm::mat4* camTransform = nullptr;
76 {
77 auto view = m_Registry.view<TransformComponent, CameraComponent>();
78 for (auto entity : view)
79 {
80 auto [transform, camera] = view.get<TransformComponent, CameraComponent>(entity);
81 if (camera.Primary) {
82 mainCamera = &camera.Camera;
83 camTransform = &transform.GetTransform();
84 break;
85 }
86 }
87 }
88 if (!mainCamera)
89 return;
90
91 Renderer2D::BeginScene(mainCamera->GetProjection(), *camTransform);
92
93
94 auto view = m_Registry.group<SpriteRendererComponent>();
95 for (auto entity : view)
96 {
97 auto& transform = m_Registry.get<TransformComponent>(entity);
98 auto& sprite = m_Registry.get<SpriteRendererComponent>(entity);
99
100 // Do not render subtextures here
101 auto stc = m_Registry.try_get<SubTextureComponent>(entity);
102 if (stc) {
103 continue;
104 }
105
106 if (sprite.TextureEnabled && !sprite.Texture)
107 Renderer2D::DrawQuadWithTexture(transform.GetTransform(), VZ_DEFAULT_TEXTURE, sprite.TilingFactor, sprite.Color);
108 else if (sprite.TextureEnabled && sprite.Texture)
109 Renderer2D::DrawQuadWithTexture(transform.GetTransform(), sprite.Texture, sprite.TilingFactor, sprite.Color);
110 else
111 Renderer2D::DrawQuad(transform.GetTransform(), sprite.Color);
112 }
113
114 auto subTextureView = m_Registry.group<SubTextureComponent>();
115 for (auto entity : subTextureView) {
116 auto& transform = m_Registry.get<TransformComponent>(entity);
117 auto& sprite = m_Registry.get<SpriteRendererComponent>(entity);
118 auto& subTexture = m_Registry.get<SubTextureComponent>(entity);
119
120
121 if (!sprite.TextureEnabled) sprite.TextureEnabled = true;
122 if (subTexture.SubTexture == nullptr)
123 {
124 subTexture.SetTexture(VZ_DEFAULT_TEXTURE);
125 }
126 else {
127 // Ensure the subtexture's texture matches the sprite's texture
128 if (sprite.Texture && subTexture.SubTexture->GetTexture() != sprite.Texture) {
129 subTexture.SetTexture(sprite.Texture);
130 }
131 }
132
133 Renderer2D::DrawQuadWithTexture(transform.GetTransform(), subTexture.GetSubTexture(), sprite.TilingFactor, sprite.Color);
134 }
135
136 //auto group1 = m_Registry.group<TextureAnimationComponent>();
137 //for (auto entity : group1)
138 //{
139 // auto& transform = m_Registry.get<TransformComponent>(entity);
140 // auto& texAnim = m_Registry.get<TextureAnimationComponent>(entity);
141 // auto& sprite = m_Registry.get<SpriteRendererComponent>(entity);
142 // texAnim.Update(ts.GetSeconds());
143 // Renderer2D::DrawQuadWithTexture(transform.GetTransform(), texAnim.SubTextures[texAnim.CurrentFrame], 1.0f, sprite.Color);
144 //}
145
147
148 }

References Vesper::Renderer2D::EndScene().

◆ OnViewportResize()

void Vesper::Scene::OnViewportResize ( uint32_t width,
uint32_t height )
194 {
195 m_ViewportWidth = width;
196 m_ViewportHeight = height;
197
198 // resize non fixed aspect ratio cameras
199 auto view = m_Registry.view<CameraComponent>();
200 for (auto entity : view)
201 {
202 auto& cameraComponent = view.get<CameraComponent>(entity);
203 if (!cameraComponent.FixedAspectRatio)
204 cameraComponent.Camera.SetViewportSize(width, height);
205 }
206 }

References m_ViewportHeight, and m_ViewportWidth.

◆ SetName()

void Vesper::Scene::SetName ( const std::string & name)
inlineprivate

TODO: friend class SceneCamera; TODO: friend class SceneRenderer;

42{ m_Name = name; }

◆ Entity

friend class Entity
friend

◆ SceneHierarchyPanel

friend class SceneHierarchyPanel
friend

◆ SceneSerializer

friend class SceneSerializer
friend

Member Data Documentation

◆ m_Name

std::string Vesper::Scene::m_Name
private

◆ m_Registry

entt::registry Vesper::Scene::m_Registry
private

◆ m_ViewportHeight

uint32_t Vesper::Scene::m_ViewportHeight = 90
private

◆ m_ViewportWidth

uint32_t Vesper::Scene::m_ViewportWidth = 160
private

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