raylibstarter 0.1.0
stage.cpp
Go to the documentation of this file.
1#include <memory>
2#include <map>
3
4#include "stage.h"
5#include "renderer.h"
6#include "sprite.h"
7
8game::core::Stage::Stage(const std::string& new_scene_name, std::unique_ptr<game::core::Scene> scene) {
9 this->next_scene_ = std::move(scene);
10 this->scenes_.insert(std::make_pair(new_scene_name, this->next_scene_));
11}
12
13void game::core::Stage::switchToScene(const std::string& new_scene_name) {
14 auto it = this->scenes_.find(new_scene_name);
15
16 if(it != this->scenes_.end())
17 this->next_scene_ = it->second;
18}
19
20void game::core::Stage::switchToNewScene(const std::string &new_scene_name, std::shared_ptr<Scene> scene) {
21 this->next_scene_ = std::move(scene);
22 this->scenes_.insert(std::make_pair(new_scene_name, this->next_scene_));
23}
24
25void game::core::Stage::replaceWithNewScene(const std::string& old_scene_name, const std::string& new_scene_name, std::shared_ptr<Scene> scene) {
26 this->scenes_.erase(old_scene_name);
27
28 this->next_scene_ = std::move(scene);
29 this->scenes_.insert(std::make_pair(new_scene_name, this->next_scene_));
30}
31
32void game::core::Stage::replaceWithExistingScene(const std::string &old_scene_name, const std::string &new_scene_name) {
33 auto it = this->scenes_.find(new_scene_name);
34
35 if(it != this->scenes_.end()) {
36 this->scenes_.erase(old_scene_name);
37 this->next_scene_ = it->second;
38 }
39}
40
41const std::shared_ptr<game::core::Scene> &game::core::Stage::scene() const {
42 return this->scene_;
43}
44
45const std::map<std::string, std::shared_ptr<game::core::Scene>> &game::core::Stage::scenes() const {
46 return this->scenes_;
47}
48
50 if (this->next_scene_) {
51 this->scene_ = std::move(this->next_scene_);
52 }
53
54 this->scene_->Update();
55}
56
58 ClearBackground(WHITE);
59
60 for (auto const& [key, val] : this->scene_->actors) {
61 val->sprite()->Update();
62
63 if(val->sprite()->visible)
65 }
66
67 this->scene_->Draw();
68}
static void DrawTexture(const std::shared_ptr< game::core::Sprite > &sprite)
Draw a sprite to the screen. Oriented to the size, position and rotation angle of the sprite object....
Definition: renderer.cpp:5
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
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
void switchToScene(const std::string &new_scene_name)
Sets next_scene_ to the scene identified by new_scene_name.
Definition: stage.cpp:13
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