File: gr_sprite.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (260 lines) | stat: -rw-r--r-- 7,065 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "common/textconsole.h"
#include "m4/graphics/gr_sprite.h"
#include "m4/graphics/gr_surface.h"
#include "m4/core/errors.h"
#include "m4/core/imath.h"
#include "m4/core/term.h"
#include "m4/mem/memman.h"
#include "m4/platform/draw.h"

namespace M4 {

/**
 * ScaleX and ScaleY are supposed to be percents, where 100 means 100%
 * S and D are Raw encoded (unencoded!) buffers.
 */
static uint8 scale_sprite(Buffer *S, Buffer *D, uint32 ScaleX, uint32 ScaleY) {
	uint16 ErrX, ErrY, i, j;
	uint8 *pScaled, *pData = S->data;

	if (!D)
		error_show(FL, 'BUF!', "scale sprite NULL D");

	if (!S)
		error_show(FL, 'BUF!', "scale sprite h:%d w:%d sx:%uld sy:%uld", D->h, D->w, ScaleX, ScaleY);

	/* calculate new x size */
	D->w = S->w * ScaleX / 100;
	if (S->w * ScaleX % 100 >= 50)
		++D->w;

	/* calculate new y size */
	D->h = S->h * ScaleY / 100;
	if (S->h * ScaleY % 100 >= 50)
		++D->h;

	D->stride = D->w;

	/* allocate 'scaled' buffer */
	if (!(D->data = pScaled = (uint8 *)mem_alloc(D->h * D->stride, "scaled buffer")))
		error_show(FL, 'OOM!', "scaled buffer h:%uld w:%uld", D->h, D->stride);

	ErrY = 50;
	for (i = 0; i < S->h; ++i) {
		ErrY += ScaleY;
		while (ErrY >= 100) {
			ErrX = 50;
			for (j = 0; j < S->w; ++j) {
				ErrX += ScaleX;
				while (ErrX >= 100) {
					*pScaled++ = *pData;
					ErrX -= 100;
				}
				++pData;
			}
			ErrY -= 100;
			pData -= S->w;
		}
		pData += S->w;
	}
	return 0;
}

#define Scaled	((drawReq->scaleY != 100) || (drawReq->scaleX != 100 && drawReq->scaleX != -100))
#define Rle	(source.encoding == RLE8)
#define Clipped ((drawReq->x < 0) || (drawReq->y < 0) || (drawReq->x + source.w > drawReq->Dest->w) || (drawReq->y + source.h > drawReq->Dest->h))
#define Forward (drawReq->scaleX > 0)
#define Depthed (drawReq->srcDepth)
#define Shadow	(source.encoding & 0x80)
#define ClipD	(leftOffset || rightOffset || bottomCut)

uint8 gr_sprite_draw(DrawRequest *drawReq) {
	Buffer source;
	uint8 *shadowBuff = nullptr, *scaledBuff = nullptr;
	Buffer afterScaled = { 0, 0, nullptr, 0, 0 };

	if (!drawReq->Src) {
		term_message("nullptr source data in sprite_draw");
		return 0;
	}

	// Negative scaleY means don't bother drawing this sprite
	if (drawReq->scaleY <= 0)
		return 0;

	if (!drawReq->Src->w || !drawReq->Src->h)
		return 1;

	// Copy DrawReq->Src to source buffer
	source = *drawReq->Src;
	assert(source.data);

	// if it's RLE encoded, ensure the sprite will decode to match the expected size
	if (source.encoding & RLE8) {
		if (RLE8Decode_Size(source.data, source.stride) != (size_t)(source.stride * source.h))
			error_show(FL, 'RLE8', "RLE8 sprite suspected BAD!");
	}

	// Check for RLE encoding in case of shadows
	// There is no RLE shadow draw routine, so we have to decode shadows ahead of time.
	if ((source.encoding & RLE8) && (source.encoding & SHADOW)) {
		if (!(shadowBuff = (uint8 *)mem_alloc(source.stride * source.h, "shadow buff")))
			error_show(FL, 'OOM!', "buffer w:%uld, h:%uld", source.w, source.h);

		RLE8Decode(source.data, shadowBuff, source.stride);
		source.data = shadowBuff;
		source.encoding &= ~RLE8;
	}

	// Check for scaling
	// We scale before we draw
	if (Scaled) {
		// Check if input is RLE8 encoded
		// If it's scaled we decode it first
		if (Rle) {
			if (!(scaledBuff = (uint8 *)mem_alloc(source.stride * source.h, "scaled buffer")))
				error_show(FL, 'OOM!', "no mem: buffer w:%d, h:%d", source.w, source.h);

			RLE8Decode(source.data, scaledBuff, source.stride);
			source.data = scaledBuff;
			source.encoding &= ~RLE8;
		}

		if (scale_sprite(&source, &afterScaled, imath_abs(drawReq->scaleX), imath_abs(drawReq->scaleY))) {
			if (shadowBuff)
				mem_free(shadowBuff);
			if (scaledBuff)
				mem_free(scaledBuff);

			error_show(FL, 'SPSF', "gr_sprite_draw");
		}

		// Preserve encoding
		afterScaled.encoding = source.encoding;

		// Copy AfterScaled to source buffer
		source = afterScaled;
	}

	bool shadow = (drawReq->Src->encoding & SHADOW) != 0;
	assert(!shadow || drawReq->ICT);

	M4Surface dst(*drawReq->Dest);
	dst.draw(source, drawReq->x, drawReq->y, drawReq->scaleX > 0,
		drawReq->srcDepth ? drawReq->depthCode : nullptr, drawReq->srcDepth,
		shadow ? drawReq->ICT : nullptr,
		drawReq->Pal);

	if (shadowBuff)
		mem_free(shadowBuff);

	if (scaledBuff)
		mem_free(scaledBuff);

	if (afterScaled.data)
		mem_free(afterScaled.data);

	return 0;
}

//----------------------------------------------------------------------------------------
//RLE8 COMPRESSION CODE...

#define ESC     ((uint8)0)
#define EOL	((uint8)0)
#define EOB	((uint8)1)
#define DELTA	((uint8)2)

#define OutBuffSize(x)	((x) + (((x) + 254) / 255 + 1) * 2 + 2)

static uint16 EncodeScan(uint8 *pi, uint8 *po, uint16 scanlen, uint8 EndByte) {
	uint8 *ps = pi + 1;
	uint16 outlen = 0, limit, run;

	while (scanlen) {
		limit = (scanlen < 255) ? scanlen : 255;
		//imath_min(scanlen, 255);
		for (run = 1; run < limit && *pi == *ps; ++run, ++ps) {}

		if (run > 1) {
			scanlen -= run;
			*po++ = run;
			*po++ = *pi;
			outlen += 2;
			pi = ps++;
		} else if (scanlen < 3) {
			for (; scanlen; --scanlen) {
				*po++ = 1;
				*po++ = *pi++;
				outlen += 2;
			}
		} else {
			--ps;
			do {
				++ps;
				while ((*ps != *(ps + 1) || *ps != *(ps + 2) || *ps != *(ps + 3)) && (ps - pi) < limit)
					++ps;
			} while ((run = ps - pi) < 3);

			scanlen -= run;
			*po++ = ESC;
			*po++ = run;
			outlen += (run + 2);

			for (limit = 0; limit < run; ++limit)
				*po++ = *pi++;
			++ps;
		}
	}

	*po++ = ESC;
	*po = EndByte;
	outlen += 2;
	return outlen;
}

uint32 gr_sprite_RLE8_encode(Buffer *Source, Buffer *Dest) {
	int i;
	uint32 Offset = 0;

	Dest->w = Source->w;
	Dest->h = Source->h;
	Dest->encoding = RLE8;
	Dest->stride = Source->stride;

	if (!(Dest->data = (uint8 *)mem_alloc(Source->h * OutBuffSize(Source->stride), "sprite data"))) {
		return 0;
	}

	for (i = 0; i < Source->h - 1; ++i)
		Offset += EncodeScan(Source->data + i * Source->stride, Dest->data + Offset, Source->w, EOL);

	Offset += EncodeScan(Source->data + i * Source->stride, Dest->data + Offset, Source->w, EOB);

	Dest->data = (uint8 *)mem_realloc(Dest->data, Offset, "rle8 sprite data");

	return Offset;
}

} // namespace M4