FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
gridrenderer.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
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"
33 #include "model/metamodel/grids/cellgrid.h"
34 #include "model/structures/instance.h"
35 #include "model/structures/layer.h"
36 #include "model/structures/location.h"
37 
38 #include "view/camera.h"
39 #include "gridrenderer.h"
40 
41 
42 namespace FIFE {
43  static Logger _log(LM_VIEWVIEW);
44 
45  GridRenderer::GridRenderer(RenderBackend* renderbackend, int32_t position):
46  RendererBase(renderbackend, position) {
47  setEnabled(false);
48  m_color.r = 0;
49  m_color.g = 255;
50  m_color.b = 0;
51  }
52 
53  GridRenderer::GridRenderer(const GridRenderer& old):
54  RendererBase(old),
55  m_color(old.m_color) {
56  setEnabled(false);
57  }
58 
59  RendererBase* GridRenderer::clone() {
60  return new GridRenderer(*this);
61  }
62 
63  GridRenderer::~GridRenderer() {
64  }
65 
66  GridRenderer* GridRenderer::getInstance(IRendererContainer* cnt) {
67  return dynamic_cast<GridRenderer*>(cnt->getRenderer("GridRenderer"));
68  }
69 
70  void GridRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
71  CellGrid* cg = layer->getCellGrid();
72  if (!cg) {
73  FL_WARN(_log, "No cellgrid assigned to layer, cannot draw grid");
74  return;
75  }
76 
77 //
78 // //render elev_coord box
79 // //draw front quad
80 // // 1,1,1
81 // //1,-1,1
82 // //-1,-1,1
83 // //-1,1,1
84  //We don't need the elevation box atm
85 // Point a,b,c,d;
86 //
87 //
88 // ScreenPoint copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,1) );
89 // ScreenPoint copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,1) );
90 // Point coptt1(copt1.x,copt1.y);
91 // Point coptt2(copt2.x,copt2.y);
92 // m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 200);
93 // a = coptt1;
94 //
95 // copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,1) );
96 // copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,1) );
97 // coptt1 = Point(copt1.x,copt1.y);
98 // coptt2 = Point(copt2.x,copt2.y);
99 // m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 200);
100 // b = coptt1;
101 //
102 // copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,1) );
103 // copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,1) );
104 // coptt1 = Point(copt1.x,copt1.y);
105 // coptt2 = Point(copt2.x,copt2.y);
106 // m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 200);
107 // c = coptt1;
108 //
109 // copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,1) );
110 // copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,1) );
111 // coptt1 = Point(copt1.x,copt1.y);
112 // coptt2 = Point(copt2.x,copt2.y);
113 // m_renderbackend->drawLine(coptt1,coptt2 ,15, 15, 20);
114 // d = coptt1;
115 //
116 // m_renderbackend->drawQuad(a,b,c,d,15, 15, 200);
117 //
118 //
119 // //draw back quad
120 // copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,-1) );
121 // copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,-1) );
122 // coptt1 = Point(copt1.x,copt1.y);
123 // coptt2 = Point(copt2.x,copt2.y);
124 // m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
125 //
126 // copt1 =cam->toScreenCoordinates(ExactModelCoordinate(-1,1,-1) );
127 // copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,-1) );
128 // coptt1 = Point(copt1.x,copt1.y);
129 // coptt2 = Point(copt2.x,copt2.y);
130 // m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
131 //
132 // copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,1,-1) );
133 // copt2 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,-1) );
134 // coptt1 = Point(copt1.x,copt1.y);
135 // coptt2 = Point(copt2.x,copt2.y);
136 // m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
137 //
138 // copt1 =cam->toScreenCoordinates(ExactModelCoordinate(1,-1,-1) );
139 // copt2 =cam->toScreenCoordinates(ExactModelCoordinate(-1,-1,-1) );
140 // coptt1 = Point(copt1.x,copt1.y);
141 // coptt2 = Point(copt2.x,copt2.y);
142 // m_renderbackend->drawLine(coptt1,coptt2 ,200, 200, 200);
143 
144 
145  // make the viewport 25% larger
146  Rect cv = cam->getViewPort();
147  int32_t cvx2 = round((cv.x+cv.w) * 1.25);
148  int32_t cvy2 = round((cv.y+cv.h) * 1.25);
149  cv.x -= round((cv.x+cv.w) * 0.125);
150  cv.y -= round((cv.y+cv.h) * 0.125);
151  RenderList::const_iterator instance_it = instances.begin();
152  for (;instance_it != instances.end(); ++instance_it) {
153  Instance* instance = (*instance_it)->instance;
154  std::vector<ExactModelCoordinate> vertices;
155  cg->getVertices(vertices, instance->getLocationRef().getLayerCoordinates());
156  std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
157  ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
158  Point pt1(firstpt.x, firstpt.y);
159  Point pt2;
160  ++it;
161  for (; it != vertices.end(); it++) {
162  ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
163  pt2.x = pts.x;
164  pt2.y = pts.y;
165  Point cpt1 = pt1;
166  Point cpt2 = pt2;
167 
168  if (cpt1.x < cv.x) cpt1.x = cv.x;
169  if (cpt2.x < cv.x) cpt2.x = cv.x;
170  if (cpt1.y < cv.y) cpt1.y = cv.y;
171  if (cpt2.y < cv.y) cpt2.y = cv.y;
172  if (cpt1.x > cvx2) cpt1.x = cvx2;
173  if (cpt2.x > cvx2) cpt2.x = cvx2;
174  if (cpt1.y > cvy2) cpt1.y = cvy2;
175  if (cpt2.y > cvy2) cpt2.y = cvy2;
176 
177  m_renderbackend->drawLine(cpt1, cpt2, m_color.r, m_color.g, m_color.b);
178  pt1 = pt2;
179  }
180  if ((pt2.x >= cv.x) && (pt2.x <= cvx2) && (pt2.y >= cv.y) && (pt2.y <= cvy2)) {
181  if ((firstpt.x >= cv.x) && (firstpt.x <= cvx2) && (firstpt.y >= cv.y) && (firstpt.y <= cvy2)) {
182  m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_color.r, m_color.g, m_color.b);
183  }
184  }
185  }
186  }
187 
188  void GridRenderer::setColor(Uint8 r, Uint8 g, Uint8 b) {
189  m_color.r = r;
190  m_color.g = g;
191  m_color.b = b;
192  }
193 }