FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
offrenderer.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2011 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 "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"
37 #include "model/metamodel/timeprovider.h"
38 
39 #include "offrenderer.h"
40 
41 
42 namespace FIFE {
43  static Logger _log(LM_VIEWVIEW);
44 
45  OffRendererLineInfo::OffRendererLineInfo(Point n1, Point n2, uint8_t r, uint8_t g, uint8_t b, uint8_t a):
46  OffRendererElementInfo(),
47  m_edge1(n1),
48  m_edge2(n2),
49  m_red(r),
50  m_green(g),
51  m_blue(b),
52  m_alpha(a) {
53  }
54  void OffRendererLineInfo::render(RenderBackend* renderbackend) {
55  renderbackend->drawLine(m_edge1, m_edge2, m_red, m_green, m_blue, m_alpha);
56  }
57 
58  OffRendererPointInfo::OffRendererPointInfo(Point anchor, uint8_t r, uint8_t g, uint8_t b, uint8_t a):
59  OffRendererElementInfo(),
60  m_anchor(anchor),
61  m_red(r),
62  m_green(g),
63  m_blue(b),
64  m_alpha(a) {
65  }
66  void OffRendererPointInfo::render(RenderBackend* renderbackend) {
67  renderbackend->putPixel(m_anchor.x, m_anchor.y, m_red, m_green, m_blue, m_alpha);
68  }
69 
70  OffRendererTriangleInfo::OffRendererTriangleInfo(Point n1, Point n2, Point n3, uint8_t r, uint8_t g, uint8_t b, uint8_t a):
71  OffRendererElementInfo(),
72  m_edge1(n1),
73  m_edge2(n2),
74  m_edge3(n3),
75  m_red(r),
76  m_green(g),
77  m_blue(b),
78  m_alpha(a) {
79  }
80  void OffRendererTriangleInfo::render(RenderBackend* renderbackend) {
81  renderbackend->drawTriangle(m_edge1, m_edge2, m_edge3, m_red, m_green, m_blue, m_alpha);
82  }
83 
84  OffRendererQuadInfo::OffRendererQuadInfo(Point n1, Point n2, Point n3, Point n4, uint8_t r, uint8_t g, uint8_t b, uint8_t a):
85  OffRendererElementInfo(),
86  m_edge1(n1),
87  m_edge2(n2),
88  m_edge3(n3),
89  m_edge4(n4),
90  m_red(r),
91  m_green(g),
92  m_blue(b),
93  m_alpha(a) {
94  }
95  void OffRendererQuadInfo::render(RenderBackend* renderbackend) {
96  renderbackend->drawQuad(m_edge1, m_edge2, m_edge3, m_edge4, m_red, m_green, m_blue, m_alpha);
97  }
98 
99  OffRendererVertexInfo::OffRendererVertexInfo(Point center, int32_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a):
100  OffRendererElementInfo(),
101  m_center(center),
102  m_size(size),
103  m_red(r),
104  m_green(g),
105  m_blue(b),
106  m_alpha(a) {
107  }
108  void OffRendererVertexInfo::render(RenderBackend* renderbackend) {
109  renderbackend->drawVertex(m_center, m_size, m_red, m_green, m_blue, m_alpha);
110  }
111 
112  OffRendererImageInfo::OffRendererImageInfo(Point anchor, ImagePtr image):
113  OffRendererElementInfo(),
114  m_anchor(anchor),
115  m_image(image) {
116  }
117  void OffRendererImageInfo::render(RenderBackend* renderbackend) {
118  Rect r;
119  uint16_t width = m_image->getWidth();
120  uint16_t height = m_image->getHeight();
121  r.x = m_anchor.x-width/2;
122  r.y = m_anchor.y-height/2;
123  r.w = width;
124  r.h = height;
125 
126  m_image->render(r);
127  }
128 
129  OffRendererAnimationInfo::OffRendererAnimationInfo(Point anchor, AnimationPtr animation):
130  OffRendererElementInfo(),
131  m_anchor(anchor),
132  m_animation(animation),
133  m_start_time(TimeManager::instance()->getTime()),
134  m_time_scale(1.0) {
135  }
136  void OffRendererAnimationInfo::render(RenderBackend* renderbackend) {
137  int32_t animtime = scaleTime(m_time_scale, TimeManager::instance()->getTime() - m_start_time) % m_animation->getDuration();
138  ImagePtr img = m_animation->getFrameByTimestamp(animtime);
139 
140  Rect r;
141  uint16_t width = img->getWidth();
142  uint16_t height = img->getHeight();
143  r.x = m_anchor.x-width/2;
144  r.y = m_anchor.y-height/2;
145  r.w = width;
146  r.h = height;
147 
148  img->render(r);
149  }
150 
151  OffRendererTextInfo::OffRendererTextInfo(Point anchor, IFont* font, std::string text):
152  OffRendererElementInfo(),
153  m_anchor(anchor),
154  m_font(font),
155  m_text(text) {
156  }
157  void OffRendererTextInfo::render(RenderBackend* renderbackend) {
158  Image* img = m_font->getAsImageMultiline(m_text);
159 
160  Rect r;
161  uint16_t width = img->getWidth();
162  uint16_t height = img->getHeight();
163  r.x = m_anchor.x-width/2;
164  r.y = m_anchor.y-height/2;
165  r.w = width;
166  r.h = height;
167 
168  img->render(r);
169  }
170 
171  OffRendererResizeInfo::OffRendererResizeInfo(Point anchor, ImagePtr image, int32_t width, int32_t height):
172  OffRendererElementInfo(),
173  m_anchor(anchor),
174  m_image(image),
175  m_width(width),
176  m_height(height){
177  }
178  void OffRendererResizeInfo::render(RenderBackend* renderbackend) {
179  Rect r;
180  uint16_t width = m_width;
181  uint16_t height = m_height;
182  r.x = m_anchor.x-width/2;
183  r.y = m_anchor.y-height/2;
184  r.w = width;
185  r.h = height;
186 
187  m_image->render(r);
188  }
189 
190  OffRenderer::OffRenderer(RenderBackend* renderbackend):
191  m_groups(),
192  m_renderbackend(renderbackend),
193  m_enabled(false),
194  m_area(renderbackend->getArea()) {
195  }
196 
197  OffRenderer::~OffRenderer() {
198  removeAll();
199  }
200 
201  void OffRenderer::setEnabled(bool enabled) {
202  m_enabled = enabled;
203  }
204 
205  bool OffRenderer::isEnabled() {
206  return m_enabled;
207  }
208 
209  void OffRenderer::setClipArea(Rect area) {
210  m_area = area;
211  }
212 
213  const Rect& OffRenderer::getClipArea() const {
214  return m_area;
215  }
216 
217  void OffRenderer::addLine(const std::string &group, Point n1, Point n2, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
218  OffRendererElementInfo* info = new OffRendererLineInfo(n1, n2, r, g, b, a);
219  m_groups[group].push_back(info);
220  }
221  void OffRenderer::addPoint(const std::string &group, Point n, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
222  OffRendererElementInfo* info = new OffRendererPointInfo(n, r, g, b, a);
223  m_groups[group].push_back(info);
224  }
225  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) {
226  OffRendererElementInfo* info = new OffRendererTriangleInfo(n1, n2, n3, r, g, b, a);
227  m_groups[group].push_back(info);
228  }
229  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) {
230  OffRendererElementInfo* info = new OffRendererQuadInfo(n1, n2, n3, n4, r, g, b, a);
231  m_groups[group].push_back(info);
232  }
233  void OffRenderer::addVertex(const std::string &group, Point n, int32_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
234  OffRendererElementInfo* info = new OffRendererVertexInfo(n, size, r, g, b, a);
235  m_groups[group].push_back(info);
236  }
237  void OffRenderer::addText(const std::string &group, Point n, IFont* font, const std::string &text) {
238  OffRendererElementInfo* info = new OffRendererTextInfo(n, font, text);
239  m_groups[group].push_back(info);
240  }
241  void OffRenderer::addImage(const std::string &group, Point n, ImagePtr image) {
242  OffRendererElementInfo* info = new OffRendererImageInfo(n, image);
243  m_groups[group].push_back(info);
244  }
245  void OffRenderer::addAnimation(const std::string &group, Point n, AnimationPtr animation) {
246  OffRendererElementInfo* info = new OffRendererAnimationInfo(n, animation);
247  m_groups[group].push_back(info);
248  }
249  void OffRenderer::resizeImage(const std::string &group, Point n, ImagePtr image, int32_t width, int32_t height) {
250  OffRendererElementInfo* info = new OffRendererResizeInfo(n, image, width, height);
251  m_groups[group].push_back(info);
252  }
253  void OffRenderer::removeAll(const std::string &group) {
254  std::vector<OffRendererElementInfo*>::const_iterator info_it = m_groups[group].begin();
255  for (;info_it != m_groups[group].end(); ++info_it) {
256  delete *info_it;
257  }
258  m_groups[group].clear();
259  m_groups.erase(group);
260  }
261  void OffRenderer::removeAll() {
262  m_groups.clear();
263  }
264 
265  void OffRenderer::render() {
266  if (!m_enabled) {
267  return;
268  }
269  m_renderbackend->pushClipArea(m_area);
270  std::map<std::string, std::vector<OffRendererElementInfo*> >::iterator group_it = m_groups.begin();
271  for(; group_it != m_groups.end(); ++group_it) {
272  std::vector<OffRendererElementInfo*>::const_iterator info_it = group_it->second.begin();
273  for (;info_it != group_it->second.end(); ++info_it) {
274  (*info_it)->render(m_renderbackend);
275  }
276  }
277  m_renderbackend->renderVertexArrays();
278  m_renderbackend->popClipArea();
279  }
280 }
uint32_t scaleTime(float multiplier, uint32_t ticks)