aboutsummaryrefslogtreecommitdiff
path: root/engines/lab/utils.cpp
blob: f9a6656174c8e0a5e169e7b4872ac6b5169dd860 (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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* 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) {
	_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;
}

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.
 */
void Utils::unDiffByteByte(byte *dest, byte *diff) {
	uint16 skip, copy;

	while (1) {
		skip = *diff;
		diff++;
		copy = *diff;
		diff++;

		if (skip == 255) {
			if (copy == 0) {
				skip = READ_LE_UINT16(diff);
				diff += 2;
				copy = READ_LE_UINT16(diff);
				diff += 2;
			} else if (copy == 255)
				return;
		}

		dest += skip;
		memcpy(dest, diff, copy);
		dest += copy;
		diff += copy;
	}
}

/**
 * Undiffs a piece of memory when header size is a byte, and copy/skip size
 * is a word.
 */
void Utils::unDiffByteWord(uint16 *dest, uint16 *diff) {
	uint16 skip, copy;

	while (1) {
		skip = ((byte *)diff)[0];
		copy = ((byte *)diff)[1];

		diff++;

		if (skip == 255) {
			if (copy == 0) {
				skip = READ_LE_UINT16(diff);
				diff++;
				copy = READ_LE_UINT16(diff);
				diff++;
			} else if (copy == 255)
				return;
		}

		dest += skip;

		while (copy > 3) {
			*dest = READ_LE_UINT16(diff);
			dest++;
			diff++;

			*dest = READ_LE_UINT16(diff);
			dest++;
			diff++;

			*dest = READ_LE_UINT16(diff);
			dest++;
			diff++;

			*dest = READ_LE_UINT16(diff);
			dest++;
			diff++;

			copy -= 4;
		}

		while (copy) {
			*dest = READ_LE_UINT16(diff);
			dest++;
			diff++;
			copy--;
		}
	}
}

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

/**
 * Undiffs a piece of memory when header size is a byte, and copy/skip size
 * is a byte.
 */
void Utils::VUnDiffByteByte(byte *dest, byte *diff, uint16 bytesPerRow) {
	byte *curPtr;
	uint16 skip, copy;
	uint16 counter = 0;


	while (counter < _dataBytesPerRow) {
		curPtr = dest + counter;

		for (;;) {
			skip = *diff;
			diff++;
			copy = *diff;
			diff++;

			if (skip == 255) {
				counter += copy;
				break;
			}

			else {
				curPtr += (skip * bytesPerRow);

				while (copy) {
					copy--;
					*curPtr = *diff;
					curPtr += bytesPerRow;
					diff++;
				}
			}
		}
	}
}

/**
 * Undiffs a piece of memory when header size is a byte, and copy/skip size
 * is a word.
 */
void Utils::VUnDiffByteWord(uint16 *dest, uint16 *diff, uint16 bytesPerRow) {
	uint16 *curPtr;
	uint16 skip, copy;
	uint16 counter = 0;

	uint16 wordsPerRow = bytesPerRow / 2;

	while (counter < (_dataBytesPerRow >> 1)) {
		curPtr = dest + counter;

		for (;;) {
			skip = ((byte *)diff)[0];
			copy = ((byte *)diff)[1];

			diff++;


			if (skip == 255) {
				counter += copy;
				break;
			}

			else {
				curPtr += (skip * wordsPerRow);

				while (copy) {
					*curPtr = *diff; //swapUShort(*diff);
					curPtr += wordsPerRow;
					diff++;
					copy--;
				}
			}
		}
	}
}

/**
 * Undiffs a piece of memory when header size is a byte, and copy/skip size
 * is a long.
 */
void Utils::VUnDiffByteLong(uint32 *dest, uint32 *diff, uint16 bytesPerRow) {
	uint32 *_curPtr;
	uint16 skip, copy;

	uint16 counter = 0;
	byte *diff1 = (byte *)diff;

	uint16 longsperrow = bytesPerRow / 4;

	while (counter < (_dataBytesPerRow >> 2)) {
		_curPtr = dest + counter;

		for (;;) {
			skip = *diff1;
			diff1++;

			copy = *diff1;
			diff1++;


			if (skip == 255) {
				counter += copy;
				break;
			}

			else {
				_curPtr += (skip * longsperrow);

				while (copy) {
					*_curPtr = *(uint32 *)diff1; //swapULong(*diff);
					_curPtr += longsperrow;
					diff1 += 4;
					copy--;
				}
			}
		}
	}
}

/**
 * Runlength decodes a chunk of memory.
 */
void Utils::runLengthDecode(byte *dest, byte *source) {
	int8 num;
	int16 count;

	while (1) {
		num = (int8)*source;
		source++;

		if (num == 127) {
			return;
		} else if (num > '\0') {
			memcpy(dest, source, num);
			source += num;
			dest   += num;
		} else {
			count = (int16)(-num);
			num   = *source;
			source++;

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

/**
 * Does a vertical run length decode.
 */
void Utils::VRunLengthDecode(byte *dest, byte *source, uint16 bytesPerRow) {
	int8 num;
	int16 count;
	byte *top = dest;

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

		num = (int8)*source;
		source++;

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

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

			num = *source;
			source++;
		}
	}
}

/**
 * Does the undiffing between the bitmaps.
 */
void Utils::unDiff(byte *newBuf, byte *oldBuf, byte *diffData, uint16 bytesPerRow, bool isV) {
	diffData++;
	byte bufType = *diffData;
	diffData++;

	if (isV) {
		if (bufType == 0)
			VUnDiffByteByte(newBuf, diffData, bytesPerRow);
		else if (bufType == 1)
			VUnDiffByteWord((uint16 *)newBuf, (uint16 *)diffData, bytesPerRow);
		else if (bufType == 3)
			VUnDiffByteLong((uint32 *)newBuf, (uint32 *)diffData, bytesPerRow);
		else
			error("Unexpected variable compression scheme %d", bufType);
	} else {
		if (bufType == 0)
			unDiffByteByte(newBuf, diffData);
		else if (bufType == 1)
			unDiffByteWord((uint16 *)newBuf, (uint16 *)diffData);
		else
			error("Unexpected compression scheme %d", bufType);
	}
}

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

/**
 * Generates a random number.
 */
uint16 Utils::getRandom(uint16 max) {
	uint32 secs, micros;

	_vm->getTime(&secs, &micros);
	return ((micros + secs) % max);
}

void Utils::readBlock(void *Buffer, uint32 Size, byte **File) {
	memcpy(Buffer, *File, (size_t)Size);
	(*File) += Size;
}

} // End of namespace Lab