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 #include "model/structures/cell.h"
38 #include "model/structures/cellcache.h"
39 
40 #include "view/camera.h"
41 #include "blockinginforenderer.h"
42 
43 
44 namespace FIFE {
45 	/** Logger to use for this source file.
46 	 *  @relates Logger
47 	 */
48 	static Logger _log(LM_VIEWVIEW);
49 
BlockingInfoRenderer(RenderBackend * renderbackend,int32_t position)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 
BlockingInfoRenderer(const BlockingInfoRenderer & old)58 	BlockingInfoRenderer::BlockingInfoRenderer(const BlockingInfoRenderer& old):
59 		RendererBase(old),
60 		m_color(old.m_color) {
61 		setEnabled(false);
62 	}
63 
clone()64 	RendererBase* BlockingInfoRenderer::clone() {
65 		return new BlockingInfoRenderer(*this);
66 	}
67 
~BlockingInfoRenderer()68 	BlockingInfoRenderer::~BlockingInfoRenderer() {
69 	}
70 
getInstance(IRendererContainer * cnt)71 	BlockingInfoRenderer* BlockingInfoRenderer::getInstance(IRendererContainer* cnt) {
72 		return dynamic_cast<BlockingInfoRenderer*>(cnt->getRenderer("BlockingInfoRenderer"));
73 	}
74 
render(Camera * cam,Layer * layer,RenderList & instances)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());
91 					ScreenPoint sp = cam->toScreenCoordinates(cg->toMapCoordinates(emc));
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 
setColor(uint8_t r,uint8_t g,uint8_t b)154 	void BlockingInfoRenderer::setColor(uint8_t r, uint8_t g, uint8_t b) {
155 		m_color.r = r;
156 		m_color.g = g;
157 		m_color.b = b;
158 	}
159 }
160