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) {
|
|
69 }
|
|
70 void GenericRendererNode::setAttached(Instance* attached_instance, const Location &relative_location) {
|
|
71 }
|
|
72 void GenericRendererNode::setAttached(Instance* attached_instance, const Point &relative_point) {
|
|
73 }
|
|
74 void GenericRendererNode::setAttached(Instance* attached_instance) {
|
|
75 }
|
|
76 void GenericRendererNode::setAttached(const Location &attached_location, const Point &relative_point) {
|
|
77 }
|
|
78 void GenericRendererNode::setAttached(const Location &attached_location) {
|
|
79 }
|
|
80 void GenericRendererNode::setAttached(const Point &attached_point) {
|
|
81 }
|
|
82
|
|
83 void GenericRendererNode::setRelative(const Location &relative_location) {
|
|
84 }
|
|
85 void GenericRendererNode::setRelative(const Location &relative_location, Point relative_point) {
|
|
86 }
|
|
87 void GenericRendererNode::setRelative(const Point &relative_point) {
|
|
88 }
|
|
89
|
|
90 Instance* GenericRendererNode::getAttachedInstance() {
|
|
91 if(m_instance == NULL) {
|
|
92 throw "asd";
|
|
93 }
|
|
94 return m_instance;
|
|
95 }
|
|
96 Location* GenericRendererNode::getAttachedLocation() {
|
|
97 if(m_instance != NULL or m_location == NULL) {
|
|
98 throw "asd";
|
|
99 }
|
|
100 return m_location;
|
|
101 }
|
|
102 Point GenericRendererNode::getAttachedPoint() {
|
|
103 if(m_instance != NULL or m_location != NULL) {
|
|
104 throw "asd";
|
|
105 }
|
|
106 return m_point;
|
|
107 }
|
|
108
|
|
109 Location* GenericRendererNode::getOffsetLocation() {
|
|
110 if(m_instance == NULL) {
|
|
111 throw "asd";
|
|
112 }
|
|
113 return m_location;
|
|
114 }
|
|
115 Point GenericRendererNode::getOffsetPoint() {
|
|
116 if(m_instance == NULL and m_location == NULL) {
|
|
117 throw "asd";
|
|
118 }
|
|
119 return m_point;
|
|
120 }
|
|
121
|
|
122 Instance* GenericRendererNode::getInstance() {
|
|
123 return m_instance;
|
|
124 }
|
|
125 Location* GenericRendererNode::getLocation() {
|
|
126 return m_location;
|
|
127 }
|
|
128 Point GenericRendererNode::getPoint() {
|
|
129 return m_point;
|
|
130 }
|
|
131
|
|
132 Point GenericRendererNode::getCalculatedPoint(Camera* cam, Layer* layer, std::vector<Instance*>& instances) {
|
|
133 ScreenPoint p;
|
|
134 if(m_instance != NULL) {
|
|
135 if(m_location != NULL) {
|
|
136 p = cam->toScreenCoordinates(m_instance->getLocationRef().getMapCoordinates() + m_location->getMapCoordinates());
|
|
137 }
|
|
138 else {
|
|
139 p = cam->toScreenCoordinates(m_instance->getLocation().getMapCoordinates());
|
|
140 }
|
|
141 }
|
|
142 else if(m_location != NULL) {
|
|
143 p = cam->toScreenCoordinates(m_location->getMapCoordinates());
|
|
144 }
|
|
145 else
|
|
146 return m_point;
|
|
147 return Point(m_point.x + p.x, m_point.y + p.y);
|
|
148 }
|
|
149
|
|
150 GenericRendererLineInfo::GenericRendererLineInfo(GenericRendererNode n1, GenericRendererNode n2, uint8_t r, uint8_t g, uint8_t b):
|
|
151 GenericRendererElementInfo(),
|
|
152 n1(n1),
|
|
153 n2(n2),
|
|
154 r(r),
|
|
155 g(g),
|
|
156 b(b) {
|
|
157 }
|
|
158 void GenericRendererLineInfo::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances, RenderBackend* renderbackend) {
|
|
159 Point p1 = n1.getCalculatedPoint(cam, layer, instances);
|
|
160 Point p2 = n2.getCalculatedPoint(cam, layer, instances);
|
|
161 renderbackend->drawLine(p1, p2, r, g, b);
|
|
162 }
|
|
163
|
|
164 GenericRendererPointInfo::GenericRendererPointInfo(GenericRendererNode n, uint8_t r, uint8_t g, uint8_t b):
|
|
165 GenericRendererElementInfo(),
|
|
166 n(n),
|
|
167 r(r),
|
|
168 g(g),
|
|
169 b(b) {
|
|
170 }
|
|
171 void GenericRendererPointInfo::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances, RenderBackend* renderbackend) {
|
|
172 Point p = n.getCalculatedPoint(cam, layer, instances);
|
|
173 renderbackend->putPixel(p.x, p.y, r, g, b);
|
|
174 }
|
|
175
|
|
176 GenericRendererQuadInfo::GenericRendererQuadInfo(GenericRendererNode n1, GenericRendererNode n2, GenericRendererNode n3, GenericRendererNode n4, uint8_t r, uint8_t g, uint8_t b):
|
|
177 GenericRendererElementInfo(),
|
|
178 n1(n1),
|
|
179 n2(n2),
|
|
180 n3(n3),
|
|
181 n4(n4),
|
|
182 r(r),
|
|
183 g(g),
|
|
184 b(b) {
|
|
185 }
|
|
186 void GenericRendererQuadInfo::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances, RenderBackend* renderbackend) {
|
|
187 Point p1 = n1.getCalculatedPoint(cam, layer, instances);
|
|
188 Point p2 = n2.getCalculatedPoint(cam, layer, instances);
|
|
189 Point p3 = n3.getCalculatedPoint(cam, layer, instances);
|
|
190 Point p4 = n4.getCalculatedPoint(cam, layer, instances);
|
|
191 renderbackend->drawQuad(p1, p2, p3, p4, r, g, b);
|
|
192 }
|
|
193
|
|
194 GenericRendererImageInfo::GenericRendererImageInfo(GenericRendererNode n, int image):
|
|
195 GenericRendererElementInfo(),
|
|
196 n(n),
|
|
197 image(image) {
|
|
198 }
|
|
199 void GenericRendererImageInfo::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances, RenderBackend* renderbackend) {
|
|
200 return;
|
|
201 }
|
|
202
|
|
203 GenericRendererAnimationInfo::GenericRendererAnimationInfo(GenericRendererNode n, int animation):
|
|
204 GenericRendererElementInfo(),
|
|
205 n(n),
|
|
206 animation(animation) {
|
|
207 }
|
|
208 void GenericRendererAnimationInfo::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances, RenderBackend* renderbackend) {
|
|
209 return;
|
|
210 }
|
|
211
|
|
212 GenericRendererTextInfo::GenericRendererTextInfo(GenericRendererNode n, AbstractFont* font, std::string text):
|
|
213 GenericRendererElementInfo(),
|
|
214 n(n),
|
|
215 font(font),
|
|
216 text(text) {
|
|
217 }
|
|
218 void GenericRendererTextInfo::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances, RenderBackend* renderbackend) {
|
|
219 return;
|
|
220 }
|
|
221
|
|
222 GenericRenderer* GenericRenderer::getInstance(IRendererContainer* cnt) {
|
|
223 return dynamic_cast<GenericRenderer*>(cnt->getRenderer("GenericRenderer"));
|
|
224 }
|
|
225
|
|
226 GenericRenderer::GenericRenderer(RenderBackend* renderbackend, int position):
|
|
227 RendererBase(renderbackend, position),
|
|
228 m_infos() {
|
|
229 setEnabled(false);
|
|
230 }
|
|
231
|
|
232 GenericRenderer::GenericRenderer(const GenericRenderer& old):
|
|
233 RendererBase(old) {
|
|
234 setEnabled(false);
|
|
235 }
|
|
236
|
|
237 RendererBase* GenericRenderer::clone() {
|
|
238 return new GenericRenderer(*this);
|
|
239 }
|
|
240
|
|
241 GenericRenderer::~GenericRenderer() {
|
|
242 }
|
|
243 void GenericRenderer::addLine(GenericRendererNode n1, GenericRendererNode n2, uint8_t r, uint8_t g, uint8_t b) {
|
|
244 GenericRendererElementInfo* info = new GenericRendererLineInfo(n1, n2, r, g, b);
|
|
245 m_infos.push_back(info);
|
|
246 }
|
|
247 void GenericRenderer::addPoint(GenericRendererNode n, uint8_t r, uint8_t g, uint8_t b) {
|
|
248 GenericRendererElementInfo* info = new GenericRendererPointInfo(n, r, g, b);
|
|
249 m_infos.push_back(info);
|
|
250 }
|
|
251 void GenericRenderer::addQuad(GenericRendererNode n1, GenericRendererNode n2, GenericRendererNode n3, GenericRendererNode n4, uint8_t r, uint8_t g, uint8_t b) {
|
|
252 GenericRendererElementInfo* info = new GenericRendererQuadInfo(n1, n2, n3, n4, r, g, b);
|
|
253 m_infos.push_back(info);
|
|
254 }
|
|
255 void GenericRenderer::addText(GenericRendererNode n, AbstractFont* font, std::string text) {
|
|
256 GenericRendererElementInfo* info = new GenericRendererTextInfo(n, font, text);
|
|
257 m_infos.push_back(info);
|
|
258 }
|
|
259 void GenericRenderer::addImage(GenericRendererNode n, int image) {
|
|
260 GenericRendererElementInfo* info = new GenericRendererImageInfo(n, image);
|
|
261 m_infos.push_back(info);
|
|
262 }
|
|
263 void GenericRenderer::addAnimation(GenericRendererNode n, int animation) {
|
|
264 GenericRendererElementInfo* info = new GenericRendererAnimationInfo(n, animation);
|
|
265 m_infos.push_back(info);
|
|
266 }
|
|
267 void GenericRenderer::removeAll() {
|
|
268 m_infos.clear();
|
|
269 }
|
|
270
|
|
271 void GenericRenderer::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances) {
|
|
272 std::vector<GenericRendererElementInfo*>::const_iterator info_it = m_infos.begin();
|
|
273 for (;info_it != m_infos.end(); ++info_it) {
|
|
274 GenericRendererElementInfo* info = *info_it;
|
|
275 info->render(cam, layer, instances, m_renderbackend);
|
|
276 }
|
|
277 }
|
|
278 }
|