FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
targetrenderer.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2011 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/imagemanager.h"
32 #include "util/log/logger.h"
33 #include "view/camera.h"
34 
35 #include "targetrenderer.h"
36 
37 
38 namespace FIFE {
39  static Logger _log(LM_VIEWVIEW);
40 
41  RenderTarget::RenderTarget(RenderBackend* rb, const std::string& name, uint32_t width, uint32_t height):
42  m_renderbackend(rb) {
43  m_target = ImageManager::instance()->loadBlank(name, width, height);
44  }
45 
46  RenderTarget::RenderTarget(RenderBackend* rb, ImagePtr& image):
47  m_renderbackend(rb), m_target(image) {
48  }
49 
50  RenderTarget::~RenderTarget() {
51  }
52 
53  void RenderTarget::addLine(const std::string &group, Point n1, Point n2, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
54  OffRendererElementInfo* info = new OffRendererLineInfo(n1, n2, r, g, b, a);
55  m_groups[group].push_back(info);
56  }
57 
58  void RenderTarget::addPoint(const std::string &group, Point n, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
59  OffRendererElementInfo* info = new OffRendererPointInfo(n, r, g, b, a);
60  m_groups[group].push_back(info);
61  }
62 
63  void RenderTarget::addTriangle(const std::string &group, Point n1, Point n2, Point n3, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
64  OffRendererElementInfo* info = new OffRendererTriangleInfo(n1, n2, n3, r, g, b, a);
65  m_groups[group].push_back(info);
66  }
67 
68  void RenderTarget::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) {
69  OffRendererElementInfo* info = new OffRendererQuadInfo(n1, n2, n3, n4, r, g, b, a);
70  m_groups[group].push_back(info);
71  }
72 
73  void RenderTarget::addVertex(const std::string &group, Point n, int32_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
74  OffRendererElementInfo* info = new OffRendererVertexInfo(n, size, r, g, b, a);
75  m_groups[group].push_back(info);
76  }
77 
78  void RenderTarget::addText(const std::string &group, Point n, IFont* font, const std::string &text) {
79  OffRendererElementInfo* info = new OffRendererTextInfo(n, font, text);
80  m_groups[group].push_back(info);
81  }
82 
83  void RenderTarget::addImage(const std::string &group, Point n, ImagePtr image) {
84  OffRendererElementInfo* info = new OffRendererImageInfo(n, image);
85  m_groups[group].push_back(info);
86  }
87 
88  void RenderTarget::addAnimation(const std::string &group, Point n, AnimationPtr animation) {
89  OffRendererElementInfo* info = new OffRendererAnimationInfo(n, animation);
90  m_groups[group].push_back(info);
91  }
92 
93  void RenderTarget::resizeImage(const std::string &group, Point n, ImagePtr image, int32_t width, int32_t height) {
94  OffRendererElementInfo* info = new OffRendererResizeInfo(n, image, width, height);
95  m_groups[group].push_back(info);
96  }
97 
98  void RenderTarget::removeAll(const std::string &group) {
99  std::vector<OffRendererElementInfo*>::const_iterator info_it = m_groups[group].begin();
100  for (;info_it != m_groups[group].end(); ++info_it) {
101  delete *info_it;
102  }
103  m_groups[group].clear();
104  m_groups.erase(group);
105  }
106 
107  void RenderTarget::removeAll() {
108  m_groups.clear();
109  }
110 
111  void RenderTarget::render() {
112  std::map<std::string, std::vector<OffRendererElementInfo*> >::iterator group_it = m_groups.begin();
113  for(; group_it != m_groups.end(); ++group_it) {
114  std::vector<OffRendererElementInfo*>::const_iterator info_it = group_it->second.begin();
115  for (; info_it != group_it->second.end(); ++info_it) {
116  (*info_it)->render(m_renderbackend);
117  }
118  }
119  }
120 
121  TargetRenderer::TargetRenderer(RenderBackend* renderbackend) :
122  m_renderbackend(renderbackend) {
123  }
124 
125  TargetRenderer::~TargetRenderer() {
126  }
127 
128  RenderTargetPtr TargetRenderer::createRenderTarget(const std::string& name, uint32_t width, uint32_t height) {
129  RenderJob rj;
130  rj.ndraws = -1;
131  rj.lasttime_draw = 1;
132  rj.target = RenderTargetPtr(new RenderTarget(m_renderbackend, name, width, height));
133  rj.discard = false;
134 
135  std::pair<RenderJobMap::iterator, bool> ret =
136  m_targets.insert(std::make_pair<std::string, RenderJob>(name, rj));
137 
138  return ret.first->second.target;
139  }
140 
141  RenderTargetPtr TargetRenderer::createRenderTarget(ImagePtr& image) {
142  RenderJob rj;
143  rj.ndraws = -1;
144  rj.lasttime_draw = 1;
145  rj.target = RenderTargetPtr(new RenderTarget(m_renderbackend, image));
146  rj.discard = false;
147 
148  std::pair<RenderJobMap::iterator, bool> ret =
149  m_targets.insert(std::make_pair<std::string, RenderJob>(image->getName(), rj));
150 
151  return ret.first->second.target;
152  }
153 
154  void TargetRenderer::setRenderTarget(const std::string& targetname, bool discard, int32_t ndraws) {
155  RenderJobMap::iterator it = m_targets.find(targetname);
156  if (it != m_targets.end()) {
157  it->second.ndraws = ndraws;
158  it->second.discard = discard;
159  }
160  }
161 
162  void TargetRenderer::render() {
163  if (!m_targets.empty()) {
164  for (RenderJobMap::iterator it = m_targets.begin(); it != m_targets.end(); ++it) {
165  if (it->second.ndraws != -1) {
166  if (it->second.ndraws <= it->second.lasttime_draw) {
167  RenderTargetPtr rt = it->second.target;
168  m_renderbackend->attachRenderTarget(rt->m_target, it->second.discard);
169  rt->render();
170  m_renderbackend->detachRenderTarget();
171 
172  if(it->second.ndraws == 0) {
173  it->second.ndraws = -1;
174  } else {
175  it->second.lasttime_draw = 1;
176  }
177  } else {
178  ++it->second.lasttime_draw;
179  }
180  }
181  }
182  }
183  }
184 }