| 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" |
| 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 |
/** Logger to use for this source file. |
| 44 |
* @relates Logger |
| 45 |
*/ |
| 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 |
|
| 56 |
GridRenderer::GridRenderer(const GridRenderer& old): |
| 57 |
RendererBase(old), |
| 58 |
m_color(old.m_color) { |
| 59 |
setEnabled(false); |
| 60 |
} |
| 61 |
|
| 62 |
RendererBase* GridRenderer::clone() { |
| 63 |
return new GridRenderer(*this); |
| 64 |
} |
| 65 |
|
| 66 |
GridRenderer::~GridRenderer() { |
| 67 |
} |
| 68 |
|
| 69 |
GridRenderer* GridRenderer::getInstance(IRendererContainer* cnt) { |
| 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 |
} |