17#include <unordered_map>
25#include <emscripten.h>
41template <
typename T>
class Scene {
72 std::erase_if(
objects_, [](
const auto& obj) {
return !obj->alive; });
76 if (obj->active && obj->alive) {
129 template <
typename ObjectType,
typename... Args>
132 static_assert(std::is_base_of_v<game::GameObject, ObjectType>,
133 "ObjectType must be derived from Scene<T>");
134 static_assert(std::is_constructible_v<ObjectType, Args...>,
135 "ObjectType must be constructible with the given arguments");
137 auto obj = std::make_shared<ObjectType>(std::forward<Args>(args)...);
146 const std::vector<std::shared_ptr<game::GameObject>>&
get_objects()
const
157 template <
typename ObjectType> std::vector<std::shared_ptr<ObjectType>>
get_object_view()
159 static_assert(std::is_base_of_v<game::GameObject, ObjectType>,
160 "ObjectType must be derived from Scene<T>");
162 std::vector<std::shared_ptr<ObjectType>> result;
164 if (
auto casted_obj = std::dynamic_pointer_cast<ObjectType>(obj)) {
165 result.push_back(casted_obj);
177 std::vector<std::shared_ptr<game::GameObject>>
objects_;
197 em_time_ = std::chrono::high_resolution_clock::now();
206 template <
typename SceneType,
typename... Args>
210 std::is_base_of_v<Scene<T>, SceneType>,
"SceneType must be derived from Scene<T>");
211 static_assert(std::is_constructible_v<SceneType, Args...>,
212 "SceneType must be constructible with the given arguments");
214 auto scene = std::make_shared<SceneType>(std::forward<Args>(args)...);
237 emscripten_set_main_loop(SceneManager::loop_emscripten, 0, 1);
240 using namespace std::chrono_literals;
241 std::chrono::nanoseconds lag(0ns);
242 auto time_start = std::chrono::high_resolution_clock::now();
243 auto last_second = std::chrono::high_resolution_clock::now();
247 auto delta_time = std::chrono::high_resolution_clock::now() - time_start;
248 time_start = std::chrono::high_resolution_clock::now();
249 lag += std::chrono::duration_cast<std::chrono::nanoseconds>(delta_time);
261 if (std::chrono::high_resolution_clock::now() - last_second >= 1s) {
264 last_second = last_second + 1s;
353 std::unordered_map<T, std::shared_ptr<Scene<T>>>
scenes_;
366 static std::chrono::high_resolution_clock::time_point em_time_;
369 static void loop_emscripten()
371 if (instance_ !=
nullptr) {
372 auto delta_time = std::chrono::high_resolution_clock::now() - SceneManager::em_time_;
373 SceneManager::em_time_ = std::chrono::high_resolution_clock::now();
375 instance_->
update(std::chrono::duration<float>(delta_time).count());
383template <
typename T> SceneManager<T>* SceneManager<T>::instance_ =
nullptr;
386template <
typename T>
auto SceneManager<T>::em_time_ = std::chrono::high_resolution_clock::now();
int z_index
The layer that the object is on.
Forward declaration of the SceneManager class.
std::shared_ptr< Scene< T > > active_scene_
The current scene of the scene engine.
void start()
Main loop for the scene engine. If this is not enough, or you want to define your own loop,...
SceneManager()
Constructor for the SceneManager class.
bool has_next_scene_
Flag to indicate if there is a next scene to change to.
void draw()
Draw the current scene.
T next_scene_
The next scene of the scene engine.
void set_timestep(std::chrono::nanoseconds ts)
Set the fixed timestep for the game loop.
std::chrono::nanoseconds timestep_
Fixed timestep for the game loop.
void update(const float dt)
Update the current scene.
std::unordered_map< T, std::shared_ptr< Scene< T > > > scenes_
Collection of all scenes registered in the scene engine.
void set_next_scene(const T scene_id)
Set the next scene.
std::chrono::nanoseconds get_timestep() const
Get the current timestep.
int get_fps() const
Get the current FPS. Only applies to the managed loop.
int fps_
FPS Counter for managed loop.
void register_scene(const T scene_id, Args &&... args)
Register a scene to be managed by the scene engine.
void change_scene()
Change the current scene to the next scene.
Base class for game scenes.
SceneManager< T > & manager
Reference to the scene manager.
std::vector< std::shared_ptr< game::GameObject > > obj_to_create_
Objects to be created in the next frame.
virtual void draw()
Draw the game scene.
Scene(SceneManager< T > &manager)
Constructor for the Scene class.
void register_object(const std::shared_ptr< game::GameObject > &obj)
Add a game object to the scene.
virtual void update(float dt)
Update the game scene.
virtual void init()
Initialize the game scene.
const std::vector< std::shared_ptr< game::GameObject > > & get_objects() const
Get game objects in the scene.
virtual void cleanup()
Handle input for the game scene.
std::vector< std::shared_ptr< game::GameObject > > objects_
Collection of game objects in the scene.
std::vector< std::shared_ptr< ObjectType > > get_object_view()
Get game objects of a specific type in the scene.
virtual ~Scene()=default
Destructor for the Scene class.
std::shared_ptr< ObjectType > create_object(Args &&... args)
Create a new game object in the scene.
Core routines including main loop and initialization.
Display and window routines for the ASW library.
bool exit
When set to true, exits the main loop.
void update()
Updates core module functionality.
void present()
Present the window.
void clear()
Clear the window.
constexpr auto DEFAULT_TIMESTEP
Default time step for the game loop.