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