aboutsummaryrefslogtreecommitdiff
path: root/engines/draci/sprite.cpp
blob: 9f1cb3371dd0143bdbc24227fae8a6237cb5ffd6 (plain)
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
/* 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

#include "common/stream.h"

#include "draci/draci.h"
#include "draci/sprite.h"
#include "draci/font.h"

#include <cmath>

namespace Draci {

/**
 *  @brief Transforms an image from column-wise to row-wise
 *	@param img pointer to the buffer containing the image data
 *		   width width of the image in the buffer
 *		   height height of the image in the buffer
 */
static void transformToRows(byte *img, uint16 width, uint16 height) {
	byte *buf = new byte[width * height];
	byte *tmp = buf;
	memcpy(buf, img, width * height);
	
	for (uint16 i = 0; i < width; ++i) {
		for (uint16 j = 0; j < height; ++j) {
			img[j * width + i] = *tmp++;
		}
	}
	
	delete[] buf;
}

/**
 *  Constructor for loading sprites from a raw data buffer, one byte per pixel.
 */
Sprite::Sprite(byte *raw_data, uint16 width, uint16 height, int x, int y, 
	bool columnwise) : _data(NULL) {

	 _width = width;
	 _height = height;
	 _x = x;
	 _y = y;
	_delay = 0;

	_mirror = false;

	_data = new byte[width * height];
	
	memcpy(_data, raw_data, width * height);

	// If the sprite is stored column-wise, transform it to row-wise
	if (columnwise) {
		transformToRows(_data, width, height);
	}	
}

/**
 *  Constructor for loading sprites from a sprite-formatted buffer, one byte per 
 *	pixel.
 */
Sprite::Sprite(byte *sprite_data, uint16 length, int x, int y, bool columnwise) 
	: _data(NULL) {

	 _x = x;
	 _y = y;
	_delay = 0;

	_mirror = false;	

	Common::MemoryReadStream reader(sprite_data, length);

	_width = reader.readSint16LE();
	_height = reader.readSint16LE();

	_data = new byte[_width * _height];

	reader.read(_data, _width * _height);

	// If the sprite is stored column-wise, transform it to row-wise
	if (columnwise) {
		transformToRows(_data, _width, _height);
	}		
}

Sprite::~Sprite() { 
	delete[] _data;
}

void Sprite::setMirrorOn() {
	_mirror = true;
}

void Sprite::setMirrorOff() {
	_mirror = false;
}

// TODO: Research what kind of sampling the original player uses
void Sprite::drawScaled(Surface *surface, double scaleX, double scaleY, bool markDirty) const {

	Common::Rect sourceRect(0, 0, _width, _height);
	Common::Rect destRect(_x, _y, _x + getScaledWidth(scaleX), _y + getScaledHeight(scaleY));
	Common::Rect surfaceRect(0, 0, surface->w, surface->h);
	Common::Rect clippedDestRect(destRect);

	clippedDestRect.clip(surfaceRect);

	// Calculate by how much we need to adjust the source rectangle to account for cropping
	const int adjustLeft = clippedDestRect.left - destRect.left;
	const int adjustRight = clippedDestRect.right - destRect.right;
	const int adjustTop = clippedDestRect.top - destRect.top;
	const int adjustBottom = clippedDestRect.bottom - destRect.bottom;

	// Resize source rectangle
	sourceRect.left += adjustLeft;
	sourceRect.right += adjustRight;
	sourceRect.top += adjustTop;
	sourceRect.bottom += adjustBottom;

	// Get pointers to source and destination buffers
	byte *dst = (byte *)surface->getBasePtr(clippedDestRect.left, clippedDestRect.top);
	byte *src = _data;

	const int transparent = surface->getTransparentColour();

	// Calculate how many rows and columns we need to draw
	const int rows = clippedDestRect.bottom - clippedDestRect.top;
	const int columns = clippedDestRect.right - clippedDestRect.left;

	int *rowIndices = new int[rows];
	int *columnIndices = new int[columns];

	// Precalculate pixel indexes
	for (int i = 0; i < rows; ++i) {
		rowIndices[i] = lround(i / scaleY);
	}

	for (int j = 0; j < columns; ++j) {
		columnIndices[j] = lround(j / scaleX);
	}

	// Blit the sprite to the surface
	for (int i = 0; i < rows; ++i) {

		// Fetch index of current row to be drawn
		int row = rowIndices[i];
		
		for (int j = 0, q = sourceRect.left; j < columns; ++j, ++q) {
			
			// Fetch index of current column to be drawn
			int column = columnIndices[j];

			// Don't blit if the pixel is transparent on the target surface
			if (src[row * _width + column] != transparent) {

				// Draw the sprite mirrored if the _mirror flag is set						
				if (_mirror) {
					dst[sourceRect.right - q - 1] = src[row * _width + column];
				} else {
					dst[q] = src[row * _width + column];
				}
			}
		}

		// Advance to next row
		dst += surface->pitch;
	}

	// Mark the sprite's rectangle dirty
	if (markDirty) {	
		surface->markDirtyRect(destRect);
	}

	delete[] rowIndices;
	delete[] columnIndices;
}
	

