FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
blockinginforenderer.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 "util/math/fife_math.h"
32 #include "util/log/logger.h"
35 #include "model/structures/layer.h"
37 #include "model/structures/cell.h"
39 
40 #include "view/camera.h"
41 #include "blockinginforenderer.h"
42 
43 
44 namespace FIFE {
48  static Logger _log(LM_VIEWVIEW);
49 
50  BlockingInfoRenderer::BlockingInfoRenderer(RenderBackend* renderbackend, int32_t position):
51  RendererBase(renderbackend, position) {
52  setEnabled(false);
53  m_color.r = 0;
54  m_color.g = 255;
55  m_color.b = 0;
56  }
57 
59  RendererBase(old),
60  m_color(old.m_color) {
61  setEnabled(false);
62  }
63 
65  return new BlockingInfoRenderer(*this);
66  }
67 
69  }
70 
72  return dynamic_cast<BlockingInfoRenderer*>(cnt->getRenderer("BlockingInfoRenderer"));
73  }
74 
75  void BlockingInfoRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
76  CellGrid* cg = layer->getCellGrid();
77  if (!cg) {
78  FL_WARN(_log, "No cellgrid assigned to layer, cannot draw grid");
79  return;
80  }
81 
82  Rect cv = cam->getViewPort();
83  CellCache* cache = layer->getCellCache();
84  if (cache) {
85  const std::vector<std::vector<Cell*> >& cells = cache->getCells();
86  std::vector<std::vector<Cell*> >::const_iterator it = cells.begin();
87  for (; it != cells.end(); ++it) {
88  std::vector<Cell*>::const_iterator cit = (*it).begin();
89  for (; cit != (*it).end(); ++cit) {
90  ExactModelCoordinate emc = FIFE::intPt2doublePt((*cit)->getLayerCoordinates());
92  // if it is not in cameras view continue
93  if (sp.x < cv.x || sp.x > cv.x + cv.w ||
94  sp.y < cv.y || sp.y > cv.y + cv.h) {
95  continue;
96  }
97  if ((*cit)->getCellType() != CTYPE_NO_BLOCKER) {
98  std::vector<ExactModelCoordinate> vertices;
99  cg->getVertices(vertices, (*cit)->getLayerCoordinates());
100  std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
101  int32_t halfind = vertices.size() / 2;
102  ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
103  Point pt1(firstpt.x, firstpt.y);
104  Point pt2;
105  ++it;
106  for (; it != vertices.end(); it++) {
107  ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
108  pt2.x = pts.x;
109  pt2.y = pts.y;
110  m_renderbackend->drawLine(pt1, pt2, m_color.r, m_color.g, m_color.b);
111  pt1 = pt2;
112  }
113  m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_color.r, m_color.g, m_color.b);
114  ScreenPoint spt1 = cam->toScreenCoordinates(cg->toMapCoordinates(vertices[0]));
115  Point pt3(spt1.x, spt1.y);
116  ScreenPoint spt2 = cam->toScreenCoordinates(cg->toMapCoordinates(vertices[halfind]));
117  Point pt4(spt2.x, spt2.y);
118  m_renderbackend->drawLine(pt3, pt4, m_color.r, m_color.g, m_color.b);
119  }
120  }
121  }
122  } else {
123  RenderList::const_iterator instance_it = instances.begin();
124  for (;instance_it != instances.end(); ++instance_it) {
125  Instance* instance = (*instance_it)->instance;
126  if (!instance->getObject()->isBlocking() || !instance->isBlocking()) {
127  continue;
128  }
129  std::vector<ExactModelCoordinate> vertices;
130  cg->getVertices(vertices, instance->getLocationRef().getLayerCoordinates());
131  std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
132  int32_t halfind = vertices.size() / 2;
133  ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
134  Point pt1(firstpt.x, firstpt.y);
135  Point pt2;
136  ++it;
137  for (; it != vertices.end(); it++) {
138  ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
139  pt2.x = pts.x;
140  pt2.y = pts.y;
141  m_renderbackend->drawLine(pt1, pt2, m_color.r, m_color.g, m_color.b);
142  pt1 = pt2;
143  }
144  m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_color.r, m_color.g, m_color.b);
145  ScreenPoint spt1 = cam->toScreenCoordinates(cg->toMapCoordinates(vertices[0]));
146  Point pt3(spt1.x, spt1.y);
147  ScreenPoint spt2 = cam->toScreenCoordinates(cg->toMapCoordinates(vertices[halfind]));
148  Point pt4(spt2.x, spt2.y);
149  m_renderbackend->drawLine(pt3, pt4, m_color.r, m_color.g, m_color.b);
150  }
151  }
152  }
153 
155  m_color.r = r;
156  m_color.g = g;
157  m_color.b = b;
158  }
159 }
#define FL_WARN(logger, msg)
Definition: logger.h:72
Abstract interface for all the renderbackends.
SDL_Color m_color
currently used color
std::vector< RenderItem * > RenderList
Definition: renderitem.h:130
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...
T h
Height of the rectangle.
Definition: rect.h:93
T x
The X Coordinate.
Definition: rect.h:84
bool isBlocking() const
Gets if instance blocks movement.
Definition: instance.cpp:352
void setColor(uint8_t r, uint8_t g, uint8_t b)
Changes the used color.
A CellCache is an abstract depiction of one or a few layers and contains additional information...
Definition: cellcache.h:111
BlockingInfoRenderer(RenderBackend *renderbackend, int32_t position)
Constructor.
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
static Logger _log(LM_AUDIO)
static BlockingInfoRenderer * getInstance(IRendererContainer *cnt)
Gets instance for interface access.
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
Location & getLocationRef()
Gets reference of current location of instance.
Definition: instance.cpp:318
ModelCoordinate getLayerCoordinates() const
Gets cell precision layer coordinates set to this location.
Definition: location.cpp:113
virtual void getVertices(std::vector< ExactModelCoordinate > &vtx, const ModelCoordinate &cell)=0
Fills given point vector with vertices from selected cell.
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
Base class for all view renderers View renderer renders one aspect of the view shown on screen...
Definition: rendererbase.h:78
Object * getObject()
Gets object where this instance is instantiated from.
Definition: instance.cpp:295
A basic layer on a map.
Definition: layer.h:99
virtual ~BlockingInfoRenderer()
Destructor.
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.
T y
The Y Coordinate.
Definition: rect.h:87
CellGrid * getCellGrid() const
Get the Cellgrid.
Definition: layer.cpp:93
ScreenPoint toScreenCoordinates(const ExactModelCoordinate &map_coords)
Transforms given point from map coordinates to screen coordinates.
Definition: camera.cpp:428
DoublePoint intPt2doublePt(Point pt)
Convert from 2D int32_t point to 2D double point.
Definition: point.h:349
const std::vector< std::vector< Cell * > > & getCells()
Returns all cells of this CellCache.
Definition: cellcache.cpp:717
RendererBase * clone()
Makes copy of this renderer.
ExactModelCoordinate toMapCoordinates(const ModelCoordinate &layer_coords)
Transforms given point from layer coordinates to map coordinates.
Definition: cellgrid.cpp:75
bool isBlocking() const
Gets if object blocks movement.
Definition: object.cpp:223
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