FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator
blockinginforenderer.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 "util/math/fife_math.h"
00032 #include "util/log/logger.h"
00033 #include "model/metamodel/grids/cellgrid.h"
00034 #include "model/structures/instance.h"
00035 #include "model/structures/layer.h"
00036 #include "model/structures/location.h"
00037 
00038 #include "view/camera.h"
00039 #include "blockinginforenderer.h"
00040 
00041 
00042 namespace FIFE {
00043     static Logger _log(LM_VIEWVIEW);
00044 
00045     BlockingInfoRenderer::BlockingInfoRenderer(RenderBackend* renderbackend, int position):
00046         RendererBase(renderbackend, position) {
00047         setEnabled(false);
00048         m_color.r = 0;
00049         m_color.g = 255;
00050         m_color.b = 0;
00051     }
00052 
00053     BlockingInfoRenderer::BlockingInfoRenderer(const BlockingInfoRenderer& old):
00054         RendererBase(old),
00055         m_color(old.m_color) {
00056         setEnabled(false);
00057     }
00058 
00059     RendererBase* BlockingInfoRenderer::clone() {
00060         return new BlockingInfoRenderer(*this);
00061     }
00062 
00063     BlockingInfoRenderer::~BlockingInfoRenderer() {
00064     }
00065 
00066     BlockingInfoRenderer* BlockingInfoRenderer::getInstance(IRendererContainer* cnt) {
00067         return dynamic_cast<BlockingInfoRenderer*>(cnt->getRenderer("BlockingInfoRenderer"));
00068     }
00069 
00070     void BlockingInfoRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
00071         CellGrid* cg = layer->getCellGrid();
00072         if (!cg) {
00073             FL_WARN(_log, "No cellgrid assigned to layer, cannot draw grid");
00074             return;
00075         }
00076         
00077         Rect cv = cam->getViewPort();
00078         RenderList::const_iterator instance_it = instances.begin();
00079         for (;instance_it != instances.end(); ++instance_it) {
00080             Instance* instance = (*instance_it)->instance;
00081             if (!instance->getObject()->isBlocking() || !instance->isBlocking()) {
00082                 continue;
00083             }
00084             std::vector<ExactModelCoordinate> vertices;
00085             cg->getVertices(vertices, instance->getLocationRef().getLayerCoordinates());
00086             std::vector<ExactModelCoordinate>::const_iterator it = vertices.begin();
00087             int halfind = vertices.size() / 2;
00088             ScreenPoint firstpt = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00089             Point pt1(firstpt.x, firstpt.y);
00090             Point pt2;
00091             ++it;
00092             for (; it != vertices.end(); it++) {
00093                 ScreenPoint pts = cam->toScreenCoordinates(cg->toMapCoordinates(*it));
00094                 pt2.x = pts.x; pt2.y = pts.y;
00095                 Point cpt1 = pt1;
00096                 Point cpt2 = pt2;
00097                 m_renderbackend->drawLine(cpt1, cpt2, m_color.r, m_color.g, m_color.b);
00098                 pt1 = pt2;
00099             }
00100             m_renderbackend->drawLine(pt2, Point(firstpt.x, firstpt.y), m_color.r, m_color.g, m_color.b);
00101             ScreenPoint spt1 = cam->toScreenCoordinates(cg->toMapCoordinates(vertices[0]));
00102             Point pt3(spt1.x, spt1.y);
00103             ScreenPoint spt2 = cam->toScreenCoordinates(cg->toMapCoordinates(vertices[halfind]));
00104             Point pt4(spt2.x, spt2.y);
00105             m_renderbackend->drawLine(pt3, pt4, m_color.r, m_color.g, m_color.b);
00106         }
00107     }
00108 
00109     void BlockingInfoRenderer::setColor(Uint8 r, Uint8 g, Uint8 b) {
00110         m_color.r = r;
00111         m_color.g = g;
00112         m_color.b = b;
00113     }
00114 }