Simulant  21.12-194
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
scene.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef SCENE_H
20 #define SCENE_H
21 
40 #include <set>
41 
42 #include "../utils/unicode.h"
43 #include "../types.h"
44 #include "../generic/managed.h"
45 #include "../generic/property.h"
46 #include "../interfaces/nameable.h"
47 #include "../interfaces/updateable.h"
48 #include "../interfaces.h"
49 #include "../stage_manager.h"
50 #include "../generic/any/any.h"
51 
52 namespace smlt {
53 
54 class Application;
55 class Window;
56 class InputManager;
57 class SceneManager;
58 
59 class SceneLoadException : public std::runtime_error {};
60 
61 typedef sig::signal<void ()> SceneOnActivatedSignal;
62 typedef sig::signal<void ()> SceneOnDeactivatedSignal;
63 
64 class SceneBase:
65  public StageManager {
66 
67  DEFINE_SIGNAL(SceneOnActivatedSignal, signal_activated);
68  DEFINE_SIGNAL(SceneOnDeactivatedSignal, signal_deactivated);
69 
70 public:
71  typedef std::shared_ptr<SceneBase> ptr;
72 
73  SceneBase(Window* window);
74  virtual ~SceneBase();
75 
76  void _call_load();
77  void _call_unload();
78 
79  void _call_activate();
80  void _call_deactivate();
81 
82  bool is_loaded() const { return is_loaded_; }
83  bool is_active() const { return is_active_; }
84 
85  const std::string name() const {
86  return name_;
87  }
88 
89  void set_name(const std::string& name) {
90  name_ = name;
91  }
92 
93  /* Whether or not to destroy the scene when it's been unloaded.
94  * If destroyed, the next time the scene is accessed by name via the scene manager
95  * a new instance will be created.
96  */
97  bool destroy_on_unload() const { return destroy_on_unload_; }
98  void set_destroy_on_unload(bool v) {
99  destroy_on_unload_ = v;
100  }
101 
102  /* Whether or not the scene should be unloaded when it's deactivated
103  * this is the default behaviour */
104  bool unload_on_deactivate() const { return unload_on_deactivate_; }
105  void set_unload_on_deactivate(bool v) {
106  unload_on_deactivate_ = v;
107  }
108 
109 protected:
110  virtual void load() = 0;
111  virtual void unload() {}
112  virtual void activate() {}
113  virtual void deactivate() {}
114 
115  /* Linked pipelines activate and deactivate with the scene */
116  void link_pipeline(const std::string& name);
117  void unlink_pipeline(const std::string& name);
118  void link_pipeline(PipelinePtr pipeline);
119  void unlink_pipeline(PipelinePtr pipeline);
120 
121  void _update_thunk(float dt) override;
122  void _fixed_update_thunk(float dt) override;
123 private:
124  std::set<std::string> linked_pipelines_;
125 
126  virtual void pre_load() {}
127  virtual void post_unload() {}
128 
129  bool is_loaded_ = false;
130  bool is_active_ = false;
131  bool unload_on_deactivate_ = true;
132 
133  std::string name_;
134 
135  bool destroy_on_unload_ = true;
136 
137  Window* window_;
138  InputManager* input_;
139  Application* app_;
140  SceneManager* scene_manager_ = nullptr;
141  Compositor* compositor_ = nullptr;
142 
143  friend class SceneManager;
144 
145  std::vector<any> load_args;
146 
147 protected:
148  /* Returns the number of arguments passed when loading */
149  std::size_t load_arg_count() const;
150 
151  template<typename T>
152  T get_load_arg(int i) {
153  return any_cast<T>(load_args[i]);
154  }
155 
156 public:
157  S_DEFINE_PROPERTY(window, &SceneBase::window_);
158  S_DEFINE_PROPERTY(app, &SceneBase::app_);
159  S_DEFINE_PROPERTY(input, &SceneBase::input_);
160  S_DEFINE_PROPERTY(scenes, &SceneBase::scene_manager_);
161  S_DEFINE_PROPERTY(compositor, &SceneBase::compositor_);
162 };
163 
164 template<typename T>
165 class Scene : public SceneBase, public RefCounted<T> {
166 public:
167  Scene(Window* window):
168  SceneBase(window) {}
169 
170  void clean_up() override {
171  _call_unload();
172  }
173 };
174 
175 }
176 
177 #endif // SCENE_H
smlt::SceneLoadException
Definition: scene.h:59
smlt::Application
Definition: application.h:167
smlt::SceneBase
Definition: scene.h:65
smlt::RefCounted
Definition: managed.h:65
smlt
Definition: animation.cpp:25
smlt::Window
Definition: window.h:65
smlt::Scene
Definition: scene.h:165
smlt::Compositor
Definition: compositor.h:38
smlt::InputManager
Definition: input_manager.h:22
smlt::StageManager
Definition: stage_manager.h:42
smlt::SceneManager
Definition: scene_manager.h:51
smlt::default_init_ptr< Pipeline >
smlt::sig::signal
Definition: signal.h:319