![]() |
CUGL 4.0
Cornell University Game Library
|
#include <CUScene2.h>
Public Member Functions | |
| Scene2 () | |
| ~Scene2 () | |
| virtual void | dispose () override |
| virtual bool | init () override |
| bool | initWithViewport (const Size &size) |
| bool | initWithViewport (float width, float height) |
| bool | initWithViewport (const Rect &rect) |
| bool | initWithViewport (const Vec2 &origin, const Size &size) |
| bool | initWithViewport (float x, float y, float width, float height) |
| bool | initWithHint (const Size &hint) |
| bool | initWithHint (float width, float height) |
| const Rect & | getSafeBounds () const |
| Color4 | getColor () const |
| void | setColor (const Color4 color) |
| virtual std::string | toString (bool verbose=false) const override |
| size_t | getChildCount () const |
| std::shared_ptr< scene2::SceneNode > & | getChild (unsigned int pos) |
| const std::shared_ptr< scene2::SceneNode > & | getChild (unsigned int pos) const |
| template<typename T > | |
| std::shared_ptr< T > | getChild (unsigned int pos) const |
| std::shared_ptr< scene2::SceneNode > | getChildByTag (unsigned int tag) const |
| template<typename T > | |
| std::shared_ptr< T > | getChildByTag (unsigned int tag) const |
| std::shared_ptr< scene2::SceneNode > | getChildByName (const std::string name) const |
| template<typename T > | |
| std::shared_ptr< T > | getChildByName (const std::string name) const |
| std::vector< std::shared_ptr< scene2::SceneNode > > & | getChildren () |
| const std::vector< std::shared_ptr< scene2::SceneNode > > & | getChildren () const |
| virtual void | addChild (const std::shared_ptr< scene2::SceneNode > &child) |
| void | addChildWithTag (const std::shared_ptr< scene2::SceneNode > &child, unsigned int tag) |
| void | addChildWithName (const std::shared_ptr< scene2::SceneNode > &child, const std::string name) |
| void | swapChild (const std::shared_ptr< scene2::SceneNode > &child1, const std::shared_ptr< scene2::SceneNode > &child2, bool inherit=false) |
| virtual void | removeChild (unsigned int pos) |
| void | removeChild (const std::shared_ptr< scene2::SceneNode > &child) |
| void | removeChildByTag (unsigned int tag) |
| void | removeChildByName (const std::string name) |
| virtual void | removeAllChildren () |
| void | resizeViewport (const Size &size) |
| void | resizeViewport (float width, float height) |
| void | resizeToHint (const Size &hint) |
| void | resizeToHint (float width, float height) |
| std::shared_ptr< graphics::SpriteBatch > | getSpriteBatch () const |
| void | setSpriteBatch (const std::shared_ptr< graphics::SpriteBatch > &batch) |
| const std::shared_ptr< graphics::FrameBuffer > & | getFrameBuffer () const |
| void | setFrameBuffer (const std::shared_ptr< graphics::FrameBuffer > &buffer) |
| virtual void | render () override |
Public Member Functions inherited from cugl::Scene | |
| Scene () | |
| ~Scene () | |
| virtual void | dispose () |
| virtual bool | init () |
| const std::string & | getName () const |
| void | setName (const std::string name) |
| std::shared_ptr< Camera > & | getCamera () |
| const std::shared_ptr< Camera > & | getCamera () const |
| virtual std::string | toString (bool verbose=false) const |
| operator std::string () const | |
| const Size & | getSize () const |
| const Rect | getBounds () const |
| Vec3 | screenToWorldCoords (const Vec2 &screenCoords) const |
| Vec2 | worldToScreenCoords (const Vec3 &worldCoords) const |
| bool | isActive () const |
| virtual void | setActive (bool value) |
| virtual void | update (float dt) |
| virtual void | reset () |
| virtual void | render () |
Static Public Member Functions | |
| static std::shared_ptr< Scene2 > | alloc () |
| static std::shared_ptr< Scene2 > | allocWithViewport (const Size &size) |
| static std::shared_ptr< Scene2 > | allocWithViewport (float width, float height) |
| static std::shared_ptr< Scene2 > | allocWithViewport (const Rect &rect) |
| static std::shared_ptr< Scene2 > | allocWithViewport (const Vec2 &origin, const Size &size) |
| static std::shared_ptr< Scene2 > | allocWithViewport (float x, float y, float width, float height) |
| static std::shared_ptr< Scene2 > | allocWithHint (const Size &hint) |
| static std::shared_ptr< Scene2 > | allocWithHint (float width, float height) |
Protected Attributes | |
| std::shared_ptr< graphics::SpriteBatch > | _batch |
| std::vector< std::shared_ptr< scene2::SceneNode > > | _children |
| Color4 | _color |
| Rect | _safearea |
| std::shared_ptr< graphics::FrameBuffer > | _framebuffer |
Protected Attributes inherited from cugl::Scene | |
| std::shared_ptr< Camera > | _camera |
| std::string | _name |
| Size | _size |
| bool | _active |
Friends | |
| class | SceneNode |
This class provides the root node of a two-dimensional scene graph.
The Scene2 class is very similar to scene2::SceneNode and shares many methods in common. The major differences are that it has no parent and it has no position (so it cannot be transformed). Instead, the Scene2 is defined by an attached OrthographicCamera.
Rendering happens by traversing the the scene graph using an "Pre-Order" tree traversal algorithm ( https://en.wikipedia.org/wiki/Tree_traversal#Pre-order ). That means that parents are always draw before (and behind children). The children of each sub tree are ordered sequentially.
Scenes do support optional z-ordering. This is not a true depth value, as depth filtering is incompatible with alpha compositing. However, it does provide a way to dynamically reorder how siblings are composed.
| cugl::scene2::Scene2::Scene2 | ( | ) |
Creates a new degenerate Scene2 on the stack.
The scene has no camera and must be initialized.
NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an object on the heap, use one of the static constructors instead.
|
inline |
Deletes this scene, disposing all resources
|
virtual |
Adds a child to this scene.
Children are not necessarily enumerated in the order that they are added. Hence you should generally attempt to retrieve a child by tag or by name instead.
| child | A child node. |
|
inline |
Adds a child to this scene with the given name.
Children are not necessarily enumerated in the order that they are added. Hence you should generally attempt to retrieve a child by tag or by name instead.
| child | A child node. |
| name | A string to identify the node. |
|
inline |
Adds a child to this scene with the given tag.
Children are not necessarily enumerated in the order that they are added. Hence you should generally attempt to retrieve a child by tag or by name instead.
| child | A child node. |
| tag | An integer to identify the node easily. |
|
inlinestatic |
|
inlinestatic |
Returns a newly allocated Scene with the given size hint.
2D Scenes are designed to fill the entire screen. However, the size of that screen can vary from device to device. To make scene design easier, designs are typically locked to a dimension: width or height.
This is the purpose of the size hint. If either of the values of hint are non-zero, then the scene will lock that dimension to that particular size. If both are non-zero, it will choose its dimension according to the device orientation. Landscape will be height, while portrait will pick width. Devices with no orientation will always priortize height over width.
If this initializer is used, the scene will fill the screen and the viewport will match. The scene will not have a sprite batch. This must be assigned separately with setSpriteBatch.
| hint | The size hint |
|
inlinestatic |
Returns a newly allocated Scene with the given size hint.
2D Scenes are designed to fill the entire screen. However, the size of that screen can vary from device to device. To make scene design easier, designs are typically locked to a dimension: width or height.
This is the purpose of the size hint. If either of the values of hint are non-zero, then the scene will lock that dimension to that particular size. If both are non-zero, it will choose its dimension according to the device orientation. Landscape will be height, while portrait will pick width. Devices with no orientation will always priortize height over width.
If this initializer is used, the scene will fill the screen and the viewport will match. The scene will not have a sprite batch. This must be assigned separately with setSpriteBatch.
| width | The width size hint |
| height | The height size hint |
|
inlinestatic |
Returns a newly allocated scene with the given viewport.
The scene will have an orthographic camera whose viewport matches the given value. If this does not align with the aspect ratio of the display this can be distortionary. Scenes are not meant to be smaller than the the size of the display. For those applications, you want a SceneNode.
The viewport and camera will have its origin in the bottom left corner of the display. The viewport origin affects the coordinate conversion methods Camera#project() and Camera#unproject() which are used to convert from the scene graph to a display. It is intended to represent the offset of the viewport in a larger canvas.
The scene will not have a sprite batch. This must be assigned separately with setSpriteBatch.
| rect | The viewport bounding box |
|
inlinestatic |
Returns a newly allocated scene with the given viewport.
The scene will have an orthographic camera whose viewport matches the given value. If this does not align with the aspect ratio of the display this can be distortionary. Scenes are not meant to be smaller than the the size of the display. For those applications, you want a SceneNode.
The viewport and camera will have its origin in the bottom left corner of the display. The scene will not have a sprite batch. This must be assigned separately with setSpriteBatch.
| size | The viewport size |
|
inlinestatic |
Returns a newly allocated scene with the given viewport.
The scene will have an orthographic camera whose viewport matches the given value. If this does not align with the aspect ratio of the display this can be distortionary. Scenes are not meant to be smaller than the the size of the display. For those applications, you want a SceneNode.
The viewport and camera will have its origin in the bottom left corner of the display. The viewport origin affects the coordinate conversion methods Camera#project() and Camera#unproject() which are used to convert from the scene graph to a display. It is intended to represent the offset of the viewport in a larger canvas.
The scene will not have a sprite batch. This must be assigned separately with setSpriteBatch.
| origin | The viewport offset |
| size | The viewport size |
|
inlinestatic |
Returns a newly allocated scene with the given viewport.
The scene will have an orthographic camera whose viewport matches the given value. If this does not align with the aspect ratio of the display this can be distortionary. Scenes are not meant to be smaller than the the size of the display. For those applications, you want a SceneNode.
The viewport and camera will have its origin in the bottom left corner of the display. The scene will not have a sprite batch. This must be assigned separately with setSpriteBatch.
| width | The viewport width |
| height | The viewport height |
|
inlinestatic |
Returns a newly allocated scene with the given viewport.
The scene will have an orthographic camera whose viewport matches the given value. If this does not align with the aspect ratio of the display this can be distortionary. Scenes are not meant to be smaller than the the size of the display. For those applications, you want a SceneNode.
The viewport and camera will have its origin in the bottom left corner of the display. The viewport origin affects the coordinate conversion methods Camera#project() and Camera#unproject() which are used to convert from the scene graph to a display. It is intended to represent the offset of the viewport in a larger canvas.
The scene will not have a sprite batch. This must be assigned separately with setSpriteBatch.
| x | The viewport x offset |
| y | The viewport y offset |
| width | The viewport width |
| height | The viewport height |
|
overridevirtual |
Disposes all of the resources used by this scene.
A disposed Scene2 can be safely reinitialized. Any children owned by this scene will be released. They will be deleted if no other object owns them.
Reimplemented from cugl::Scene.
Reimplemented in cugl::scene2::LoadingScene.
| std::shared_ptr< scene2::SceneNode > & cugl::scene2::Scene2::getChild | ( | unsigned int | pos | ) |
Returns the child at the given position.
Children are not necessarily enumerated in the order that they are added. For example, they may be resorted by their z-order. Hence you should generally attempt to retrieve a child by tag or by name instead.
| pos | The child position. |
| const std::shared_ptr< scene2::SceneNode > & cugl::scene2::Scene2::getChild | ( | unsigned int | pos | ) | const |
Returns the child at the given position.
Children are not necessarily enumerated in the order that they are added. Hence you should generally attempt to retrieve a child by tag or by name instead.
| pos | The child position. |
|
inline |
Returns the child at the given position, typecast to a shared T pointer.
This method is provided to simplify the polymorphism of a scene graph. While all children are a subclass of type Node, you may want to access them by their specific subclass. If the child is not an instance of type T (or a subclass), this method returns nullptr.
Children are not necessarily enumerated in the order that they are added. Hence you should generally attempt to retrieve a child by tag or by name instead.
| pos | The child position. |
| std::shared_ptr< scene2::SceneNode > cugl::scene2::Scene2::getChildByName | ( | const std::string | name | ) | const |
Returns the (first) child with the given name.
If there is more than one child of the given name, it returns the first one that is found. Children are not necessarily enumerated in the order that they are added. Hence it is very important that names be unique.
| name | An identifier to find the child node. |
|
inline |
Returns the (first) child with the given name, typecast to a shared T pointer.
This method is provided to simplify the polymorphism of a scene graph. While all children are a subclass of type Node, you may want to access them by their specific subclass. If the child is not an instance of type T (or a subclass), this method returns nullptr.
If there is more than one child of the given name, it returns the first one that is found. Children are not necessarily enumerated in the order that they are added. Hence it is very important that names be unique.
| name | An identifier to find the child node. |
| std::shared_ptr< scene2::SceneNode > cugl::scene2::Scene2::getChildByTag | ( | unsigned int | tag | ) | const |
Returns the (first) child with the given tag.
If there is more than one child of the given tag, it returns the first one that is found. Children are not necessarily enumerated in the order that they are added. Hence it is very important that tags be unique.
| tag | An identifier to find the child node. |
|
inline |
Returns the (first) child with the given tag, typecast to a shared T pointer.
This method is provided to simplify the polymorphism of a scene graph. While all children are a subclass of type Node, you may want to access them by their specific subclass. If the child is not an instance of type T (or a subclass), this method returns nullptr.
If there is more than one child of the given tag, it returns the first one that is found. Children are not necessarily enumerated in the order that they are added. Hence it is very important that tags be unique.
| tag | An identifier to find the child node. |
|
inline |
Returns the number of immediate children of this scene.
|
inline |
Returns the list of the scene's immediate children.
|
inline |
Returns the list of the scene's immediate children.
|
inline |
Returns the tint color for this scene.
During the render phase, this color will be applied to any child for which hasRelativeColor() is true.
|
inline |
Returns the offscreen framebuffer associated with this scene.
If this value is nullptr (the default), then this scene will be rendered to the primary display. Otherwise, it will be rendered to the given framebuffer. The framebuffer must be compatible with a sprite batch. However, we do not require that the framebuffer be the same size as the display.
|
inline |
Returns the safe area of this Scene.
This value is analogous to Application#getSafeBounds, except that it is scaled to match the camera. Note that the position of the camera does not affect this value.
|
inline |
Returns the sprite batch for rendering this scene.
Scene2 objects are rendered with a sprite batch by default. In particular the method render traverses the scene graph in a pre-order traversal, calling SceneNode#render on each node (though this behavior can be overridden).
As sprite batches are fairly heavy-weight pipelines, we do not construct a sprite batch for each scene. Instead a sprite batch has to be assigned to the scene. If no sprite batch is assigned, nothing is drawn.
|
overridevirtual |
Initializes a Scene to fill the display.
This scene will have a viewport that matches the display and an orthographic camera of the same size. The camera will have its origin in the bottom left corner of the display.
Reimplemented from cugl::Scene.
|
inline |
Initializes a Scene with the given size hint.
2D Scenes are designed to fill the entire screen. However, the size of that screen can vary from device to device. To make scene design easier, designs are typically locked to a dimension: width or height.
This is the purpose of the size hint. If either of the values of hint are non-zero, then the scene will lock that dimension to that particular size. If both are non-zero, it will choose its dimension according to the device orientation. Landscape will be height, while portrait will pick width. Devices with no orientation will always priortize height over width.
If this initializer is used, the scene will fill the screen and the viewport will match. The scene will not have a sprite batch. This must be assigned separately with setSpriteBatch.
| hint | The size hint |
| bool cugl::scene2::Scene2::initWithHint | ( | float | width, |
| float | height | ||
| ) |
Initializes a Scene with the given size hint.
2D Scenes are designed to fill the entire screen. However, the size of that screen can vary from device to device. To make scene design easier, designs are typically locked to a dimension: width or height.
This is the purpose of the size hint. If either of the values of hint are non-zero, then the scene will lock that dimension to that particular size. If both are non-zero, it will choose its dimension according to the device orientation. Landscape will be height, while portrait will pick width. Devices with no orientation will always priortize height over width.
If this initializer is used, the scene will fill the screen and the viewport will match. The scene will not have a sprite batch. This must be assigned separately with setSpriteBatch.
| width | The width size hint |
| height | The height size hint |
|
inline |
Initializes a scene with the given viewport.
The scene will have an orthographic camera whose viewport matches the given value. If this does not align with the aspect ratio of the display this can be distortionary. Scenes are not meant to be smaller than the the size of the display. For those applications, you want a SceneNode.
The viewport and camera will have its origin in the bottom left corner of the display. The viewport origin affects the coordinate conversion methods Camera#project() and Camera#unproject() which are used to convert from the scene graph to a display. It is intended to represent the offset of the viewport in a larger canvas.
The scene will not have a sprite batch. This must be assigned separately with setSpriteBatch.
| rect | The viewport bounding box |
|
inline |
Initializes a scene with the given viewport.
The scene will have an orthographic camera whose viewport matches the given value. If this does not align with the aspect ratio of the display this can be distortionary. Scenes are not meant to be smaller than the the size of the display. For those applications, you want a SceneNode.
The viewport and camera will have its origin in the bottom left corner of the display. The scene will not have a sprite batch. This must be assigned separately with setSpriteBatch.
| size | The viewport size |
Initializes a scene with the given viewport.
The scene will have an orthographic camera whose viewport matches the given value. If this does not align with the aspect ratio of the display this can be distortionary. Scenes are not meant to be smaller than the the size of the display. For those applications, you want a SceneNode.
The viewport and camera will have its origin in the bottom left corner of the display. The viewport origin affects the coordinate conversion methods Camera#project() and Camera#unproject() which are used to convert from the scene graph to a display. It is intended to represent the offset of the viewport in a larger canvas.
The scene will not have a sprite batch. This must be assigned separately with setSpriteBatch.
| origin | The viewport offset |
| size | The viewport size |
|
inline |
Initializes a scene with the given viewport.
The scene will have an orthographic camera whose viewport matches the given value. If this does not align with the aspect ratio of the display this can be distortionary. Scenes are not meant to be smaller than the the size of the display. For those applications, you want a SceneNode.
The viewport and camera will have its origin in the bottom left corner of the display. The scene will not have a sprite batch. This must be assigned separately with setSpriteBatch.
| width | The viewport width |
| height | The viewport height |
| bool cugl::scene2::Scene2::initWithViewport | ( | float | x, |
| float | y, | ||
| float | width, | ||
| float | height | ||
| ) |
Initializes a scene with the given viewport.
The scene will have an orthographic camera whose viewport matches the given value. If this does not align with the aspect ratio of the display this can be distortionary. Scenes are not meant to be smaller than the the size of the display. For those applications, you want a SceneNode.
The viewport and camera will have its origin in the bottom left corner of the display. The viewport origin affects the coordinate conversion methods Camera#project() and Camera#unproject() which are used to convert from the scene graph to a display. It is intended to represent the offset of the viewport in a larger canvas.
The scene will not have a sprite batch. This must be assigned separately with setSpriteBatch.
| x | The viewport x offset |
| y | The viewport y offset |
| width | The viewport width |
| height | The viewport height |
|
virtual |
Removes all children from this Node.
| void cugl::scene2::Scene2::removeChild | ( | const std::shared_ptr< scene2::SceneNode > & | child | ) |
Removes a child from this Scene.
Removing a child alters the position of every child after it. Hence it is unsafe to cache child positions.
If the child is not in this node, nothing happens.
| child | The child node which will be removed. |
|
virtual |
Removes the child at the given position from this Scene.
Removing a child alters the position of every child after it. Hence it is unsafe to cache child positions.
| pos | The position of the child node which will be removed. |
| void cugl::scene2::Scene2::removeChildByName | ( | const std::string | name | ) |
Removes a child from the Scene by name.
If there is more than one child of the given name, it removes the first one that is found. Children are not necessarily enumerated in the order that they are added. Hence it is very important that names be unique.
| name | A string to identify the node. |
| void cugl::scene2::Scene2::removeChildByTag | ( | unsigned int | tag | ) |
Removes a child from the Scene by tag value.
If there is more than one child of the given tag, it removes the first one that is found. Children are not necessarily enumerated in the order that they are added. Hence it is very important that tags be unique.
| tag | An integer to identify the node easily. |
|
overridevirtual |
Draws all of the children in this scene with the given SpriteBatch.
This method with draw using getSpriteBatch. If not sprite batch has been assigned, nothing will be drawn.
Rendering happens by traversing the the scene graph using an "Pre-Order" tree traversal algorithm ( https://en.wikipedia.org/wiki/Tree_traversal#Pre-order ). That means that parents are always draw before (and behind children). To override this draw order, you should place an OrderedNode in the scene graph to specify an alternative order.
Reimplemented from cugl::Scene.
|
inline |
Resizes the viewport according to the given hint.
This will resize the camera viewport for this scene according to the given hint. If either of the values of hint are non-zero, then the scene will lock that dimension to that particular size. If both are non-zero, it will choose its dimension according to the device orientation. Landscape will be height, while portrait will pick width. Devices with no orientation will always priortize height over width.
Once the scene is resized, this method will also resize the content size of any immediate child that is (1) unscaled and (2) matches the size of the display or the safe area. Finally, it will relayout those scene nodes, potentially causing UI elements to move about the screen.
| hint | The size hint |
| void cugl::scene2::Scene2::resizeToHint | ( | float | width, |
| float | height | ||
| ) |
Resizes the viewport according to the given hint.
This will resize the camera viewport for this scene according to the given hint. If either of the values of hint are non-zero, then the scene will lock that dimension to that particular size. If both are non-zero, it will choose its dimension according to the device orientation. Landscape will be height, while portrait will pick width. Devices with no orientation will always priortize height over width.
Once the scene is resized, this method will also resize the content size of any immediate child that is (1) unscaled and (2) matches the size of the display or the safe area. Finally, it will relayout those scene nodes, potentially causing UI elements to move about the screen.
| width | The width hint |
| height | The height hint |
|
inline |
Resizes the viewport and adjusts all the children to match
This will resize the camera viewport for this scene to match the given size. It will also resize the content size of any immediate child that is (1) unscaled and (2) matches the size of the display or the safe area. Finally, it will relayout those scene nodes, potentially causing UI elements to move about the screen.
| size | The new viewport size |
| void cugl::scene2::Scene2::resizeViewport | ( | float | width, |
| float | height | ||
| ) |
Resizes the viewport and adjusts all the children to match
This will resize the camera viewport for this scene to match the given size. It will also resize the content size of any immediate child that is (1) unscaled and (2) matches the size of the display or the safe area. Finally, it will relayout those scene nodes, potentially causing UI elements to move about the screen.
| width | The new viewport width |
| height | The new viewport height |
|
inline |
Sets the tint color for this scene.
During the render phase, this color will be applied to any child for which hasRelativeColor() is true.
| color | The tint color for this scene. |
|
inline |
Sets the offscreen framebuffer associated with this scene.
If this value is nullptr (the default), then this scene will be rendered to the primary display. Otherwise, it will be rendered to the given framebuffer. The framebuffer must be compatible with a sprite batch. However, we do not require that the framebuffer be the same size as the display.
| buffer | The offscreen framebuffer for this scene (or nullptr) |
|
inline |
Sets the sprite batch for rendering this scene.
Scene2 objects are rendered with a sprite batch by default. In particular the method render traverses the scene graph in a pre-order traversal, calling SceneNode#render on each node (though this behavior can be overridden).
As sprite batches are fairly heavy-weight pipelines, we do not construct a sprite batch for each scene. Instead a sprite batch has to be assigned to the scene. If no sprite batch is assigned, nothing is drawn.
| batch | The sprite batch for rendering this scene. |
| void cugl::scene2::Scene2::swapChild | ( | const std::shared_ptr< scene2::SceneNode > & | child1, |
| const std::shared_ptr< scene2::SceneNode > & | child2, | ||
| bool | inherit = false |
||
| ) |
Swaps the current child child1 with the new child child2.
If inherit is true, the children of child1 are assigned to child2 after the swap; this value is false by default. The purpose of this value is to allow transitions in the scene graph.
This method is undefined if child1 is not a child of this scene.
| child1 | The current child of this node |
| child2 | The child to swap it with. |
| inherit | Whether the new child should inherit the children of child1. |
|
overridevirtual |
Returns a string representation of this scene for debugging purposes.
If verbose is true, the string will include class information. This allows us to unambiguously identify the class.
| verbose | Whether to include class information |
Reimplemented from cugl::Scene.
|
protected |
The sprite batch for rendering this scene
|
protected |
The array of internal nodes
|
protected |
The default tint for this scene
|
protected |
An (optional) offscreen buffer for rendering the scene.
|
protected |
The safe bounds of this scene