Debian Codegraph @

engine/core/view/renderers/cellrenderer.cpp

raw
1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the FIFE team *
3 * http://www.fifengine.net *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2.1 of the License, or (at your option) any later version. *
10 * *
11 * This library 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 GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, 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 "video/image.h"
32 #include "video/imagemanager.h"
33 #include "video/fonts/ifont.h"
34 #include "util/math/fife_math.h"
35 #include "util/log/logger.h"
36 #include "model/metamodel/grids/cellgrid.h"
37 #include "model/structures/instance.h"
38 #include "model/structures/layer.h"
39 #include "model/structures/location.h"
40 #include "model/structures/cell.h"
41 #include "model/structures/cellcache.h"
42 #include "pathfinder/route.h"
43
44 #include "view/camera.h"
45 #include "cellrenderer.h"
46
47
48 namespace FIFE {
49 static Logger _log(LM_VIEWVIEW);
50
51 CellRenderer::CellRenderer(RenderBackend* renderbackend, int32_t position):
52 RendererBase(renderbackend, position) {
53 setEnabled(false);
54 m_blockerColor.r = 255;
55 m_blockerColor.g = 0;
56 m_blockerColor.b = 0;
57 m_pathColor.r = 0;
58 m_pathColor.g = 0;
59 m_pathColor.b = 255;
60 m_blockingEnabled = false;
61 m_pathVisualEnabled = false;
62 m_font = NULL;
63
64 }
65
66 CellRenderer::CellRenderer(const CellRenderer& old):
67 RendererBase(old),
68 m_blockerColor(old.m_blockerColor),
69 m_pathColor(old.m_pathColor){
70 setEnabled(false);
71 m_blockingEnabled = false;
72 m_pathVisualEnabled = false;
73 m_font = NULL;
74 }
75
76 RendererBase* CellRenderer::clone() {
77 return new CellRenderer(*this);
78 }
79
80 CellRenderer::~CellRenderer() {
81 }
82
83 CellRenderer* CellRenderer::getInstance(IRendererContainer* cnt) {
84 return dynamic_cast<CellRenderer*>(cnt->getRenderer("CellRenderer"));
85 }
86
87 std::string CellRenderer::getName() {
88 return "CellRenderer";
89 }
90
91 void CellRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
92 CellGrid* cg = layer->getCellGrid();
93 if (!cg) {
94 FL_WARN(_log, "No cellgrid assigned to layer, cannot draw grid");
95 return;
96 }
97 CellCache* cache = layer->getCellCache();
98 if (!cache) {
99 FL_WARN(_log, "No cellcache on layer created, cannot draw cells");
100 return;
101 }
102
103 const bool render_costs = (!m_visualCosts.empty() && m_font);
104 const bool zoomed = !Mathd::Equal(1.0, cam->getZoom());
105
106 Rect layerView = cam->getLayerViewPort(layer);
107 std::vector<Cell*> cells = cache->getCellsInRect(layerView);
108 std::vector<Cell*>::iterator cit = cells.begin();
109 for (; cit != cells.end(); ++cit) {
110 if (m_blockingEnabled) {
111 if ((*cit)->getCellType() != CTYPE_NO_BLOCKER) {
112 std::vector<ExactModelCoordinate> vertices;
113 cg->getVertices(vertices, (*cit)->getLayerCoordinates());
114 std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
115 int32_t halfind = vertices.size() / 2;
116 ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
117 Point pt1(firstpt.x, firstpt.y);
118 Point pt2;
119 ++it;
120 for (; it != vertices.end(); it++) {
121 ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
122 pt2.x = pts.x;
123 pt2.y = pts.y;
124 m_renderbackend->drawLine(pt1, pt2, m_blockerColor.r, m_blockerColor.g, m_blockerColor.b);
125 pt1 = pt2;
126 }
127 m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_blockerColor.r, m_blockerColor.g, m_blockerColor.b);
128 ScreenPoint spt1 = cam->toScreenCoordinates(cg->toMapCoordinates(vertices[0]));
129 Point pt3(spt1.x, spt1.y);
130 ScreenPoint spt2 = cam->toScreenCoordinates(cg->toMapCoordinates(vertices[halfind]));
131 Point pt4(spt2.x, spt2.y);
132 m_renderbackend->drawLine(pt3, pt4, m_blockerColor.r, m_blockerColor.g, m_blockerColor.b);
133 }
134 }
135
136 if (render_costs) {
137 bool match = false;
138 double cost;
139 std::set<std::string>::iterator cost_it = m_visualCosts.begin();
140 for (; cost_it != m_visualCosts.end(); ++cost_it) {
141 std::vector<std::string> cell_costs = cache->getCellCosts(*cit);
142 std::vector<std::string>::iterator cc_it = cell_costs.begin();
143 for (; cc_it != cell_costs.end(); ++cc_it) {
144 if (*cc_it == *cost_it) {
145 match = true;
146 cost = cache->getCost(*cost_it);
147 break;
148 }
149 }
150 if (match) {
151 break;
152 }
153 }
154 if (match) {
155 Location loc(layer);
156 loc.setLayerCoordinates((*cit)->getLayerCoordinates());
157 ScreenPoint drawpt = cam->toScreenCoordinates(loc.getMapCoordinates());
158
159 std::stringstream stream;
160 stream << cost;
161 Image* img = m_font->getAsImage(stream.str());
162
163 Rect r;
164 if (zoomed) {
165 double zoom = cam->getZoom();
166 r.x = drawpt.x - (img->getWidth()/2) * zoom;
167 r.y = drawpt.y - (img->getHeight()/2) * zoom;
168 r.w = img->getWidth() * zoom;
169 r.h = img->getHeight() * zoom;
170 img->render(r);
171 } else {
172 r.x = drawpt.x - img->getWidth()/2;
173 r.y = drawpt.y - img->getHeight()/2;
174 r.w = img->getWidth();
175 r.h = img->getHeight();
176 img->render(r);
177 }
178 }
179 }
180 }
181
182 if (m_pathVisualEnabled && !m_visualPaths.empty()) {
183 std::vector<Instance*>::iterator it = m_visualPaths.begin();
184 for (; it != m_visualPaths.end(); ++it) {
185 Route* route = (*it)->getRoute();
186 if (route) {
187 Path path = route->getPath();
188 if (!path.empty()) {
189 Path::iterator pit = path.begin();
190 for (; pit != path.end(); ++pit) {
191 if ((*pit).getLayer() != layer) {
192 continue;
193 }
194 std::vector<ExactModelCoordinate> vertices;
195 cg->getVertices(vertices, (*pit).getLayerCoordinates());
196 std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
197 int32_t halfind = vertices.size() / 2;
198 ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
199 Point pt1(firstpt.x, firstpt.y);
200 Point pt2;
201 ++it;
202 for (; it != vertices.end(); it++) {
203 ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
204 pt2.x = pts.x;
205 pt2.y = pts.y;
206 m_renderbackend->drawLine(pt1, pt2, m_pathColor.r, m_pathColor.g, m_pathColor.b);
207 pt1 = pt2;
208 }
209 m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_pathColor.r, m_pathColor.g, m_pathColor.b);
210 }
211 }
212 }
213 }
214 }
215 }
216
217 void CellRenderer::setBlockerColor(uint8_t r, uint8_t g, uint8_t b) {
218 m_blockerColor.r = r;
219 m_blockerColor.g = g;
220 m_blockerColor.b = b;
221 }
222
223 void CellRenderer::setPathColor(uint8_t r, uint8_t g, uint8_t b) {
224 m_pathColor.r = r;
225 m_pathColor.g = g;
226 m_pathColor.b = b;
227 }
228
229 void CellRenderer::setEnabledBlocking(bool enabled) {
230 m_blockingEnabled = enabled;
231 }
232
233 bool CellRenderer::isEnabledBlocking() {
234 return m_blockingEnabled;
235 }
236
237 void CellRenderer::setEnabledPathVisual(bool enabled) {
238 m_pathVisualEnabled = enabled;
239 }
240
241 bool CellRenderer::isEnabledPathVisual() {
242 return m_pathVisualEnabled;
243 }
244
245 void CellRenderer::setFont(IFont* font) {
246 m_font = font;
247 }
248
249 IFont* CellRenderer::getFont() {
250 return m_font;
251 }
252
253 void CellRenderer::addPathVisual(Instance* instance) {
254 m_visualPaths.push_back(instance);
255 }
256
257 void CellRenderer::removePathVisual(Instance* instance) {
258 for (std::vector<Instance*>::iterator it = m_visualPaths.begin();
259 it != m_visualPaths.end(); ++it) {
260
261 if (*it == instance) {
262 m_visualPaths.erase(it);
263 break;
264 }
265 }
266 }
267
268 void CellRenderer::setEnabledCost(const std::string& costId, bool enabled) {
269 if (enabled) {
270 m_visualCosts.insert(costId);
271 } else {
272 m_visualCosts.erase(costId);
273 }
274 }
275
276 bool CellRenderer::isEnabledCost(const std::string& costId) {
277 std::set<std::string>::iterator it = m_visualCosts.find(costId);
278 if (it != m_visualCosts.end()) {
279 return true;
280 }
281 return false;
282 }
283 }