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/imagepool.h"
33 #include "video/fonts/abstractfont.h"
34 #include "util/math/fife_math.h"
35 #include "util/log/logger.h"
36 #include "model/metamodel/grids/cellgrid.h"
37 #include "model/metamodel/action.h"
38 #include "model/structures/instance.h"
39 #include "model/structures/layer.h"
40 #include "model/structures/location.h"
41 
42 #include "view/camera.h"
43 #include "view/visual.h"
44 #include "coordinaterenderer.h"
45 
46 
47 namespace FIFE {
48  static Logger _log(LM_VIEWVIEW);
49 
50  CoordinateRenderer::CoordinateRenderer(RenderBackend* renderbackend, int position, AbstractFont* font):
51  RendererBase(renderbackend, position),
52  m_layer_area(),
53  m_tmploc(),
54  m_c(),
55  m_font(font) {
56  setEnabled(false);
57  m_font_color = false;
58  m_color = m_font->getColor();
59  }
60 
61  CoordinateRenderer::CoordinateRenderer(const CoordinateRenderer& old):
62  RendererBase(old),
63  m_layer_area(),
64  m_tmploc(),
65  m_c(),
66  m_font(old.m_font),
67  m_font_color(old.m_font_color),
68  m_color(old.m_color) {
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 int MIN_COORD = -9999999;
93  const int MAX_COORD = 9999999;
94  void CoordinateRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
95  m_layer_area.x = MAX_COORD;
96  m_layer_area.y = MAX_COORD;
97  m_layer_area.w = MIN_COORD;
98  m_layer_area.h = MIN_COORD;
99 
100  m_tmploc.setLayer(layer);
101  Rect cv = cam->getViewPort();
102  m_c = cam->toMapCoordinates(ScreenPoint(cv.x, cv.y), false);
103  adjustLayerArea();
104  m_c = cam->toMapCoordinates(ScreenPoint(cv.x+cv.w, cv.y), false);
105  adjustLayerArea();
106  m_c = cam->toMapCoordinates(ScreenPoint(cv.x, cv.y+cv.h), false);
107  adjustLayerArea();
108  m_c = cam->toMapCoordinates(ScreenPoint(cv.x+cv.w, cv.y+cv.h), false);
109  adjustLayerArea();
110 
111  Rect r = Rect();
112  SDL_Color old_color = m_font->getColor();
113  if(old_color.r != m_color.r || old_color.g != m_color.g || old_color.b != m_color.b) {
114  m_font->setColor(m_color.r, m_color.g, m_color.b);
115  m_font_color = true;
116  }
117  for (int x = m_layer_area.x-1; x < m_layer_area.w+1; x++) {
118  for (int y = m_layer_area.y-1; y < m_layer_area.h+1; y++) {
119  ModelCoordinate mc(x, y);
120  m_tmploc.setLayerCoordinates(mc);
121  ScreenPoint drawpt = cam->toScreenCoordinates(m_tmploc.getMapCoordinates());
122  if ((drawpt.x >= cv.x) && (drawpt.x <= cv.x + cv.w) &&
123  (drawpt.y >= cv.y) && (drawpt.y <= cv.y + cv.h)) {
124  std::stringstream ss;
125  ss << mc.x <<","<< mc.y;
126  Image * img = m_font->getAsImage(ss.str());
127  r.x = drawpt.x;
128  r.y = drawpt.y;
129  r.w = img->getWidth();
130  r.h = img->getHeight();
131  img->render(r);
132  }
133  }
134  }
135  if(m_font_color) {
136  m_font->setColor(old_color.r, old_color.g, old_color.b);
137  m_font_color = false;
138  }
139  }
140 
141  void CoordinateRenderer::setColor(Uint8 r, Uint8 g, Uint8 b) {
142  m_color.r = r;
143  m_color.g = g;
144  m_color.b = b;
145  }
146 }