Public Types | Public Member Functions | Protected Member Functions | Friends | List of all members
ime::RenderLayer Class Reference

A layer for drawable entities. More...

#include <RenderLayer.h>

Inheritance diagram for ime::RenderLayer:
ime::Object

Public Types

using Ptr = std::shared_ptr< RenderLayer >
 Shared render layer pointer. More...
 

Public Member Functions

 RenderLayer (const RenderLayer &)=delete
 Copy constructor. More...
 
RenderLayeroperator= (const RenderLayer &)=delete
 Copy assignment operator. More...
 
 RenderLayer (RenderLayer &&) noexcept=default
 Move constructor. More...
 
RenderLayeroperator= (RenderLayer &&) noexcept=default
 Move assignment operator. More...
 
std::string getClassName () const override
 Get the name of this class. More...
 
void setDrawable (bool render)
 Set whether or not the layer should be drawn by the scene. More...
 
bool isDrawable () const
 Check whether or not the layer is drawn by the scene. More...
 
void toggleDrawable ()
 Toggle the draw state of the layer. More...
 
unsigned int getIndex () const
 Get the index of the layer in the RenderLayerContainer. More...
 
void add (const Drawable &drawable, int renderOrder=0)
 Add a drawable to the layer. More...
 
bool has (const Drawable &drawable) const
 Check if the render layer has a given drawable or not. More...
 
bool remove (const Drawable &drawable)
 Remove a drawable from the render layer. More...
 
void removeAll ()
 Remove all drawables from the render layer. More...
 
std::size_t getCount () const
 Get the number of drawables rendered by the layer. More...
 
void render (Window &window) const
 
 ~RenderLayer () override
 Destructor. More...
 
void setTag (const std::string &tag)
 Assign the object an alias. More...
 
const std::string & getTag () const
 Get the alias of the object. More...
 
unsigned int getObjectId () const
 Get the id of the object. More...
 
virtual std::string getClassType () const
 Get the name of the direct parent of an object instance. More...
 
int onPropertyChange (const std::string &property, const Callback< Property > &callback)
 Add an event listener to a specific property change event. More...
 
void onPropertyChange (const Callback< Property > &callback)
 Add an event listener to a property change event. More...
 
int onEvent (const std::string &event, const Callback<> &callback)
 Add an event listener to an event. More...
 
bool unsubscribe (const std::string &event, int id)
 Remove an event listener from an event. More...
 
int onDestruction (const Callback<> &callback)
 Add a destruction listener. More...
 
bool removeDestructionListener (int id)
 Remove a destruction listener form the object. More...
 
bool operator== (const Object &rhs) const
 Check if two objects are the same object or not. More...
 
bool operator!= (const Object &rhs) const
 Check if two objects are not the same object. More...
 

Protected Member Functions

void emitChange (const Property &property)
 Dispatch a property change event. More...
 
void emit (const std::string &event)
 Dispatch an action event. More...
 

Friends

class RenderLayerContainer
 Needs access to constructor. More...
 

Detailed Description

A layer for drawable entities.

Typically you add objects that should be drawn together to the same layer. The order in which render layers are rendered is specified in the RenderLayerContainer. Objects in the same layer can also be sorted. For example, you may have a background layer which has mountains, trees and valleys. However you don't want the objects to be drawn non-deterministically. You can specify which objects are drawn over which and which objects are drawn under which by specifying the render order of the object when adding it to the layer. Objects with the lowest render order are drawn first whilst objects with the highest render order are drawn last. When objects have the same render order, the object that was added first to the layer will be rendered first.

By default, all objects have the same render order, as a result, they are drawn in the order in which they were added to the layer

A layer is not instantiated directly but rather using a RenderLayerContainer

Definition at line 60 of file RenderLayer.h.

Member Typedef Documentation

◆ Ptr

using ime::RenderLayer::Ptr = std::shared_ptr<RenderLayer>

Shared render layer pointer.

Definition at line 62 of file RenderLayer.h.

Constructor & Destructor Documentation

◆ RenderLayer() [1/2]

ime::RenderLayer::RenderLayer ( const RenderLayer )
delete

Copy constructor.

◆ RenderLayer() [2/2]

ime::RenderLayer::RenderLayer ( RenderLayer &&  )
defaultnoexcept

Move constructor.

◆ ~RenderLayer()

ime::RenderLayer::~RenderLayer ( )
override

Destructor.

Member Function Documentation

◆ add()

void ime::RenderLayer::add ( const Drawable drawable,
int  renderOrder = 0 
)

Add a drawable to the layer.

Parameters
drawableThe drawable to be added
renderOrderThe layer level render order of the drawable

