FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
cellrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 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_fowEnabled = false;
62  m_pathVisualEnabled = false;
64  m_fowLayer = NULL;
65  m_font = NULL;
66 
67  }
68 
70  RendererBase(old),
73  setEnabled(false);
74  m_blockingEnabled = false;
75  m_fowEnabled = false;
76  m_pathVisualEnabled = false;
78  m_fowLayer = NULL;
79  m_font = NULL;
80  }
81 
83  return new CellRenderer(*this);
84  }
85 
87  delete m_targetRenderer;
88  }
89 
91  return dynamic_cast<CellRenderer*>(cnt->getRenderer("CellRenderer"));
92  }
93 
94  std::string CellRenderer::getName() {
95  return "CellRenderer";
96  }
97 
98  void CellRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
99  CellGrid* cg = layer->getCellGrid();
100  if (!cg) {
101  FL_WARN(_log, "No cellgrid assigned to layer, cannot draw grid");
102  return;
103  }
104  CellCache* cache = layer->getCellCache();
105  if (!cache) {
106  FL_WARN(_log, "No cellcache on layer created, cannot draw cells");
107  return;
108  }
109 
110  const bool fow = m_fowEnabled && (m_fowLayer == layer);
111  bool fow_update = fow && (cam->isUpdated() || cache->isUpdated());
112  if (!m_fowImage.get() && fow) {
113  createFowMap(cam, layer);
114  }
115 
116  const bool render_costs = (!m_visualCosts.empty() && m_font);
117  const bool zoomed = !Mathd::Equal(1.0, cam->getZoom());
118 
119  Rect layerView = cam->getLayerViewPort(layer);
120  std::vector<Cell*> cells = cache->getCellsInRect(layerView);
121  std::vector<Cell*>::iterator cit = cells.begin();
122  for (; cit != cells.end(); ++cit) {
123  if (m_blockingEnabled) {
124  if ((*cit)->getCellType() != CTYPE_NO_BLOCKER) {
125  std::vector<ExactModelCoordinate> vertices;
126  cg->getVertices(vertices, (*cit)->getLayerCoordinates());
127  std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
128  int32_t halfind = vertices.size() / 2;
129  ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
130  Point pt1(firstpt.x, firstpt.y);
131  Point pt2;
132  ++it;
133  for (; it != vertices.end(); it++) {
134  ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
135  pt2.x = pts.x;
136  pt2.y = pts.y;
138  pt1 = pt2;
139  }
140  m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_blockerColor.r, m_blockerColor.g, m_blockerColor.b);
141  ScreenPoint spt1 = cam->toScreenCoordinates(cg->toMapCoordinates(vertices[0]));
142  Point pt3(spt1.x, spt1.y);
143  ScreenPoint spt2 = cam->toScreenCoordinates(cg->toMapCoordinates(vertices[halfind]));
144  Point pt4(spt2.x, spt2.y);
146  }
147  }
148 
149  if (render_costs) {
150  bool match = false;
151  double cost;
152  std::set<std::string>::iterator cost_it = m_visualCosts.begin();
153  for (; cost_it != m_visualCosts.end(); ++cost_it) {
154  std::vector<std::string> cell_costs = cache->getCellCosts(*cit);
155  std::vector<std::string>::iterator cc_it = cell_costs.begin();
156  for (; cc_it != cell_costs.end(); ++cc_it) {
157  if (*cc_it == *cost_it) {
158  match = true;
159  cost = cache->getCost(*cost_it);
160  break;
161  }
162  }
163  if (match) {
164  break;
165  }
166  }
167  if (match) {
168  Location loc(layer);
169  loc.setLayerCoordinates((*cit)->getLayerCoordinates());
171 
172  std::stringstream stream;
173  stream << cost;
174  Image* img = m_font->getAsImage(stream.str());
175 
176  Rect r;
177  if (zoomed) {
178  double zoom = cam->getZoom();
179  r.x = drawpt.x - (img->getWidth()/2) * zoom;
180  r.y = drawpt.y - (img->getHeight()/2) * zoom;
181  r.w = img->getWidth() * zoom;
182  r.h = img->getHeight() * zoom;
183  img->render(r);
184  } else {
185  r.x = drawpt.x - img->getWidth()/2;
186  r.y = drawpt.y - img->getHeight()/2;
187  r.w = img->getWidth();
188  r.h = img->getHeight();
189  img->render(r);
190  }
191  }
192  }
193 
194  if (fow_update || (fow && !m_targetRenderer)) {
196  FIFE::intPt2doublePt((*cit)->getLayerCoordinates())));
197  CellVisualEffect cve = (*cit)->getFoWType();
198  if (cve == CELLV_CONCEALED) {
199  if (m_concealImage.get()) {
200  addImageToMap(cam, Point(sp.x, sp.y), m_concealImage, "c_img");
201  }
202  } else if (cve == CELLV_MASKED) {
203  if (m_maskImage.get()) {
204  addImageToMap(cam, Point(sp.x, sp.y), m_maskImage, "b_img");
205  }
206  }
207  }
208  }
209 
210  if (m_pathVisualEnabled && !m_visualPaths.empty()) {
211  std::vector<Instance*>::iterator it = m_visualPaths.begin();
212  for (; it != m_visualPaths.end(); ++it) {
213  Route* route = (*it)->getRoute();
214  if (route) {
215  Path path = route->getPath();
216  if (!path.empty()) {
217  Path::iterator pit = path.begin();
218  for (; pit != path.end(); ++pit) {
219  if ((*pit).getLayer() != layer) {
220  continue;
221  }
222  std::vector<ExactModelCoordinate> vertices;
223  cg->getVertices(vertices, (*pit).getLayerCoordinates());
224  std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
225  int32_t halfind = vertices.size() / 2;
226  ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
227  Point pt1(firstpt.x, firstpt.y);
228  Point pt2;
229  ++it;
230  for (; it != vertices.end(); it++) {
231  ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
232  pt2.x = pts.x;
233  pt2.y = pts.y;
235  pt1 = pt2;
236  }
237  m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_pathColor.r, m_pathColor.g, m_pathColor.b);
238  }
239  }
240  }
241  }
242  }
243 
244  if (fow && m_targetRenderer) {
245  if (fow_update) {
246  updateFowMap();
247  }
248  m_fowImage.get()->render(cam->getViewPort());
249  }
250  }
251 
253  m_blockerColor.r = r;
254  m_blockerColor.g = g;
255  m_blockerColor.b = b;
256  }
257 
259  m_pathColor.r = r;
260  m_pathColor.g = g;
261  m_pathColor.b = b;
262  }
263 
265  m_fowLayer = layer;
266  }
267 
268  void CellRenderer::setEnabledBlocking(bool enabled) {
269  m_blockingEnabled = enabled;
270  }
271 
273  return m_blockingEnabled;
274  }
275 
276  void CellRenderer::setEnabledFogOfWar(bool enabled) {
277  m_fowEnabled = enabled;
278  }
279 
281  return m_fowEnabled;
282  }
283 
285  m_pathVisualEnabled = enabled;
286  }
287 
289  return m_pathVisualEnabled;
290  }
291 
293  if (!m_targetRenderer) {
294  return;
295  }
296  Rect view = cam->getViewPort();
297  m_fowImage = ImageManager::instance()->loadBlank("virtual_fow_map", view.w, view.h);
299  m_targetRenderer->setRenderTarget("virtual_fow_map", true, 0);
300  Point n1(0, 0);
301  Point n2(0, view.h);
302  Point n3(view.w, view.h);
303  Point n4(view.w, 0);
304  m_fowTarget->addQuad("a_quad", n1, n2, n3, n4, 0, 0, 0, 0);
305  }
306 
307 
309  m_concealImage = image;
310  }
311 
313  m_maskImage = image;
314  }
315 
316  void CellRenderer::addImageToMap(Camera* cam, Point center, ImagePtr image, const std::string& id) {
317  double zoom = cam->getZoom();
318  bool zoomed = !Mathd::Equal(zoom, 1.0);
319  bool straightZoom = Mathd::Equal(fmod(zoom, 1.0), 0.0);
320  if (m_targetRenderer) {
321  if (!zoomed) {
322  m_fowTarget->addImage(id, center, image);
323  } else {
324  int32_t w = round(image->getWidth() * zoom);
325  int32_t h = round(image->getHeight() * zoom);
326  m_fowTarget->resizeImage(id, center, image, w, h);
327  }
328  } else {
329  int32_t w = image->getWidth();
330  int32_t h = image->getHeight();
331  if (zoomed) {
332  w = round(w * zoom);
333  h = round(h * zoom);
334  }
335  Rect dimensions(center.x, center.y, w, h);
336  image->render(dimensions);
337  }
338  }
339 
341  return m_fowLayer;
342  }
343 
345  m_font = font;
346  }
347 
349  return m_font;
350  }
351 
353  m_targetRenderer->setRenderTarget("virtual_fow_map", true, 0);
355  m_fowTarget->removeAll("b_img");
356  m_fowTarget->removeAll("c_img");
357  }
358 
360  m_visualPaths.push_back(instance);
361  }
362 
364  for (std::vector<Instance*>::iterator it = m_visualPaths.begin();
365  it != m_visualPaths.end(); ++it) {
366 
367  if (*it == instance) {
368  m_visualPaths.erase(it);
369  break;
370  }
371  }
372  }
373 
374  void CellRenderer::setEnabledCost(const std::string& costId, bool enabled) {
375  if (enabled) {
376  m_visualCosts.insert(costId);
377  } else {
378  m_visualCosts.erase(costId);
379  }
380  }
381 
382  bool CellRenderer::isEnabledCost(const std::string& costId) {
383  std::set<std::string>::iterator it = m_visualCosts.find(costId);
384  if (it != m_visualCosts.end()) {
385  return true;
386  }
387  return false;
388  }
389 }
std::vector< Instance * > m_visualPaths
Definition: cellrenderer.h:232
#define FL_WARN(logger, msg)
Definition: logger.h:72
Abstract interface for all the renderbackends.
virtual ImagePtr loadBlank(uint32_t width, uint32_t height)
Loads a blank resource.
static CellRenderer * getInstance(IRendererContainer *cnt)
Gets instance for interface access.
T * get() const
allows direct access to underlying pointer
Definition: sharedptr.h:155
void addPathVisual(Instance *instance)
Adds a instance to path visualization.
void createFowMap(Camera *cam, Layer *layer)
Creates render target for Fog of War.
bool isUpdated()
Gets whether the CellCache need to be updated.
Definition: cellcache.cpp:862
void setLayerCoordinates(const ModelCoordinate &coordinates)
Sets "cell precise" layer coordinates to this location.
Definition: location.cpp:94
void addQuad(const std::string &group, Point n1, Point n2, Point n3, Point n4, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
void setFogOfWarLayer(Layer *layer)
Sets the layer that is used for Fog of War visualization.
std::vector< RenderItem * > RenderList
Definition: renderitem.h:130
void setConcealImage(ImagePtr image)
Sets the image that is used for concealed spaces.
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
void resizeImage(const std::string &group, Point n, ImagePtr image, int32_t width, int32_t height)
std::vector< std::string > getCellCosts(Cell *cell)
Returns cost identifiers for cell.
Definition: cellcache.cpp:1080
void setRenderTarget(const std::string &targetname, bool discard, int32_t ndraws=0)
std::set< std::string > m_visualCosts
Definition: cellrenderer.h:233
T x
The X Coordinate.
Definition: rect.h:84
bool isEnabledFogOfWar()
Gets whether Fog of War visualization is enabled.
std::vector< Cell * > getCellsInRect(const Rect &rec)
Returns all cells in the rect.
Definition: cellcache.cpp:883
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.
bool isFramebufferEnabled() const
static Logger _log(LM_AUDIO)
void addImage(const std::string &group, Point n, ImagePtr image)
void setMaskImage(ImagePtr image)
Sets the fog image.
IFont * getFont()
Returns the font.
void setEnabledFogOfWar(bool enabled)
Enables Fog of War visualization.
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
static ImageManager * instance()
Definition: singleton.h:84
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.
uint32_t getHeight() const
Definition: image.cpp:160
RenderBackend * m_renderbackend
Definition: rendererbase.h:171
unsigned char uint8_t
Definition: core.h:38
PointType2D< int32_t > Point
Definition: point.h:195
const Rect & getViewPort() const
Gets the viewport for camera in pixel coordinates.
Definition: camera.cpp:304
ImagePtr m_concealImage
Definition: cellrenderer.h:227
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
double getZoom() const
Gets camera zoom.
Definition: camera.cpp:172
TargetRenderer * m_targetRenderer
Definition: cellrenderer.h:224
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.
uint32_t getWidth() const
Definition: image.cpp:151
void setEnabledPathVisual(bool enabled)
Enables path visualization.
T y
The Y Coordinate.
Definition: rect.h:87
void setEnabledBlocking(bool enabled)
Enables blocking visualization.
uint8_t CellVisualEffect
Definition: cell.h:78
CellGrid * getCellGrid() const
Get the Cellgrid.
Definition: layer.cpp:93
Layer * getFowLayer()
Returns the layer that is used for Fog of War visualization.
void addImageToMap(Camera *cam, Point center, ImagePtr image, const std::string &id)
Adds image to FoW render target.
Path getPath()
Returns the path.
Definition: route.cpp:177
bool isUpdated()
Returns true if camera view has been updated, otherwise false.
Definition: camera.h:335
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:428
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.
DoublePoint intPt2doublePt(Point pt)
Convert from 2D int32_t point to 2D double point.
Definition: point.h:349
SDL_Color m_blockerColor
Definition: cellrenderer.h:218
bool isEnabledBlocking()
Gets whether blocking visualization is enabled.
RenderTargetPtr m_fowTarget
Definition: cellrenderer.h:225
void updateFowMap()
Updates render target.
RendererBase * clone()
Makes copy of this renderer.
double getCost(const std::string &costId)
Returns the cost value for the given id.
Definition: cellcache.cpp:991
SDL_Color m_pathColor
Definition: cellrenderer.h:219
ExactModelCoordinate toMapCoordinates(const ModelCoordinate &layer_coords)
Transforms given point from layer coordinates to map coordinates.
Definition: cellgrid.cpp:75
RenderTargetPtr createRenderTarget(const std::string &name, uint32_t width, uint32_t height)
Creates render target.
T w
Width of the rectangle.
Definition: rect.h:90
An Instance is an "instantiation" of an Object at a Location.
Definition: instance.h:101
ExactModelCoordinate getMapCoordinates() const
Gets map coordinates set to this location.
Definition: location.cpp:117
bool isEnabledPathVisual()
Gets whether path visualization is enabled.
void removeAll(const std::string &group)
Rect getLayerViewPort(Layer *layer)
Gets the viewport for camera in layer coordinates.
Definition: camera.cpp:341
virtual ~CellRenderer()
Destructor.