Mercurial > fife-parpg
annotate engine/core/view/renderers/genericrenderer.cpp @ 25:fc6713d72b80
add setter for nodes
author | spq@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Fri, 11 Jul 2008 11:25:11 +0000 |
parents | cb87d40e9342 |
children | 70f41ea8e7db |
rev | line source |
---|---|
23 | 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 | |
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 "video/renderbackend.h" | |
31 #include "util/math/fife_math.h" | |
32 #include "util/log/logger.h" | |
33 #include "model/metamodel/grids/cellgrid.h" | |
34 #include "model/structures/instance.h" | |
35 #include "model/structures/layer.h" | |
36 #include "model/structures/location.h" | |
37 | |
38 #include "view/camera.h" | |
39 #include "genericrenderer.h" | |
40 | |
41 | |
42 namespace FIFE { | |
43 static Logger _log(LM_VIEWVIEW); | |
44 | |
45 GenericRendererNode::GenericRendererNode(Instance* attached_instance, Location* relative_location, const Point &relative_point): | |
46 m_instance(attached_instance), | |
47 m_location(relative_location), | |
48 m_point(relative_point) { | |
49 } | |
50 GenericRendererNode::GenericRendererNode(Instance* attached_instance, const Point &relative_point): | |
51 m_instance(attached_instance), | |
52 m_location(NULL), | |
53 m_point(relative_point) { | |
54 } | |
55 GenericRendererNode::GenericRendererNode(Location* attached_location, const Point &relative_point): | |
56 m_instance(NULL), | |
57 m_location(attached_location), | |
58 m_point(relative_point) { | |
59 } | |
60 GenericRendererNode::GenericRendererNode(const Point &attached_point): | |
61 m_instance(NULL), | |
62 m_location(NULL), | |
63 m_point(attached_point) { | |
64 } | |
65 GenericRendererNode::~GenericRendererNode() { | |
66 } | |
67 | |
68 void GenericRendererNode::setAttached(Instance* attached_instance, const Location &relative_location, const Point &relative_point) { | |
25
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
69 m_instance = attached_instance; |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
70 m_location = relative_location; |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
71 m_point = relative_point; |
23 | 72 } |
73 void GenericRendererNode::setAttached(Instance* attached_instance, const Location &relative_location) { | |
25
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
74 m_instance = attached_instance; |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
75 m_location = relative_location; |
23 | 76 } |
77 void GenericRendererNode::setAttached(Instance* attached_instance, const Point &relative_point) { | |
25
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
78 m_instance = attached_instance; |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
79 m_point = relative_point; |
23 | 80 } |
81 void GenericRendererNode::setAttached(Instance* attached_instance) { | |
25
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
82 m_instance = attached_instance; |
23 | 83 } |
84 void GenericRendererNode::setAttached(const Location &attached_location, const Point &relative_point) { | |
25
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
85 m_instance = NULL; |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
86 m_location = attached_location; |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
87 m_point = relative_point; |
23 | 88 } |
89 void GenericRendererNode::setAttached(const Location &attached_location) { | |
25
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
90 m_instance = NULL; |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
91 m_location = attached_location; |
23 | 92 } |
93 void GenericRendererNode::setAttached(const Point &attached_point) { | |
25
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
94 m_instance = NULL; |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
95 m_location = NULL; |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
96 m_point = attached_point; |
23 | 97 } |
98 | |
99 void GenericRendererNode::setRelative(const Location &relative_location) { | |
25
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
100 if(m_instance == NULL) { |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
101 throw NotSupported("No instance attached."); |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
102 } |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
103 m_location = relative_location; |
23 | 104 } |
105 void GenericRendererNode::setRelative(const Location &relative_location, Point relative_point) { | |
25
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
106 if(m_instance == NULL) { |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
107 throw NotSupported("No instance attached."); |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
108 } |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
109 m_location = relative_location; |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
110 m_point = relative_point; |
23 | 111 } |
112 void GenericRendererNode::setRelative(const Point &relative_point) { | |
25
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
113 if(m_instance == NULL or m_location == NULL) { |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
114 throw NotSupported("No instance or location attached."); |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
115 } |
fc6713d72b80
add setter for nodes
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
24
diff
changeset
|
116 m_point = relative_point; |
23 | 117 } |
118 | |
119 Instance* GenericRendererNode::getAttachedInstance() { | |
120 if(m_instance == NULL) { | |
24
cb87d40e9342
fix exceptions and cleanup code
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
23
diff
changeset
|
121 throw NotSupported("No instance attached."); |
23 | 122 } |
123 return m_instance; | |
124 } | |
125 Location* GenericRendererNode::getAttachedLocation() { | |
126 if(m_instance != NULL or m_location == NULL) { | |
24
cb87d40e9342
fix exceptions and cleanup code
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
23
diff
changeset
|
127 throw NotSupported("No location attached."); |
23 | 128 } |
129 return m_location; | |
130 } | |
131 Point GenericRendererNode::getAttachedPoint() { | |
132 if(m_instance != NULL or m_location != NULL) { | |
24
cb87d40e9342
fix exceptions and cleanup code
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
23
diff
changeset
|
133 throw NotSupported("No point attached."); |
23 | 134 } |
135 return m_point; | |
136 } | |
137 | |
138 Location* GenericRendererNode::getOffsetLocation() { | |
24
cb87d40e9342
fix exceptions and cleanup code
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
23
diff
changeset
|
139 if(m_instance == NULL or m_location == NULL) { |
cb87d40e9342
fix exceptions and cleanup code
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
23
diff
changeset
|
140 throw NotSupported("No location as offset used."); |
23 | 141 } |
142 return m_location; | |
143 } | |
144 Point GenericRendererNode::getOffsetPoint() { | |
145 if(m_instance == NULL and m_location == NULL) { | |
24
cb87d40e9342
fix exceptions and cleanup code
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
23
diff
changeset
|
146 throw NotSupported("No point as offset used."); |
23 | 147 } |
148 return m_point; | |
149 } | |
150 | |
151 Instance* GenericRendererNode::getInstance() { | |
152 return m_instance; | |
153 } | |
154 Location* GenericRendererNode::getLocation() { | |
155 return m_location; | |
156 } | |
157 Point GenericRendererNode::getPoint() { | |
158 return m_point; | |
159 } | |
160 | |
161 Point GenericRendererNode::getCalculatedPoint(Camera* cam, Layer* layer, std::vector<Instance*>& instances) { | |
162 ScreenPoint p; | |
163 if(m_instance != NULL) { | |
164 if(m_location != NULL) { | |
165 p = cam->toScreenCoordinates(m_instance->getLocationRef().getMapCoordinates() + m_location->getMapCoordinates()); | |
166 } | |
167 else { | |
168 p = cam->toScreenCoordinates(m_instance->getLocation().getMapCoordinates()); | |
169 } | |
170 } | |
171 else if(m_location != NULL) { | |
172 p = cam->toScreenCoordinates(m_location->getMapCoordinates()); | |
173 } | |
174 else | |
175 return m_point; | |
176 return Point(m_point.x + p.x, m_point.y + p.y); | |
177 } | |
178 | |
179 GenericRendererLineInfo::GenericRendererLineInfo(GenericRendererNode n1, GenericRendererNode n2, uint8_t r, uint8_t g, uint8_t b): | |
180 GenericRendererElementInfo(), | |
181 n1(n1), | |
182 n2(n2), | |
183 r(r), | |
184 g(g), | |
185 b(b) { | |
186 } | |
187 void GenericRendererLineInfo::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances, RenderBackend* renderbackend) { | |
188 Point p1 = n1.getCalculatedPoint(cam, layer, instances); | |
189 Point p2 = n2.getCalculatedPoint(cam, layer, instances); | |
190 renderbackend->drawLine(p1, p2, r, g, b); | |
191 } | |
192 | |
193 GenericRendererPointInfo::GenericRendererPointInfo(GenericRendererNode n, uint8_t r, uint8_t g, uint8_t b): | |
194 GenericRendererElementInfo(), | |
195 n(n), | |
196 r(r), | |
197 g(g), | |
198 b(b) { | |
199 } | |
200 void GenericRendererPointInfo::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances, RenderBackend* renderbackend) { | |
201 Point p = n.getCalculatedPoint(cam, layer, instances); | |
202 renderbackend->putPixel(p.x, p.y, r, g, b); | |
203 } | |
204 | |
205 GenericRendererQuadInfo::GenericRendererQuadInfo(GenericRendererNode n1, GenericRendererNode n2, GenericRendererNode n3, GenericRendererNode n4, uint8_t r, uint8_t g, uint8_t b): | |
206 GenericRendererElementInfo(), | |
207 n1(n1), | |
208 n2(n2), | |
209 n3(n3), | |
210 n4(n4), | |
211 r(r), | |
212 g(g), | |
213 b(b) { | |
214 } | |
215 void GenericRendererQuadInfo::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances, RenderBackend* renderbackend) { | |
216 Point p1 = n1.getCalculatedPoint(cam, layer, instances); | |
217 Point p2 = n2.getCalculatedPoint(cam, layer, instances); | |
218 Point p3 = n3.getCalculatedPoint(cam, layer, instances); | |
219 Point p4 = n4.getCalculatedPoint(cam, layer, instances); | |
220 renderbackend->drawQuad(p1, p2, p3, p4, r, g, b); | |
221 } | |
222 | |
223 GenericRendererImageInfo::GenericRendererImageInfo(GenericRendererNode n, int image): | |
224 GenericRendererElementInfo(), | |
225 n(n), | |
226 image(image) { | |
227 } | |
228 void GenericRendererImageInfo::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances, RenderBackend* renderbackend) { | |
229 return; | |
230 } | |
231 | |
232 GenericRendererAnimationInfo::GenericRendererAnimationInfo(GenericRendererNode n, int animation): | |
233 GenericRendererElementInfo(), | |
234 n(n), | |
235 animation(animation) { | |
236 } | |
237 void GenericRendererAnimationInfo::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances, RenderBackend* renderbackend) { | |
238 return; | |
239 } | |
240 | |
241 GenericRendererTextInfo::GenericRendererTextInfo(GenericRendererNode n, AbstractFont* font, std::string text): | |
242 GenericRendererElementInfo(), | |
243 n(n), | |
244 font(font), | |
245 text(text) { | |
246 } | |
247 void GenericRendererTextInfo::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances, RenderBackend* renderbackend) { | |
248 return; | |
249 } | |
250 | |
251 GenericRenderer* GenericRenderer::getInstance(IRendererContainer* cnt) { | |
252 return dynamic_cast<GenericRenderer*>(cnt->getRenderer("GenericRenderer")); | |
253 } | |
254 | |
255 GenericRenderer::GenericRenderer(RenderBackend* renderbackend, int position): | |
256 RendererBase(renderbackend, position), | |
257 m_infos() { | |
258 setEnabled(false); | |
259 } | |
260 | |
261 GenericRenderer::GenericRenderer(const GenericRenderer& old): | |
262 RendererBase(old) { | |
263 setEnabled(false); | |
264 } | |
265 | |
266 RendererBase* GenericRenderer::clone() { | |
267 return new GenericRenderer(*this); | |
268 } | |
269 | |
270 GenericRenderer::~GenericRenderer() { | |
271 } | |
272 void GenericRenderer::addLine(GenericRendererNode n1, GenericRendererNode n2, uint8_t r, uint8_t g, uint8_t b) { | |
273 GenericRendererElementInfo* info = new GenericRendererLineInfo(n1, n2, r, g, b); | |
274 m_infos.push_back(info); | |
275 } | |
276 void GenericRenderer::addPoint(GenericRendererNode n, uint8_t r, uint8_t g, uint8_t b) { | |
277 GenericRendererElementInfo* info = new GenericRendererPointInfo(n, r, g, b); | |
278 m_infos.push_back(info); | |
279 } | |
280 void GenericRenderer::addQuad(GenericRendererNode n1, GenericRendererNode n2, GenericRendererNode n3, GenericRendererNode n4, uint8_t r, uint8_t g, uint8_t b) { | |
281 GenericRendererElementInfo* info = new GenericRendererQuadInfo(n1, n2, n3, n4, r, g, b); | |
282 m_infos.push_back(info); | |
283 } | |
284 void GenericRenderer::addText(GenericRendererNode n, AbstractFont* font, std::string text) { | |
285 GenericRendererElementInfo* info = new GenericRendererTextInfo(n, font, text); | |
286 m_infos.push_back(info); | |
287 } | |
288 void GenericRenderer::addImage(GenericRendererNode n, int image) { | |
289 GenericRendererElementInfo* info = new GenericRendererImageInfo(n, image); | |
290 m_infos.push_back(info); | |
291 } | |
292 void GenericRenderer::addAnimation(GenericRendererNode n, int animation) { | |
293 GenericRendererElementInfo* info = new GenericRendererAnimationInfo(n, animation); | |
294 m_infos.push_back(info); | |
295 } | |
296 void GenericRenderer::removeAll() { | |
24
cb87d40e9342
fix exceptions and cleanup code
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
23
diff
changeset
|
297 std::vector<GenericRendererElementInfo*>::const_iterator info_it = m_infos.begin(); |
cb87d40e9342
fix exceptions and cleanup code
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
23
diff
changeset
|
298 for (;info_it != m_infos.end(); ++info_it) { |
cb87d40e9342
fix exceptions and cleanup code
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
23
diff
changeset
|
299 delete *info_it; |
cb87d40e9342
fix exceptions and cleanup code
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
23
diff
changeset
|
300 } |
23 | 301 m_infos.clear(); |
302 } | |
303 | |
304 void GenericRenderer::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances) { | |
305 std::vector<GenericRendererElementInfo*>::const_iterator info_it = m_infos.begin(); | |
306 for (;info_it != m_infos.end(); ++info_it) { | |
307 GenericRendererElementInfo* info = *info_it; | |
308 info->render(cam, layer, instances, m_renderbackend); | |
309 } | |
310 } | |
311 } |