43 #ifndef __CU_SCENE_H__
44 #define __CU_SCENE_H__
46 #include <cugl/math/cu_math.h>
47 #include <cugl/2d/CUNode.h>
48 #include <cugl/renderer/CUOrthographicCamera.h>
71 std::shared_ptr<OrthographicCamera>
_camera;
92 #pragma mark Constructors
140 bool init(
float width,
float height) {
141 return init(0,0,width,height);
192 bool init(
float x,
float y,
float width,
float height);
195 #pragma mark Static Constructors
206 std::shared_ptr<Scene> result = std::make_shared<Scene>();
207 return (result->init(size) ? result :
nullptr);
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);
237 std::shared_ptr<Scene> result = std::make_shared<Scene>();
238 return (result->init(rect) ? result :
nullptr);
254 std::shared_ptr<Scene> result = std::make_shared<Scene>();
255 return (result->init(origin,size) ? result :
nullptr);
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);
278 #pragma mark Attributes
291 void setName(
const std::string& name) { _name = name; }
305 const std::shared_ptr<Camera>
getCamera()
const;
337 virtual std::string
toString(
bool verbose =
false)
const;
340 operator std::string()
const {
return toString(); }
343 #pragma mark View Size
364 _camera->set(width,height);
376 _camera->set(width,_camera->getViewport().size.height);
388 _camera->set(_camera->getViewport().size.width,height);
416 _camera->set(origin,size);
431 void setBounds(
float x,
float y,
float width,
float height) {
432 _camera->set(x,y,width,height);
445 _camera->set(origin,_camera->getViewport().size);
468 return _camera->screenToWorldCoords(screenCoords);
487 return _camera->worldToScreenCoords(worldCoords);
491 #pragma mark Scene Graph
510 std::shared_ptr<Node>
getChild(
unsigned int pos);
523 const std::shared_ptr<Node>&
getChild(
unsigned int pos)
const;
541 template <
typename T>
542 inline std::shared_ptr<T>
getChild(
unsigned int pos)
const {
543 return std::dynamic_pointer_cast<T>(
getChild(pos));
577 template <
typename T>
594 std::shared_ptr<Node>
getChildByName(
const std::string& name)
const;
613 template <
typename T>
657 virtual void addChild(
const std::shared_ptr<Node>& child,
int zval);
687 void addChildWithTag(
const std::shared_ptr<Node>& child,
unsigned int tag,
int zval) {
704 child->setName(name);
720 void addChildWithName(
const std::shared_ptr<Node>& child,
const std::string &name,
int zval) {
722 child->setName(name);
738 void swapChild(
const std::shared_ptr<Node>& child1,
const std::shared_ptr<Node>& child2,
bool inherit=
false);
760 void removeChild(
const std::shared_ptr<Node>& child);
862 #pragma mark Scene Logic
904 void render(
const std::shared_ptr<SpriteBatch>& batch);
908 #pragma mark Internal Helpers
926 void setZDirty(
bool value) { _zDirty = value; }
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
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
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()
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)
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
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
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