FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
cellrenderer.cpp
Go to the documentation of this file.
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"
38 #include "model/structures/layer.h"
40 #include "model/structures/cell.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 
67  RendererBase(old),
70  setEnabled(false);
71  m_blockingEnabled = false;
72  m_pathVisualEnabled = false;
73  m_font = NULL;
74  }
75 
77  return new CellRenderer(*this);
78  }
79 
81  }
82 
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;
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);
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());
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;
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 
218  m_blockerColor.r = r;
219  m_blockerColor.g = g;
220  m_blockerColor.b = b;
221  }
222 
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 
234  return m_blockingEnabled;
235  }
236 
238  m_pathVisualEnabled = enabled;
239  }
240 
242  return m_pathVisualEnabled;
243  }
244 
246  m_font = font;
247  }
248 
250  return m_font;
251  }
252 
254  m_visualPaths.push_back(instance);
255  }
256 
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 }
std::vector< Instance * > m_visualPaths
Definition: cellrenderer.h:168
#define FL_WARN(logger, msg)
Definition: logger.h:72
Abstract interface for all the renderbackends.
static CellRenderer * getInstance(IRendererContainer *cnt)
Gets instance for interface access.
void addPathVisual(Instance *instance)
Adds a instance to path visualization.
void setLayerCoordinates(const ModelCoordinate &coordinates)
Sets "cell precise" layer coordinates to this location.
Definition: location.cpp:94
std::vector< RenderItem * > RenderList
Definition: renderitem.h:130
Base Class for Images.
Definition: image.h:48
std::list< Location > Path
A path is a list with locations. Each location holds the coordinate for one cell. ...
Definition: ipather.h:38
T h
Height of the rectangle.
Definition: rect.h:93
std::vector< std::string > getCellCosts(Cell *cell)
Returns cost identifiers for cell.
Definition: cellcache.cpp:1089
std::set< std::string > m_visualCosts
Definition: cellrenderer.h:169
T x
The X Coordinate.
Definition: rect.h:84
std::vector< Cell * > getCellsInRect(const Rect &rec)
Returns all cells in the rect.
Definition: cellcache.cpp:873
A CellCache is an abstract depiction of one or a few layers and contains additional information...
Definition: cellcache.h:111
CellCache * getCellCache()
Returns the CellCache of this layer.
Definition: layer.cpp:573
Interface to class owning the renderers Used to get correct subclass of renderer in scripting side (v...
Definition: rendererbase.h:66
virtual Image * getAsImage(const std::string &text)=0
Gets given text as Image The rsulting image is pooled, so it&#39;s not that time critical.
static Logger _log(LM_AUDIO)
IFont * getFont()
Returns the font.
virtual RendererBase * getRenderer(const std::string &renderername)=0
Returns renderer with given name.
Camera describes properties of a view port shown in the main screen Main screen can have multiple cam...
Definition: camera.h:59
A basic route.
Definition: route.h:64
virtual void getVertices(std::vector< ExactModelCoordinate > &vtx, const ModelCoordinate &cell)=0
Fills given point vector with vertices from selected cell.
void setBlockerColor(uint8_t r, uint8_t g, uint8_t b)
Sets color that is used to visualize blocker.
RenderBackend * m_renderbackend
Definition: rendererbase.h:171
unsigned char uint8_t
Definition: core.h:38
PointType2D< int32_t > Point
Definition: point.h:195
uint32_t getHeight() const
Definition: image.cpp:160
static bool Equal(T _val1, T _val2)
Definition: fife_math.h:287
Base class for all view renderers View renderer renders one aspect of the view shown on screen...
Definition: rendererbase.h:78
void removePathVisual(Instance *instance)
Removes a instance from path visualization.
bool isEnabledCost(const std::string &costId)
Gets whether a cost visualization is enabled.
CellRenderer(RenderBackend *renderbackend, int32_t position)
Constructor.
void setEnabledCost(const std::string &costId, bool enabled)
Enables cost visualization.
A basic layer on a map.
Definition: layer.h:99
virtual void drawLine(const Point &p1, const Point &p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws line between given points with given RGBA.
virtual void setEnabled(bool enabled)
Enables renderer.
void setEnabledPathVisual(bool enabled)
Enables path visualization.
T y
The Y Coordinate.
Definition: rect.h:87
void setEnabledBlocking(bool enabled)
Enables blocking visualization.
double getZoom() const
Gets camera zoom.
Definition: camera.cpp:188
Path getPath()
Returns the path.
Definition: route.cpp:177
CellGrid * getCellGrid() const
Get the Cellgrid.
Definition: layer.cpp:93
Pure abstract Font interface.
Definition: ifont.h:43
void setPathColor(uint8_t r, uint8_t g, uint8_t b)
Sets color that is used to visualize paths.
virtual void render(const Rect &rect, uint8_t alpha=255, uint8_t const *rgb=0)=0
Renders itself to the current render target (main screen or attached destination image) at the rectan...
ScreenPoint toScreenCoordinates(const ExactModelCoordinate &map_coords)
Transforms given point from map coordinates to screen coordinates.
Definition: camera.cpp:423
A 3D Point.
Definition: point.h:205
void render(Camera *cam, Layer *layer, RenderList &instances)
This method is called by the view to ask renderer to draw its rendering aspect based on given paramet...
void setFont(IFont *font)
Sets the font.
std::string getName()
Returns the renderer name.
SDL_Color m_blockerColor
Definition: cellrenderer.h:162
bool isEnabledBlocking()
Gets whether blocking visualization is enabled.
ExactModelCoordinate getMapCoordinates() const
Gets map coordinates set to this location.
Definition: location.cpp:117
RendererBase * clone()
Makes copy of this renderer.
double getCost(const std::string &costId)
Returns the cost value for the given id.
Definition: cellcache.cpp:1000
SDL_Color m_pathColor
Definition: cellrenderer.h:163
ExactModelCoordinate toMapCoordinates(const ModelCoordinate &layer_coords)
Transforms given point from layer coordinates to map coordinates.
Definition: cellgrid.cpp:75
T w
Width of the rectangle.
Definition: rect.h:90
An Instance is an "instantiation" of an Object at a Location.
Definition: instance.h:94
bool isEnabledPathVisual()
Gets whether path visualization is enabled.
uint32_t getWidth() const
Definition: image.cpp:151
Rect getLayerViewPort(Layer *layer)
Gets the viewport for camera in layer coordinates.
Definition: camera.cpp:326
virtual ~CellRenderer()
Destructor.