Debian Codegraph @

engine/core/view/renderers/targetrenderer.cpp

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