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     }
00058 
00059     CoordinateRenderer::CoordinateRenderer(const CoordinateRenderer& old):
00060         m_layer_area(),
00061         m_tmploc(),
00062         m_c(),
00063         m_font(old.m_font) {
00064         setEnabled(false);
00065     }
00066     
00067     CoordinateRenderer::~CoordinateRenderer() {
00068     }
00069 
00070     RendererBase* CoordinateRenderer::clone() {
00071         return new CoordinateRenderer(*this);
00072     }
00073 
00074     void CoordinateRenderer::adjustLayerArea() {
00075         m_tmploc.setMapCoordinates(m_c);
00076         ModelCoordinate c = m_tmploc.getLayerCoordinates();
00077         m_layer_area.x = std::min(c.x, m_layer_area.x);
00078         m_layer_area.w = std::max(c.x, m_layer_area.w);
00079         m_layer_area.y = std::min(c.y, m_layer_area.y);
00080         m_layer_area.h = std::max(c.y, m_layer_area.h);
00081     }
00082 
00083     const int MIN_COORD = -9999999;
00084     const int MAX_COORD = 9999999;
00085     void CoordinateRenderer::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances) {
00086         m_layer_area.x = MAX_COORD;
00087         m_layer_area.y = MAX_COORD;
00088         m_layer_area.w = MIN_COORD;
00089         m_layer_area.h = MIN_COORD;
00090 
00091         m_tmploc.setLayer(layer);
00092         Rect cv = cam->getViewPort();
00093         m_c = cam->toMapCoordinates(ScreenPoint(cv.x, cv.y), false);
00094         adjustLayerArea();
00095         m_c = cam->toMapCoordinates(ScreenPoint(cv.x+cv.w, cv.y), false);
00096         adjustLayerArea();
00097         m_c = cam->toMapCoordinates(ScreenPoint(cv.x, cv.y+cv.h), false);
00098         adjustLayerArea();
00099         m_c = cam->toMapCoordinates(ScreenPoint(cv.x+cv.w, cv.y+cv.h), false);
00100         adjustLayerArea();
00101 
00102         Rect r = Rect();
00103         for (int x = m_layer_area.x-1; x < m_layer_area.w+1; x++) {
00104             for (int y = m_layer_area.y-1; y < m_layer_area.h+1; y++) {
00105                 ModelCoordinate mc(x, y);
00106                 m_tmploc.setLayerCoordinates(mc);
00107                 ScreenPoint drawpt = cam->toScreenCoordinates(m_tmploc.getMapCoordinates());
00108                 if ((drawpt.x >= cv.x) && (drawpt.x <= cv.x + cv.w) &&
00109                     (drawpt.y >= cv.y) && (drawpt.y <= cv.y + cv.h)) {
00110                     std::stringstream ss;
00111                     ss << mc.x <<","<< mc.y;
00112                     m_font->setColor(255,255,255);
00113                     Image * img = m_font->getAsImage(ss.str());
00114                     r.x = drawpt.x;
00115                     r.y = drawpt.y;
00116                     r.w = img->getWidth();
00117                     r.h = img->getHeight();
00118                     img->render(r);
00119                 }
00120 
00121             }
00122         }
00123     }
00124 }
Generated by  doxygen 1.6.2-20100208