FIFE 2008.0
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/imagepool.h"
00033 #include "video/fonts/abstractfont.h"
00034 #include "util/math/fife_math.h"
00035 #include "util/log/logger.h"
00036 #include "model/metamodel/grids/cellgrid.h"
00037 #include "model/metamodel/action.h"
00038 #include "model/structures/instance.h"
00039 #include "model/structures/layer.h"
00040 #include "model/structures/location.h"
00041 
00042 #include "view/camera.h"
00043 #include "view/visual.h"
00044 #include "coordinaterenderer.h"
00045 
00046 
00047 namespace FIFE {
00048     static Logger _log(LM_VIEWVIEW);
00049 
00050     CoordinateRenderer::CoordinateRenderer(RenderBackend* renderbackend, int position, AbstractFont* font):
00051         RendererBase(renderbackend, position),
00052         m_layer_area(),
00053         m_tmploc(),
00054         m_c(),
00055         m_font(font) {
00056         setEnabled(false);
00057         m_font_color = false;
00058         m_color = m_font->getColor();
00059     }
00060 
00061     CoordinateRenderer::CoordinateRenderer(const CoordinateRenderer& old):
00062         RendererBase(old),
00063         m_layer_area(),
00064         m_tmploc(),
00065         m_c(),
00066         m_font(old.m_font),
00067         m_font_color(old.m_font_color),
00068         m_color(old.m_color) {
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 int MIN_COORD = -9999999;
00093     const int MAX_COORD = 9999999;
00094     void CoordinateRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
00095         m_layer_area.x = MAX_COORD;
00096         m_layer_area.y = MAX_COORD;
00097         m_layer_area.w = MIN_COORD;
00098         m_layer_area.h = MIN_COORD;
00099 
00100         m_tmploc.setLayer(layer);
00101         Rect cv = cam->getViewPort();
00102         m_c = cam->toMapCoordinates(ScreenPoint(cv.x, cv.y), false);
00103         adjustLayerArea();
00104         m_c = cam->toMapCoordinates(ScreenPoint(cv.x+cv.w, cv.y), false);
00105         adjustLayerArea();
00106         m_c = cam->toMapCoordinates(ScreenPoint(cv.x, cv.y+cv.h), false);
00107         adjustLayerArea();
00108         m_c = cam->toMapCoordinates(ScreenPoint(cv.x+cv.w, cv.y+cv.h), false);
00109         adjustLayerArea();
00110 
00111         Rect r = Rect();
00112         SDL_Color old_color = m_font->getColor();
00113         if(old_color.r != m_color.r || old_color.g != m_color.g || old_color.b != m_color.b) {
00114             m_font->setColor(m_color.r, m_color.g, m_color.b);
00115             m_font_color = true;
00116         }
00117         for (int x = m_layer_area.x-1; x < m_layer_area.w+1; x++) {
00118             for (int y = m_layer_area.y-1; y < m_layer_area.h+1; y++) {
00119                 ModelCoordinate mc(x, y);
00120                 m_tmploc.setLayerCoordinates(mc);
00121                 ScreenPoint drawpt = cam->toScreenCoordinates(m_tmploc.getMapCoordinates());
00122                 if ((drawpt.x >= cv.x) && (drawpt.x <= cv.x + cv.w) &&
00123                     (drawpt.y >= cv.y) && (drawpt.y <= cv.y + cv.h)) {
00124                     std::stringstream ss;
00125                     ss << mc.x <<","<< mc.y;
00126                     Image * img = m_font->getAsImage(ss.str());
00127                     r.x = drawpt.x;
00128                     r.y = drawpt.y;
00129                     r.w = img->getWidth();
00130                     r.h = img->getHeight();
00131                     img->render(r);
00132                 }
00133             }
00134         }
00135         if(m_font_color) {
00136             m_font->setColor(old_color.r, old_color.g, old_color.b);
00137             m_font_color = false;
00138         }
00139     }
00140 
00141     void CoordinateRenderer::setColor(Uint8 r, Uint8 g, Uint8 b) {
00142         m_color.r = r;
00143         m_color.g = g;
00144         m_color.b = b;
00145     }
00146 }
 All Classes Namespaces Functions Variables Enumerations Enumerator