File: renderernode.cpp

package info (click to toggle)
fife 0.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,012 kB
  • ctags: 26,660
  • sloc: cpp: 84,903; sh: 10,269; python: 8,753; xml: 1,213; makefile: 265; objc: 245; ansic: 229
file content (318 lines) | stat: -rw-r--r-- 10,683 bytes parent folder | download | duplicates (2)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/***************************************************************************
 *   Copyright (C) 2005-2013 by the FIFE team                              *
 *   http://www.fifengine.net                                              *
 *   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

// 3rd party library includes

// 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 "video/renderbackend.h"
#include "util/math/fife_math.h"
#include "util/log/logger.h"
#include "model/structures/instance.h"
#include "model/structures/layer.h"
#include "model/structures/location.h"
#include "view/camera.h"

#include "renderernode.h"


namespace FIFE {
	static Logger _log(LM_VIEWVIEW);

	class NodeInstanceDeleteListener : public InstanceDeleteListener {
	public:
		NodeInstanceDeleteListener(RendererNode* node)	{
			m_node = node;
		}
		virtual ~NodeInstanceDeleteListener() {}

		virtual void onInstanceDeleted(Instance* instance) {
			m_node->removeInstance(instance, false);
		}

	private:
		RendererNode* m_node;
	};

	RendererNode::RendererNode(Instance* attached_instance, const Location &relative_location, Layer* relative_layer, const Point &relative_point):
		m_instance(NULL),
		m_location(relative_location),
		m_layer(relative_layer),
		m_point(relative_point),
		m_listener(NULL) {
		addInstance(attached_instance);
	}
	RendererNode::RendererNode(Instance* attached_instance, const Location &relative_location, const Point &relative_point):
		m_instance(NULL),
		m_location(relative_location),
		m_layer(NULL),
		m_point(relative_point),
		m_listener(NULL) {
		addInstance(attached_instance);
	}
	RendererNode::RendererNode(Instance* attached_instance, Layer* relative_layer, const Point &relative_point):
		m_instance(NULL),
		m_location(NULL),
		m_layer(relative_layer),
		m_point(relative_point),
		m_listener(NULL) {
		addInstance(attached_instance);
	}
	RendererNode::RendererNode(Instance* attached_instance, const Point &relative_point):
		m_instance(NULL),
		m_location(NULL),
		m_layer(NULL),
		m_point(relative_point),
		m_listener(NULL) {
		addInstance(attached_instance);
	}
	RendererNode::RendererNode(const Location &attached_location, Layer* relative_layer, const Point &relative_point):
		m_instance(NULL),
		m_location(attached_location),
		m_layer(relative_layer),
		m_point(relative_point),
		m_listener(NULL) {
	}
	RendererNode::RendererNode(const Location &attached_location, const Point &relative_point):
		m_instance(NULL),
		m_location(attached_location),
		m_layer(NULL),
		m_point(relative_point),
		m_listener(NULL) {
	}
	RendererNode::RendererNode(Layer* attached_layer, const Point &relative_point):
		m_instance(NULL),
		m_location(NULL),
		m_layer(attached_layer),
		m_point(relative_point),
		m_listener(NULL) {
	}
	RendererNode::RendererNode(const Point &attached_point):
		m_instance(NULL),
		m_location(NULL),
		m_layer(NULL),
		m_point(attached_point),
		m_listener(NULL) {
	}
	RendererNode::RendererNode(const RendererNode& old):
		m_instance(NULL),
		m_location(old.m_location),
		m_layer(old.m_layer),
		m_point(old.m_point),
		m_listener(NULL) {
		addInstance(old.m_instance);
	}
	RendererNode& RendererNode::operator=(const RendererNode &source) {
		if (this != &source) {
			changeInstance(source.m_instance);
			m_location = source.m_location;
			m_layer = source.m_layer;
			m_point = source.m_point;
		}
		return *this;
	}
	RendererNode::~RendererNode() {
		removeInstance(m_instance);
		delete m_listener;
	}

	void RendererNode::setAttached(Instance* attached_instance, const Location &relative_location, const Point &relative_point) {
		changeInstance(attached_instance);
		m_location = relative_location;
		m_point = relative_point;
	}
	void RendererNode::setAttached(Instance* attached_instance, const Location &relative_location) {
		changeInstance(attached_instance);
		m_location = relative_location;
	}
	void RendererNode::setAttached(Instance* attached_instance, const Point &relative_point) {
		changeInstance(attached_instance);
		m_point = relative_point;
	}
	void RendererNode::setAttached(Instance* attached_instance) {
		changeInstance(attached_instance);
	}
	void RendererNode::setAttached(const Location &attached_location, const Point &relative_point) {
		changeInstance(NULL);
		m_location = attached_location;
		m_point = relative_point;
	}
	void RendererNode::setAttached(const Location &attached_location) {
		changeInstance(NULL);
		m_location = attached_location;
	}
	void RendererNode::setAttached(Layer* attached_layer) {
		m_layer = attached_layer;
	}
	void RendererNode::setAttached(const Point &attached_point) {
		changeInstance(NULL);
		m_location = NULL;
		m_point = attached_point;
	}

	void RendererNode::setRelative(const Location &relative_location) {
		if(m_instance == NULL) {
			FL_WARN(_log, LMsg("RendererNode::setRelative(Location) - ") << "No instance attached.");
		}
		m_location = relative_location;
	}
	void RendererNode::setRelative(const Location &relative_location, Point relative_point) {
		if(m_instance == NULL) {
			FL_WARN(_log, LMsg("RendererNode::setRelative(Location, Point) - ") << "No instance attached.");
		}
		m_location = relative_location;
		m_point = relative_point;
	}
	void RendererNode::setRelative(const Point &relative_point) {
		if(m_instance == NULL || m_location == NULL) {
			FL_WARN(_log, LMsg("RendererNode::setRelative(Point) - ") << "No instance or location attached.");
		}
		m_point = relative_point;
	}

	Instance* RendererNode::getAttachedInstance() {
		if(m_instance == NULL) {
			FL_WARN(_log, LMsg("RendererNode::getAttachedInstance() - ") << "No instance attached.");
		}
		return m_instance;
	}
	Location RendererNode::getAttachedLocation() {
		if(m_instance != NULL || m_location == NULL) {
			FL_WARN(_log, LMsg("RendererNode::getAttachedLocation() - ") << "No location attached.");
		}
		return m_location;
	}
	Layer* RendererNode::getAttachedLayer() {
		if(m_layer == NULL) {
			FL_WARN(_log, LMsg("RendererNode::getAttachedLayer() - ") << "No layer attached.");
		}
		return m_layer;
	}
	Point RendererNode::getAttachedPoint() {
		if(m_instance != NULL || m_location != NULL) {
			FL_WARN(_log, LMsg("RendererNode::getAttachedPoint() - ") << "No point attached.");
		}
		return m_point;
	}

	Location RendererNode::getOffsetLocation() {
		if(m_instance == NULL || m_location == NULL) {
			FL_WARN(_log, LMsg("RendererNode::getOffsetLocation() - ") << "No location as offset used.");
		}
		return m_location;
	}
	Point RendererNode::getOffsetPoint() {
		if(m_instance == NULL && m_location == NULL) {
			FL_WARN(_log, LMsg("RendererNode::getOffsetPoint() - ") << "No point as offset used.");
		}
		return m_point;
	}

	Instance* RendererNode::getInstance() {
		return m_instance;
	}
	Location RendererNode::getLocation() {
		return m_location;
	}
	const Location& RendererNode::getLocationRef() {
		return m_location;
	}
	Layer* RendererNode::getLayer() {
		return m_layer;
	}
	Point RendererNode::getPoint() {
		return m_point;
	}
	const Point& RendererNode::getPointRef() {
		return m_point;
	}

	void RendererNode::addInstance(Instance* instance) {
		checkDeleteListener();
		m_instance = instance;
		if (m_instance) {
			m_instance->addDeleteListener(m_listener);
		}
	}

	void RendererNode::changeInstance(Instance* instance) {
		if (m_instance == instance) {
			return;
		}
		checkDeleteListener();
		if (m_instance) {
			m_instance->removeDeleteListener(m_listener);
		}
		m_instance = instance;
		if (m_instance) {
			m_instance->addDeleteListener(m_listener);
		}
	}

	void RendererNode::removeInstance(Instance* instance, bool listener) {
		if (m_instance == instance && instance) {
			if (listener) {
				m_instance->removeDeleteListener(m_listener);
			}
			m_instance = NULL;
		}
	}

	void RendererNode::checkDeleteListener() {
		if (m_listener) {
			return;
		}
		m_listener = new NodeInstanceDeleteListener(this);
	}

	Point RendererNode::getCalculatedPoint(Camera* cam, Layer* layer, const bool zoomed) {
		ScreenPoint p;
		if(m_instance != NULL) {
			if(m_layer == NULL) {
				m_layer = m_instance->getLocationRef().getLayer();
			}
			if(m_location != NULL) {
				p = cam->toScreenCoordinates(m_instance->getLocationRef().getMapCoordinates() + m_location.getMapCoordinates());
			} else {
				p = cam->toScreenCoordinates(m_instance->getLocationRef().getMapCoordinates());
			}
		} else if(m_location != NULL) {
			if(m_layer == NULL) {
				m_layer = m_location.getLayer();
			}
			p = cam->toScreenCoordinates(m_location.getMapCoordinates());
		} else if(m_layer == NULL) {
			// FIXME
			FL_WARN(_log, LMsg("RendererNode::getCalculatedPoint(Camera, Layer) - ") << "No layer attached. So we use the first active layer of the renderer.");
			setAttached(layer);
		}
		if(zoomed) {
			return Point(round(m_point.x * cam->getZoom()) + p.x, round(m_point.y * cam->getZoom()) + p.y);
		} else {
			return Point(m_point.x + p.x, m_point.y + p.y);
		}

	}
}