comparison engine/core/model/structures/map.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 90005975cdbb
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /***************************************************************************
2 * Copyright (C) 2005-2008 by the FIFE team *
3 * http://www.fifengine.de *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program 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 *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23 #include <string>
24
25 // 3rd party library includes
26 #include <boost/lexical_cast.hpp>
27
28 // FIFE includes
29 // These includes are split up in two parts, separated by one empty line
30 // First block: files included from the FIFE root src directory
31 // Second block: files included from the same folder
32 #include "util/base/exception.h"
33 #include "util/structures/purge.h"
34
35 #include "map.h"
36 #include "layer.h"
37
38 namespace FIFE {
39
40 Map::Map(const std::string& identifier, TimeProvider* tp_master):
41 m_id(identifier),
42 m_timeprovider(tp_master),
43 m_changelisteners(),
44 m_changedlayers(),
45 m_changed(false) {
46 }
47
48 Map::~Map() {
49 deleteLayers();
50 }
51
52 Layer* Map::getLayer(const std::string& id) {
53 std::list<Layer*>::const_iterator it = m_layers.begin();
54 for(; it != m_layers.end(); ++it) {
55 if((*it)->getId() == id)
56 return *it;
57 }
58
59 throw NotFound(id);
60 }
61
62 size_t Map::getNumLayers() const {
63 return m_layers.size();
64 }
65
66 Layer* Map::createLayer(const std::string& identifier, CellGrid* grid) {
67 std::list<Layer*>::const_iterator it = m_layers.begin();
68 for(; it != m_layers.end(); ++it) {
69 if(identifier == (*it)->getId())
70 throw NameClash(identifier);
71 }
72
73 Layer* layer = new Layer(identifier, this, grid);
74 m_layers.push_back(layer);
75 m_changed = true;
76 std::vector<MapChangeListener*>::iterator i = m_changelisteners.begin();
77 while (i != m_changelisteners.end()) {
78 (*i)->onLayerCreate(this, layer);
79 ++i;
80 }
81
82 return layer;
83 }
84
85 void Map::deleteLayer(Layer* layer) {
86 std::list<Layer*>::iterator it = m_layers.begin();
87 for(; it != m_layers.end(); ++it) {
88 if((*it) == layer) {
89 std::vector<MapChangeListener*>::iterator i = m_changelisteners.begin();
90 while (i != m_changelisteners.end()) {
91 (*i)->onLayerDelete(this, layer);
92 ++i;
93 }
94 delete layer;
95 m_layers.erase(it);
96 return ;
97 }
98 }
99 m_changed = true;
100 }
101
102 void Map::deleteLayers() {
103 std::list<Layer*>::iterator it = m_layers.begin();
104 for(; it != m_layers.end(); ++it) {
105 std::vector<MapChangeListener*>::iterator i = m_changelisteners.begin();
106 while (i != m_changelisteners.end()) {
107 (*i)->onLayerDelete(this, *it);
108 ++i;
109 }
110 }
111 purge(m_layers);
112 m_layers.clear();
113 }
114
115 bool Map::update() {
116 m_changedlayers.clear();
117 std::list<Layer*>::iterator it = m_layers.begin();
118 for(; it != m_layers.end(); ++it) {
119 if ((*it)->update()) {
120 m_changedlayers.push_back(*it);
121 }
122 }
123 if (!m_changedlayers.empty()) {
124 std::vector<MapChangeListener*>::iterator i = m_changelisteners.begin();
125 while (i != m_changelisteners.end()) {
126 (*i)->onMapChanged(this, m_changedlayers);
127 ++i;
128 }
129 }
130 bool retval = m_changed;
131 m_changed = false;
132 return retval;
133 }
134
135 void Map::addChangeListener(MapChangeListener* listener) {
136 m_changelisteners.push_back(listener);
137 }
138
139 void Map::removeChangeListener(MapChangeListener* listener) {
140 std::vector<MapChangeListener*>::iterator i = m_changelisteners.begin();
141 while (i != m_changelisteners.end()) {
142 if ((*i) == listener) {
143 m_changelisteners.erase(i);
144 return;
145 }
146 ++i;
147 }
148 }
149
150 } //FIFE
151