Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
stage.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 STAGE_H
20 #define STAGE_H
21 
22 #include <functional>
23 
24 #include "generic/object_manager.h"
25 #include "generic/managed.h"
26 #include "generic/generic_tree.h"
27 #include "generic/data_carrier.h"
28 #include "threads/atomic.h"
29 
30 #include "managers/window_holder.h"
31 
32 #include "nodes/skies/skybox_manager.h"
33 #include "nodes/sprites/sprite_manager.h"
34 #include "nodes/actor.h"
35 #include "nodes/geom.h"
36 #include "nodes/particle_system.h"
37 #include "nodes/stage_node.h"
38 #include "nodes/light.h"
39 #include "nodes/mesh_instancer.h"
40 #include "nodes/stage_node_manager.h"
41 
42 #include "types.h"
43 #include "asset_manager.h"
44 
45 #include "macros.h"
46 #include "stage_manager.h"
47 
48 namespace smlt {
49 
50 namespace ui {
51  class UIManager;
52 }
53 
54 class Partitioner;
55 
56 class Debug;
57 class Sprite;
58 
59 typedef StageNodeManager<StageNodePool, ActorID, Actor> ActorManager;
60 typedef StageNodeManager<StageNodePool, GeomID, Geom> GeomManager;
61 typedef StageNodeManager<StageNodePool, LightID, Light> LightManager;
62 typedef StageNodeManager<StageNodePool, ParticleSystemID, ParticleSystem> ParticleSystemManager;
63 typedef StageNodeManager<StageNodePool, CameraID, Camera> CameraManager;
64 typedef StageNodeManager<StageNodePool, MeshInstancerID, MeshInstancer> MeshInstancerManager;
65 
66 typedef sig::signal<void (StageNode*, StageNodeType)> StageNodeCreatedSignal;
67 typedef sig::signal<void (StageNode*, StageNodeType)> StageNodeDestroyedSignal;
68 
69 typedef sig::signal<void (CameraID, Viewport)> StagePreRenderSignal;
70 typedef sig::signal<void (CameraID, Viewport)> StagePostRenderSignal;
71 
72 extern const Colour DEFAULT_LIGHT_COLOUR;
73 
74 class Stage:
75  public TypedDestroyableObject<Stage, StageManager>,
76  public ContainerNode,
77  public generic::Identifiable<StageID>,
78  public Loadable,
79  public ChainNameable<Stage>,
80  public RefCounted<Stage> {
81 
82  DEFINE_SIGNAL(StageNodeCreatedSignal, signal_stage_node_created);
83  DEFINE_SIGNAL(StageNodeDestroyedSignal, signal_stage_node_destroyed);
84 
85  DEFINE_SIGNAL(StagePreRenderSignal, signal_stage_pre_render);
86  DEFINE_SIGNAL(StagePostRenderSignal, signal_stage_post_render);
87 
88  /* Necessary to access asset_manager before the assets Property<> is initialized */
89  friend class ui::UIManager;
90 
91 public:
92  Stage(
93  StageManager *parent,
94  StageNodePool* node_pool,
95  AvailablePartitioner partitioner
96  );
97 
98  virtual ~Stage();
99 
100  ActorPtr new_actor();
101  ActorPtr new_actor_with_name(const std::string& name);
102 
103  ActorPtr new_actor_with_mesh(MeshID mid);
104  ActorPtr new_actor_with_name_and_mesh(const std::string& name, MeshID mid);
105 
106  ActorPtr new_actor_with_parent(ActorID parent);
107  ActorPtr new_actor_with_parent_and_mesh(ActorID parent, MeshID mid);
108  ActorPtr new_actor_with_parent_and_mesh(SpriteID parent, MeshID mid);
109  ActorPtr actor(ActorID e);
110  ActorPtr actor(ActorID e) const;
111  bool has_actor(ActorID e) const;
112  void destroy_actor(ActorID e);
113  std::size_t actor_count() const;
114 
122 
129 
136 
141  std::size_t mesh_instancer_count() const;
142 
148  bool has_mesh_instancer(MeshInstancerID mid) const;
149 
150  CameraPtr new_camera();
151  CameraPtr new_camera_with_orthographic_projection(double left=0, double right=0, double bottom=0, double top=0, double near=-1.0, double far=1.0);
152  CameraPtr new_camera_for_ui();
153  CameraPtr new_camera_for_viewport(const Viewport& vp);
154  CameraPtr camera(CameraID c);
155  void destroy_camera(CameraID cid);
156  uint32_t camera_count() const;
157  bool has_camera(CameraID id) const;
158  void destroy_all_cameras();
159 
160  GeomPtr new_geom_with_mesh(MeshID mid, const GeomCullerOptions& culler_options=GeomCullerOptions());
161  GeomPtr new_geom_with_mesh_at_position(
162  MeshID mid, const Vec3& position,
163  const Quaternion& rotation=Quaternion(),
164  const Vec3& scale=Vec3(1, 1, 1),
165  const GeomCullerOptions& culler_options=GeomCullerOptions()
166  );
167  GeomPtr geom(const GeomID gid) const;
168  bool has_geom(GeomID geom_id) const;
169  void destroy_geom(GeomID geom_id);
170  std::size_t geom_count() const;
171 
172  ParticleSystemPtr new_particle_system(ParticleScriptID particle_script);
173  ParticleSystemPtr new_particle_system_with_parent(ParticleScriptID particle_script, ActorID parent);
174  ParticleSystemPtr particle_system(ParticleSystemID pid);
175  bool has_particle_system(ParticleSystemID pid) const;
176  void destroy_particle_system(ParticleSystemID pid);
177  std::size_t particle_system_count() const;
178 
179  LightPtr new_light_as_directional(const Vec3& direction=Vec3(1, -0.5, 0), const smlt::Colour& colour=DEFAULT_LIGHT_COLOUR);
180  LightPtr new_light_as_point(const Vec3& position=Vec3(), const smlt::Colour& colour=DEFAULT_LIGHT_COLOUR);
181 
182  LightPtr light(LightID light);
183  void destroy_light(LightID light_id);
184  std::size_t light_count() const;
185 
186  smlt::Colour ambient_light() const { return ambient_light_; }
187  void set_ambient_light(const smlt::Colour& c) { ambient_light_ = c; }
188 
189  void move(float x, float y, float z) {
190  _S_UNUSED(x);
191  _S_UNUSED(y);
192  _S_UNUSED(z);
193 
194  throw std::logic_error("You cannot move the stage");
195  }
196 
197  bool init() override;
198  void clean_up() override;
199 
200  // Updateable interface
201  void update(float dt) override;
202 
203  const AABB& aabb() const override { return aabb_; }
204  const AABB transformed_aabb() const override { return aabb_; }
205 
206  /* Enables the debug actor to allow drawing of debug lines and points */
207  Debug* enable_debug(bool v=true);
208 
209  /* Implementation for TypedDestroyableObject (INTERNAL) */
210  void destroy_object(Actor* object);
211  void destroy_object(Light* object);
212  void destroy_object(Camera* object);
213  void destroy_object(Geom* object);
214  void destroy_object(ParticleSystem* object);
215  void destroy_object(MeshInstancer* object);
216 
217  void destroy_object_immediately(Actor* object);
218  void destroy_object_immediately(Light* object);
219  void destroy_object_immediately(Camera* object);
220  void destroy_object_immediately(Geom* object);
221  void destroy_object_immediately(ParticleSystem* object);
222  void destroy_object_immediately(MeshInstancer* object);
223 
224  bool is_part_of_active_pipeline() const {
225  return active_pipeline_count_ > 0;
226  }
227 
228 private:
229  UniqueIDKey make_key() const override {
230  return make_unique_id_key(id());
231  }
232 
233  StageNodePool* node_pool_ = nullptr;
234 
235  AABB aabb_;
236 
237  std::shared_ptr<Partitioner> partitioner_;
238 
239  void set_partitioner(AvailablePartitioner partitioner);
240 
241  std::shared_ptr<Debug> debug_;
242 
243  //FIXME: All managers should be composition rather than inheritence,
244  // like this one!
245  std::shared_ptr<AssetManager> asset_manager_;
246  std::unique_ptr<ui::UIManager> ui_;
247 
248  unicode name_;
249 
250  smlt::Colour ambient_light_ = smlt::Colour(0.3, 0.3, 0.3, 1.0);
251 
252  std::unique_ptr<GeomManager> geom_manager_;
253  std::unique_ptr<SkyManager> sky_manager_;
254  std::unique_ptr<SpriteManager> sprite_manager_;
255  std::unique_ptr<MeshInstancerManager> mesh_instancer_manager_;
256  std::unique_ptr<ActorManager> actor_manager_;
257  std::unique_ptr<ParticleSystemManager> particle_system_manager_;
258  std::unique_ptr<LightManager> light_manager_;
259  std::unique_ptr<CameraManager> camera_manager_;
260 
261  generic::DataCarrier data_;
262 
263  friend class Pipeline;
264  thread::Atomic<uint8_t> active_pipeline_count_ = {0};
265 
266 private:
267  friend class StageManager;
268 
269  void on_actor_created(ActorID actor_id);
270  void on_actor_destroyed(ActorID actor_id);
271 
272  void clean_up_dead_objects();
273 
274 public:
275  Property<decltype(&Stage::debug_)> debug = {this, &Stage::debug_};
276  Property<decltype(&Stage::partitioner_)> partitioner = {this, &Stage::partitioner_};
277  Property<decltype(&Stage::asset_manager_)> assets = {this, &Stage::asset_manager_};
278  Property<decltype(&Stage::data_)> data = {this, &Stage::data_};
279  Property<decltype(&Stage::ui_)> ui = {this, &Stage::ui_};
280  Property<decltype(&Stage::sky_manager_)> skies = {this, &Stage::sky_manager_};
281  Property<decltype(&Stage::sprite_manager_)> sprites = {this, &Stage::sprite_manager_};
282 };
283 
284 }
285 #endif // SUBSCENE_H
smlt::Actor
Definition: actor.h:48
smlt::Property
Definition: property.h:202
smlt::ui::UIManager
Definition: ui_manager.h:55
smlt::Geom
The Geom class.
Definition: geom.h:64
smlt::Vec3
Definition: vec3.h:23
smlt::Stage::has_mesh_instancer
bool has_mesh_instancer(MeshInstancerID mid) const
Checks to see if this MeshInstancer exists.
Definition: stage.cpp:258
smlt::Quaternion
Definition: quaternion.h:20
smlt::Stage
Definition: stage.h:80
smlt::RefCounted
Definition: managed.h:65
smlt
Definition: animation.cpp:25
smlt::GeomCullerOptions
Definition: geom.h:40
smlt::Light
Definition: light.h:35
smlt::Polylist
Definition: polylist.h:67
smlt::ParticleSystem
Definition: particle_system.h:43
smlt::generic::Identifiable
Definition: identifiable.h:26
smlt::ContainerNode
Definition: stage_node.h:304
smlt::UniqueID< MeshPtr >
smlt::thread::Atomic< uint8_t >
smlt::TypedDestroyableObject
Definition: manual_object.h:34
smlt::Camera
Definition: camera.h:21
smlt::Stage::new_mesh_instancer
MeshInstancerPtr new_mesh_instancer(MeshID mid)
Creates a new MeshInstancer from a mesh and returns it.
Definition: stage.cpp:219
smlt::Pipeline
Definition: pipeline.h:21
smlt::MeshInstancer
The MeshInstancer class.
Definition: mesh_instancer.h:44
smlt::Stage::mesh_instancer_count
std::size_t mesh_instancer_count() const
Returns the number of MeshInstancers in the stage.
Definition: stage.cpp:254
smlt::Loadable
Definition: loadable.h:26
smlt::ChainNameable
Definition: nameable.h:34
smlt::AABB
Definition: aabb.h:22
unicode
Definition: unicode.h:36
smlt::Stage::mesh_instancer
MeshInstancerPtr mesh_instancer(MeshInstancerID mid)
Returns the MeshInstancerPtr associated with the ID.
Definition: stage.cpp:250
smlt::StageManager
Definition: stage_manager.h:42
smlt::default_init_ptr< Actor >
smlt::Debug
Definition: debug.h:28
smlt::generic::DataCarrier
Definition: data_carrier.h:37
smlt::Stage::destroy_mesh_instancer
bool destroy_mesh_instancer(MeshInstancerID mid)
Destroys a MeshInstancer by its ID.
Definition: stage.cpp:240
smlt::Colour
Definition: colour.h:29
smlt::Viewport
Definition: viewport.h:44
smlt::sig::signal
Definition: signal.h:330