FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
quadtreerenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2019 by the FIFE team *
3  * http://www.fifengine.net *
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"
35 #include "model/structures/layer.h"
37 
38 #include "view/camera.h"
39 #include "quadtreerenderer.h"
42 
43 //credit to phoku for his NodeDisplay example which the visitor code is adapted from ( he coded the quadtree after all )
44 
45 namespace FIFE {
49  static Logger _log(LM_VIEWVIEW);
50 
51  QuadTreeRenderer::QuadTreeRenderer(RenderBackend* renderbackend, int32_t position):
52  RendererBase(renderbackend, position) {
53  setEnabled(false);
54  }
55 
57  RendererBase(old) {
58  setEnabled(false);
59  }
60 
62  return new QuadTreeRenderer(*this);
63  }
64 
67 
68  m_renderbackend = rb;
69  m_layer = layer;
70  m_camera = camera;
71  }
72 
74 
75  template<typename T> bool RenderVisitor::visit(QuadNode<T,InstanceTree::MIN_TREE_SIZE>* node, int32_t d) {
76 
77  if (d==0)
78  visited = 0;
79 
80  int32_t x = node->x();
81  int32_t y = node->y();
82  int32_t size = node->size();
83 
84  ++visited;
85  CellGrid *cg = m_layer->getCellGrid();
86 
87 
88  ExactModelCoordinate emc= cg->toMapCoordinates(ExactModelCoordinate( x,y) );//0.5 for each cell's half-width
89  ScreenPoint scrpt1 =m_camera->toScreenCoordinates( emc );
90  emc= cg->toMapCoordinates(ExactModelCoordinate( x,y+size) );// this size usage is wrong.. me thinks
91  ScreenPoint scrpt2 =m_camera->toScreenCoordinates( emc );
92  emc= cg->toMapCoordinates(ExactModelCoordinate( x+size,y) );
93  ScreenPoint scrpt3 =m_camera->toScreenCoordinates( emc );
94  emc= cg->toMapCoordinates(ExactModelCoordinate( x+size,y+size) );
95  ScreenPoint scrpt4 =m_camera->toScreenCoordinates( emc );
96 
97  m_renderbackend->drawLine(Point(scrpt1.x,scrpt1.y), Point(scrpt2.x,scrpt2.y), 255, 255, 255);
98  m_renderbackend->drawLine(Point(scrpt1.x,scrpt1.y), Point(scrpt3.x,scrpt3.y), 255, 255, 255);
99  m_renderbackend->drawLine(Point(scrpt3.x,scrpt3.y), Point(scrpt4.x,scrpt4.y), 255, 255, 255);
100  m_renderbackend->drawLine(Point(scrpt2.x,scrpt2.y), Point(scrpt4.x,scrpt4.y), 255, 255, 255);
101 
102  return true;
103  }
104 
105 
106  void QuadTreeRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
107  CellGrid* cg = layer->getCellGrid();
108  if (!cg) {
109  FL_WARN(_log, "No cellgrid assigned to layer, cannot draw grid");
110  return;
111  }
112  InstanceTree * itree = layer->getInstanceTree();
113  RenderVisitor VIPguess(m_renderbackend, layer,cam);
114  itree->applyVisitor(VIPguess);
115  }
116 
117 }
118 
#define FL_WARN(logger, msg)
Definition: logger.h:72
void applyVisitor(Visitor &visitor)
See QuadNode::apply_visitor.
Definition: instancetree.h:93
void render(Camera *cam, Layer *layer, RenderList &instances)
This method is called by the view to ask renderer to draw its rendering aspect based on given paramet...
RendererBase * clone()
Makes copy of this renderer.
Abstract interface for all the renderbackends.
std::vector< RenderItem * > RenderList
Definition: renderitem.h:130
static Logger _log(LM_AUDIO)
Camera describes properties of a view port shown in the main screen Main screen can have multiple cam...
Definition: camera.h:59
RenderBackend * m_renderbackend
Definition: rendererbase.h:171
PointType2D< int32_t > Point
Definition: point.h:195
Base class for all view renderers View renderer renders one aspect of the view shown on screen...
Definition: rendererbase.h:78
int32_t size() const
Return the size (width and height) of the node.
Definition: quadtree.h:112
A basic layer on a map.
Definition: layer.h:99
virtual ~QuadTreeRenderer()
Destructor.
virtual void drawLine(const Point &p1, const Point &p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws line between given points with given RGBA.
virtual void setEnabled(bool enabled)
Enables renderer.
QuadTree Node.
Definition: quadtree.h:41
QuadTreeRenderer(RenderBackend *renderbackend, int32_t position)
constructor.
InstanceTree * getInstanceTree(void) const
Get the instance tree.
Definition: layer.cpp:101
DoublePoint3D ExactModelCoordinate
Definition: modelcoords.h:37
CellGrid * getCellGrid() const
Get the Cellgrid.
Definition: layer.cpp:93
int32_t y() const
Return the Y position of the node.
Definition: quadtree.h:108
RenderVisitor(RenderBackend *rb, Layer *layer, Camera *camera)
ExactModelCoordinate toMapCoordinates(const ModelCoordinate &layer_coords)
Transforms given point from layer coordinates to map coordinates.
Definition: cellgrid.cpp:75
int32_t x() const
Return the X position of the node.
Definition: quadtree.h:104
bool visit(QuadNode< T, InstanceTree::MIN_TREE_SIZE > *node, int32_t d)