FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
genericrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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 "video/animation.h"
32 #include "video/fonts/ifont.h"
33 #include "video/image.h"
34 #include "video/imagemanager.h"
35 #include "util/math/fife_math.h"
36 #include "util/log/logger.h"
37 #include "util/time/timemanager.h"
41 #include "model/structures/layer.h"
43 
44 #include "view/camera.h"
45 #include "genericrenderer.h"
46 
47 
48 namespace FIFE {
52  static Logger _log(LM_VIEWVIEW);
53 
56  m_edge1(n1),
57  m_edge2(n2),
58  m_red(r),
59  m_green(g),
60  m_blue(b),
61  m_alpha(a) {
62  }
63  void GenericRendererLineInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
64  Point p1 = m_edge1.getCalculatedPoint(cam, layer);
65  Point p2 = m_edge2.getCalculatedPoint(cam, layer);
66  if(m_edge1.getLayer() == layer) {
67  renderbackend->drawLine(p1, p2, m_red, m_green, m_blue, m_alpha);
68  }
69  }
70 
73  m_anchor(anchor),
74  m_red(r),
75  m_green(g),
76  m_blue(b),
77  m_alpha(a) {
78  }
79  void GenericRendererPointInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
80  Point p = m_anchor.getCalculatedPoint(cam, layer);
81  if(m_anchor.getLayer() == layer) {
82  renderbackend->putPixel(p.x, p.y, m_red, m_green, m_blue, m_alpha);
83  }
84  }
85 
88  m_edge1(n1),
89  m_edge2(n2),
90  m_edge3(n3),
91  m_red(r),
92  m_green(g),
93  m_blue(b),
94  m_alpha(a) {
95  }
96  void GenericRendererTriangleInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
97  Point p1 = m_edge1.getCalculatedPoint(cam, layer);
98  Point p2 = m_edge2.getCalculatedPoint(cam, layer);
99  Point p3 = m_edge3.getCalculatedPoint(cam, layer);
100  if(m_edge1.getLayer() == layer) {
101  renderbackend->drawTriangle(p1, p2, p3, m_red, m_green, m_blue, m_alpha);
102  }
103  }
104 
107  m_edge1(n1),
108  m_edge2(n2),
109  m_edge3(n3),
110  m_edge4(n4),
111  m_red(r),
112  m_green(g),
113  m_blue(b),
114  m_alpha(a) {
115  }
116  void GenericRendererQuadInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
117  Point p1 = m_edge1.getCalculatedPoint(cam, layer);
118  Point p2 = m_edge2.getCalculatedPoint(cam, layer);
119  Point p3 = m_edge3.getCalculatedPoint(cam, layer);
120  Point p4 = m_edge4.getCalculatedPoint(cam, layer);
121  if(m_edge1.getLayer() == layer) {
122  renderbackend->drawQuad(p1, p2, p3, p4, m_red, m_green, m_blue, m_alpha);
123  }
124  }
125 
128  m_center(center),
129  m_size(size),
130  m_red(r),
131  m_green(g),
132  m_blue(b),
133  m_alpha(a) {
134  }
135  void GenericRendererVertexInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
136  Point p = m_center.getCalculatedPoint(cam, layer);
137  if(m_center.getLayer() == layer) {
138  renderbackend->drawVertex(p, m_size, m_red, m_green, m_blue, m_alpha);
139  }
140  }
141 
144  m_anchor(anchor),
145  m_image(image),
146  m_zoomed(zoomed) {
147  }
148  void GenericRendererImageInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
149  Point p = m_anchor.getCalculatedPoint(cam, layer, m_zoomed);
150  if(m_anchor.getLayer() == layer) {
151  Rect r;
152  Rect viewport = cam->getViewPort();
153  uint32_t width, height;
154  if(m_zoomed) {
155  width = static_cast<uint32_t>(round(m_image->getWidth() * cam->getZoom()));
156  height = static_cast<uint32_t>(round(m_image->getHeight() * cam->getZoom()));
157  }
158  else {
159  width = m_image->getWidth();
160  height = m_image->getHeight();
161  }
162  r.x = p.x-width/2;
163  r.y = p.y-height/2;
164  r.w = width;
165  r.h = height;
166  if(r.intersects(viewport)) {
167  m_image->render(r);
168  }
169  }
170  }
171 
174  m_anchor(anchor),
175  m_animation(animation),
176  m_start_time(TimeManager::instance()->getTime()),
177  m_time_scale(1.0),
178  m_zoomed(zoomed) {
179  }
180  void GenericRendererAnimationInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
181  Point p = m_anchor.getCalculatedPoint(cam, layer, m_zoomed);
182  if(m_anchor.getLayer() == layer) {
183  int32_t animtime = scaleTime(m_time_scale, TimeManager::instance()->getTime() - m_start_time) % m_animation->getDuration();
184  ImagePtr img = m_animation->getFrameByTimestamp(animtime);
185  Rect r;
186  Rect viewport = cam->getViewPort();
187  uint32_t width, height;
188  if(m_zoomed) {
189  width = static_cast<uint32_t>(round(img->getWidth() * cam->getZoom()));
190  height = static_cast<uint32_t>(round(img->getHeight() * cam->getZoom()));
191  } else {
192  width = img->getWidth();
193  height = img->getHeight();
194  }
195  r.x = p.x-width/2;
196  r.y = p.y-height/2;
197  r.w = width;
198  r.h = height;
199  if(r.intersects(viewport)) {
200  img->render(r);
201  }
202  }
203  }
204 
205  GenericRendererTextInfo::GenericRendererTextInfo(RendererNode anchor, IFont* font, std::string text, bool zoomed):
207  m_anchor(anchor),
208  m_font(font),
209  m_text(text),
210  m_zoomed(zoomed) {
211  }
212  void GenericRendererTextInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
213  Point p = m_anchor.getCalculatedPoint(cam, layer, m_zoomed);
214  if(m_anchor.getLayer() == layer) {
216  Rect r;
217  Rect viewport = cam->getViewPort();
218  uint32_t width, height;
219  if(m_zoomed) {
220  width = static_cast<uint32_t>(round(img->getWidth() * cam->getZoom()));
221  height = static_cast<uint32_t>(round(img->getHeight() * cam->getZoom()));
222  } else {
223  width = img->getWidth();
224  height = img->getHeight();
225  }
226  r.x = p.x-width/2;
227  r.y = p.y-height/2;
228  r.w = width;
229  r.h = height;
230  if(r.intersects(viewport)) {
231  img->render(r);
232  if (renderbackend->getLightingModel() > 0) {
233  renderbackend->changeRenderInfos(1, 4, 5, false, false, 0, KEEP, ALWAYS);
234  }
235  }
236  }
237  }
238 
239  GenericRendererResizeInfo::GenericRendererResizeInfo(RendererNode anchor, ImagePtr image, int32_t width, int32_t height, bool zoomed):
241  m_anchor(anchor),
242  m_image(image),
243  m_width(width),
244  m_height(height),
245  m_zoomed(zoomed) {
246  }
247  void GenericRendererResizeInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
248  Point p = m_anchor.getCalculatedPoint(cam, layer, m_zoomed);
249  if(m_anchor.getLayer() == layer) {
250  Rect r;
251  Rect viewport = cam->getViewPort();
252  uint32_t width, height;
253  if(m_zoomed) {
254  width = static_cast<uint32_t>(round(m_width * cam->getZoom()));
255  height = static_cast<uint32_t>(round(m_height * cam->getZoom()));
256  } else {
257  width = m_width;
258  height = m_height;
259  }
260  r.x = p.x-width/2;
261  r.y = p.y-height/2;
262  r.w = width;
263  r.h = height;
264  if(r.intersects(viewport)) {
265  m_image->render(r);
266  }
267  }
268  }
269 
271  return dynamic_cast<GenericRenderer*>(cnt->getRenderer("GenericRenderer"));
272  }
273 
274  GenericRenderer::GenericRenderer(RenderBackend* renderbackend, int32_t position):
275  RendererBase(renderbackend, position),
276  m_groups() {
277  setEnabled(false);
278  }
279 
281  RendererBase(old),
282  m_groups() {
283  setEnabled(false);
284  }
285 
287  return new GenericRenderer(*this);
288  }
289 
291  }
292  void GenericRenderer::addLine(const std::string &group, RendererNode n1, RendererNode n2, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
293  GenericRendererElementInfo* info = new GenericRendererLineInfo(n1, n2, r, g, b, a);
294  m_groups[group].push_back(info);
295  }
296  void GenericRenderer::addPoint(const std::string &group, RendererNode n, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
297  GenericRendererElementInfo* info = new GenericRendererPointInfo(n, r, g, b, a);
298  m_groups[group].push_back(info);
299  }
300  void GenericRenderer::addTriangle(const std::string &group, RendererNode n1, RendererNode n2, RendererNode n3, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
301  GenericRendererElementInfo* info = new GenericRendererTriangleInfo(n1, n2, n3, r, g, b, a);
302  m_groups[group].push_back(info);
303  }
304  void GenericRenderer::addQuad(const std::string &group, RendererNode n1, RendererNode n2, RendererNode n3, RendererNode n4, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
305  GenericRendererElementInfo* info = new GenericRendererQuadInfo(n1, n2, n3, n4, r, g, b, a);
306  m_groups[group].push_back(info);
307  }
308  void GenericRenderer::addVertex(const std::string &group, RendererNode n, int32_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
309  GenericRendererElementInfo* info = new GenericRendererVertexInfo(n, size, r, g, b, a);
310  m_groups[group].push_back(info);
311  }
312  void GenericRenderer::addText(const std::string &group, RendererNode n, IFont* font, const std::string &text, bool zoomed) {
313  GenericRendererElementInfo* info = new GenericRendererTextInfo(n, font, text, zoomed);
314  m_groups[group].push_back(info);
315  }
316  void GenericRenderer::addImage(const std::string &group, RendererNode n, ImagePtr image, bool zoomed) {
317  GenericRendererElementInfo* info = new GenericRendererImageInfo(n, image, zoomed);
318  m_groups[group].push_back(info);
319  }
320  void GenericRenderer::addAnimation(const std::string &group, RendererNode n, AnimationPtr animation, bool zoomed) {
321  GenericRendererElementInfo* info = new GenericRendererAnimationInfo(n, animation, zoomed);
322  m_groups[group].push_back(info);
323  }
324  void GenericRenderer::resizeImage(const std::string &group, RendererNode n, ImagePtr image, int32_t width, int32_t height, bool zoomed) {
325  GenericRendererElementInfo* info = new GenericRendererResizeInfo(n, image, width, height, zoomed);
326  m_groups[group].push_back(info);
327  }
328  void GenericRenderer::removeAll(const std::string &group) {
329  std::vector<GenericRendererElementInfo*>::const_iterator info_it = m_groups[group].begin();
330  for (;info_it != m_groups[group].end(); ++info_it) {
331  delete *info_it;
332  }
333  m_groups[group].clear();
334  m_groups.erase(group);
335  }
336  // Remove all groups
338  m_groups.clear();
339  }
340  // Clear all groups
342  removeAll();
343  }
344 
345  void GenericRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
346  std::map<std::string, std::vector<GenericRendererElementInfo*> >::iterator group_it = m_groups.begin();
347  for(; group_it != m_groups.end(); ++group_it) {
348  std::vector<GenericRendererElementInfo*>::const_iterator info_it = group_it->second.begin();
349  for (;info_it != group_it->second.end(); ++info_it) {
350  (*info_it)->render(cam, layer, instances, m_renderbackend);
351  }
352  }
353  }
354 }
void render(Camera *cam, Layer *layer, RenderList &instances, RenderBackend *renderbackend)
Abstract interface for all the renderbackends.
Definition: renderbackend.h:92
void render(Camera *cam, Layer *layer, RenderList &instances, RenderBackend *renderbackend)
std::vector< RenderItem * > RenderList
Definition: renderitem.h:82
virtual void changeRenderInfos(uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc)=0
Dirty helper function to change the render infos.
Base Class for Images.
Definition: image.h:47
void render(Camera *cam, Layer *layer, RenderList &instances, RenderBackend *renderbackend)
T h
Height of the rectangle.
Definition: rect.h:93
GenericRendererTriangleInfo(RendererNode n1, RendererNode n2, RendererNode n3, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
GenericRendererLineInfo(RendererNode n1, RendererNode n2, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
T x
The X Coordinate.
Definition: rect.h:84
GenericRendererTextInfo(RendererNode n, IFont *font, std::string text, bool zoomed=true)
uint32_t getDuration() const
Gets the total duration for the whole animation.
Definition: animation.h:129
Point getCalculatedPoint(Camera *cam, Layer *layer, const bool zoomed=false)
void removeAll()
Removes all elements.
void render(Camera *cam, Layer *layer, RenderList &instances, RenderBackend *renderbackend)
void render(Camera *cam, Layer *layer, RenderList &instances, RenderBackend *renderbackend)
Interface to class owning the renderers Used to get correct subclass of renderer in scripting side (v...
Definition: rendererbase.h:66
static Logger _log(LM_AUDIO)
virtual Image * getAsImageMultiline(const std::string &text)=0
Gets given text as Image.
virtual RendererBase * getRenderer(const std::string &renderername)=0
Returns renderer with given name.
void addAnimation(const std::string &group, RendererNode n, AnimationPtr animation, bool zoomed=true)
Adds an animation.
Camera describes properties of a view port shown in the main screen Main screen can have multiple cam...
Definition: camera.h:58
static TimeManager * instance()
Definition: singleton.h:84
virtual void drawVertex(const Point &p, const uint8_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws a quad that represents a vertex with given RGBA.
virtual uint32_t getLightingModel() const =0
Gets the current light model.
virtual void drawTriangle(const Point &p1, const Point &p2, const Point &p3, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws triangle between given points with given RGBA.
uint32_t getHeight() const
Definition: image.cpp:155
RenderBackend * m_renderbackend
Definition: rendererbase.h:171
unsigned char uint8_t
Definition: core.h:38
const Rect & getViewPort() const
Gets the viewport for camera in pixel coordinates.
Definition: camera.cpp:299
GenericRendererResizeInfo(RendererNode n, ImagePtr image, int32_t width, int32_t height, bool zoomed=true)
ImagePtr getFrameByTimestamp(uint32_t timestamp)
Gets the frame image that matches the given timestamp.
Definition: animation.cpp:99
Base class for all view renderers View renderer renders one aspect of the view shown on screen...
Definition: rendererbase.h:78
void reset()
Resets the renderer.
A basic layer on a map.
Definition: layer.h:98
double getZoom() const
Gets camera zoom.
Definition: camera.cpp:168
GenericRendererVertexInfo(RendererNode center, int32_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
GenericRenderer(RenderBackend *renderbackend, int32_t position)
Constructor.
void addTriangle(const std::string &group, RendererNode n1, RendererNode n2, RendererNode n3, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Adds a triangle.
virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Writes pixel to given position.
void render(Camera *cam, Layer *layer, RenderList &instances, RenderBackend *renderbackend)
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.
uint32_t getWidth() const
Definition: image.cpp:146
T y
The Y Coordinate.
Definition: rect.h:87
void addLine(const std::string &group, RendererNode n1, RendererNode n2, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Adds a line.
static GenericRenderer * getInstance(IRendererContainer *cnt)
Gets instance for interface access.
Time Manager.
Definition: timemanager.h:50
GenericRendererQuadInfo(RendererNode n1, RendererNode n2, RendererNode n3, RendererNode n4, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
void addQuad(const std::string &group, RendererNode n1, RendererNode n2, RendererNode n3, RendererNode n4, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Adds a quad.
GenericRendererPointInfo(RendererNode n, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
bool intersects(const RectType< T > &rect) const
Check whether two rectangles share some area.
Definition: rect.h:227
Pure abstract Font interface.
Definition: ifont.h:43
virtual void render(const Rect &rect, uint8_t alpha=255, uint8_t const *rgb=0)=0
Renders itself to the current render target (main screen or attached destination image) at the rectan...
void render(Camera *cam, Layer *layer, RenderList &instances, RenderBackend *renderbackend)
void render(Camera *cam, Layer *layer, RenderList &instances, RenderBackend *renderbackend)
std::map< std::string, std::vector< GenericRendererElementInfo * > > m_groups
A map that holds the groups together with the appended render elements.
void addVertex(const std::string &group, RendererNode n, int32_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Adds a vertex.
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.
void resizeImage(const std::string &group, RendererNode n, ImagePtr image, int32_t width, int32_t height, bool zoomed=true)
Adds an image with another size.
GenericRendererAnimationInfo(RendererNode n, AnimationPtr animation, bool zoomed=true)
void addText(const std::string &group, RendererNode n, IFont *font, const std::string &text, bool zoomed=true)
Adds text.
GenericRendererImageInfo(RendererNode n, ImagePtr image, bool zoomed=true)
uint32_t scaleTime(float multiplier, uint32_t ticks)
Utility function to calculate time scaling.
unsigned int uint32_t
Definition: core.h:40
void render(Camera *cam, Layer *layer, RenderList &instances, RenderBackend *renderbackend)
void addImage(const std::string &group, RendererNode n, ImagePtr image, bool zoomed=true)
Adds an image.
T w
Width of the rectangle.
Definition: rect.h:90
void addPoint(const std::string &group, RendererNode n, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Adds a point.
virtual ~GenericRenderer()
Destructor.
virtual void drawQuad(const Point &p1, const Point &p2, const Point &p3, const Point &p4, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws quad between given points with given RGBA.