coordinaterenderer.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 
00024 // 3rd party library includes
00025 
00026 // FIFE includes
00027 // These includes are split up in two parts, separated by one empty line
00028 // First block: files included from the FIFE root src directory
00029 // Second block: files included from the same folder
00030 #include "video/renderbackend.h"
00031 #include "video/image.h"
00032 #include "video/fonts/ifont.h"
00033 #include "util/math/fife_math.h"
00034 #include "util/log/logger.h"
00035 #include "model/metamodel/grids/cellgrid.h"
00036 #include "model/metamodel/action.h"
00037 #include "model/structures/instance.h"
00038 #include "model/structures/layer.h"
00039 #include "model/structures/location.h"
00040 
00041 #include "view/camera.h"
00042 #include "view/visual.h"
00043 #include "coordinaterenderer.h"
00044 
00045 
00046 namespace FIFE {
00047     static Logger _log(LM_VIEWVIEW);
00048 
00049     CoordinateRenderer::CoordinateRenderer(RenderBackend* renderbackend, int32_t position):
00050         RendererBase(renderbackend, position),
00051         m_layer_area(),
00052         m_tmploc(),
00053         m_c(),
00054         m_font(0),
00055         m_font_color(false),
00056         m_zoom(true) {
00057         setEnabled(false);
00058     }
00059 
00060     CoordinateRenderer::CoordinateRenderer(const CoordinateRenderer& old):
00061         RendererBase(old),
00062         m_layer_area(),
00063         m_tmploc(),
00064         m_c(),
00065         m_font(old.m_font),
00066         m_font_color(false),
00067         m_color(old.m_color),
00068         m_zoom(old.m_zoom) {
00069         setEnabled(false);
00070     }
00071 
00072     RendererBase* CoordinateRenderer::clone() {
00073         return new CoordinateRenderer(*this);
00074     }
00075 
00076     CoordinateRenderer::~CoordinateRenderer() {
00077     }
00078 
00079     CoordinateRenderer* CoordinateRenderer::getInstance(IRendererContainer* cnt) {
00080         return dynamic_cast<CoordinateRenderer*>(cnt->getRenderer("CoordinateRenderer"));
00081     }
00082 
00083     void CoordinateRenderer::adjustLayerArea() {
00084         m_tmploc.setMapCoordinates(m_c);
00085         ModelCoordinate c = m_tmploc.getLayerCoordinates();
00086         m_layer_area.x = std::min(c.x, m_layer_area.x);
00087         m_layer_area.w = std::max(c.x, m_layer_area.w);
00088         m_layer_area.y = std::min(c.y, m_layer_area.y);
00089         m_layer_area.h = std::max(c.y, m_layer_area.h);
00090     }
00091 
00092     const int32_t MIN_COORD = -9999999;
00093     const int32_t MAX_COORD = 9999999;
00094     void CoordinateRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
00095         if (!m_font) {
00096             //no font selected.. nothing to render
00097             return;
00098         }
00099         Rect r = Rect();
00100         const bool zoomed = (!Mathd::Equal(1.0, cam->getZoom()) && m_zoom);
00101         Rect cv = cam->getViewPort();
00102 
00103         m_tmploc.setLayer(layer);
00104         m_layer_area.x = MAX_COORD;
00105         m_layer_area.y = MAX_COORD;
00106         m_layer_area.w = MIN_COORD;
00107         m_layer_area.h = MIN_COORD;
00108 
00109         m_c = cam->toMapCoordinates(ScreenPoint(cv.x, cv.y), false);
00110         adjustLayerArea();
00111         m_c = cam->toMapCoordinates(ScreenPoint(cv.x+cv.w, cv.y), false);
00112         adjustLayerArea();
00113         m_c = cam->toMapCoordinates(ScreenPoint(cv.x, cv.y+cv.h), false);
00114         adjustLayerArea();
00115         m_c = cam->toMapCoordinates(ScreenPoint(cv.x+cv.w, cv.y+cv.h), false);
00116         adjustLayerArea();
00117 
00118         SDL_Color old_color = m_font->getColor();
00119         if(old_color.r != m_color.r || old_color.g != m_color.g || old_color.b != m_color.b) {
00120             m_font->setColor(m_color.r, m_color.g, m_color.b);
00121             m_font_color = true;
00122         }
00123         for (int32_t x = m_layer_area.x-1; x < m_layer_area.w+1; x++) {
00124             for (int32_t y = m_layer_area.y-1; y < m_layer_area.h+1; y++) {
00125                 ModelCoordinate mc(x, y);
00126                 m_tmploc.setLayerCoordinates(mc);
00127                 ScreenPoint drawpt = cam->toScreenCoordinates(m_tmploc.getMapCoordinates());
00128                 if (drawpt.x < cv.x || drawpt.x > cv.x + cv.w ||
00129                     drawpt.y < cv.y || drawpt.y > cv.y + cv.h) {
00130                     continue;
00131                 }
00132 
00133                 // we split the stringstream in three images, because so the TextRenderPool can reuse the most images.
00134                 // more to render but less images to generate
00135                 std::stringstream sts;
00136                 sts << mc.x;
00137                 Image* imgx = m_font->getAsImage(sts.str());
00138                 sts.str(",");
00139                 Image* imgc = m_font->getAsImage(sts.str());
00140                 sts.str("");
00141                 sts << mc.y;
00142                 Image* imgy = m_font->getAsImage(sts.str());
00143 
00144                 if (zoomed) {
00145                     double zoom = cam->getZoom();
00146                     r.x = drawpt.x - ((imgx->getWidth() + imgc->getWidth() + imgy->getWidth())/2) * zoom;
00147                     r.y = drawpt.y - (imgx->getHeight()/2) * zoom;
00148                     r.w = imgx->getWidth() * zoom;
00149                     r.h = imgx->getHeight() * zoom;
00150                     imgx->render(r);
00151                     r.x += r.w;
00152                     r.w = imgc->getWidth() * zoom;
00153                     imgc->render(r);
00154                     r.x += r.w;
00155                     r.w = imgy->getWidth() * zoom;
00156                     imgy->render(r);
00157                 } else {
00158                     r.x = drawpt.x - (imgx->getWidth() + imgc->getWidth() + imgy->getWidth())/2;
00159                     r.y = drawpt.y - imgx->getHeight()/2;
00160                     r.w = imgx->getWidth();
00161                     r.h = imgx->getHeight();
00162                     imgx->render(r);
00163                     r.x += r.w;
00164                     r.w = imgc->getWidth();
00165                     imgc->render(r);
00166                     r.x += r.w;
00167                     r.w = imgy->getWidth();
00168                     imgy->render(r);
00169                 }
00170             }
00171         }
00172         if(m_font_color) {
00173             m_font->setColor(old_color.r, old_color.g, old_color.b);
00174             m_font_color = false;
00175         }
00176     }
00177 
00178     void CoordinateRenderer::setColor(uint8_t r, uint8_t g, uint8_t b) {
00179         m_color.r = r;
00180         m_color.g = g;
00181         m_color.b = b;
00182     }
00183 }