FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
offrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 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/fonts/ifont.h"
32 #include "video/image.h"
33 #include "video/imagemanager.h"
34 #include "util/math/fife_math.h"
35 #include "util/log/logger.h"
36 #include "util/time/timemanager.h"
38 
39 #include "offrenderer.h"
40 
41 
42 namespace FIFE {
46  static Logger _log(LM_VIEWVIEW);
47 
50  m_edge1(n1),
51  m_edge2(n2),
52  m_red(r),
53  m_green(g),
54  m_blue(b),
55  m_alpha(a) {
56  }
58  renderbackend->drawLine(m_edge1, m_edge2, m_red, m_green, m_blue, m_alpha);
59  }
60 
63  m_anchor(anchor),
64  m_red(r),
65  m_green(g),
66  m_blue(b),
67  m_alpha(a) {
68  }
70  renderbackend->putPixel(m_anchor.x, m_anchor.y, m_red, m_green, m_blue, m_alpha);
71  }
72 
75  m_edge1(n1),
76  m_edge2(n2),
77  m_edge3(n3),
78  m_red(r),
79  m_green(g),
80  m_blue(b),
81  m_alpha(a) {
82  }
85  }
86 
89  m_edge1(n1),
90  m_edge2(n2),
91  m_edge3(n3),
92  m_edge4(n4),
93  m_red(r),
94  m_green(g),
95  m_blue(b),
96  m_alpha(a) {
97  }
100  }
101 
104  m_center(center),
105  m_size(size),
106  m_red(r),
107  m_green(g),
108  m_blue(b),
109  m_alpha(a) {
110  }
112  renderbackend->drawVertex(m_center, m_size, m_red, m_green, m_blue, m_alpha);
113  }
114 
117  m_anchor(anchor),
118  m_image(image) {
119  }
121  Rect r;
122  uint16_t width = m_image->getWidth();
123  uint16_t height = m_image->getHeight();
124  r.x = m_anchor.x-width/2;
125  r.y = m_anchor.y-height/2;
126  r.w = width;
127  r.h = height;
128 
129  m_image->render(r);
130  }
131 
134  m_anchor(anchor),
135  m_animation(animation),
136  m_start_time(TimeManager::instance()->getTime()),
137  m_time_scale(1.0) {
138  }
140  int32_t animtime = scaleTime(m_time_scale, TimeManager::instance()->getTime() - m_start_time) % m_animation->getDuration();
141  ImagePtr img = m_animation->getFrameByTimestamp(animtime);
142 
143  Rect r;
144  uint16_t width = img->getWidth();
145  uint16_t height = img->getHeight();
146  r.x = m_anchor.x-width/2;
147  r.y = m_anchor.y-height/2;
148  r.w = width;
149  r.h = height;
150 
151  img->render(r);
152  }
153 
154  OffRendererTextInfo::OffRendererTextInfo(Point anchor, IFont* font, std::string text):
156  m_anchor(anchor),
157  m_font(font),
158  m_text(text) {
159  }
162 
163  Rect r;
164  uint16_t width = img->getWidth();
165  uint16_t height = img->getHeight();
166  r.x = m_anchor.x-width/2;
167  r.y = m_anchor.y-height/2;
168  r.w = width;
169  r.h = height;
170 
171  img->render(r);
172  }
173 
174  OffRendererResizeInfo::OffRendererResizeInfo(Point anchor, ImagePtr image, int32_t width, int32_t height):
176  m_anchor(anchor),
177  m_image(image),
178  m_width(width),
179  m_height(height){
180  }
182  Rect r;
183  uint16_t width = m_width;
184  uint16_t height = m_height;
185  r.x = m_anchor.x-width/2;
186  r.y = m_anchor.y-height/2;
187  r.w = width;
188  r.h = height;
189 
190  m_image->render(r);
191  }
192 
194  m_groups(),
195  m_renderbackend(renderbackend),
196  m_enabled(false),
197  m_area(renderbackend->getArea()) {
198  }
199 
201  removeAll();
202  }
203 
204  void OffRenderer::setEnabled(bool enabled) {
205  m_enabled = enabled;
206  }
207 
209  return m_enabled;
210  }
211 
213  m_area = area;
214  }
215 
217  return m_area;
218  }
219 
220  void OffRenderer::addLine(const std::string &group, Point n1, Point n2, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
221  OffRendererElementInfo* info = new OffRendererLineInfo(n1, n2, r, g, b, a);
222  m_groups[group].push_back(info);
223  }
224  void OffRenderer::addPoint(const std::string &group, Point n, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
225  OffRendererElementInfo* info = new OffRendererPointInfo(n, r, g, b, a);
226  m_groups[group].push_back(info);
227  }
228  void OffRenderer::addTriangle(const std::string &group, Point n1, Point n2, Point n3, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
229  OffRendererElementInfo* info = new OffRendererTriangleInfo(n1, n2, n3, r, g, b, a);
230  m_groups[group].push_back(info);
231  }
232  void OffRenderer::addQuad(const std::string &group, Point n1, Point n2, Point n3, Point n4, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
233  OffRendererElementInfo* info = new OffRendererQuadInfo(n1, n2, n3, n4, r, g, b, a);
234  m_groups[group].push_back(info);
235  }
236  void OffRenderer::addVertex(const std::string &group, Point n, int32_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
237  OffRendererElementInfo* info = new OffRendererVertexInfo(n, size, r, g, b, a);
238  m_groups[group].push_back(info);
239  }
240  void OffRenderer::addText(const std::string &group, Point n, IFont* font, const std::string &text) {
241  OffRendererElementInfo* info = new OffRendererTextInfo(n, font, text);
242  m_groups[group].push_back(info);
243  }
244  void OffRenderer::addImage(const std::string &group, Point n, ImagePtr image) {
245  OffRendererElementInfo* info = new OffRendererImageInfo(n, image);
246  m_groups[group].push_back(info);
247  }
248  void OffRenderer::addAnimation(const std::string &group, Point n, AnimationPtr animation) {
249  OffRendererElementInfo* info = new OffRendererAnimationInfo(n, animation);
250  m_groups[group].push_back(info);
251  }
252  void OffRenderer::resizeImage(const std::string &group, Point n, ImagePtr image, int32_t width, int32_t height) {
253  OffRendererElementInfo* info = new OffRendererResizeInfo(n, image, width, height);
254  m_groups[group].push_back(info);
255  }
256  void OffRenderer::removeAll(const std::string &group) {
257  std::vector<OffRendererElementInfo*>::const_iterator info_it = m_groups[group].begin();
258  for (;info_it != m_groups[group].end(); ++info_it) {
259  delete *info_it;
260  }
261  m_groups[group].clear();
262  m_groups.erase(group);
263  }
265  m_groups.clear();
266  }
267 
269  if (!m_enabled) {
270  return;
271  }
273  std::map<std::string, std::vector<OffRendererElementInfo*> >::iterator group_it = m_groups.begin();
274  for(; group_it != m_groups.end(); ++group_it) {
275  std::vector<OffRendererElementInfo*>::const_iterator info_it = group_it->second.begin();
276  for (;info_it != group_it->second.end(); ++info_it) {
277  (*info_it)->render(m_renderbackend);
278  }
279  }
282  }
283 }
OffRendererVertexInfo(Point center, int32_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Abstract interface for all the renderbackends.
Base Class for Images.
Definition: image.h:48
OffRendererImageInfo(Point n, ImagePtr image)
T h
Height of the rectangle.
Definition: rect.h:93
void render(RenderBackend *renderbackend)
T x
The X Coordinate.
Definition: rect.h:84
uint32_t getDuration() const
Gets the total duration for the whole animation.
Definition: animation.h:138
void addText(const std::string &group, Point n, IFont *font, const std::string &text)
static Logger _log(LM_AUDIO)
virtual Image * getAsImageMultiline(const std::string &text)=0
Gets given text as Image.
OffRendererResizeInfo(Point n, ImagePtr image, int32_t width, int32_t height)
OffRendererLineInfo(Point n1, Point n2, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Definition: offrenderer.cpp:48
void setEnabled(bool enabled)
void render(RenderBackend *renderbackend)
Definition: offrenderer.cpp:57
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.
OffRendererQuadInfo(Point n1, Point n2, Point n3, Point n4, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Definition: offrenderer.cpp:87
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:160
void render(RenderBackend *renderbackend)
void addTriangle(const std::string &group, Point n1, Point n2, Point n3, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
unsigned char uint8_t
Definition: core.h:38
ImagePtr getFrameByTimestamp(uint32_t timestamp)
Gets the frame image that matches the given timestamp.
Definition: animation.cpp:150
void pushClipArea(const Rect &cliparea, bool clear=true)
Pushes clip area to clip stack Clip areas define which area is drawn on screen.
std::map< std::string, std::vector< OffRendererElementInfo * > > m_groups
Definition: offrenderer.h:185
void render(RenderBackend *renderbackend)
Definition: offrenderer.cpp:83
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(RenderBackend *renderbackend)
OffRendererAnimationInfo(Point n, AnimationPtr animation)
OffRendererPointInfo(Point n, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Definition: offrenderer.cpp:61
void popClipArea()
Pops clip area from clip stack.
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.
uint32_t getWidth() const
Definition: image.cpp:151
void render(RenderBackend *renderbackend)
unsigned short uint16_t
Definition: core.h:39
T y
The Y Coordinate.
Definition: rect.h:87
void render(RenderBackend *renderbackend)
void render(RenderBackend *renderbackend)
Definition: offrenderer.cpp:98
Time Manager.
Definition: timemanager.h:50
void addPoint(const std::string &group, Point n, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
OffRendererTextInfo(Point n, IFont *font, std::string text)
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...
RenderBackend * m_renderbackend
Definition: offrenderer.h:186
void addQuad(const std::string &group, Point n1, Point n2, Point n3, Point n4, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
void render(RenderBackend *renderbackend)
Definition: offrenderer.cpp:69
void setClipArea(Rect area)
virtual void renderVertexArrays()=0
Render the Vertex Arrays, only for primitives (points, lines,...)
void addImage(const std::string &group, Point n, ImagePtr image)
void resizeImage(const std::string &group, Point n, ImagePtr image, int32_t width, int32_t height)
uint32_t scaleTime(float multiplier, uint32_t ticks)
Utility function to calculate time scaling.
void addAnimation(const std::string &group, Point n, AnimationPtr animation)
OffRendererTriangleInfo(Point n1, Point n2, Point n3, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Definition: offrenderer.cpp:73
OffRenderer(RenderBackend *renderbackend)
constructor.
T w
Width of the rectangle.
Definition: rect.h:90
virtual ~OffRenderer()
Destructor.
void addVertex(const std::string &group, Point n, int32_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
void addLine(const std::string &group, Point n1, Point n2, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
const Rect & getClipArea() const
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.