FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
genericrenderer.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 "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  if (renderbackend->getLightingModel() > 0) {
69  renderbackend->changeRenderInfos(RENDER_DATA_WITHOUT_Z, 1, 4, 5, false, false, 0, KEEP, ALWAYS);
70  }
71  }
72  }
73 
76  m_anchor(anchor),
77  m_red(r),
78  m_green(g),
79  m_blue(b),
80  m_alpha(a) {
81  }
82  void GenericRendererPointInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
83  Point p = m_anchor.getCalculatedPoint(cam, layer);
84  if(m_anchor.getLayer() == layer) {
85  renderbackend->putPixel(p.x, p.y, m_red, m_green, m_blue, m_alpha);
86  if (renderbackend->getLightingModel() > 0) {
87  renderbackend->changeRenderInfos(RENDER_DATA_WITHOUT_Z, 1, 4, 5, false, false, 0, KEEP, ALWAYS);
88  }
89  }
90  }
91 
94  m_edge1(n1),
95  m_edge2(n2),
96  m_edge3(n3),
97  m_red(r),
98  m_green(g),
99  m_blue(b),
100  m_alpha(a) {
101  }
102  void GenericRendererTriangleInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
103  Point p1 = m_edge1.getCalculatedPoint(cam, layer);
104  Point p2 = m_edge2.getCalculatedPoint(cam, layer);
105  Point p3 = m_edge3.getCalculatedPoint(cam, layer);
106  if(m_edge1.getLayer() == layer) {
107  renderbackend->drawTriangle(p1, p2, p3, m_red, m_green, m_blue, m_alpha);
108  if (renderbackend->getLightingModel() > 0) {
109  renderbackend->changeRenderInfos(RENDER_DATA_WITHOUT_Z, 1, 4, 5, false, false, 0, KEEP, ALWAYS);
110  }
111  }
112  }
113 
116  m_edge1(n1),
117  m_edge2(n2),
118  m_edge3(n3),
119  m_edge4(n4),
120  m_red(r),
121  m_green(g),
122  m_blue(b),
123  m_alpha(a) {
124  }
125  void GenericRendererQuadInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
126  Point p1 = m_edge1.getCalculatedPoint(cam, layer);
127  Point p2 = m_edge2.getCalculatedPoint(cam, layer);
128  Point p3 = m_edge3.getCalculatedPoint(cam, layer);
129  Point p4 = m_edge4.getCalculatedPoint(cam, layer);
130  if(m_edge1.getLayer() == layer) {
131  renderbackend->drawQuad(p1, p2, p3, p4, m_red, m_green, m_blue, m_alpha);
132  if (renderbackend->getLightingModel() > 0) {
133  renderbackend->changeRenderInfos(RENDER_DATA_WITHOUT_Z, 1, 4, 5, false, false, 0, KEEP, ALWAYS);
134  }
135  }
136  }
137 
140  m_center(center),
141  m_size(size),
142  m_red(r),
143  m_green(g),
144  m_blue(b),
145  m_alpha(a) {
146  }
147  void GenericRendererVertexInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
148  Point p = m_center.getCalculatedPoint(cam, layer);
149  if(m_center.getLayer() == layer) {
150  renderbackend->drawVertex(p, m_size, m_red, m_green, m_blue, m_alpha);
151  if (renderbackend->getLightingModel() > 0) {
152  renderbackend->changeRenderInfos(RENDER_DATA_WITHOUT_Z, 1, 4, 5, false, false, 0, KEEP, ALWAYS);
153  }
154  }
155  }
156 
159  m_anchor(anchor),
160  m_image(image),
161  m_zoomed(zoomed) {
162  }
163  void GenericRendererImageInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
164  Point p = m_anchor.getCalculatedPoint(cam, layer, m_zoomed);
165  if(m_anchor.getLayer() == layer) {
166  Rect r;
167  Rect viewport = cam->getViewPort();
168  uint32_t width, height;
169  if(m_zoomed) {
170  width = static_cast<uint32_t>(round(m_image->getWidth() * cam->getZoom()));
171  height = static_cast<uint32_t>(round(m_image->getHeight() * cam->getZoom()));
172  }
173  else {
174  width = m_image->getWidth();
175  height = m_image->getHeight();
176  }
177  r.x = p.x-width/2;
178  r.y = p.y-height/2;
179  r.w = width;
180  r.h = height;
181  if(r.intersects(viewport)) {
182  m_image->render(r);
183  }
184  }
185  }
186 
189  m_anchor(anchor),
190  m_animation(animation),
191  m_start_time(TimeManager::instance()->getTime()),
192  m_time_scale(1.0),
193  m_zoomed(zoomed) {
194  }
195  void GenericRendererAnimationInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
196  Point p = m_anchor.getCalculatedPoint(cam, layer, m_zoomed);
197  if(m_anchor.getLayer() == layer) {
198  int32_t animtime = scaleTime(m_time_scale, TimeManager::instance()->getTime() - m_start_time) % m_animation->getDuration();
199  ImagePtr img = m_animation->getFrameByTimestamp(animtime);
200  Rect r;
201  Rect viewport = cam->getViewPort();
202  uint32_t width, height;
203  if(m_zoomed) {
204  width = static_cast<uint32_t>(round(img->getWidth() * cam->getZoom()));
205  height = static_cast<uint32_t>(round(img->getHeight() * cam->getZoom()));
206  } else {
207  width = img->getWidth();
208  height = img->getHeight();
209  }
210  r.x = p.x-width/2;
211  r.y = p.y-height/2;
212  r.w = width;
213  r.h = height;
214  if(r.intersects(viewport)) {
215  img->render(r);
216  }
217  }
218  }
219 
220  GenericRendererTextInfo::GenericRendererTextInfo(RendererNode anchor, IFont* font, std::string text, bool zoomed):
222  m_anchor(anchor),
223  m_font(font),
224  m_text(text),
225  m_zoomed(zoomed) {
226  }
227  void GenericRendererTextInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
228  Point p = m_anchor.getCalculatedPoint(cam, layer, m_zoomed);
229  if(m_anchor.getLayer() == layer) {
231  Rect r;
232  Rect viewport = cam->getViewPort();
233  uint32_t width, height;
234  if(m_zoomed) {
235  width = static_cast<uint32_t>(round(img->getWidth() * cam->getZoom()));
236  height = static_cast<uint32_t>(round(img->getHeight() * cam->getZoom()));
237  } else {
238  width = img->getWidth();
239  height = img->getHeight();
240  }
241  r.x = p.x-width/2;
242  r.y = p.y-height/2;
243  r.w = width;
244  r.h = height;
245  if(r.intersects(viewport)) {
246  img->render(r);
247  if (renderbackend->getLightingModel() > 0) {
248  renderbackend->changeRenderInfos(RENDER_DATA_WITHOUT_Z, 1, 4, 5, false, false, 0, KEEP, ALWAYS);
249  }
250  }
251  }
252  }
253 
254  GenericRendererResizeInfo::GenericRendererResizeInfo(RendererNode anchor, ImagePtr image, int32_t width, int32_t height, bool zoomed):
256  m_anchor(anchor),
257  m_image(image),
258  m_width(width),
259  m_height(height),
260  m_zoomed(zoomed) {
261  }
262  void GenericRendererResizeInfo::render(Camera* cam, Layer* layer, RenderList& instances, RenderBackend* renderbackend) {
263  Point p = m_anchor.getCalculatedPoint(cam, layer, m_zoomed);
264  if(m_anchor.getLayer() == layer) {
265  Rect r;
266  Rect viewport = cam->getViewPort();
267  uint32_t width, height;
268  if(m_zoomed) {
269  width = static_cast<uint32_t>(round(m_width * cam->getZoom()));
270  height = static_cast<uint32_t>(round(m_height * cam->getZoom()));
271  } else {
272  width = m_width;
273  height = m_height;
274  }
275  r.x = p.x-width/2;
276  r.y = p.y-height/2;
277  r.w = width;
278  r.h = height;
279  if(r.intersects(viewport)) {
280  m_image->render(r);
281  }
282  }
283  }
284 
286  return dynamic_cast<GenericRenderer*>(cnt->getRenderer("GenericRenderer"));
287  }
288 
289  GenericRenderer::GenericRenderer(RenderBackend* renderbackend, int32_t position):
290  RendererBase(renderbackend, position),
291  m_groups() {
292  setEnabled(false);
293  }
294 
296  RendererBase(old),
297  m_groups() {
298  setEnabled(false);
299  }
300 
302  return new GenericRenderer(*this);
303  }
304 
306  }
307  void GenericRenderer::addLine(const std::string &group, RendererNode n1, RendererNode n2, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
308  GenericRendererElementInfo* info = new GenericRendererLineInfo(n1, n2, r, g, b, a);
309  m_groups[group].push_back(info);
310  }
311  void GenericRenderer::addPoint(const std::string &group, RendererNode n, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
312  GenericRendererElementInfo* info = new GenericRendererPointInfo(n, r, g, b, a);
313  m_groups[group].push_back(info);
314  }
315  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) {
316  GenericRendererElementInfo* info = new GenericRendererTriangleInfo(n1, n2, n3, r, g, b, a);
317  m_groups[group].push_back(info);
318  }
319  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) {
320  GenericRendererElementInfo* info = new GenericRendererQuadInfo(n1, n2, n3, n4, r, g, b, a);
321  m_groups[group].push_back(info);
322  }
323  void GenericRenderer::addVertex(const std::string &group, RendererNode n, int32_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
324  GenericRendererElementInfo* info = new GenericRendererVertexInfo(n, size, r, g, b, a);
325  m_groups[group].push_back(info);
326  }
327  void GenericRenderer::addText(const std::string &group, RendererNode n, IFont* font, const std::string &text, bool zoomed) {
328  GenericRendererElementInfo* info = new GenericRendererTextInfo(n, font, text, zoomed);
329  m_groups[group].push_back(info);
330  }
331  void GenericRenderer::addImage(const std::string &group, RendererNode n, ImagePtr image, bool zoomed) {
332  GenericRendererElementInfo* info = new GenericRendererImageInfo(n, image, zoomed);
333  m_groups[group].push_back(info);
334  }
335  void GenericRenderer::addAnimation(const std::string &group, RendererNode n, AnimationPtr animation, bool zoomed) {
336  GenericRendererElementInfo* info = new GenericRendererAnimationInfo(n, animation, zoomed);
337  m_groups[group].push_back(info);
338  }
339  void GenericRenderer::resizeImage(const std::string &group, RendererNode n, ImagePtr image, int32_t width, int32_t height, bool zoomed) {
340  GenericRendererElementInfo* info = new GenericRendererResizeInfo(n, image, width, height, zoomed);
341  m_groups[group].push_back(info);
342  }
343  void GenericRenderer::removeAll(const std::string &group) {
344  std::vector<GenericRendererElementInfo*>::const_iterator info_it = m_groups[group].begin();
345  for (;info_it != m_groups[group].end(); ++info_it) {
346  delete *info_it;
347  }
348  m_groups[group].clear();
349  m_groups.erase(group);
350  }
351  // Remove all groups
353  std::map<std::string, std::vector<GenericRendererElementInfo*> >::iterator it = m_groups.begin();
354  for (; it != m_groups.end(); ++it) {
355  std::vector<GenericRendererElementInfo*>::const_iterator info_it = it->second.begin();
356  for (;info_it != it->second.end(); ++info_it) {
357  delete *info_it;
358  }
359  }
360  m_groups.clear();
361  }
362  // Clear all groups
364  removeAll();
365  }
366 
367  void GenericRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
368  std::map<std::string, std::vector<GenericRendererElementInfo*> >::iterator group_it = m_groups.begin();
369  for(; group_it != m_groups.end(); ++group_it) {
370  std::vector<GenericRendererElementInfo*>::const_iterator info_it = group_it->second.begin();
371  for (;info_it != group_it->second.end(); ++info_it) {
372  (*info_it)->render(cam, layer, instances, m_renderbackend);
373  }
374  }
375  }
376 }
void render(Camera *cam, Layer *layer, RenderList &instances, RenderBackend *renderbackend)
Abstract interface for all the renderbackends.
void render(Camera *cam, Layer *layer, RenderList &instances, RenderBackend *renderbackend)
std::vector< RenderItem * > RenderList
Definition: renderitem.h:130
Base Class for Images.
Definition: image.h:48
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)
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:59
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.
bool intersects(const RectType< T > &rect) const
Check whether two rectangles share some area.
Definition: rect.h:227
RenderBackend * m_renderbackend
Definition: rendererbase.h:171
unsigned char uint8_t
Definition: core.h:38
virtual void changeRenderInfos(RenderDataType type, uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc, OverlayType otype=OVERLAY_TYPE_NONE)=0
Dirty helper function to change the render infos.
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:150
uint32_t getHeight() const
Definition: image.cpp:160
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:99
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.
T y
The Y Coordinate.
Definition: rect.h:87
double getZoom() const
Gets camera zoom.
Definition: camera.cpp:188
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.
const Rect & getViewPort() const
Gets the viewport for camera in pixel coordinates.
Definition: camera.cpp:289
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)
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)
uint32_t getDuration() const
Gets the total duration for the whole animation.
Definition: animation.h:138
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.
uint32_t getWidth() const
Definition: image.cpp:151
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.