ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
Loading...
Searching...
No Matches
SceneManager.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <memory> // for std::unique_ptr to store the Scenes
10#include <string> // for scene names
11#include <unordered_map> // to store the live scenes and scene factories
12#include <type_traits> // for std::is... methods in register static asserts
13
15
16namespace Imago::Engine
17{
18
26 public:
27
28 // NOTE: SceneFactoryFn is a function pointer to an instance of the static create_scene() method that exists for each
29 // registered Scene Type. Creating a Scene requires knowing its concrete type at compile time but not only is the Scene
30 // to build chosen at runtime but the SceneManager cannot even know what user-defined Scenes even exist at compile time.
31 // To solve this we use factories. register_scene<T> is the one place that the Scene type is known at compile time so it
32 // instantiates create_scene<T> and stores a pointer ot it. The SceneManager can then just call the stored function
33 // without even having to know what type the Scene it is instantiating is.
34
36 using SceneFactoryFn = std::unique_ptr<Scene>(*)(const SceneContext&);
37
38 private:
39
46 template <typename T>
47 static std::unique_ptr<Scene> create_scene(const SceneContext& gameCtx);
48
54 void add_factory(const std::string& name, SceneFactoryFn factory);
55
56 const SceneContext& _gameCtx;
57
58 // scene storage
59
60 std::unordered_map<std::string, SceneFactoryFn> _factories;
61 std::unordered_map<std::string, std::unique_ptr<Scene>> _scenes;
62 Scene* _active = nullptr;
63 std::string _activeName;
64
65 // switch data
66
67 bool _switchPending = false;
68 std::string _pendingName;
69 bool _pendingPersist = false;
70
71 public:
72
77 explicit SceneManager(const SceneContext& gameCtx);
78
83
84 // explicitly dissallow copy and move behavior
85 SceneManager(const SceneManager&) = delete;
86 SceneManager& operator=(const SceneManager&) = delete;
87 SceneManager(SceneManager&&) = delete;
88 SceneManager& operator=(SceneManager&&) = delete;
89
96 template <typename T>
97 void register_scene(const std::string& name);
98
105 void request_switch(const std::string& name, bool persist);
106
111 bool apply_pending_switch();
112
117 void update(float dt);
118 };
119
120 //-----------
121 //definitions
122 //-----------
123
124 template <typename T>
125 std::unique_ptr<Scene> SceneManager::create_scene(const SceneContext& gameCtx)
126 {
127 return std::make_unique<T>(gameCtx);
128 }
129
130 template <typename T>
131 void SceneManager::register_scene(const std::string& name)
132 {
133 // ensure at compile time that the T passed in is a Scene
134 static_assert(std::is_base_of_v<Scene, T>, "[SceneManager] You cannot register an object that does not extend Scene!");
135
136 // ensure at compile time that the T passed in has overridden on_load
137 static_assert(!std::is_abstract_v<T>, "[SceneManager] You cannot register a Scene that has not overridden on_load()!");
138
139 // ensure at compile time that the T passed in can be built from a SceneContext
140 static_assert(std::is_constructible_v<T, const SceneContext&>, "[SceneManager] The Scene you are trying to register is not constructible. Use 'using Scene::Scene'");
141
142 // register the scene
143 add_factory(name, &create_scene<T>);
144 }
145}
Defines the Scene, the abstract world that owns a Nexus, Broadcaster, and SystemScheduler.
~SceneManager()
Unloads the active scene then destroys every live scene in _scenes.
Definition SceneManager.cpp:14
void register_scene(const std::string &name)
Registers a Scene type under a name. The scene is not build on registration but instead a factory to ...
Definition SceneManager.hpp:131
SceneManager(const SceneContext &gameCtx)
Construct a new SceneManager object, instantiating gameCtx so it can be copied into every Scene it bu...
Definition SceneManager.cpp:10
void update(float dt)
Updates the active scene. Does nothing if there is no active Scene.
Definition SceneManager.cpp:26
std::unique_ptr< Scene >(*)(const SceneContext &) SceneFactoryFn
A function pointer to a function that builds one specific Scene type. One is instantiated per registe...
Definition SceneManager.hpp:36
void request_switch(const std::string &name, bool persist)
Records a request to switch scenes. The switch is not actually applied until the end of the frame....
Definition SceneManager.cpp:50
bool apply_pending_switch()
Actually switch the pending Scene. Called by Game at the end of each frame.
Definition SceneManager.cpp:70
Abstract Base Class for every Scene. Users inherit from this class and override on_load() and optiona...
Definition Scene.hpp:24
Gives systems access to the persistent engine objects they could need.
Definition SceneContext.hpp:30