raylibstarter 0.1.0
stage.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <string>
5
6#include <raylib.h>
7#include <map>
8
9#include "scene.h"
10#include "sprite.h"
11
12namespace game::core {
13 /**
14 * A main stage object is created by the Game class instance. This stage object is accessible under game::core::Store::stage.
15 *
16 * Several scenes can be managed by the stage object. Scenes can be replaced by new scenes and you can switch between existing scenes.
17 * The change of scenes can be triggered by the respective scenes themselves.
18 *
19 * Stage also calls the Update() and Draw() methods of the active scene. Draw functions that are triggered by
20 * the scene's Draw() method are always executed after the scene's actors are already drawn to the screen.
21 *
22 * @brief Stage is used to manage scenes. The Stage automatically draws all Actor objects of a scene if they are set to visible.
23 */
24 class Stage final {
25 public:
26 Stage() = delete;
27
28 /**
29 * @brief Constructor.
30 *
31 * Create a new Stage object
32 * @param new_scene_name Name of the new scene.
33 * @param scene Unique pointer to a new scene object.
34 */
35 Stage(const std::string& new_scene_name, std::unique_ptr<game::core::Scene> scene);
36
37 Stage(const game::core::Stage &stage) = delete;
38
39 Stage &operator=(const Stage &) = delete;
40
41 virtual ~Stage() = default;
42
43 /**
44 * @return Returns a reference to currently active scene.
45 */
46 [[nodiscard]] const std::shared_ptr<game::core::Scene> &scene() const;
47
48 /**
49 * @return Returns a reference to the map containing all managed scenes.
50 */
51 [[nodiscard]] const std::map<std::string, std::shared_ptr<game::core::Scene>> &scenes() const;
52
53 /**
54 * @brief Inserts a new scene into scenes_, sets next_scene_ to the new scene and removes the scene identified by old_scene_name.
55 * @param old_scene_name Name of the scene to be removed.
56 * @param new_scene_name Name of the new scene to switch to.
57 * @param scene Shared pointer to the new scene to switch to.
58 */
59 void replaceWithNewScene(const std::string& old_scene_name, const std::string& new_scene_name, std::shared_ptr<Scene> scene);
60
61 /**
62 * @brief Sets next_scene_ to the existing scene identified by new_scene_name and removes the scene identified by old_scene_name.
63 * @param old_scene_name Name of the scene to be removed.
64 * @param new_scene_name Name of the existing scene to switch to.
65 */
66 void replaceWithExistingScene(const std::string& old_scene_name, const std::string& new_scene_name);
67
68 /**
69 * @brief Inserts a new scene into scenes_ and sets next_scene_ to the new scene.
70 * @param new_scene_name Name of the new scene to switch to.
71 * @param scene Shared pointer to the new scene to switch to.
72 */
73 void switchToNewScene(const std::string& new_scene_name, std::shared_ptr<Scene> scene);
74
75 /**
76 * @brief Sets next_scene_ to the scene identified by new_scene_name
77 * @param new_scene_name Name of the scene to switch to.
78 */
79 void switchToScene(const std::string& new_scene_name);
80
81 /**
82 * @brief Changes the active scene if next_scene_ is set. Then calls the update() method of the scene.
83 */
84 void Update();
85
86 /**
87 * @brief Draws all visible actors of the active scene. Then calls the Draw() method of the active scene.
88 */
89 void Draw();
90
91 private:
92 /// Active scene object.
93 std::shared_ptr<game::core::Scene> scene_;
94
95 /// Pointer to the next scene to be active. The switch will happen when Update() is called the next time.
96 std::shared_ptr<game::core::Scene> next_scene_;
97
98 /// This maps stores all referenced scenes. They can be addressed by their names.
99 std::map<std::string, std::shared_ptr<game::core::Scene>> scenes_;
100
101 };
102}
Stage is used to manage scenes. The Stage automatically draws all Actor objects of a scene if they ar...
Definition: stage.h:24
std::map< std::string, std::shared_ptr< game::core::Scene > > scenes_
This maps stores all referenced scenes. They can be addressed by their names.
Definition: stage.h:99
std::shared_ptr< game::core::Scene > next_scene_
Pointer to the next scene to be active. The switch will happen when Update() is called the next time.
Definition: stage.h:96
virtual ~Stage()=default
void replaceWithNewScene(const std::string &old_scene_name, const std::string &new_scene_name, std::shared_ptr< Scene > scene)
Inserts a new scene into scenes_, sets next_scene_ to the new scene and removes the scene identified ...
Definition: stage.cpp:25
const std::map< std::string, std::shared_ptr< game::core::Scene > > & scenes() const
Definition: stage.cpp:45
void Draw()
Draws all visible actors of the active scene. Then calls the Draw() method of the active scene.
Definition: stage.cpp:57
const std::shared_ptr< game::core::Scene > & scene() const
Definition: stage.cpp:41
std::shared_ptr< game::core::Scene > scene_
Active scene object.
Definition: stage.h:93
void switchToScene(const std::string &new_scene_name)
Sets next_scene_ to the scene identified by new_scene_name.
Definition: stage.cpp:13
Stage & operator=(const Stage &)=delete
void switchToNewScene(const std::string &new_scene_name, std::shared_ptr< Scene > scene)
Inserts a new scene into scenes_ and sets next_scene_ to the new scene.
Definition: stage.cpp:20
void Update()
Changes the active scene if next_scene_ is set. Then calls the update() method of the scene.
Definition: stage.cpp:49
void replaceWithExistingScene(const std::string &old_scene_name, const std::string &new_scene_name)
Sets next_scene_ to the existing scene identified by new_scene_name and removes the scene identified ...
Definition: stage.cpp:32
Stage(const game::core::Stage &stage)=delete