/**
 *  @brief Draws the sprite to a Draci::Surface
 *  @param surface Pointer to a Draci::Surface
 *
 *  Draws the sprite to a Draci::Surface and marks its rectangle on the surface as dirty.
 *  It is safe to call it for sprites that would overflow the surface.
 */
void Sprite::draw(Surface *surface, bool markDirty) const { 

	Common::Rect sourceRect(0, 0, _width, _height);
	Common::Rect destRect(_x, _y, _x + _width, _y + _height);
	Common::Rect surfaceRect(0, 0, surface->w, surface->h);
	Common::Rect clippedDestRect(destRect);

	clippedDestRect.clip(surfaceRect);

	// Calculate by how much we need to adjust the source rectangle to account for cropping
	const int adjustLeft = clippedDestRect.left - destRect.left;
	const int adjustRight = clippedDestRect.right - destRect.right;
	const int adjustTop = clippedDestRect.top - destRect.top;
 	const int adjustBottom = clippedDestRect.bottom - destRect.bottom;

	// Resize source rectangle
	sourceRect.left += adjustLeft;
	sourceRect.right += adjustRight;
	sourceRect.top += adjustTop;
	sourceRect.bottom += adjustBottom;

	// Get pointers to source and destination buffers
	byte *dst = (byte *)surface->getBasePtr(clippedDestRect.left, clippedDestRect.top);
	byte *src = _data;

	const int transparent = surface->getTransparentColour();

	// Blit the sprite to the surface
	for (int i = sourceRect.top; i < sourceRect.bottom; ++i) {
		for (int j = sourceRect.left; j < sourceRect.right; ++j) {
			
			// Don't blit if the pixel is transparent on the target surface
			if (src[i * _width + j] != transparent) {

				// Draw the sprite mirrored if the _mirror flag is set						
				if (_mirror) {
					dst[sourceRect.right - j - 1] = src[i * _width + j];
				} else {	
					dst[j] = src[i * _width + j];
				}
			}		
		}

		// Advance to next row
		dst += surface->pitch;
	}

	// Mark the sprite's rectangle dirty
	if (markDirty) {	
		surface->markDirtyRect(destRect);
	}
}

Common::Rect Sprite::getRect() const {
	return Common::Rect(_x, _y, _x + _width, _y + _height);
}

Common::Rect Sprite::getScaledRect(double scaleX, double scaleY) const {
	return Common::Rect(_x, _y, _x + getScaledWidth(scaleX), _y + getScaledHeight(scaleY));
}

uint Sprite::getScaledHeight(double scaleY) const {
	return lround(scaleY * _height);
}

uint Sprite::getScaledWidth(double scaleX) const {
	return lround(scaleX * _width);
}

Text::Text(const Common::String &str, Font *font, byte fontColour, 
				int x, int y, uint spacing) {
	uint len = str.size();
	_length = len;
	
	_x = x;
	_y = y;
	_delay = 0;
	
	_text = new byte[len];
	memcpy(_text, str.c_str(), len);
	
	_spacing = spacing;
	_colour = fontColour;
	
	_font = font;

	_width = _font->getStringWidth(str, _spacing);
	_height = _font->getFontHeight();
} 

Text::~Text() {
	delete[] _text;
}

void Text::setText(const Common::String &str) {
	delete[] _text;
	
	uint len = str.size();
	_length = len;

	_width = _font->getStringWidth(str, _spacing);
	_height = _font->getFontHeight();

	 _text = new byte[len];
	memcpy(_text, str.c_str(), len);
}

void Text::setColour(byte fontColour) {
	_colour = fontColour;
}

void Text::setSpacing(uint spacing) {
	_spacing = spacing;
}

void Text::draw(Surface *surface, bool markDirty) const {
	_font->setColour(_colour);
	_font->drawString(surface, _text, _length, _x, _y, _spacing);
}

Common::Rect Text::getRect() const {
	return Common::Rect(_x, _y, _x + _width, _y + _height);
}
			
} // End of namespace Draci