| 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 "video/image.h" |
| 32 |
#include "video/fonts/ifont.h" |
| 33 |
#include "util/math/fife_math.h" |
| 34 |
#include "util/log/logger.h" |
| 35 |
#include "model/metamodel/grids/cellgrid.h" |
| 36 |
#include "model/metamodel/action.h" |
| 37 |
#include "model/structures/instance.h" |
| 38 |
#include "model/structures/layer.h" |
| 39 |
#include "model/structures/location.h" |
| 40 |
|
| 41 |
#include "view/camera.h" |
| 42 |
#include "view/visual.h" |
| 43 |
#include "coordinaterenderer.h" |
| 44 |
|
| 45 |
|
| 46 |
namespace FIFE { |
| 47 |
/** Logger to use for this source file. |
| 48 |
* @relates Logger |
| 49 |
*/ |
| 50 |
static Logger _log(LM_VIEWVIEW); |
| 51 |
|
| 52 |
CoordinateRenderer::CoordinateRenderer(RenderBackend* renderbackend, int32_t position): |
| 53 |
RendererBase(renderbackend, position), |
| 54 |
m_layer_area(), |
| 55 |
m_tmploc(), |
| 56 |
m_c(), |
| 57 |
m_font(0), |
| 58 |
m_font_color(false), |
| 59 |
m_zoom(true) { |
| 60 |
setEnabled(false); |
| 61 |
} |
| 62 |
|
| 63 |
CoordinateRenderer::CoordinateRenderer(const CoordinateRenderer& old): |
| 64 |
RendererBase(old), |
| 65 |
m_layer_area(), |
| 66 |
m_tmploc(), |
| 67 |
m_c(), |
| 68 |
m_font(old.m_font), |
| 69 |
m_font_color(false), |
| 70 |
m_color(old.m_color), |
| 71 |
m_zoom(old.m_zoom) { |
| 72 |
setEnabled(false); |
| 73 |
} |
| 74 |
|
| 75 |
RendererBase* CoordinateRenderer::clone() { |
| 76 |
return new CoordinateRenderer(*this); |
| 77 |
} |
| 78 |
|
| 79 |
CoordinateRenderer::~CoordinateRenderer() { |
| 80 |
} |
| 81 |
|
| 82 |
CoordinateRenderer* CoordinateRenderer::getInstance(IRendererContainer* cnt) { |
| 83 |
return dynamic_cast<CoordinateRenderer*>(cnt->getRenderer("CoordinateRenderer")); |
| 84 |
} |
| 85 |
|
| 86 |
void CoordinateRenderer::adjustLayerArea() { |
| 87 |
m_tmploc.setMapCoordinates(m_c); |
| 88 |
ModelCoordinate c = m_tmploc.getLayerCoordinates(); |
| 89 |
m_layer_area.x = std::min(c.x, m_layer_area.x); |
| 90 |
m_layer_area.w = std::max(c.x, m_layer_area.w); |
| 91 |
m_layer_area.y = std::min(c.y, m_layer_area.y); |
| 92 |
m_layer_area.h = std::max(c.y, m_layer_area.h); |
| 93 |
} |
| 94 |
|
| 95 |
const int32_t MIN_COORD = -9999999; |
| 96 |
const int32_t MAX_COORD = 9999999; |
| 97 |
void CoordinateRenderer::render(Camera* cam, Layer* layer, RenderList& instances) { |
| 98 |
if (!m_font) { |
| 99 |
//no font selected.. nothing to render |
| 100 |
return; |
| 101 |
} |
| 102 |
Rect r = Rect(); |
| 103 |
const bool zoomed = (!Mathd::Equal(1.0, cam->getZoom()) && m_zoom); |
| 104 |
Rect cv = cam->getViewPort(); |
| 105 |
|
| 106 |
m_tmploc.setLayer(layer); |
| 107 |
m_layer_area.x = MAX_COORD; |
| 108 |
m_layer_area.y = MAX_COORD; |
| 109 |
m_layer_area.w = MIN_COORD; |
| 110 |
m_layer_area.h = MIN_COORD; |
| 111 |
|
| 112 |
m_c = cam->toMapCoordinates(ScreenPoint(cv.x, cv.y), false); |
| 113 |
adjustLayerArea(); |
| 114 |
m_c = cam->toMapCoordinates(ScreenPoint(cv.x+cv.w, cv.y), false); |
| 115 |
adjustLayerArea(); |
| 116 |
m_c = cam->toMapCoordinates(ScreenPoint(cv.x, cv.y+cv.h), false); |
| 117 |
adjustLayerArea(); |
| 118 |
m_c = cam->toMapCoordinates(ScreenPoint(cv.x+cv.w, cv.y+cv.h), false); |
| 119 |
adjustLayerArea(); |
| 120 |
|
| 121 |
SDL_Color old_color = m_font->getColor(); |
| 122 |
if(old_color.r != m_color.r || old_color.g != m_color.g || old_color.b != m_color.b) { |
| 123 |
m_font->setColor(m_color.r, m_color.g, m_color.b); |
| 124 |
m_font_color = true; |
| 125 |
} |
| 126 |
for (int32_t x = m_layer_area.x-1; x < m_layer_area.w+1; x++) { |
| 127 |
for (int32_t y = m_layer_area.y-1; y < m_layer_area.h+1; y++) { |
| 128 |
ModelCoordinate mc(x, y); |
| 129 |
m_tmploc.setLayerCoordinates(mc); |
| 130 |
ScreenPoint drawpt = cam->toScreenCoordinates(m_tmploc.getMapCoordinates()); |
| 131 |
if (drawpt.x < cv.x || drawpt.x > cv.x + cv.w || |
| 132 |
drawpt.y < cv.y || drawpt.y > cv.y + cv.h) { |
| 133 |
continue; |
| 134 |
} |
| 135 |
|
| 136 |
// we split the stringstream in three images, because so the TextRenderPool can reuse the most images. |
| 137 |
// more to render but less images to generate |
| 138 |
std::stringstream sts; |
| 139 |
sts << mc.x; |
| 140 |
Image* imgx = m_font->getAsImage(sts.str()); |
| 141 |
sts.str(","); |
| 142 |
Image* imgc = m_font->getAsImage(sts.str()); |
| 143 |
sts.str(""); |
| 144 |
sts << mc.y; |
| 145 |
Image* imgy = m_font->getAsImage(sts.str()); |
| 146 |
|
| 147 |
if (zoomed) { |
| 148 |
double zoom = cam->getZoom(); |
| 149 |
r.x = drawpt.x - ((imgx->getWidth() + imgc->getWidth() + imgy->getWidth())/2) * zoom; |
| 150 |
r.y = drawpt.y - (imgx->getHeight()/2) * zoom; |
| 151 |
r.w = imgx->getWidth() * zoom; |
| 152 |
r.h = imgx->getHeight() * zoom; |
| 153 |
imgx->render(r); |
| 154 |
r.x += r.w; |
| 155 |
r.w = imgc->getWidth() * zoom; |
| 156 |
imgc->render(r); |
| 157 |
r.x += r.w; |
| 158 |
r.w = imgy->getWidth() * zoom; |
| 159 |
imgy->render(r); |
| 160 |
} else { |
| 161 |
r.x = drawpt.x - (imgx->getWidth() + imgc->getWidth() + imgy->getWidth())/2; |
| 162 |
r.y = drawpt.y - imgx->getHeight()/2; |
| 163 |
r.w = imgx->getWidth(); |
| 164 |
r.h = imgx->getHeight(); |
| 165 |
imgx->render(r); |
| 166 |
r.x += r.w; |
| 167 |
r.w = imgc->getWidth(); |
| 168 |
imgc->render(r); |
| 169 |
r.x += r.w; |
| 170 |
r.w = imgy->getWidth(); |
| 171 |
imgy->render(r); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
if(m_font_color) { |
| 176 |
m_font->setColor(old_color.r, old_color.g, old_color.b); |
| 177 |
m_font_color = false; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
void CoordinateRenderer::setColor(uint8_t r, uint8_t g, uint8_t b) { |
| 182 |
m_color.r = r; |
| 183 |
m_color.g = g; |
| 184 |
m_color.b = b; |
| 185 |
} |
| 186 |
} |