Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
Scene.h
Go to the documentation of this file.
1#pragma once
2
3#include "Actor.h"
7#include "Entity.h"
8
9#include <filesystem>
10#include <memory>
11#include <stdexcept>
12#include <type_traits>
13#include <utility>
14#include <vector>
15
16namespace Engine
17{
18 class EngineKern;
19} // namespace Engine
20
21namespace Engine::Core
22{
23 class AssetManager;
24} // namespace Engine::Core
25
26namespace Engine::Entities {
27 class Scene;
28 class StaticMeshActor;
29
30 namespace detail {
31 struct GltfSpawnPlanData;
32
38
40 std::shared_ptr<const GltfSpawnPlanData> data;
41 std::vector<GltfSpawnNode> meshNodes;
42 };
43
44 GltfSpawnPlan prepareGltfSpawnPlan( Scene * scene, const std::filesystem::path & gltfPath );
45
47 Actor * actor,
48 const GltfSpawnPlan & plan,
49 const GltfSpawnNode & spawnNode
50 );
51 } // namespace detail
52
56 class Scene : public Core::ITickable {
57 public:
59 ~Scene() override;
60
71 template<typename T, typename... Args>
72 T* spawnActor(const Ecs::Transform& spawnTransform, Args&&... args) {
73 static_assert(std::is_base_of_v<Entities::Actor, T>, "T must derive from Actor");
74 TRACY_ZONE_SCOPED_NAMED("Spawned actor of class");
75
76 try {
77 if (!sceneGraph) {
78 throw std::runtime_error("Failed to spawn Actor with world node");
79 }
80 std::shared_ptr<Entities::SceneNode> node = sceneGraph->createNode(spawnTransform, sceneGraph->getRoot(), false);
81
82 T* rawPtr = new T(node, this, std::forward<Args>(args)...);
83 entities.push_back(rawPtr);
84
85 rawPtr->sceneNode = node;
86 rawPtr->beginPlay();
87
88 return rawPtr;
89 } catch (const std::exception &e) {
90 throw std::runtime_error("Failed to spawn actor: " + std::string(e.what()));
91 }
92 }
93
105 template<typename ActorT = StaticMeshActor>
106 static std::vector<ActorT*> spawnAll(
107 Scene* scene,
108 const std::filesystem::path& gltfPath)
109 {
110 detail::GltfSpawnPlan spawnPlan = detail::prepareGltfSpawnPlan( scene, gltfPath );
111
112 std::vector<ActorT*> spawnedActors;
113 spawnedActors.reserve( spawnPlan.meshNodes.size() );
114
115 for ( detail::GltfSpawnNode & spawnNode : spawnPlan.meshNodes ) {
116 auto* actor = scene->spawnActor<ActorT>(spawnNode.transform, std::move(spawnNode.meshPath));
117 spawnedActors.push_back(actor);
118 detail::applyGltfNodeExtensions( actor, spawnPlan, spawnNode );
119 }
120
121 return spawnedActors;
122 }
123
125
126 virtual void loadContent();
127 virtual void unloadContent();
128
129 [[nodiscard]] std::vector<Entities::Entity*> getEntities() const;
130
134 void reserveActorCapacity(size_t additionalActors);
135
136 void cleanup();
137 [[nodiscard]] Core::SceneManager* getSceneManager() const;
138
143 [[nodiscard]] Core::AssetManager* getAssetManager() const;
144
150
155 [[nodiscard]] EngineKern* getEngine() const { return engine_; }
156 [[nodiscard]] SceneGraph* getSceneGraph() const { return sceneGraph.get(); }
157
158 protected:
159 EngineKern* engine_ = nullptr;
161 private:
162
163 std::unique_ptr<SceneGraph> sceneGraph;
164
165 std::vector<Entity*> entities;
166 };
167}
#define TRACY_ZONE_SCOPED_NAMED(name)
This is the interface which is used to call a tick function on an object. Everything which should be ...
Definition Tick.h:24
Manages game objects within a scene, handling registration, ID allocation, and GPU buffer synchroniza...
An Actor is similar to an Engine::Entities::Entity. An actor is an Entity with a transform.
Definition Actor.h:20
Represents the entire scene as a graph (specifically, a tree) of SceneNode objects....
Definition SceneGraph.h:194
A scene is the overarching structure which can spawn, contain and destroy actors or entities.
Definition Scene.h:56
static std::vector< ActorT * > spawnAll(Scene *scene, const std::filesystem::path &gltfPath)
Spawns all meshes from a GLTF file into the scene.
Definition Scene.h:106
void destroyActor(Entities::Actor *actor)
Core::SceneManager * getSceneManager() const
SceneGraph * getSceneGraph() const
Definition Scene.h:156
std::vector< Entities::Entity * > getEntities() const
Core::AssetManager * getAssetManager() const
Gets the asset manager for this scene.
virtual void unloadContent()
void reserveActorCapacity(size_t additionalActors)
Reserves storage for a known number of upcoming actor spawns.
EngineKern * engine_
Definition Scene.h:159
std::unique_ptr< SceneGraph > sceneGraph
Definition Scene.h:163
void setAssetManager(Core::AssetManager *manager)
Sets the asset manager for this scene (used for dependency injection in tests)
virtual void loadContent()
T * spawnActor(const Ecs::Transform &spawnTransform, Args &&... args)
Spawns an actor from a specific class in somewhere into the scene.
Definition Scene.h:72
std::vector< Entity * > entities
Definition Scene.h:165
Core::AssetManager * assetManager
Definition Scene.h:160
EngineKern * getEngine() const
Gets the engine instance for this scene.
Definition Scene.h:155
Wrapper for entt component creation which ensures types safety and attaches the correct component wit...
Core audio subsystem owning the miniaudio engine and managing playback.
Definition AudioConfig.h:9
GltfSpawnPlan prepareGltfSpawnPlan(Scene *scene, const std::filesystem::path &gltfPath)
void applyGltfNodeExtensions(Actor *actor, const GltfSpawnPlan &plan, const GltfSpawnNode &spawnNode)
std::shared_ptr< const GltfSpawnPlanData > data
Definition Scene.h:40
std::vector< GltfSpawnNode > meshNodes
Definition Scene.h:41