File: map.cpp

package info (click to toggle)
fife 0.3.3%2Br3-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 16,312 kB
  • sloc: cpp: 83,234; python: 16,261; sh: 10,134; xml: 3,435; makefile: 265; objc: 245; ansic: 229
file content (261 lines) | stat: -rw-r--r-- 7,539 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/***************************************************************************
 *   Copyright (C) 2005-2008 by the FIFE team                              *
 *   http://www.fifengine.de                                               *
 *   This file is part of FIFE.                                            *
 *                                                                         *
 *   FIFE is free software; you can redistribute it and/or                 *
 *   modify it under the terms of the GNU Lesser General Public            *
 *   License as published by the Free Software Foundation; either          *
 *   version 2.1 of the License, or (at your option) any later version.    *
 *                                                                         *
 *   This library is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

// Standard C++ library includes
#include <string>

// 3rd party library includes
#include <boost/lexical_cast.hpp>

// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "util/base/exception.h"
#include "util/structures/purge.h"
#include "util/structures/rect.h"
#include "view/camera.h"
#include "view/rendererbase.h"
#include "video/renderbackend.h"

#include "map.h"
#include "layer.h"

namespace FIFE {

	Map::Map(const std::string& identifier, RenderBackend* renderBackend,
			const std::vector<RendererBase*>& renderers, TimeProvider* tp_master):
		m_id(identifier),
		m_filename(""),
		m_timeprovider(tp_master),
		m_changelisteners(),
		m_changedlayers(),
		m_renderbackend(renderBackend),
		m_renderers(renderers),
		m_changed(false){
	}

	Map::~Map() {
        // remove all cameras
        std::vector<Camera*>::iterator iter = m_cameras.begin();
        for ( ; iter != m_cameras.end(); ++iter) {
            delete *iter;
        }
        m_cameras.clear();

		deleteLayers();
	}

	Layer* Map::getLayer(const std::string& id) {
		std::list<Layer*>::const_iterator it = m_layers.begin();
		for(; it != m_layers.end(); ++it) {
			if((*it)->getId() == id)
				return *it;
		}

		throw NotFound(id);
	}

	uint32_t Map::getLayerCount() const {
		return m_layers.size();
	}

	Layer* Map::createLayer(const std::string& identifier, CellGrid* grid) {
		std::list<Layer*>::const_iterator it = m_layers.begin();
		for(; it != m_layers.end(); ++it) {
			if(identifier == (*it)->getId())
				throw NameClash(identifier);
		}

		Layer* layer = new Layer(identifier, this, grid);
		m_layers.push_back(layer);
		m_changed = true;
		std::vector<MapChangeListener*>::iterator i = m_changelisteners.begin();
		while (i != m_changelisteners.end()) {
			(*i)->onLayerCreate(this, layer);
			++i;
		}

		return layer;
	}

	void Map::deleteLayer(Layer* layer) {
		std::list<Layer*>::iterator it = m_layers.begin();
		for(; it != m_layers.end(); ++it) {
			if((*it) == layer) {
				std::vector<MapChangeListener*>::iterator i = m_changelisteners.begin();
				while (i != m_changelisteners.end()) {
					(*i)->onLayerDelete(this, layer);
					++i;
				}
				delete layer;
				m_layers.erase(it);
				return ;
			}
		}
		m_changed = true;
	}

	void Map::deleteLayers() {
		std::list<Layer*>::iterator it = m_layers.begin();
		for(; it != m_layers.end(); ++it) {
			std::vector<MapChangeListener*>::iterator i = m_changelisteners.begin();
			while (i != m_changelisteners.end()) {
				(*i)->onLayerDelete(this, *it);
				++i;
			}
		}
		purge(m_layers);
		m_layers.clear();
	}

	void Map::getMinMaxCoordinates(ExactModelCoordinate& min, ExactModelCoordinate& max) {
		Location lmin;
		Location lmax;
		std::list<Layer*>::iterator it = m_layers.begin();
		Layer* layer = *it;
		for (; it != m_layers.end(); ++it) {
			ModelCoordinate newMin, newMax;
			(*it)->getMinMaxCoordinates(newMin, newMax, layer);

			if (newMin.x < min.x) {
				min.x = newMin.x;
			}
			if (newMax.x > max.x) {
				max.x = newMax.x;
			}
			if (newMin.y < min.y) {
				min.y = newMin.y;
			}
			if (newMax.y > max.y) {
				max.y = newMax.y;
			}
		}
		lmin.setLayer(layer);
		lmax.setLayer(layer);
		lmin.setExactLayerCoordinates(min);
		lmax.setExactLayerCoordinates(max);

		min = lmin.getMapCoordinates();
		max = lmax.getMapCoordinates();
	}

	bool Map::update() {
		m_changedlayers.clear();
		std::list<Layer*>::iterator it = m_layers.begin();
		for(; it != m_layers.end(); ++it) {
			if ((*it)->update()) {
				m_changedlayers.push_back(*it);
			}
		}
		if (!m_changedlayers.empty()) {
			std::vector<MapChangeListener*>::iterator i = m_changelisteners.begin();
			while (i != m_changelisteners.end()) {
				(*i)->onMapChanged(this, m_changedlayers);
				++i;
			}
		}

		// loop over cameras and update if enabled
		std::vector<Camera*>::iterator camIter = m_cameras.begin();
		for ( ; camIter != m_cameras.end(); ++camIter) {
			if ((*camIter)->isEnabled()) {
				(*camIter)->update();
				(*camIter)->render();
			}
		}

		bool retval = m_changed;
		m_changed = false;
		return retval;
	}

	void Map::addChangeListener(MapChangeListener* listener) {
		m_changelisteners.push_back(listener);
	}

	void Map::removeChangeListener(MapChangeListener* listener) {
		std::vector<MapChangeListener*>::iterator i = m_changelisteners.begin();
		while (i != m_changelisteners.end()) {
			if ((*i) == listener) {
				m_changelisteners.erase(i);
				return;
			}
			++i;
		}
	}

	Camera* Map::addCamera(const std::string &id, Layer *layer, const Rect& viewport) {
		if (layer == NULL) {
			throw NotSupported("Must have valid layer for camera");
		}

		if (getCamera(id)) {
			std::string errorStr = "Camera: " + id + " already exists";
			throw NameClash(errorStr);
		}

		// create new camera and add to list of cameras
		Camera* camera = new Camera(id, layer, viewport, m_renderbackend);
		m_cameras.push_back(camera);

		std::vector<RendererBase*>::iterator iter = m_renderers.begin();
		for ( ; iter != m_renderers.end(); ++iter) {
			camera->addRenderer((*iter)->clone());
		}

		return camera;
	}

	void Map::removeCamera(const std::string &id) {
		std::vector<Camera*>::iterator iter = m_cameras.begin();
		for ( ; iter != m_cameras.end(); ++iter) {
			if ((*iter)->getId() == id) {
				// camera has been found delete it
				delete *iter;

				// now remove it from the vector
				// note this invalidates iterators, but we do not need
				// to worry about it in this case since we are done
				m_cameras.erase(iter);

				break;
			}
		}
	}

	Camera* Map::getCamera(const std::string &id) {
		std::vector<Camera*>::iterator iter = m_cameras.begin();
		for ( ; iter != m_cameras.end(); ++iter) {
			if ((*iter)->getId() == id) {
				return *iter;
			}
		}

		return NULL;
	}

	const std::vector<Camera*>& Map::getCameras() const {
		return m_cameras;
	}

} //FIFE