FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
coordinaterenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 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"
36 #include "model/metamodel/action.h"
38 #include "model/structures/layer.h"
40 
41 #include "view/camera.h"
42 #include "view/visual.h"
43 #include "coordinaterenderer.h"
44 
45 
46 namespace FIFE {
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 
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 
76  return new CoordinateRenderer(*this);
77  }
78 
80  }
81 
83  return dynamic_cast<CoordinateRenderer*>(cnt->getRenderer("CoordinateRenderer"));
84  }
85 
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);
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) {
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);
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 
182  m_color.r = r;
183  m_color.g = g;
184  m_color.b = b;
185  }
186 }
Abstract interface for all the renderbackends.
void setLayerCoordinates(const ModelCoordinate &coordinates)
Sets "cell precise" layer coordinates to this location.
Definition: location.cpp:94
std::vector< RenderItem * > RenderList
Definition: renderitem.h:130
Base Class for Images.
Definition: image.h:48
T h
Height of the rectangle.
Definition: rect.h:93
void setLayer(Layer *layer)
Sets layer where this location is pointing to.
Definition: location.cpp:79
T x
The X Coordinate.
Definition: rect.h:84
void setColor(uint8_t r, uint8_t g, uint8_t b)
Changes the used color.
void setMapCoordinates(const ExactModelCoordinate &coordinates)
Sets map coordinates to this location.
Definition: location.cpp:98
const int32_t MIN_COORD
Interface to class owning the renderers Used to get correct subclass of renderer in scripting side (v...
Definition: rendererbase.h:66
virtual Image * getAsImage(const std::string &text)=0
Gets given text as Image The rsulting image is pooled, so it&#39;s not that time critical.
static Logger _log(LM_AUDIO)
virtual void setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Set the color the text should be rendered in.
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...
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
virtual SDL_Color getColor() const =0
Get the color the text was rendered in.
ModelCoordinate getLayerCoordinates() const
Gets cell precision layer coordinates set to this location.
Definition: location.cpp:113
uint32_t getHeight() const
Definition: image.cpp:160
unsigned char uint8_t
Definition: core.h:38
const Rect & getViewPort() const
Gets the viewport for camera in pixel coordinates.
Definition: camera.cpp:304
static bool Equal(T _val1, T _val2)
Definition: fife_math.h:287
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
double getZoom() const
Gets camera zoom.
Definition: camera.cpp:172
virtual void setEnabled(bool enabled)
Enables renderer.
const int32_t MAX_COORD
uint32_t getWidth() const
Definition: image.cpp:151
RendererBase * clone()
Makes copy of this renderer.
T y
The Y Coordinate.
Definition: rect.h:87
virtual void render(const Rect &rect, uint8_t alpha=255, uint8_t const *rgb=0)=0
Renders itself to the current render target (main screen or attached destination image) at the rectan...
ScreenPoint toScreenCoordinates(const ExactModelCoordinate &map_coords)
Transforms given point from map coordinates to screen coordinates.
Definition: camera.cpp:428
A 3D Point.
Definition: point.h:205
RectType< int32_t > Rect
Definition: rect.h:258
ExactModelCoordinate toMapCoordinates(ScreenPoint screen_coords, bool z_calculated=true)
Transforms given point from screen coordinates to map coordinates.
Definition: camera.cpp:420
ExactModelCoordinate m_c
virtual ~CoordinateRenderer()
Destructor.
T w
Width of the rectangle.
Definition: rect.h:90
CoordinateRenderer(RenderBackend *renderbackend, int32_t position)
Constructor.
ExactModelCoordinate getMapCoordinates() const
Gets map coordinates set to this location.
Definition: location.cpp:117
static CoordinateRenderer * getInstance(IRendererContainer *cnt)
Gets instance for interface access.
Point3D ScreenPoint
Definition: camera.h:46