aboutsummaryrefslogtreecommitdiff
path: root/engines/lab/utils.cpp
blob: 1ddb454895313be3aab7fecc32733ace82e2134a (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
/* 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.
 *
 */

/*
 * This code is based on Labyrinth of Time code with assistance of
 *
 * Copyright (c) 1993 Terra Nova Development
 * Copyright (c) 2004 The Wyrmkeep Entertainment Co.
 *
 */

#include "common/file.h"

#include "lab/lab.h"
#include "lab/utils.h"

namespace Lab {
Utils::Utils(LabEngine *vm) : _vm(vm), _rnd("lab") {
	_dataBytesPerRow = 0;
}

/**
 * Scales the x co-ordinates to that of the new display.  In the room parser
 * file, co-ordinates are set up on a 360x336 display.
 */
uint16 Utils::scaleX(uint16 x) {
	if (_vm->_isHiRes)
		return (uint16)((x * 16) / 9);
	else
		return (uint16)((x * 8) / 9);
}

/**
 * Scales the y co-ordinates to that of the new display.  In the room parser
 * file, co-ordinates are set up on a 368x336 display.
 */
uint16 Utils::scaleY(uint16 y) {
	if (_vm->_isHiRes)
		return (y + (y / 14));
	else
		return ((y * 10) / 24);
}

uint16 Utils::mapScaleX(uint16 x) {
	if (_vm->_isHiRes)
		return (x - 45);
	else
		return ((x - 45) >> 1);
}

uint16 Utils::mapScaleY(uint16 y) {
	if (_vm->_isHiRes)
		return y;
	else
		return ((y - 35) >> 1) - (y >> 6);
}

/**
 * Scales the VGA coords to SVGA if necessary; otherwise, returns VGA coords.
 */
int16 Utils::vgaScaleX(int16 x) {
	if (_vm->_isHiRes)
		return (x * 2);
	else
		return x;
}

/**
 * Scales the VGA coords to SVGA if necessary; otherwise, returns VGA coords.
 */
int16 Utils::vgaScaleY(int16 y) {
	if (_vm->_isHiRes)
		return ((y * 12) / 5);
	else
		return y;
}

Common::Rect Utils::vgaRectScale(int16 x1, int16 y1, int16 x2, int16 y2) {
	return Common::Rect(vgaScaleX(x1), vgaScaleY(y1), vgaScaleX(x2), vgaScaleY(y2));
}

uint16 Utils::svgaCord(uint16 cord) {
	if (_vm->_isHiRes)
		return cord;
	else
		return 0;
}

/**
 * Converts SVGA coords to VGA if necessary, otherwise returns VGA coords.
 */
Common::Point Utils::vgaUnscale(Common::Point pos) {
	Common::Point result;
	if (_vm->_isHiRes) {
		result.x = pos.x / 2;
		result.y = (pos.y * 5) / 12;
	} else
		result = pos;

	return result;
}

/**
 * Undiffs a piece of memory when header size is a byte, and copy/skip size
 * is also a byte or a word.
 */
template<typename T>
void Utils::unDiff(T *dest, Common::File *sourceFile) {
	byte bytesPerWord = sizeof(T);

	while (1) {
		uint16 skip = sourceFile->readByte();
		uint16 copy = sourceFile->readByte();

		if (skip == 255) {
			if (copy == 0) {
				skip = sourceFile->readUint16LE();
				copy = sourceFile->readUint16LE();
			} else if (copy == 255)
				return;
		}

		dest += skip;

		if (bytesPerWord == 1) {
			sourceFile->read(dest, copy);
			dest += copy;
		} else {
			while (copy) {
				*dest = sourceFile->readUint16LE();
				dest++;
				copy--;
			}
		}
	}
}

/*------------------------- unDiff Vertical Memory --------------------------*/

/**
 * Undiffs a piece of memory when header size is a byte, and copy/skip size
 * is a byte or a word or a double word.
 */
template<typename T>
void Utils::verticalUnDiff(T *dest, Common::File *sourceFile, uint16 bytesPerRow) {
	uint16 counter = 0;
	byte bytesPerWord = sizeof(T);
	uint16 wordsPerRow = bytesPerRow / bytesPerWord;

	while (counter < wordsPerRow) {
		T *curPtr = dest + counter;

		for (;;) {
			uint16 skip = sourceFile->readByte();
			uint16 copy = sourceFile->readByte();

			if (skip == 255) {
				counter += copy;
				break;
			} else {
				curPtr += (skip * wordsPerRow);

				while (copy) {
					if (bytesPerWord == 1)
						*curPtr++ = sourceFile->readByte();
					else if (bytesPerWord == 2)
						*curPtr = sourceFile->readUint16LE();
					else if (bytesPerWord == 4)
						*curPtr = sourceFile->readUint32LE();
					else
						error("verticalUnDiff: Invalid bytesPerWord (%d)", bytesPerWord);
					curPtr += wordsPerRow;
					copy--;
				}
			}
		}
	}
}

void Utils::runLengthDecode(byte *dest, Common::File *sourceFile) {
	int8 num;
	int16 count;

	while (1) {
		num = sourceFile->readSByte();

		if (num == 127) {
			return;
		} else if (num > '\0') {
			sourceFile->read(dest, num);
			dest   += num;
		} else {
			count = (int16)(-num);
			num = sourceFile->readSByte();

			while (count) {
				*dest = num;
				dest++;
				count--;
			}
		}
	}
}

void Utils::verticalRunLengthDecode(byte *dest, Common::File *sourceFile, uint16 bytesPerRow) {
	int16 count;
	byte *top = dest;

	for (uint16 i = 0; i < _dataBytesPerRow; i++) {
		dest = top;
		dest += i;

		int8 num = sourceFile->readSByte();

		while (num != 127) {
			if (num > '\0') {
				while (num) {
					*dest = sourceFile->readByte();
					dest += bytesPerRow;
					num--;
				}
			} else {
				count = (int16)(-num);
				num = sourceFile->readSByte();

				while (count) {
					*dest = num;
					dest += bytesPerRow;
					count--;
				}
			}

			num = sourceFile->readSByte();
		}
	}
}

/**
 * Does the undiffing between the bitmaps.
 */
void Utils::unDiff(byte *newBuf, byte *oldBuf, Common::File *sourceFile, uint16 bytesPerRow, bool isVertical) {
	sourceFile->skip(1);
	byte bufType = sourceFile->readByte();

	if (isVertical) {
		if (bufType == 0)
			verticalUnDiff<byte>(newBuf, sourceFile, bytesPerRow);
		else if (bufType == 1)
			verticalUnDiff<uint16>((uint16 *)newBuf, sourceFile, bytesPerRow);
		else if (bufType == 3)
			verticalUnDiff<uint32>((uint32 *)newBuf, sourceFile, bytesPerRow);
		else
			error("Unexpected variable compression scheme %d", bufType);
	} else {
		if (bufType == 0)
			unDiff<byte>(newBuf, sourceFile);
		else if (bufType == 1)
			unDiff<uint16>((uint16 *)newBuf, sourceFile);
		else
			error("Unexpected compression scheme %d", bufType);
	}
}

void Utils::setBytesPerRow(int num) {
	_dataBytesPerRow = num;
}

uint16 Utils::getRandom(uint16 max) {
	if (max > 1)
		return _rnd.getRandomNumber(max - 1);
	else
		return 0;
}

} // End of namespace Lab