CUGL 1.3
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 
414  void setBounds(const Vec2& origin, const Size& size) {
415  _camera->set(origin,size);
416  }
417 
430  void setBounds(float x, float y, float width, float height) {
431  _camera->set(x,y,width,height);
432  }
433 
443  void setOffset(const Vec2& origin) {
444  _camera->set(origin,_camera->getViewport().size);
445  }
446 
466  Vec3 screenToWorldCoords(const Vec2& screenCoords) const {
467  return _camera->screenToWorldCoords(screenCoords);
468  }
469 
485  Vec2 worldToScreenCoords(const Vec3& worldCoords) const {
486  return _camera->worldToScreenCoords(worldCoords);
487  }
488 
489 #pragma mark -
490 #pragma mark Scene Graph
491 
496  size_t getChildCount() const { return _children.size(); }
497 
509  std::shared_ptr<Node> getChild(unsigned int pos);
510 
522  const std::shared_ptr<Node>& getChild(unsigned int pos) const;
523 
540  template <typename T>
541  inline std::shared_ptr<T> getChild(unsigned int pos) const {
542  return std::dynamic_pointer_cast<T>(getChild(pos));
543  }
544 
557  std::shared_ptr<Node> getChildByTag(unsigned int tag) const;
558 
576  template <typename T>
577  inline std::shared_ptr<T> getChildByTag(unsigned int tag) const {
578  return std::dynamic_pointer_cast<T>(getChildByTag(tag));
579  }
580 
593  std::shared_ptr<Node> getChildByName(const std::string& name) const;
594 
612  template <typename T>
613  inline std::shared_ptr<T> getChildByName(const std::string& name) const {
614  return std::dynamic_pointer_cast<T>(getChildByName(name));
615  }
616 
622  std::vector<std::shared_ptr<Node>> getChildren() { return _children; }
623 
629  const std::vector<std::shared_ptr<Node>>& getChildren() const { return _children; }
630 
640  void addChild(std::shared_ptr<Node> child) {
641  addChild(child,child->getZOrder());
642  }
643 
656  virtual void addChild(const std::shared_ptr<Node>& child, int zval);
657 
668  void addChildWithTag(const std::shared_ptr<Node>& child, unsigned int tag) {
669  addChild(child);
670  child->setTag(tag);
671  }
672 
686  void addChildWithTag(const std::shared_ptr<Node>& child, unsigned int tag, int zval) {
687  addChild(child,zval);
688  child->setTag(tag);
689  }
690 
701  void addChildWithName(const std::shared_ptr<Node>& child, const std::string &name) {
702  addChild(child);
703  child->setName(name);
704  }
705 
719  void addChildWithName(const std::shared_ptr<Node>& child, const std::string &name, int zval) {
720  addChild(child,zval);
721  child->setName(name);
722  }
723 
737  void swapChild(const std::shared_ptr<Node>& child1, const std::shared_ptr<Node>& child2, bool inherit=false);
738 
747  virtual void removeChild(unsigned int pos);
748 
759  void removeChild(const std::shared_ptr<Node>& child);
760 
771  void removeChildByTag(unsigned int tag);
772 
783  void removeChildByName(const std::string &name);
784 
788  virtual void removeAllChildren();
789 
790 #pragma mark -
791 #pragma mark Z-order
792 
805  bool isZAutoSort() { return _zSort; }
806 
820  void setZAutoSort(bool value) { _zSort = value; }
821 
839  bool isZDirty() const { return _zDirty; }
840 
858  void sortZOrder();
859 
860 #pragma mark -
861 #pragma mark Scene Logic
862 
867  bool isActive( ) const { return _active; }
868 
874  virtual void setActive(bool value) { _active = value; }
875 
883  virtual void update(float timestep) {}
884 
888  virtual void reset() {}
889 
903  void render(const std::shared_ptr<SpriteBatch>& batch);
904 
905 private:
906 #pragma mark -
907 #pragma mark Internal Helpers
908 
925  void setZDirty(bool value) { _zDirty = value; }
926 
927  // Tightly couple with Node
928  friend class Node;
929 };
930 
931 }
932 #endif /* __CU_SCENE_H__ */
cugl::Scene::setColor
void setColor(Color4 color)
Definition: CUScene.h:325
cugl::Scene::init
virtual bool init(const Size &size)
Definition: CUScene.h:126
cugl::Scene::_active
bool _active
Definition: CUScene.h:89
cugl::Scene::setZAutoSort
void setZAutoSort(bool value)
Definition: CUScene.h:820
cugl::Scene::alloc
static std::shared_ptr< Scene > alloc(float width, float height)
Definition: CUScene.h:220
cugl::Scene::setSize
void setSize(float width, float height)
Definition: CUScene.h:363
cugl::Scene::init
virtual bool init(const Vec2 &origin, const Size &size)
Definition: CUScene.h:173
cugl::Scene::isZAutoSort
bool isZAutoSort()
Definition: CUScene.h:805
cugl::Scene::isActive
bool isActive() const
Definition: CUScene.h:867
cugl::Scene::setWidth
void setWidth(float width)
Definition: CUScene.h:375
cugl::Scene::alloc
static std::shared_ptr< Scene > alloc(const Vec2 &origin, const Size &size)
Definition: CUScene.h:253
cugl::Scene::setBounds
void setBounds(float x, float y, float width, float height)
Definition: CUScene.h:430
cugl::Scene::getChildByTag
std::shared_ptr< T > getChildByTag(unsigned int tag) const
Definition: CUScene.h:577
cugl::Scene::alloc
static std::shared_ptr< Scene > alloc(const Rect &rect)
Definition: CUScene.h:236
cugl::Scene::screenToWorldCoords
Vec3 screenToWorldCoords(const Vec2 &screenCoords) const
Definition: CUScene.h:466
cugl::Size
Definition: CUSize.h:57
cugl::Color4
Definition: CUColor4.h:1084
cugl::Scene::removeChild
virtual void removeChild(unsigned int pos)
cugl::Size::width
float width
Definition: CUSize.h:61
cugl::Scene::getChildren
const std::vector< std::shared_ptr< Node > > & getChildren() const
Definition: CUScene.h:629
cugl::Scene::Scene
Scene()
cugl::Vec2::x
float x
Definition: CUVec2.h:66
cugl::Scene::alloc
static std::shared_ptr< Scene > alloc(const Size &size)
Definition: CUScene.h:205
cugl::Rect
Definition: CURect.h:45
cugl::Scene::getChildByName
std::shared_ptr< T > getChildByName(const std::string &name) const
Definition: CUScene.h:613
cugl::Scene::_dstFactor
GLenum _dstFactor
Definition: CUScene.h:86
cugl::Scene::getCamera
std::shared_ptr< Camera > getCamera()
cugl::Scene::getChild
std::shared_ptr< Node > getChild(unsigned int pos)
cugl::Scene::_name
std::string _name
Definition: CUScene.h:69
cugl::Scene
Definition: CUScene.h:65
cugl::Scene::swapChild
void swapChild(const std::shared_ptr< Node > &child1, const std::shared_ptr< Node > &child2, bool inherit=false)
cugl::Scene::render
void render(const std::shared_ptr< SpriteBatch > &batch)
cugl::Scene::sortZOrder
void sortZOrder()
cugl::Rect::origin
Vec2 origin
Definition: CURect.h:49
cugl::Scene::removeChildByName
void removeChildByName(const std::string &name)
cugl::Scene::setBounds
void setBounds(const Rect &rect)
Definition: CUScene.h:400
cugl::Scene::update
virtual void update(float timestep)
Definition: CUScene.h:883
cugl::Scene::_zDirty
bool _zDirty
Definition: CUScene.h:77
cugl::Scene::isZDirty
bool isZDirty() const
Definition: CUScene.h:839
cugl::Vec2::y
float y
Definition: CUVec2.h:68
cugl::Scene::setBounds
void setBounds(const Vec2 &origin, const Size &size)
Definition: CUScene.h:414
cugl::Scene::reset
virtual void reset()
Definition: CUScene.h:888
cugl::Scene::_zSort
bool _zSort
Definition: CUScene.h:79
cugl::Scene::removeAllChildren
virtual void removeAllChildren()
cugl::Scene::init
virtual bool init(const Rect &rect)
Definition: CUScene.h:156
cugl::Scene::addChild
void addChild(std::shared_ptr< Node > child)
Definition: CUScene.h:640
cugl::Scene::getColor
Color4 getColor()
Definition: CUScene.h:315
cugl::Scene::getChildByName
std::shared_ptr< Node > getChildByName(const std::string &name) const
cugl::Vec2
Definition: CUVec2.h:61
cugl::Scene::addChildWithTag
void addChildWithTag(const std::shared_ptr< Node > &child, unsigned int tag)
Definition: CUScene.h:668
cugl::Scene::setHeight
void setHeight(float height)
Definition: CUScene.h:387
cugl::Scene::_children
std::vector< std::shared_ptr< Node > > _children
Definition: CUScene.h:73
cugl::Scene::toString
virtual std::string toString(bool verbose=false) const
cugl::Scene::_color
Color4 _color
Definition: CUScene.h:75
cugl::Scene::worldToScreenCoords
Vec2 worldToScreenCoords(const Vec3 &worldCoords) const
Definition: CUScene.h:485
cugl::Scene::getChildCount
size_t getChildCount() const
Definition: CUScene.h:496
cugl::Scene::addChildWithName
void addChildWithName(const std::shared_ptr< Node > &child, const std::string &name)
Definition: CUScene.h:701
cugl::Scene::setActive
virtual void setActive(bool value)
Definition: CUScene.h:874
cugl::Scene::setName
void setName(const std::string &name)
Definition: CUScene.h:291
cugl::Scene::dispose
virtual void dispose()
cugl::Scene::addChildWithName
void addChildWithName(const std::shared_ptr< Node > &child, const std::string &name, int zval)
Definition: CUScene.h:719
cugl::Scene::init
bool init(float width, float height)
Definition: CUScene.h:140
cugl::Scene::~Scene
~Scene()
Definition: CUScene.h:107
cugl::Scene::_srcFactor
GLenum _srcFactor
Definition: CUScene.h:84
cugl::Scene::_blendEquation
GLenum _blendEquation
Definition: CUScene.h:82
cugl::Scene::setOffset
void setOffset(const Vec2 &origin)
Definition: CUScene.h:443
cugl::Rect::size
Size size
Definition: CURect.h:51
cugl::Scene::getChildren
std::vector< std::shared_ptr< Node > > getChildren()
Definition: CUScene.h:622
cugl::Scene::_camera
std::shared_ptr< OrthographicCamera > _camera
Definition: CUScene.h:71
cugl::Scene::getChild
std::shared_ptr< T > getChild(unsigned int pos) const
Definition: CUScene.h:541
cugl::Vec3
Definition: CUVec3.h:61
cugl::Scene::removeChildByTag
void removeChildByTag(unsigned int tag)
cugl::Scene::getChildByTag
std::shared_ptr< Node > getChildByTag(unsigned int tag) const
cugl::Size::height
float height
Definition: CUSize.h:63
cugl::Scene::setSize
void setSize(const Size &size)
Definition: CUScene.h:351
cugl::Scene::getName
const std::string & getName() const
Definition: CUScene.h:284
cugl::Scene::addChildWithTag
void addChildWithTag(const std::shared_ptr< Node > &child, unsigned int tag, int zval)
Definition: CUScene.h:686
cugl::Scene::alloc
static std::shared_ptr< Scene > alloc(float x, float y, float width, float height)
Definition: CUScene.h:272