Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
EntityContainer.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_ENTITY_CONTAINER_H
22 #define GF_ENTITY_CONTAINER_H
23 
24 #include <type_traits>
25 #include <vector>
26 
27 #include "Portability.h"
28 
29 namespace gf {
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 inline namespace v1 {
32 #endif
33 
34  class Entity;
35  class RenderTarget;
36 
37  /**
38  * @ingroup game
39  * @brief A collection of entities
40  *
41  * gf::EntityContainer represents a collection of entities that are
42  * updated and rendered automatically. The entity manager takes care of
43  * the liveness of the entities and remove the dead entities from the
44  * collection.
45  *
46  * The entity manager is *not* responsible for the memory of the entities.
47  * The entities must be allocated by the user and not deleted while they
48  * are handled by the entity manager.
49  *
50  * Generally, you only need one entity manager in your game. You create it
51  * at the beginning of the game and put all your entities in it. Then you
52  * can call gf::EntityContainer::update() and gf::EntityContainer::render() in your
53  * game loop.
54  *
55  * @sa gf::Entity
56  */
58  public:
59 
60  /**
61  * @brief Update the entities
62  *
63  * This function first eliminates the dead entities, then sort them
64  * by priority. Finally, each entity is updated.
65  *
66  * @param dt The time (in seconds) since the last update
67  * @sa gf::Entity::update()
68  */
69  void update(float dt);
70 
71  /**
72  * @brief Render the entities on the target
73  *
74  * The entities are rendered by priority: lower priority first and
75  * higher priority last.
76  *
77  * @param target The render target
78  * @sa gf::Entity::render()
79  */
80  void render(RenderTarget& target);
81 
82  /**
83  * @name Entities management
84  * @{
85  */
86 
87  /**
88  * @brief Add an entity to the collection
89  *
90  * The entity must not be deleted while it is handled by the entity
91  * manager.
92  *
93  * @param entity An entity
94  * @sa removeEntity()
95  */
96  void addEntity(Entity& entity);
97 
98  /**
99  * @brief Remove an entity from the collection
100  *
101  * @param entity The entity to remove
102  * @return The removed entity or `nullptr` if the entity was not present
103  * @sa addEntity()
104  */
105  Entity *removeEntity(Entity *entity);
106 
107  /**
108  * @brief Remove a typed entity from the collection
109  *
110  * This function is a shortcut to avoid typecasting.
111  *
112  * Example:
113  *
114  * ~~~{.cc}
115  *
116  * class Foo : public Entity {
117  * ...
118  * };
119  *
120  * Foo foo;
121  * manager.addEntity(foo);
122  *
123  * ...
124  *
125  * Foo *removed = manager.removeTypedEntity<Foo>(&foo);
126  * removed->doSomething();
127  * ~~~
128  *
129  * @sa removeEntity()
130  */
131  template<typename E>
132  E *removeTypedEntity(E *entity) {
133  static_assert(std::is_base_of<Entity, E>::value, "E must be an Entity");
134  return static_cast<E*>(removeEntity(entity));
135  }
136 
137  /** @} */
138 
139  private:
140  std::vector<Entity *> m_entities;
141  };
142 
143 #ifndef DOXYGEN_SHOULD_SKIP_THIS
144 }
145 #endif
146 }
147 
148 #endif // GF_ENTITY_CONTAINER_H
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:65
friend class RenderTarget
Definition: Shader.h:387
A game entity.
Definition: Entity.h:51
Definition: Action.h:34
void addEntity(Entity &entity)
Add an entity to the collection.
Entity * removeEntity(Entity *entity)
Remove an entity from the collection.
void render(RenderTarget &target)
Render the entities on the target.
#define GF_API
Definition: Portability.h:35
void update(float dt)
Update the entities.
E * removeTypedEntity(E *entity)
Remove a typed entity from the collection.
Definition: EntityContainer.h:132
A collection of entities.
Definition: EntityContainer.h:57