Gamedev Framework (gf)  0.4.0
A C++11 framework for 2D games
Public Member Functions | List of all members
gf::EntityContainer Class Reference

A collection of entities. More...

#include <gf/EntityContainer.h>

Public Member Functions

void update (float dt)
 Update the entities. More...
 
void render (RenderTarget &target)
 Render the entities on the target. More...
 
Entities management
void addEntity (Entity &entity)
 Add an entity to the collection. More...
 
EntityremoveEntity (Entity *entity)
 Remove an entity from the collection. More...
 
template<typename E >
EremoveTypedEntity (E *entity)
 Remove a typed entity from the collection. More...
 

Detailed Description

A collection of entities.

gf::EntityContainer represents a collection of entities that are updated and rendered automatically. The entity manager takes care of the liveness of the entities and remove the dead entities from the collection.

The entity manager is not responsible for the memory of the entities. The entities must be allocated by the user and not deleted while they are handled by the entity manager.

Generally, you only need one entity manager in your game. You create it at the beginning of the game and put all your entities in it. Then you can call gf::EntityContainer::update() and gf::EntityContainer::render() in your game loop.

See also
gf::Entity

Member Function Documentation

void gf::EntityContainer::addEntity ( Entity entity)

Add an entity to the collection.

The entity must not be deleted while it is handled by the entity manager.

Parameters
entityAn entity
See also
removeEntity()
Entity* gf::EntityContainer::removeEntity ( Entity entity)

Remove an entity from the collection.

Parameters
entityThe entity to remove
Returns
The removed entity or nullptr if the entity was not present
See also
addEntity()
template<typename E >
E* gf::EntityContainer::removeTypedEntity ( E entity)
inline

Remove a typed entity from the collection.

This function is a shortcut to avoid typecasting.

Example:

class Foo : public Entity {
...
};
Foo foo;
manager.addEntity(foo);
...
Foo *removed = manager.removeTypedEntity<Foo>(&foo);
removed->doSomething();
See also
removeEntity()
void gf::EntityContainer::render ( RenderTarget target)

Render the entities on the target.

The entities are rendered by priority: lower priority first and higher priority last.

Parameters
targetThe render target
See also
gf::Entity::render()
void gf::EntityContainer::update ( float  dt)

Update the entities.

This function first eliminates the dead entities, then sort them by priority. Finally, each entity is updated.

Parameters
dtThe time (in seconds) since the last update
See also
gf::Entity::update()