FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
gridrenderer.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 "util/math/fife_math.h"
32 #include "util/log/logger.h"
35 #include "model/structures/layer.h"
37 
38 #include "view/camera.h"
39 #include "gridrenderer.h"
40 
41 
42 namespace FIFE {
46  static Logger _log(LM_VIEWVIEW);
47 
48  GridRenderer::GridRenderer(RenderBackend* renderbackend, int32_t position):
49  RendererBase(renderbackend, position) {
50  setEnabled(false);
51  m_color.r = 0;
52  m_color.g = 255;
53  m_color.b = 0;
54  }
55 
57  RendererBase(old),
58  m_color(old.m_color) {
59  setEnabled(false);
60  }
61 
63  return new GridRenderer(*this);
64  }
65 
67  }
68 
70  return dynamic_cast<GridRenderer*>(cnt->getRenderer("GridRenderer"));
71  }
72 
73  void GridRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
74  CellGrid* cg = layer->getCellGrid();
75  if (!cg) {
76  FL_WARN(_log, "No cellgrid assigned to layer, cannot draw grid");
77  return;
78  }
79 
80 //
81 // //render elev_coord box
82 // //draw front quad
83 // // 1,1,1
84 // //1,-1,1
85 // //-1,-1,1
86 // //-1,1,1
87  //We don't need the elevation box atm
88 // Point a,b,c,d;
89 //
90 //
91 // ScreenPoint copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,1) );
92 // ScreenPoint copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,1) );
93 // Point coptt1(copt1.x,copt1.y);
94 // Point coptt2(copt2.x,copt2.y);
95 // m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 200);
96 // a = coptt1;
97 //
98 // copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,1) );
99 // copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,1) );
100 // coptt1 = Point(copt1.x,copt1.y);
101 // coptt2 = Point(copt2.x,copt2.y);
102 // m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 200);
103 // b = coptt1;
104 //
105 // copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,1) );
106 // copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,1) );
107 // coptt1 = Point(copt1.x,copt1.y);
108 // coptt2 = Point(copt2.x,copt2.y);
109 // m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 200);
110 // c = coptt1;
111 //
112 // copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,1) );
113 // copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,1) );
114 // coptt1 = Point(copt1.x,copt1.y);
115 // coptt2 = Point(copt2.x,copt2.y);
116 // m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 20);
117 // d = coptt1;
118 //
119 // m_renderbackend->drawQuad(a,b,c,d,15, 15, 200);
120 //
121 //
122 // //draw back quad
123 // copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,-1) );
124 // copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,-1) );
125 // coptt1 = Point(copt1.x,copt1.y);
126 // coptt2 = Point(copt2.x,copt2.y);
127 // m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
128 //
129 // copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,-1) );
130 // copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,-1) );
131 // coptt1 = Point(copt1.x,copt1.y);
132 // coptt2 = Point(copt2.x,copt2.y);
133 // m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
134 //
135 // copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,-1) );
136 // copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,-1) );
137 // coptt1 = Point(copt1.x,copt1.y);
138 // coptt2 = Point(copt2.x,copt2.y);
139 // m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
140 //
141 // copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,-1) );
142 // copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,-1) );
143 // coptt1 = Point(copt1.x,copt1.y);
144 // coptt2 = Point(copt2.x,copt2.y);
145 // m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
146 
147 
148  // make the viewport 25% larger
149  Rect cv = cam->getViewPort();
150  int32_t cvx2 = round((cv.x+cv.w) * 1.25);
151  int32_t cvy2 = round((cv.y+cv.h) * 1.25);
152  cv.x -= round((cv.x+cv.w) * 0.125);
153  cv.y -= round((cv.y+cv.h) * 0.125);
154  RenderList::const_iterator instance_it = instances.begin();
155  for (;instance_it != instances.end(); ++instance_it) {
156  Instance* instance = (*instance_it)->instance;
157  std::vector<ExactModelCoordinate> vertices;
158  cg->getVertices(vertices, instance->getLocationRef().getLayerCoordinates());
159  std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
160  ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
161  Point pt1(firstpt.x, firstpt.y);
162  Point pt2;
163  ++it;
164  for (; it != vertices.end(); it++) {
165  ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
166  pt2.x = pts.x;
167  pt2.y = pts.y;
168  Point cpt1 = pt1;
169  Point cpt2 = pt2;
170 
171  if (cpt1.x < cv.x) cpt1.x = cv.x;
172  if (cpt2.x < cv.x) cpt2.x = cv.x;
173  if (cpt1.y < cv.y) cpt1.y = cv.y;
174  if (cpt2.y < cv.y) cpt2.y = cv.y;
175  if (cpt1.x > cvx2) cpt1.x = cvx2;
176  if (cpt2.x > cvx2) cpt2.x = cvx2;
177  if (cpt1.y > cvy2) cpt1.y = cvy2;
178  if (cpt2.y > cvy2) cpt2.y = cvy2;
179 
180  m_renderbackend->drawLine(cpt1, cpt2, m_color.r, m_color.g, m_color.b);
181  pt1 = pt2;
182  }
183  if ((pt2.x >= cv.x) && (pt2.x <= cvx2) && (pt2.y >= cv.y) && (pt2.y <= cvy2)) {
184  if ((firstpt.x >= cv.x) && (firstpt.x <= cvx2) && (firstpt.y >= cv.y) && (firstpt.y <= cvy2)) {
185  m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_color.r, m_color.g, m_color.b);
186  }
187  }
188  }
189  }
190 
191  void GridRenderer::setColor(Uint8 r, Uint8 g, Uint8 b) {
192  m_color.r = r;
193  m_color.g = g;
194  m_color.b = b;
195  }
196 }
#define FL_WARN(logger, msg)
Definition: logger.h:72
Abstract interface for all the renderbackends.
std::vector< RenderItem * > RenderList
Definition: renderitem.h:130
T h
Height of the rectangle.
Definition: rect.h:93
T x
The X Coordinate.
Definition: rect.h:84
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)
virtual ~GridRenderer()
Destructor.
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:315
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
PointType2D< int32_t > Point
Definition: point.h:195
Base class for all view renderers View renderer renders one aspect of the view shown on screen...
Definition: rendererbase.h:78
A basic layer on a map.
Definition: layer.h:99
static GridRenderer * getInstance(IRendererContainer *cnt)
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
const Rect & getViewPort() const
Gets the viewport for camera in pixel coordinates.
Definition: camera.cpp:289
CellGrid * getCellGrid() const
Get the Cellgrid.
Definition: layer.cpp:93
GridRenderer(RenderBackend *renderbackend, int32_t position)
constructor.
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 setColor(Uint8 r, Uint8 g, Uint8 b)
ModelCoordinate getLayerCoordinates() const
Gets cell precision layer coordinates set to this location.
Definition: location.cpp:113
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
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