File: renderbackend.cpp

package info (click to toggle)
fife 0.4.2-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,204 kB
  • sloc: cpp: 42,642; xml: 18,881; python: 13,521; makefile: 23
file content (375 lines) | stat: -rw-r--r-- 9,833 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/***************************************************************************
 *   Copyright (C) 2005-2019 by the FIFE team                              *
 *   http://www.fifengine.net                                              *
 *   This file is part of FIFE.                                            *
 *                                                                         *
 *   FIFE is free software; you can redistribute it and/or                 *
 *   modify it under the terms of the GNU Lesser General Public            *
 *   License as published by the Free Software Foundation; either          *
 *   version 2.1 of the License, or (at your option) any later version.    *
 *                                                                         *
 *   This library is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

// Standard C++ library includes

// 3rd party library includes

// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "renderbackend.h"
#include "video/devicecaps.h"

namespace FIFE {
	RenderBackend::RenderBackend(const SDL_Color& colorkey):
		m_window(NULL),
		m_screen(NULL),
		m_target(NULL),
		m_compressimages(false),
		m_useframebuffer(false),
		m_usenpot(false),
		m_isalphaoptimized(false),
		m_iscolorkeyenabled(false),
		m_colorkey(colorkey),
		m_isMipmapping(false),
		m_textureFilter(TEXTURE_FILTER_NONE),
		m_maxAnisotropy(0),
		m_monochrome(false),
		m_isDepthBuffer(false),
		m_alphaValue(0.3),
		m_vSync(false),
		m_isframelimit(false),
		m_frame_start(0),
		m_framelimit(60) {

		m_isbackgroundcolor = false;
		m_backgroundcolor.r = 0;
		m_backgroundcolor.g = 0;
		m_backgroundcolor.b = 0;
	}

	RenderBackend::~RenderBackend() {
	}

	void RenderBackend::deinit() {
		SDL_QuitSubSystem(SDL_INIT_VIDEO);
		SDL_Quit();
	}

	void RenderBackend::startFrame() {
		if (m_isframelimit) {
			m_frame_start = SDL_GetTicks();
		}
	}

	void RenderBackend::endFrame () {
		if (m_isframelimit) {
			uint16_t frame_time = SDL_GetTicks() - m_frame_start;
			const float frame_limit = 1000.0f/m_framelimit;
			if (frame_time < frame_limit) {
				SDL_Delay(static_cast<Uint32>(frame_limit) - frame_time);
			}
		}
	}

	const ScreenMode& RenderBackend::getCurrentScreenMode() const{
		return m_screenMode;
	}

	uint32_t RenderBackend::getWidth() const {
		return m_screen->w;
	}

	uint32_t RenderBackend::getHeight() const {
		return m_screen->h;
	}

	const Rect& RenderBackend::getArea() const {
		static Rect r(0, 0, m_screen->w, m_screen->h);
		return r;
	}

	void RenderBackend::pushClipArea(const Rect& cliparea, bool clear) {
		ClipInfo ci;
		ci.r = cliparea;
		ci.clearing = clear;
		m_clipstack.push(ci);
		setClipArea(cliparea, clear);
	}

	void RenderBackend::popClipArea() {
		assert(!m_clipstack.empty());
		m_clipstack.pop();
		if (m_clipstack.empty()) {
			setClipArea(getArea(), false);
		} else {
			ClipInfo ci = m_clipstack.top();
			// instead of using ci.clearing, we set it to false
			// to avoid double clearing
			setClipArea(ci.r, false);
		}
	}

	const Rect& RenderBackend::getClipArea() const {
		if (!m_clipstack.empty()) {
			return m_clipstack.top().r;
		} else {
			return getArea();
		}
	}

	void RenderBackend::clearClipArea() {
		setClipArea(getArea(), true);
	}

	void RenderBackend::setTextureFiltering(TextureFiltering filter) {
		m_textureFilter = filter;
	}

	TextureFiltering RenderBackend::getTextureFiltering() const {
		return m_textureFilter;
	}

	void RenderBackend::setMipmappingEnabled(bool enabled) {
		m_isMipmapping = enabled;
	}

	bool RenderBackend::isMipmappingEnabled() const {
		return m_isMipmapping;
	}
	
	int32_t RenderBackend::getMaxAnisotropy() const {
		return m_maxAnisotropy;
	}
	
	void RenderBackend::setMonochromeEnabled(bool enabled) {
		m_monochrome = enabled;
	}

	bool RenderBackend::isMonochromeEnabled() const {
		return m_monochrome;
	}

	void RenderBackend::setDepthBufferEnabled(bool enabled) {
		m_isDepthBuffer = enabled;
	}

	bool RenderBackend::isDepthBufferEnabled() const {
		return m_isDepthBuffer;
	}

	void RenderBackend::setAlphaTestValue(float alpha) {
		m_alphaValue = alpha;
	}

	float RenderBackend::getAlphaTestValue() const {
		return m_alphaValue;
	}

	void RenderBackend::setColorKeyEnabled(bool colorkeyenable) {
		m_iscolorkeyenabled = colorkeyenable;
	}

	bool RenderBackend::isColorKeyEnabled() const {
		return m_iscolorkeyenabled;
	}

	void RenderBackend::setColorKey(const SDL_Color& colorkey) {
		m_colorkey = colorkey;
	}

	const SDL_Color& RenderBackend::getColorKey() const {
		return m_colorkey;
	}

	void RenderBackend::setBackgroundColor(uint8_t r, uint8_t g, uint8_t b) {
		if (r != m_backgroundcolor.r || g != m_backgroundcolor.g || b != m_backgroundcolor.b) {
			m_isbackgroundcolor = true;
			m_backgroundcolor.r = r;
			m_backgroundcolor.g = g;
			m_backgroundcolor.b = b;
		}
	}

	void RenderBackend::resetBackgroundColor() {
		setBackgroundColor(0,0,0);
	}
	
	const SDL_PixelFormat& RenderBackend::getPixelFormat() const {
		return m_rgba_format;
	}

	void RenderBackend::setVSyncEnabled(bool vsync) {
		m_vSync = vsync;
	}

	bool RenderBackend::isVSyncEnabled() const {
		return m_vSync;
	}

	void RenderBackend::setFrameLimitEnabled(bool limited) {
		m_isframelimit = limited;
	}

	bool RenderBackend::isFrameLimitEnabled() const {
		return m_isframelimit;
	}

	void RenderBackend::setFrameLimit(uint16_t framelimit) {
		m_framelimit = framelimit;
	}

	uint16_t RenderBackend::getFrameLimit() const {
		return m_framelimit;
	}

	SDL_Surface* RenderBackend::getScreenSurface() {
		return m_screen;
	}

	SDL_Surface* RenderBackend::getRenderTargetSurface() {
		return m_target;
	}

	Point RenderBackend::getBezierPoint(const std::vector<Point>& points, int32_t elements, float t) {
		if (t < 0.0) {
			return points[0];
		} else if (t >= static_cast<double>(elements)) {
			return points.back();
		}

		// Interpolate
		double px = 0.0;
		double py = 0.0;
		int32_t n = elements - 1;
		double muk = 1.0;
		double mu = static_cast<double>(t) / static_cast<double>(elements);
		double munk = Mathd::Pow(1.0 - mu, static_cast<double>(n));
		for (int32_t i = 0; i <= n; ++i) {
			int32_t tmpn = n;
			int32_t tmpi = i;
			int32_t diffn = n - i;
			double blend = muk * munk;
			muk *= mu;
			munk /= 1.0 - mu;
			while (tmpn) {
				blend *= static_cast<double>(tmpn);
				tmpn--;
				if (tmpi > 1) {
					blend /= static_cast<double>(tmpi);
					tmpi--;
				}
				if (diffn > 1) {
					blend /= static_cast<double>(diffn);
					diffn--;
				}
			}
			px += static_cast<double>(points[i].x) * blend;
			py += static_cast<double>(points[i].y) * blend;
		}

		return Point(static_cast<int32_t>(px), static_cast<int32_t>(py));
	}

	void RenderBackend::addControlPoints(const std::vector<Point>& points, std::vector<Point>& newPoints) {
		if (points.empty()) {
			return;
		}

		int32_t n = points.size() - 1;
		// min 2 points
		if (n < 1) {
			return;
		}

		Point p;
		// straight line
		if (n == 1) {
			newPoints.push_back(points[0]);
			p.x = (2 * points[0].x + points[1].x) / 3;
			p.y = (2 * points[0].y + points[1].y) / 3;
			newPoints.push_back(p);
			p.x = 2 * p.x - points[0].x;
			p.y = 2 * p.y - points[0].y;
			newPoints.push_back(p);
			newPoints.push_back(points[1]);
			return;
		}

		// calculate x and y values
		float* xrhs = new float[n];
		float* yrhs = new float[n];
		// first
		xrhs[0] = points[0].x + 2 * points[1].x;
		yrhs[0] = points[0].y + 2 * points[1].y;
		// last
		xrhs[n - 1] = (8 * points[n - 1].x + points[n].x) / 2.0;
		yrhs[n - 1] = (8 * points[n - 1].y + points[n].y) / 2.0;
		// rest
		for (int32_t i = 1; i < n - 1; ++i) {
			xrhs[i] = 4 * points[i].x + 2 * points[i + 1].x;
			yrhs[i] = 4 * points[i].y + 2 * points[i + 1].y;
		}

		float* x = new float[n];
		float* y = new float[n];
		float* xtmp = new float[n];
		float* ytmp = new float[n];
		float xb = 2.0;
		float yb = 2.0;
		x[0] = xrhs[0] / xb;
		y[0] = yrhs[0] / yb;
		// Decomposition and forward substitution.
		for (int32_t i = 1; i < n; i++) {
			xtmp[i] = 1 / xb;
			ytmp[i] = 1 / yb;
			xb = (i < n - 1 ? 4.0 : 3.5) - xtmp[i];
			yb = (i < n - 1 ? 4.0 : 3.5) - ytmp[i];
			x[i] = (xrhs[i] - x[i - 1]) / xb;
			y[i] = (yrhs[i] - y[i - 1]) / yb;
		}
		// Backward substitution
		for (int32_t i = 1; i < n; i++) {
			x[n - i - 1] -= xtmp[n - i] * x[n - i];
			y[n - i - 1] -= ytmp[n - i] * y[n - i];
		}

		// start point
		newPoints.push_back(points[0]);
		for (int32_t i = 0; i < n - 1; ++i) {
			p.x = x[i];
			p.y = y[i];
			newPoints.push_back(p);
			p.x = 2 * points[i + 1].x - x[i + 1];
			p.y = 2 * points[i + 1].y - y[i + 1];
			newPoints.push_back(p);

			newPoints.push_back(points[i+1]);
		}
		p.x = x[n - 1];
		p.y = y[n - 1];
		newPoints.push_back(p);
		p.x = (points[n].x + x[n - 1]) / 2;
		p.y = (points[n].y + y[n - 1]) / 2;
		newPoints.push_back(p);
		// end point
		newPoints.push_back(points[n]);

		delete[] xrhs;
		delete[] yrhs;
		delete[] x;
		delete[] y;
		delete[] xtmp;
		delete[] ytmp;
	}
}