CUGL 1.2
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUScene.h
1 //
2 // CUScene.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for the root node of a (2d) scene graph. After
6 // much debate, we have decided to decouple this from the application class
7 // (which is different than Cocos2d). However, scenes are still permitted
8 // to contain controller code. They are in a sense a "subapplication".
9 //
10 // This class uses our standard shared-pointer architecture.
11 //
12 // 1. The constructor does not perform any initialization; it just sets all
13 // attributes to their defaults.
14 //
15 // 2. All initialization takes place via init methods, which can fail if an
16 // object is initialized more than once.
17 //
18 // 3. All allocation takes place via static constructors which return a shared
19 // pointer.
20 //
21 // CUGL MIT License:
22 // This software is provided 'as-is', without any express or implied
23 // warranty. In no event will the authors be held liable for any damages
24 // arising from the use of this software.
25 //
26 // Permission is granted to anyone to use this software for any purpose,
27 // including commercial applications, and to alter it and redistribute it
28 // freely, subject to the following restrictions:
29 //
30 // 1. The origin of this software must not be misrepresented; you must not
31 // claim that you wrote the original software. If you use this software
32 // in a product, an acknowledgment in the product documentation would be
33 // appreciated but is not required.
34 //
35 // 2. Altered source versions must be plainly marked as such, and must not
36 // be misrepresented as being the original software.
37 //
38 // 3. This notice may not be removed or altered from any source distribution.
39 //
40 // Author: Walker White
41 // Version: 7/1/16
42 
43 #ifndef __CU_SCENE_H__
44 #define __CU_SCENE_H__
45 
46 #include <cugl/math/cu_math.h>
47 #include <cugl/2d/CUNode.h>
48 #include <cugl/renderer/CUOrthographicCamera.h>
49 
50 namespace cugl {
51 
65 class Scene {
66 #pragma mark Values
67 protected:
69  std::string _name;
71  std::shared_ptr<OrthographicCamera> _camera;
73  std::vector<std::shared_ptr<Node>> _children;
77  bool _zDirty;
79  bool _zSort;
80 
84  GLenum _srcFactor;
86  GLenum _dstFactor;
87 
89  bool _active;
90 
91 #pragma mark -
92 #pragma mark Constructors
93 public:
102  Scene();
103 
107  ~Scene() { dispose(); }
108 
115  virtual void dispose();
116 
126  virtual bool init(const Size& size) {
127  return init(0,0,size.width,size.height);
128  }
129 
140  bool init(float width, float height) {
141  return init(0,0,width,height);
142  }
143 
156  virtual bool init(const Rect& rect) {
157  return init(rect.origin.x,rect.origin.y,rect.size.width, rect.size.height);
158  }
159 
173  virtual bool init(const Vec2& origin, const Size& size) {
174  return init(origin.x,origin.y,size.width, size.height);
175  }
176 
192  bool init(float x, float y, float width, float height);
193 
194 #pragma mark -
195 #pragma mark Static Constructors
196 
205  static std::shared_ptr<Scene> alloc(const Size& size) {
206  std::shared_ptr<Scene> result = std::make_shared<Scene>();
207  return (result->init(size) ? result : nullptr);
208  }
209 
220  static std::shared_ptr<Scene> alloc(float width, float height) {
221  std::shared_ptr<Scene> result = std::make_shared<Scene>();
222  return (result->init(width,height) ? result : nullptr);
223  }
224 
236  static std::shared_ptr<Scene> alloc(const Rect& rect) {
237  std::shared_ptr<Scene> result = std::make_shared<Scene>();
238  return (result->init(rect) ? result : nullptr);
239  }
240 
253  static std::shared_ptr<Scene> alloc(const Vec2& origin, const Size& size) {
254  std::shared_ptr<Scene> result = std::make_shared<Scene>();
255  return (result->init(origin,size) ? result : nullptr);
256  }
257 
272  static std::shared_ptr<Scene> alloc(float x, float y, float width, float height) {
273  std::shared_ptr<Scene> result = std::make_shared<Scene>();
274  return (result->init(x,y,width,height) ? result : nullptr);
275  }
276 
277 #pragma mark -
278 #pragma mark Attributes
279 
284  const std::string& getName() const { return _name; }
285 
291  void setName(const std::string& name) { _name = name; }
292 
298  std::shared_ptr<Camera> getCamera();
299 
305  const std::shared_ptr<Camera> getCamera() const;
306 
315  Color4 getColor() { return _color; }
316 
325  void setColor(Color4 color) { _color = color; }
326 
337  virtual std::string toString(bool verbose = false) const;
338 
340  operator std::string() const { return toString(); }
341 
342 #pragma mark -
343 #pragma mark View Size
344 
351  void setSize(const Size& size) {
352  _camera->set(size);
353  }
354 
363  void setSize(float width, float height) {
364  _camera->set(width,height);
365  }
366 
367 
375  void setWidth(float width) {
376  _camera->set(width,_camera->getViewport().size.height);
377  }
378 
387  void setHeight(float height) {
388  _camera->set(_camera->getViewport().size.width,height);
389  }
390 
400  void setBounds(const Rect& rect) {
401  _camera->set(rect);
402  }
403 
415  void setBounds(const Vec2& origin, const Size& size) {
416  _camera->set(origin,size);
417  }
418 
431  void setBounds(float x, float y, float width, float height) {
432  _camera->set(x,y,width,height);
433  }
434 
444  void setOffset(const Vec2& origin) {
445  _camera->set(origin,_camera->getViewport().size);
446  }
447 
467  Vec3 screenToWorldCoords(const Vec2& screenCoords) const {
468  return _camera->screenToWorldCoords(screenCoords);
469  }
470 
486  Vec2 worldToScreenCoords(const Vec3& worldCoords) const {
487  return _camera->worldToScreenCoords(worldCoords);
488  }
489 
490 #pragma mark -
491 #pragma mark Scene Graph
492 
497  size_t getChildCount() const { return _children.size(); }
498 
510  std::shared_ptr<Node> getChild(unsigned int pos);
511 
523  const std::shared_ptr<Node>& getChild(unsigned int pos) const;
524 
541  template <typename T>
542  inline std::shared_ptr<T> getChild(unsigned int pos) const {
543  return std::dynamic_pointer_cast<T>(getChild(pos));
544  }
545 
558  std::shared_ptr<Node> getChildByTag(unsigned int tag) const;
559 
577  template <typename T>
578  inline std::shared_ptr<T> getChildByTag(unsigned int tag) const {
579  return std::dynamic_pointer_cast<T>(getChildByTag(tag));
580  }
581 
594  std::shared_ptr<Node> getChildByName(const std::string& name) const;
595 
613  template <typename T>
614  inline std::shared_ptr<T> getChildByName(const std::string& name) const {
615  return std::dynamic_pointer_cast<T>(getChildByName(name));
616  }
617 
623  std::vector<std::shared_ptr<Node>> getChildren() { return _children; }
624 
630  const std::vector<std::shared_ptr<Node>>& getChildren() const { return _children; }
631 
641  void addChild(std::shared_ptr<Node> child) {
642  addChild(child,child->getZOrder());
643  }
644 
657  virtual void addChild(const std::shared_ptr<Node>& child, int zval);
658 
669  void addChildWithTag(const std::shared_ptr<Node>& child, unsigned int tag) {
670  addChild(child);
671  child->setTag(tag);
672  }
673 
687  void addChildWithTag(const std::shared_ptr<Node>& child, unsigned int tag, int zval) {
688  addChild(child,zval);
689  child->setTag(tag);
690  }
691 
702  void addChildWithName(const std::shared_ptr<Node>& child, const std::string &name) {
703  addChild(child);
704  child->setName(name);
705  }
706 
720  void addChildWithName(const std::shared_ptr<Node>& child, const std::string &name, int zval) {
721  addChild(child,zval);
722  child->setName(name);
723  }
724 
738  void swapChild(const std::shared_ptr<Node>& child1, const std::shared_ptr<Node>& child2, bool inherit=false);
739 
748  virtual void removeChild(unsigned int pos);
749 
760  void removeChild(const std::shared_ptr<Node>& child);
761 
772  void removeChildByTag(unsigned int tag);
773 
784  void removeChildByName(const std::string &name);
785 
789  virtual void removeAllChildren();
790 
791 #pragma mark -
792 #pragma mark Z-order
793 
806  bool isZAutoSort() { return _zSort; }
807 
821  void setZAutoSort(bool value) { _zSort = value; }
822 
840  bool isZDirty() const { return _zDirty; }
841 
859  void sortZOrder();
860 
861 #pragma mark -
862 #pragma mark Scene Logic
863 
868  bool isActive( ) const { return _active; }
869 
875  virtual void setActive(bool value) { _active = value; }
876 
884  virtual void update(float timestep) {}
885 
889  virtual void reset() {}
890 
904  void render(const std::shared_ptr<SpriteBatch>& batch);
905 
906 private:
907 #pragma mark -
908 #pragma mark Internal Helpers
909 
926  void setZDirty(bool value) { _zDirty = value; }
927 
928  // Tightly couple with Node
929  friend class Node;
930 };
931 
932 }
933 #endif /* __CU_SCENE_H__ */
Definition: CUSize.h:57
std::shared_ptr< Node > getChildByName(const std::string &name) const
bool _active
Definition: CUScene.h:89
std::shared_ptr< Node > getChildByTag(unsigned int tag) const
float x
Definition: CUVec2.h:66
virtual void removeChild(unsigned int pos)
std::shared_ptr< T > getChildByName(const std::string &name) const
Definition: CUScene.h:614
void addChildWithName(const std::shared_ptr< Node > &child, const std::string &name, int zval)
Definition: CUScene.h:720
float y
Definition: CUVec2.h:68
void setSize(const Size &size)
Definition: CUScene.h:351
void setOffset(const Vec2 &origin)
Definition: CUScene.h:444
Definition: CUVec2.h:61
void setBounds(const Rect &rect)
Definition: CUScene.h:400
static std::shared_ptr< Scene > alloc(float x, float y, float width, float height)
Definition: CUScene.h:272
static std::shared_ptr< Scene > alloc(const Size &size)
Definition: CUScene.h:205
std::string _name
Definition: CUScene.h:69
void sortZOrder()
bool isActive() const
Definition: CUScene.h:868
void removeChildByName(const std::string &name)
void setWidth(float width)
Definition: CUScene.h:375
void setBounds(float x, float y, float width, float height)
Definition: CUScene.h:431
Color4 getColor()
Definition: CUScene.h:315
void setBounds(const Vec2 &origin, const Size &size)
Definition: CUScene.h:415
std::shared_ptr< Node > getChild(unsigned int pos)
void addChildWithTag(const std::shared_ptr< Node > &child, unsigned int tag)
Definition: CUScene.h:669
bool _zDirty
Definition: CUScene.h:77
Vec2 worldToScreenCoords(const Vec3 &worldCoords) const
Definition: CUScene.h:486
std::vector< std::shared_ptr< Node > > _children
Definition: CUScene.h:73
void addChildWithName(const std::shared_ptr< Node > &child, const std::string &name)
Definition: CUScene.h:702
bool isZAutoSort()
Definition: CUScene.h:806
void addChild(std::shared_ptr< Node > child)
Definition: CUScene.h:641
virtual void setActive(bool value)
Definition: CUScene.h:875
std::shared_ptr< T > getChild(unsigned int pos) const
Definition: CUScene.h:542
static std::shared_ptr< Scene > alloc(const Rect &rect)
Definition: CUScene.h:236
void setZAutoSort(bool value)
Definition: CUScene.h:821
virtual void update(float timestep)
Definition: CUScene.h:884
static std::shared_ptr< Scene > alloc(float width, float height)
Definition: CUScene.h:220
bool init(float width, float height)
Definition: CUScene.h:140
float width
Definition: CUSize.h:61
Vec3 screenToWorldCoords(const Vec2 &screenCoords) const
Definition: CUScene.h:467
void setHeight(float height)
Definition: CUScene.h:387
virtual bool init(const Vec2 &origin, const Size &size)
Definition: CUScene.h:173
std::shared_ptr< Camera > getCamera()
virtual void removeAllChildren()
virtual void dispose()
float height
Definition: CUSize.h:63
void render(const std::shared_ptr< SpriteBatch > &batch)
GLenum _dstFactor
Definition: CUScene.h:86
Color4 _color
Definition: CUScene.h:75
GLenum _blendEquation
Definition: CUScene.h:82
void swapChild(const std::shared_ptr< Node > &child1, const std::shared_ptr< Node > &child2, bool inherit=false)
Definition: CURect.h:45
Vec2 origin
Definition: CURect.h:49
bool _zSort
Definition: CUScene.h:79
const std::vector< std::shared_ptr< Node > > & getChildren() const
Definition: CUScene.h:630
size_t getChildCount() const
Definition: CUScene.h:497
Size size
Definition: CURect.h:51
std::shared_ptr< OrthographicCamera > _camera
Definition: CUScene.h:71
std::shared_ptr< T > getChildByTag(unsigned int tag) const
Definition: CUScene.h:578
Definition: CUVec3.h:61
void removeChildByTag(unsigned int tag)
void setName(const std::string &name)
Definition: CUScene.h:291
const std::string & getName() const
Definition: CUScene.h:284
void setSize(float width, float height)
Definition: CUScene.h:363
void setColor(Color4 color)
Definition: CUScene.h:325
Definition: CUColor4.h:1084
virtual bool init(const Size &size)
Definition: CUScene.h:126
~Scene()
Definition: CUScene.h:107
void addChildWithTag(const std::shared_ptr< Node > &child, unsigned int tag, int zval)
Definition: CUScene.h:687
Definition: CUAction.h:51
virtual void reset()
Definition: CUScene.h:889
GLenum _srcFactor
Definition: CUScene.h:84
Definition: CUScene.h:65
static std::shared_ptr< Scene > alloc(const Vec2 &origin, const Size &size)
Definition: CUScene.h:253
std::vector< std::shared_ptr< Node > > getChildren()
Definition: CUScene.h:623
virtual bool init(const Rect &rect)
Definition: CUScene.h:156
virtual std::string toString(bool verbose=false) const
bool isZDirty() const
Definition: CUScene.h:840