FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
model.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 
24 // 3rd party library includes
25 
26 // FIFE includes
27 // These includes are split up in two parts, separated by one empty line
28 // First block: files included from the FIFE root src directory
29 // Second block: files included from the same folder
30 #include "util/structures/purge.h"
31 #include "util/log/logger.h"
33 #include "model/metamodel/object.h"
35 #include "structures/map.h"
36 #include "structures/layer.h"
37 #include "structures/instance.h"
38 #include "util/base/exception.h"
39 #include "view/rendererbase.h"
40 #include "video/renderbackend.h"
41 
42 #include "model.h"
43 
44 namespace FIFE {
45  static Logger _log(LM_MODEL);
46 
47  Model::Model(RenderBackend* renderbackend, const std::vector<RendererBase*>& renderers)
48  : FifeClass(),
49  m_last_namespace(NULL),
50  m_timeprovider(NULL),
51  m_renderbackend(renderbackend),
52  m_renderers(renderers){
53  }
54 
56  purge(m_maps);
57  for(std::list<namespace_t>::iterator nspace = m_namespaces.begin(); nspace != m_namespaces.end(); ++nspace)
58  purge_map(nspace->second);
62  }
63 
64  Map* Model::createMap(const std::string& identifier) {
65  std::list<Map*>::const_iterator it = m_maps.begin();
66  for(; it != m_maps.end(); ++it) {
67  if(identifier == (*it)->getId()) {
68  throw NameClash(identifier);
69  }
70  }
71 
72  Map* map = new Map(identifier, m_renderbackend, m_renderers, &m_timeprovider);
73  m_maps.push_back(map);
74  return map;
75  }
76 
77  void Model::adoptPather(IPather* pather) {
78  m_pathers.push_back(pather);
79  }
80 
81  IPather* Model::getPather(const std::string& pathername) {
82  std::vector<IPather*>::const_iterator it = m_pathers.begin();
83  for(; it != m_pathers.end(); ++it) {
84  if ((*it)->getName() == pathername) {
85  return *it;
86  }
87  }
88  FL_WARN(_log, "No pather of requested type \"" + pathername + "\" found.");
89  return NULL;
90  }
91 
93  m_adopted_grids.push_back(grid);
94  }
95 
96  CellGrid* Model::getCellGrid(const std::string& gridtype) {
97  std::vector<CellGrid*>::const_iterator it = m_adopted_grids.begin();
98  for(; it != m_adopted_grids.end(); ++it) {
99  if ((*it)->getType() == gridtype) {
100  CellGrid* newcg = (*it)->clone();
101  m_created_grids.push_back(newcg);
102  return newcg;
103  }
104  }
105  FL_WARN(_log, "No cellgrid of requested type \"" + gridtype + "\" found.");
106  return NULL;
107  }
108 
109 
110  Map* Model::getMap(const std::string& identifier) const {
111  std::list<Map*>::const_iterator it = m_maps.begin();
112  for(; it != m_maps.end(); ++it) {
113  if((*it)->getId() == identifier)
114  return *it;
115  }
116 
117  throw NotFound(std::string("Tried to get non-existent map: ") + identifier + ".");
118  }
119 
120  void Model::deleteMap(Map* map) {
121  std::list<Map*>::iterator it = m_maps.begin();
122  for(; it != m_maps.end(); ++it) {
123  if(*it == map) {
124  delete *it;
125  m_maps.erase(it);
126  return ;
127  }
128  }
129  }
130 
132  return m_maps.size();
133  }
134 
136  purge(m_maps);
137  m_maps.clear();
138  }
139 
141  uint32_t count = 0;
142  std::list<Map*>::const_iterator it = m_maps.begin();
143  for(; it != m_maps.end(); ++it) {
144  count += (*it)->getActiveCameraCount();
145  }
146  return count;
147  }
148 
149  std::list<std::string> Model::getNamespaces() const {
150  std::list<std::string> namespace_list;
151  std::list<namespace_t>::const_iterator nspace = m_namespaces.begin();
152  for(; nspace != m_namespaces.end(); ++nspace) {
153  namespace_list.push_back(nspace->first);
154  }
155  return namespace_list;
156  }
157 
158  Object* Model::createObject(const std::string& identifier, const std::string& name_space, Object* parent) {
159  // Find or create namespace
160  namespace_t* nspace = selectNamespace(name_space);
161  if(!nspace) {
162  m_namespaces.push_back(namespace_t(name_space,objectmap_t()));
163  nspace = selectNamespace(name_space);
164  }
165 
166  // Check for nameclashes
167  objectmap_t::const_iterator it = nspace->second.find(identifier);
168  if( it != nspace->second.end() ) {
169  throw NameClash(identifier);
170  }
171 
172  // Finally insert & create
173  Object* object = new Object(identifier, name_space, parent);
174  nspace->second[identifier] = object;
175  return object;
176  }
177 
178  bool Model::deleteObject(Object* object) {
179  // WARNING: This code has obviously not been tested (thoroughly).
180 
181  // Check if any instances exist. If yes - bail out.
182  std::list<Layer*>::const_iterator jt;
183  std::vector<Instance*>::const_iterator kt;
184  for(std::list<Map*>::iterator it = m_maps.begin(); it != m_maps.end(); ++it) {
185  for(jt = (*it)->getLayers().begin(); jt != (*it)->getLayers().end(); ++jt) {
186  for(kt = (*jt)->getInstances().begin(); kt != (*jt)->getInstances().end(); ++kt) {
187  Object* o = (*kt)->getObject();
188  if(o == object) {
189  return false;
190  }
191  }
192  }
193  }
194 
195  // Check if the namespace exists
196  namespace_t* nspace = selectNamespace(object->getNamespace());
197  if(!nspace)
198  return true;
199 
200  // If yes - delete+erase object.
201  objectmap_t::iterator it = nspace->second.find(object->getId());
202  if( it != nspace->second.end()) {
203  delete it->second;
204  nspace->second.erase(it);
205  }
206 
207  return true;
208  }
209 
211  // If we have layers with instances - bail out.
212  std::list<Layer*>::const_iterator jt;
213  for(std::list<Map*>::iterator it = m_maps.begin(); it != m_maps.end(); ++it) {
214  for(jt = (*it)->getLayers().begin(); jt != (*it)->getLayers().end(); ++jt) {
215  if((*jt)->hasInstances())
216  return false;
217  }
218  }
219 
220  // Otherwise delete every object in every namespace
221  std::list<namespace_t>::iterator nspace = m_namespaces.begin();
222  while(nspace != m_namespaces.end()) {
223  objectmap_t::iterator it = nspace->second.begin();
224  for(; it != nspace->second.end(); ++it) {
225  delete it->second;
226  }
227  nspace = m_namespaces.erase(nspace);
228  }
229  m_last_namespace = 0;
230  return true;
231  }
232 
233  Object* Model::getObject(const std::string& id, const std::string& name_space) {
234  namespace_t* nspace = selectNamespace(name_space);
235  if(nspace) {
236  objectmap_t::iterator it = nspace->second.find(id);
237  if( it != nspace->second.end() )
238  return it->second;
239  }
240  return NULL;
241  }
242 
243  std::list<Object*> Model::getObjects(const std::string& name_space) const {
244  std::list<Object*> object_list;
245  const namespace_t* nspace = selectNamespace(name_space);
246  if(nspace) {
247  objectmap_t::const_iterator it = nspace->second.begin();
248  for(; it != nspace->second.end(); ++it )
249  object_list.push_back(it->second);
250  }
251 
252  return object_list;
253  }
254 
255  const Model::namespace_t* Model::selectNamespace(const std::string& name_space) const {
256  std::list<namespace_t>::const_iterator nspace = m_namespaces.begin();
257  for(; nspace != m_namespaces.end(); ++nspace) {
258  if( nspace->first == name_space ) {
259  return &(*nspace);
260  }
261  }
262 
263  return NULL;
264  }
265 
266  Model::namespace_t* Model::selectNamespace(const std::string& name_space) {
267  if( m_last_namespace && m_last_namespace->first == name_space )
268  return m_last_namespace;
269  std::list<namespace_t>::iterator nspace = m_namespaces.begin();
270  for(; nspace != m_namespaces.end(); ++nspace) {
271  if( nspace->first == name_space ) {
272  m_last_namespace = &(*nspace);
273  return m_last_namespace;
274  }
275  }
276  m_last_namespace = 0;
277  return NULL;
278  }
279 
280  void Model::update() {
281  std::list<Map*>::iterator it = m_maps.begin();
282  for(; it != m_maps.end(); ++it) {
283  (*it)->update();
284  }
285  std::vector<IPather*>::iterator jt = m_pathers.begin();
286  for(; jt != m_pathers.end(); ++jt) {
287  (*jt)->update();
288  }
289  }
290 
291 } //FIFE
292 
std::list< std::string > getNamespaces() const
Get a list of namespaces currently referenced by objects in the metamodel.
Definition: model.cpp:149
#define FL_WARN(logger, msg)
Definition: logger.h:72
Abstract interface for all the renderbackends.
Map * getMap(const std::string &identifier) const
Get a map.
Definition: model.cpp:110
void adoptCellGrid(CellGrid *grid)
Adds cellgrid to model.
Definition: model.cpp:92
std::vector< CellGrid * > m_adopted_grids
Definition: model.h:177
RenderBackend * m_renderbackend
Definition: model.h:181
bool deleteObjects()
Attempt to remove all objects from the model Fails and returns false if any maps with instances are p...
Definition: model.cpp:210
namespace_t * m_last_namespace
Used to remember last 'selected' namespace.
Definition: model.h:167
const std::string & getNamespace() const
Definition: object.h:69
Object class.
Definition: object.h:51
const std::string & getId() const
Definition: object.h:68
std::vector< IPather * > m_pathers
Definition: model.h:175
Object * createObject(const std::string &identifier, const std::string &name_space, Object *parent=0)
Add an object to the metamodel.
Definition: model.cpp:158
uint32_t getActiveCameraCount() const
Return the number of enabled cameras in this model.
Definition: model.cpp:140
Base class for all fife classes Used e.g.
Definition: fifeclass.h:42
void update()
Called periodically to update events on model.
Definition: model.cpp:280
static Logger _log(LM_AUDIO)
std::vector< RendererBase * > m_renderers
Definition: model.h:183
void purge(Seq &c)
Definition: purge.h:28
std::list< namespace_t > m_namespaces
Definition: model.h:164
void deleteMaps()
Removes all maps from this model.
Definition: model.cpp:135
uint32_t getMapCount() const
Return the number of maps in this model.
Definition: model.cpp:131
void purge_map(Seq &c)
Definition: purge.h:45
std::list< Map * > m_maps
Definition: model.h:160
CellGrid * getCellGrid(const std::string &gridtype)
Returns new copy of cellgrid corresponding given name.
Definition: model.cpp:96
~Model()
Destructor.
Definition: model.cpp:55
Model(RenderBackend *renderbackend, const std::vector< RendererBase * > &renderers)
Constructor.
Definition: model.cpp:47
virtual CellGrid * clone()=0
Returns clone of this cellgrid.
namespace_t * selectNamespace(const std::string &name_space)
Convenience function to retrieve a pointer to a namespace or NULL if it doesn't exist.
Definition: model.cpp:266
TimeProvider m_timeprovider
Definition: model.h:179
void deleteMap(Map *)
Remove a map from this model.
Definition: model.cpp:120
IPather * getPather(const std::string &pathername)
Returns pather corresponding given name.
Definition: model.cpp:81
void adoptPather(IPather *pather)
Adds pather to model.
Definition: model.cpp:77
std::pair< std::string, objectmap_t > namespace_t
Definition: model.h:163
Object * getObject(const std::string &id, const std::string &name_space)
Get an object by its id.
Definition: model.cpp:233
bool deleteObject(Object *)
Attempt to remove an object from the model Fails and returns false if the object is referenced by an ...
Definition: model.cpp:178
A container of Layer(s).
Definition: map.h:88
std::vector< CellGrid * > m_created_grids
Definition: model.h:176
unsigned int uint32_t
Definition: core.h:40
std::map< std::string, Object * > objectmap_t
Definition: model.h:162
std::list< Object * > getObjects(const std::string &name_space) const
Get all the objects in the given namespace.
Definition: model.cpp:243
Map * createMap(const std::string &identifier)
Add a map this model, and get a pointer to it.
Definition: model.cpp:64