FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
quadtreerenderer.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 
24 // 3rd party library includes
25 
26 // FIFE includes
27 // These includes are split up in two parts, separated by one empty line
28 // First block: files included from the FIFE root src directory
29 // Second block: files included from the same folder
30 #include "video/renderbackend.h"
31 #include "util/math/fife_math.h"
32 #include "util/log/logger.h"
33 #include "model/metamodel/grids/cellgrid.h"
34 #include "model/structures/instance.h"
35 #include "model/structures/layer.h"
36 #include "model/structures/location.h"
37 
38 #include "view/camera.h"
39 #include "quadtreerenderer.h"
40 #include "model/structures/instancetree.h"
41 #include "util/structures/quadtree.h"
42 
44 namespace FIFE {
45  static Logger _log(LM_VIEWVIEW);
46 
47  QuadTreeRenderer::QuadTreeRenderer(RenderBackend* renderbackend, int32_t position):
48  RendererBase(renderbackend, position) {
49  setEnabled(false);
50  }
51 
52  QuadTreeRenderer::QuadTreeRenderer(const QuadTreeRenderer& old):
53  RendererBase(old) {
54  setEnabled(false);
55  }
56 
57  RendererBase* QuadTreeRenderer::clone() {
58  return new QuadTreeRenderer(*this);
59  }
60 
61  QuadTreeRenderer::~QuadTreeRenderer() { }
62  RenderVisitor::RenderVisitor(RenderBackend * rb, Layer * layer, Camera *camera) {
63 
64  m_renderbackend = rb;
65  m_layer = layer;
66  m_camera = camera;
67  }
68 
69  RenderVisitor::~RenderVisitor() {}
70 
71  template<typename T> bool RenderVisitor::visit(QuadNode<T,InstanceTree::MIN_TREE_SIZE>* node, int32_t d) {
72 
73  if (d==0)
74  visited = 0;
75 
76  int32_t x = node->x();
77  int32_t y = node->y();
78  int32_t size = node->size();
79 
80  ++visited;
81  CellGrid *cg = m_layer->getCellGrid();
82 
83 
84  ExactModelCoordinate emc= cg->toMapCoordinates(ExactModelCoordinate( x,y) );//0.5 for each cell's half-width
85  ScreenPoint scrpt1 =m_camera->toScreenCoordinates( emc );
86  emc= cg->toMapCoordinates(ExactModelCoordinate( x,y+size) );// this size usage is wrong.. me thinks
87  ScreenPoint scrpt2 =m_camera->toScreenCoordinates( emc );
88  emc= cg->toMapCoordinates(ExactModelCoordinate( x+size,y) );
89  ScreenPoint scrpt3 =m_camera->toScreenCoordinates( emc );
90  emc= cg->toMapCoordinates(ExactModelCoordinate( x+size,y+size) );
91  ScreenPoint scrpt4 =m_camera->toScreenCoordinates( emc );
92 
93  m_renderbackend->drawLine(Point(scrpt1.x,scrpt1.y), Point(scrpt2.x,scrpt2.y), 255, 255, 255);
94  m_renderbackend->drawLine(Point(scrpt1.x,scrpt1.y), Point(scrpt3.x,scrpt3.y), 255, 255, 255);
95  m_renderbackend->drawLine(Point(scrpt3.x,scrpt3.y), Point(scrpt4.x,scrpt4.y), 255, 255, 255);
96  m_renderbackend->drawLine(Point(scrpt2.x,scrpt2.y), Point(scrpt4.x,scrpt4.y), 255, 255, 255);
97 
98  return true;
99  }
100 
101 
102  void QuadTreeRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
103  CellGrid* cg = layer->getCellGrid();
104  if (!cg) {
105  FL_WARN(_log, "No cellgrid assigned to layer, cannot draw grid");
106  return;
107  }
108  InstanceTree * itree = layer->getInstanceTree();
109  RenderVisitor VIPguess(m_renderbackend, layer,cam);
110  itree->applyVisitor(VIPguess);
111  }
112 
113 }
114 
virtual void setEnabled(bool enabled)