The render order determines which game objects get rendered first and which game objects get rendered last in the layer. Therefore the order in which drawables are added to the layer does not necessarily matter. Drawables with the smallest render order are be rendered first. Note that multiple drawables can have the same render order, in such a case the drawables will be drawn in the order in which they were added to the layer

By default, all drawables have the same render order of 0

Warning
Th render layer keeps a reference to the drawable, therefore, it must remain alive for as long as it is used by the render layer

◆ emit()

void ime::Object::emit ( const std::string &  event)
protectedinherited

Dispatch an action event.

Parameters
eventThe name of the event to be dispatched

This function will invoke all event listeners of the specified event. The function should be used for events that represent an action, rather than those that represent a property change (Use emitChange for that)

See also
emitChange

◆ emitChange()

void ime::Object::emitChange ( const Property property)
protectedinherited

Dispatch a property change event.

Parameters
propertyThe property that changed

This function will invoke all the event listeners of the specified property

See also
emit

◆ getClassName()

std::string ime::RenderLayer::getClassName ( ) const
overridevirtual

Get the name of this class.

Returns
The name of this class

Implements ime::Object.

◆ getClassType()

virtual std::string ime::Object::getClassType ( ) const
virtualinherited

Get the name of the direct parent of an object instance.

Returns
The name of the direct parent of an object instance

In contrast to getClassName which returns the name of the concrete class, this function returns the name of the concrete class's base class. This function is implemented by all derived classes of Object which also serve as base classes for other Objects. For classes whose direct parent is this class, this function will return the name of this class

auto rectangle = ime::RectangleShape(); // RectangleShape is derived from Shape
std::cout << rectangle->getClassName(); // Prints "RectangleShape"
std::cout << rectangle->getClassType(); // Prints "Shape"
See also
getClassName

Reimplemented in ime::SpriteImage, ime::Shape, ime::Drawable, ime::GridMover, ime::Joint, ime::Collider, and ime::GameObject.

◆ getCount()

std::size_t ime::RenderLayer::getCount ( ) const

Get the number of drawables rendered by the layer.

Returns
The number of drawables rendered by the layer

◆ getIndex()

unsigned int ime::RenderLayer::getIndex ( ) const

Get the index of the layer in the RenderLayerContainer.

Returns
The index of the layer in the RenderLayerContainer

The index indicates the render order of the layer. The bigger the index the further away the layer is from the foreground. The layer with the an index of 0 is always the foreground layer and all other layers are rendered behind it

◆ getObjectId()

unsigned int ime::Object::getObjectId ( ) const
inherited

Get the id of the object.

Returns
The id of the object

Each object has a unique id

◆ getTag()

const std::string& ime::Object::getTag ( ) const
inherited

Get the alias of the object.

Returns
The alias of the object
See also
setTag and getObjectId

◆ has()

bool ime::RenderLayer::has ( const Drawable drawable) const

Check if the render layer has a given drawable or not.

Parameters
drawableThe drawable to be checked
Returns
True if the render layer has the drawable, otherwise false

When a render layer "has" a drawable, it means that the drawable is rendered by that layer to the render window

◆ isDrawable()

bool ime::RenderLayer::isDrawable ( ) const

Check whether or not the layer is drawn by the scene.

Returns
True if drawn by the scene, otherwise false
See also
setDrawable

◆ onDestruction()

int ime::Object::onDestruction ( const Callback<> &  callback)
inherited

Add a destruction listener.

Parameters
callbackFunction to be executed when the object is destroyed
Returns
The id of the destruction listener

Note that an object may have more than one destruction listeners, however, you have to keep the returned id if you may want to remove the callback at a later time

Warning
The callback is called when the object is destroyed. Do not try to access the object in the callback. Doing so is undefined behavior
See also
removeDestructionListener

◆ onEvent()

int ime::Object::onEvent ( const std::string &  event,
const Callback<> &  callback 
)
inherited

Add an event listener to an event.

Parameters
eventThe name of the event to add an an event listener to
callbackThe function to be executed when the event takes place
Returns
The unique identification number of the event listener

Unlike onPropertyChange, this function registers event listeners to events that occur when something happens to the object, or when the object does something (action events). Usually the name of the event/action is the name of the function:

// Add event listeners to the object
object.onEvent("attachRigidBody", [] {
std::cout << "Rigid body attached to object << std::endl;
});
object.onEvent("removeRigidBody", [] {
std::cout << "Rigid body removed from object << std::endl;
});
// Invokes event listener(s)
object.attachRigidBody(body);
object.removeRigidBody();
See also
onPropertyChange and unsubscribe

◆ onPropertyChange() [1/2]

void ime::Object::onPropertyChange ( const Callback< Property > &  callback)
inherited

