FIFE  2008.0
quadtreerenderer.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 "quadtreerenderer.h"
00040 #include "model/structures/instancetree.h"
00041 #include "util/structures/quadtree.h"
00042 
00044 namespace FIFE {
00045     static Logger _log(LM_VIEWVIEW);
00046 
00047     QuadTreeRenderer::QuadTreeRenderer(RenderBackend* renderbackend, int position):
00048         RendererBase(renderbackend, position) {
00049         setEnabled(false);
00050     }
00051 
00052     QuadTreeRenderer::QuadTreeRenderer(const QuadTreeRenderer& old):
00053         RendererBase(old) {
00054         setEnabled(false);
00055     }
00056 
00057     RendererBase* QuadTreeRenderer::clone() {
00058         return new QuadTreeRenderer(*this);
00059     }
00060 
00061     QuadTreeRenderer::~QuadTreeRenderer() { }
00062     RenderVisitor::RenderVisitor(RenderBackend * rb, Layer * layer, Camera *camera) {
00063 
00064         m_renderbackend = rb;
00065         m_layer = layer;
00066         m_camera = camera;
00067     }
00068 
00069     RenderVisitor::~RenderVisitor() {}
00070 
00071     template<typename T> bool RenderVisitor::visit(QuadNode<T,2>* node, int d) {
00072 
00073         if (d==0)
00074             visited = 0;
00075 
00076         int x = node->x();
00077         int y = node->y();
00078         int size = node->size();
00079 
00080         ++visited;
00081         CellGrid *cg = m_layer->getCellGrid(); 
00082 
00083 
00084         ExactModelCoordinate emc= cg->toMapCoordinates(ExactModelCoordinate( x,y) );//0.5 for each cell's half-width
00085         ScreenPoint scrpt1 =m_camera->toScreenCoordinates( emc );
00086         emc= cg->toMapCoordinates(ExactModelCoordinate( x,y+size) );// this size usage is wrong.. me thinks
00087         ScreenPoint scrpt2 =m_camera->toScreenCoordinates( emc );
00088         emc= cg->toMapCoordinates(ExactModelCoordinate( x+size,y) );
00089         ScreenPoint scrpt3 =m_camera->toScreenCoordinates( emc );
00090         emc= cg->toMapCoordinates(ExactModelCoordinate( x+size,y+size) );
00091         ScreenPoint scrpt4 =m_camera->toScreenCoordinates( emc );
00092 
00093         m_renderbackend->disableLighting();
00094         m_renderbackend->drawLine( Point(scrpt1.x,scrpt1.y) , Point(scrpt2.x,scrpt2.y), 255, 255, 255);
00095         m_renderbackend->drawLine(Point(scrpt1.x,scrpt1.y), Point(scrpt3.x,scrpt3.y), 255, 255, 255);
00096         m_renderbackend->drawLine(Point(scrpt3.x,scrpt3.y), Point(scrpt4.x,scrpt4.y), 255, 255, 255);
00097         m_renderbackend->drawLine(Point(scrpt2.x,scrpt2.y), Point(scrpt4.x,scrpt4.y), 255, 255, 255);
00098         m_renderbackend->enableLighting();
00099 
00100         return true;
00101     }
00102 
00103 
00104     void QuadTreeRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
00105         CellGrid* cg = layer->getCellGrid();
00106         if (!cg) {
00107             FL_WARN(_log, "No cellgrid assigned to layer, cannot draw grid");
00108             return;
00109         }
00110         InstanceTree * itree = layer->getInstanceTree();
00111         RenderVisitor VIPguess(m_renderbackend, layer,cam);
00112         itree->applyVisitor(VIPguess);
00113     }
00114 
00115 }
00116