CUGL
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).
8 //
9 // This class uses our standard shared-pointer architecture.
10 //
11 // 1. The constructor does not perform any initialization; it just sets all
12 // attributes to their defaults.
13 //
14 // 2. All initialization takes place via init methods, which can fail if an
15 // object is initialized more than once.
16 //
17 // 3. All allocation takes place via static constructors which return a shared
18 // pointer.
19 //
20 // CUGL zlib License:
21 // This software is provided 'as-is', without any express or implied
22 // warranty. In no event will the authors be held liable for any damages
23 // arising from the use of this software.
24 //
25 // Permission is granted to anyone to use this software for any purpose,
26 // including commercial applications, and to alter it and redistribute it
27 // freely, subject to the following restrictions:
28 //
29 // 1. The origin of this software must not be misrepresented; you must not
30 // claim that you wrote the original software. If you use this software
31 // in a product, an acknowledgment in the product documentation would be
32 // appreciated but is not required.
33 //
34 // 2. Altered source versions must be plainly marked as such, and must not
35 // be misrepresented as being the original software.
36 //
37 // 3. This notice may not be removed or altered from any source distribution.
38 //
39 // Author: Walker White
40 // Version: 7/1/16
41 
42 #ifndef __CU_SCENE_H__
43 #define __CU_SCENE_H__
44 
45 #include "../math/cu_math.h"
46 #include "CUNode.h"
47 #include "../renderer/CUOrthographicCamera.h"
48 
49 namespace cugl {
50 
64 class Scene {
65 #pragma mark Values
66 protected:
68  std::string _name;
70  std::shared_ptr<OrthographicCamera> _camera;
72  std::vector<std::shared_ptr<Node>> _children;
76  bool _zDirty;
78  bool _zSort;
79 
80 #pragma mark -
81 #pragma mark Constructors
82 public:
91  Scene();
92 
96  ~Scene() { _camera = nullptr; _children.clear(); }
97 
104  void dispose();
105 
115  bool init(const Size& size) {
116  return init(0,0,size.width,size.height);
117  }
118 
129  bool init(float width, float height) {
130  return init(0,0,width,height);
131  }
132 
145  bool init(const Rect& rect) {
146  return init(rect.origin.x,rect.origin.y,rect.size.width, rect.size.height);
147  }
148 
162  bool init(const Vec2& origin, const Size& size) {
163  return init(origin.x,origin.y,size.width, size.height);
164  }
165 
181  bool init(float x, float y, float width, float height);
182 
183 #pragma mark -
184 #pragma mark Static Constructors
185 
194  static std::shared_ptr<Scene> alloc(const Size& size) {
195  std::shared_ptr<Scene> result = std::make_shared<Scene>();
196  return (result->init(size) ? result : nullptr);
197  }
198 
209  static std::shared_ptr<Scene> alloc(float width, float height) {
210  std::shared_ptr<Scene> result = std::make_shared<Scene>();
211  return (result->init(width,height) ? result : nullptr);
212  }
213 
225  static std::shared_ptr<Scene> alloc(const Rect& rect) {
226  std::shared_ptr<Scene> result = std::make_shared<Scene>();
227  return (result->init(rect) ? result : nullptr);
228  }
229 
242  static std::shared_ptr<Scene> alloc(const Vec2& origin, const Size& size) {
243  std::shared_ptr<Scene> result = std::make_shared<Scene>();
244  return (result->init(origin,size) ? result : nullptr);
245  }
246 
261  static std::shared_ptr<Scene> alloc(float x, float y, float width, float height) {
262  std::shared_ptr<Scene> result = std::make_shared<Scene>();
263  return (result->init(x,y,width,height) ? result : nullptr);
264  }
265 
266 #pragma mark -
267 #pragma mark Attributes
268 
273  const std::string& getName() const { return _name; }
274 
280  void setName(const std::string& name) { _name = name; }
281 
287  std::shared_ptr<Camera> getCamera();
288 
294  const std::shared_ptr<Camera> getCamera() const;
295 
304  Color4 getColor() { return _color; }
305 
314  void setColor(Color4 color) { _color = color; }
315 
326  virtual std::string toString(bool verbose = false) const;
327 
329  operator std::string() const { return toString(); }
330 
331 #pragma mark -
332 #pragma mark View Size
333 
340  void setSize(const Size& size) {
341  _camera->set(size);
342  }
343 
352  void setSize(float width, float height) {
353  _camera->set(width,height);
354  }
355 
356 
364  void setWidth(float width) {
365  _camera->set(width,_camera->getViewport().size.height);
366  }
367 
376  void setHeight(float height) {
377  _camera->set(_camera->getViewport().size.width,height);
378  }
379 
389  void setBounds(const Rect& rect) {
390  _camera->set(rect);
391  }
392 
404  void setBounds(const Vec2& origin, const Size& size) {
405  _camera->set(origin,size);
406  }
407 
420  void setBounds(float x, float y, float width, float height) {
421  _camera->set(x,y,width,height);
422  }
423 
433  void setOffset(const Vec2& origin) {
434  _camera->set(origin,_camera->getViewport().size);
435  }
436 
456  Vec3 screenToWorldCoords(const Vec2& screenCoords) const {
457  return _camera->screenToWorldCoords(screenCoords);
458  }
459 
475  Vec2 worldToScreenCoords(const Vec3& worldCoords) const {
476  return _camera->worldToScreenCoords(worldCoords);
477  }
478 
479 #pragma mark -
480 #pragma mark Scene Graph
481 
486  size_t getChildCount() const { return _children.size(); }
487 
499  std::shared_ptr<Node> getChild(unsigned int pos);
500 
512  const std::shared_ptr<Node>& getChild(unsigned int pos) const;
513 
530  template <typename T>
531  inline std::shared_ptr<T> getChild(unsigned int pos) const {
532  return std::dynamic_pointer_cast<T>(getChild(pos));
533  }
534 
547  std::shared_ptr<Node> getChildByTag(unsigned int tag) const;
548 
566  template <typename T>
567  inline std::shared_ptr<T> getChildByTag(unsigned int tag) const {
568  return std::dynamic_pointer_cast<T>(getChildByTag(tag));
569  }
570 
583  std::shared_ptr<Node> getChildByName(const std::string& name) const;
584 
602  template <typename T>
603  inline std::shared_ptr<T> getChildByName(const std::string& name) const {
604  return std::dynamic_pointer_cast<T>(getChildByName(name));
605  }
606 
612  std::vector<std::shared_ptr<Node>> getChildren() { return _children; }
613 
619  const std::vector<std::shared_ptr<Node>>& getChildren() const { return _children; }
620 
630  void addChild(std::shared_ptr<Node> child) {
631  addChild(child,child->getZOrder());
632  }
633 
646  virtual void addChild(const std::shared_ptr<Node>& child, int zval);
647 
658  void addChildWithTag(const std::shared_ptr<Node>& child, unsigned int tag) {
659  addChild(child);
660  child->setTag(tag);
661  }
662 
676  void addChildWithTag(const std::shared_ptr<Node>& child, unsigned int tag, int zval) {
677  addChild(child,zval);
678  child->setTag(tag);
679  }
680 
691  void addChildWithName(const std::shared_ptr<Node>& child, const std::string &name) {
692  addChild(child);
693  child->setName(name);
694  }
695 
709  void addChildWithName(const std::shared_ptr<Node>& child, const std::string &name, int zval) {
710  addChild(child,zval);
711  child->setName(name);
712  }
713 
727  void swapChild(const std::shared_ptr<Node>& child1, const std::shared_ptr<Node>& child2, bool inherit=false);
728 
737  virtual void removeChild(unsigned int pos);
738 
749  void removeChild(const std::shared_ptr<Node>& child);
750 
761  void removeChildByTag(unsigned int tag);
762 
773  void removeChildByName(const std::string &name);
774 
778  virtual void removeAllChildren();
779 
780 #pragma mark -
781 #pragma mark Z-order
782 
795  bool isZAutoSort() { return _zSort; }
796 
810  void setZAutoSort(bool value) { _zSort = value; }
811 
829  bool isZDirty() const { return _zDirty; }
830 
848  void sortZOrder();
849 
850 #pragma mark -
851 #pragma mark Rendering
852 
865  void render(const std::shared_ptr<SpriteBatch>& batch);
866 
867 private:
868 #pragma mark -
869 #pragma mark Internal Helpers
870 
887  void setZDirty(bool value) { _zDirty = value; }
888 
889  // Tightly couple with Node
890  friend class Node;
891 };
892 
893 }
894 #endif /* __CU_SCENE_H__ */
Definition: CUSize.h:57
std::shared_ptr< Node > getChildByName(const std::string &name) const
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:603
void addChildWithName(const std::shared_ptr< Node > &child, const std::string &name, int zval)
Definition: CUScene.h:709
float y
Definition: CUVec2.h:68
void setSize(const Size &size)
Definition: CUScene.h:340
void setOffset(const Vec2 &origin)
Definition: CUScene.h:433
Definition: CUVec2.h:61
bool init(const Rect &rect)
Definition: CUScene.h:145
void setBounds(const Rect &rect)
Definition: CUScene.h:389
static std::shared_ptr< Scene > alloc(float x, float y, float width, float height)
Definition: CUScene.h:261
static std::shared_ptr< Scene > alloc(const Size &size)
Definition: CUScene.h:194
std::string _name
Definition: CUScene.h:68
void sortZOrder()
void removeChildByName(const std::string &name)
void setWidth(float width)
Definition: CUScene.h:364
void setBounds(float x, float y, float width, float height)
Definition: CUScene.h:420
Color4 getColor()
Definition: CUScene.h:304
bool init(const Vec2 &origin, const Size &size)
Definition: CUScene.h:162
void setBounds(const Vec2 &origin, const Size &size)
Definition: CUScene.h:404
std::shared_ptr< Node > getChild(unsigned int pos)
void addChildWithTag(const std::shared_ptr< Node > &child, unsigned int tag)
Definition: CUScene.h:658
bool _zDirty
Definition: CUScene.h:76
Vec2 worldToScreenCoords(const Vec3 &worldCoords) const
Definition: CUScene.h:475
std::vector< std::shared_ptr< Node > > _children
Definition: CUScene.h:72
void addChildWithName(const std::shared_ptr< Node > &child, const std::string &name)
Definition: CUScene.h:691
bool isZAutoSort()
Definition: CUScene.h:795
void addChild(std::shared_ptr< Node > child)
Definition: CUScene.h:630
std::shared_ptr< T > getChild(unsigned int pos) const
Definition: CUScene.h:531
static std::shared_ptr< Scene > alloc(const Rect &rect)
Definition: CUScene.h:225
void setZAutoSort(bool value)
Definition: CUScene.h:810
static std::shared_ptr< Scene > alloc(float width, float height)
Definition: CUScene.h:209
bool init(float width, float height)
Definition: CUScene.h:129
float width
Definition: CUSize.h:61
Vec3 screenToWorldCoords(const Vec2 &screenCoords) const
Definition: CUScene.h:456
void setHeight(float height)
Definition: CUScene.h:376
std::shared_ptr< Camera > getCamera()
void dispose()
virtual void removeAllChildren()
float height
Definition: CUSize.h:63
void render(const std::shared_ptr< SpriteBatch > &batch)
Color4 _color
Definition: CUScene.h:74
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:78
const std::vector< std::shared_ptr< Node > > & getChildren() const
Definition: CUScene.h:619
size_t getChildCount() const
Definition: CUScene.h:486
bool init(const Size &size)
Definition: CUScene.h:115
Size size
Definition: CURect.h:51
std::shared_ptr< OrthographicCamera > _camera
Definition: CUScene.h:70
std::shared_ptr< T > getChildByTag(unsigned int tag) const
Definition: CUScene.h:567
Definition: CUVec3.h:61
void removeChildByTag(unsigned int tag)
void setName(const std::string &name)
Definition: CUScene.h:280
const std::string & getName() const
Definition: CUScene.h:273
void setSize(float width, float height)
Definition: CUScene.h:352
void setColor(Color4 color)
Definition: CUScene.h:314
Definition: CUColor4.h:1104
~Scene()
Definition: CUScene.h:96
void addChildWithTag(const std::shared_ptr< Node > &child, unsigned int tag, int zval)
Definition: CUScene.h:676
Definition: CUAnimationNode.h:52
Definition: CUScene.h:64
static std::shared_ptr< Scene > alloc(const Vec2 &origin, const Size &size)
Definition: CUScene.h:242
std::vector< std::shared_ptr< Node > > getChildren()
Definition: CUScene.h:612
virtual std::string toString(bool verbose=false) const
bool isZDirty() const
Definition: CUScene.h:829