Add an event listener to a property change event.

Parameters
callbackThe function to be executed when the property changes
Returns
The unique id of the event listener

Note that only one callback function may be registered with this function. This means that adding a new event listener overwrites the previous event listener. To remove the callback, pass a nullptr as an argument. The function may be useful if you want to write the logic for property changes in one function.

See also
onPropertyChange(std::string, Callback)

◆ onPropertyChange() [2/2]

int ime::Object::onPropertyChange ( const std::string &  property,
const Callback< Property > &  callback 
)
inherited

Add an event listener to a specific property change event.

Parameters
propertyThe name of the property to listen for
callbackThe function to be executed when the property changes
Returns
The unique id of the event listener

A property change event is triggered by any function that begins with set, where the the text after set is the name of the property. For example, for the setTag function, the property that the function modifies is Tag.

Note that when adding a property change event listener, the name of the property must be in lowercase:

// Prints the tag of the player object to the console everytime it changes
player.onPropertyChange("tag", [](const Property& property) {
cout << "New tag: " << property.getValue<std::string>() << endl;
});
...
//Sets tag = "player1" and invokes event listener(s)
player.setTag("player1");

Unlike onPropertyChange(const Callback&) you can add multiple event listeners to the same property using this function. However you must store the unique id of the event listener if you wish to remove it at a later time

See also
unsubscribe and onPropertyChange(Callback)

◆ operator!=()

bool ime::Object::operator!= ( const Object rhs) const
inherited

Check if two objects are not the same object.

Parameters
rhsObject to compare against this object
Returns
True if rhs is NOT the same object as this object, otherwise false

Two objects are different from each other if they have different object id's

See also
getObjectId and operator==

◆ operator=() [1/2]

RenderLayer& ime::RenderLayer::operator= ( const RenderLayer )
delete

Copy assignment operator.

◆ operator=() [2/2]

RenderLayer& ime::RenderLayer::operator= ( RenderLayer &&  )
defaultnoexcept

Move assignment operator.

◆ operator==()

bool ime::Object::operator== ( const Object rhs) const
inherited

Check if two objects are the same object or not.

Parameters
rhsObject to compare against this object
Returns
True if rhs is the same object as this object, otherwise false

Two objects are the same object if they have the same object id. Recall that each object instance has a unique id

See also
getObjectId and operator!=

◆ remove()

bool ime::RenderLayer::remove ( const Drawable drawable)

Remove a drawable from the render layer.

Parameters
drawableThe drawable to be removed
Returns
True if the drawable was removed, otherwise false

When a drawable is removed from the render layer, it will no longer be rendered by the scene, hence it won't appear on the game window

◆ removeAll()

void ime::RenderLayer::removeAll ( )

Remove all drawables from the render layer.

◆ removeDestructionListener()

bool ime::Object::removeDestructionListener ( int  id)
inherited

Remove a destruction listener form the object.

Parameters
Theid of the destruction listener to be removed
Returns
True if the destruction listener was removed or false if the listener with the given id does not exist

◆ setDrawable()

void ime::RenderLayer::setDrawable ( bool  render)

Set whether or not the layer should be drawn by the scene.

Parameters
renderTrue to have the layer drawn by the scene otherwise false

By default the layer is drawable

◆ setTag()

void ime::Object::setTag ( const std::string &  tag)
inherited

Assign the object an alias.

Parameters
nameThe alias of the object

This function is useful if you want to refer to the object by tag instead of its id. Unlike an object id, multiple objects may have the same tag.

By default, the tag is an empty string

◆ toggleDrawable()

void ime::RenderLayer::toggleDrawable ( )

Toggle the draw state of the layer.

This function will set the layer to drawable if its currently not drawable and vice versa

See also
setDrawable

◆ unsubscribe()

bool ime::Object::unsubscribe ( const std::string &  event,
int  id 
)
inherited

Remove an event listener from an event.

Parameters
eventThe name of the event to remove event listener from
idThe unique id of the event listener to be removed
Returns
True if the event listener was removed or false if the event or the event listener is does not exist
// Display the name of the object to console every time it changes
auto nameChangeId = object.onPropertyChange("name", [](Property name) {
std::cout << name.getValue<std::string>() << std::endl;
});
// Stop displaying the name of the object when it changes
object.unsubscribe("name", id);

Friends And Related Function Documentation

◆ RenderLayerContainer

friend class RenderLayerContainer
friend

Needs access to constructor.

Definition at line 215 of file RenderLayer.h.


The documentation for this class was generated from the following file:
ime::RectangleShape
A 2D shape having four sides and four corners (90 degree angles)
Definition: RectangleShape.h:36