aboutsummaryrefslogtreecommitdiff
path: root/engines/dreamweb/vgagrafx.cpp
blob: 1c297f1e93db1fbbbeb619e81274ff6f3e63075a (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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/* 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.
 *
 */

#include "dreamweb/dreamweb.h"
#include "engines/util.h"
#include "graphics/surface.h"

namespace DreamGen {

uint8 *DreamGenContext::workspace() {
	uint8 *result = getSegment(data.word(kWorkspace)).ptr(0, 0);
	return result;
}

void DreamGenContext::allocateWork() {
	data.word(kWorkspace) = allocateMem(0x1000);
}

void DreamGenContext::multiGet() {
	multiGet(ds.ptr(si, 0), di, bx, cl, ch);
	si += cl * ch;
	di += bx * kScreenwidth + kScreenwidth * ch;
	cx = 0;
}

void DreamGenContext::multiGet(uint8 *dst, uint16 x, uint16 y, uint8 w, uint8 h) {
	assert(x < 320);
	assert(y < 200);
	const uint8 *src = workspace() + x + y * kScreenwidth;
	if (y + h > 200)
		h = 200 - y;
	if (x + w > 320)
		w = 320 - x;
	//debug(1, "multiGet %u,%u %ux%u -> segment: %04x->%04x", x, y, w, h, (uint16)ds, (uint16)es);
	for(unsigned l = 0; l < h; ++l) {
		const uint8 *src_p = src + kScreenwidth * l;
		uint8 *dst_p = dst + w * l;
		memcpy(dst_p, src_p, w);
	}
}

void DreamGenContext::multiPut() {
	multiPut(ds.ptr(si, 0), di, bx, cl, ch);
	si += cl * ch;
	di += bx * kScreenwidth + kScreenwidth * ch;
	cx = 0;
}

void DreamGenContext::multiPut(const uint8 *src, uint16 x, uint16 y, uint8 w, uint8 h) {
	assert(x < 320);
	assert(y < 200);
	uint8 *dst = workspace() + x + y * kScreenwidth;
	if (y + h > 200)
		h = 200 - y;
	if (x + w > 320)
		w = 320 - x;
	//debug(1, "multiPut %ux%u -> segment: %04x->%04x", w, h, (uint16)ds, (uint16)es);
	for(unsigned l = 0; l < h; ++l) {
		const uint8 *src_p = src + w * l;
		uint8 *dst_p = dst + kScreenwidth * l;
		memcpy(dst_p, src_p, w);
	}
}

void DreamGenContext::multiDump(uint16 x, uint16 y, uint8 width, uint8 height) {
	unsigned offset = x + y * kScreenwidth;
	//debug(1, "multiDump %ux%u(segment: %04x) -> %d,%d(address: %d)", w, h, (uint16)ds, x, y, offset);
	engine->blit(workspace() + offset, kScreenwidth, x, y, width, height);
}

void DreamGenContext::multiDump() {
	multiDump(di, bx, cl, ch);
	unsigned offset = di + bx * kScreenwidth;
	si = di = offset + ch * kScreenwidth;
	cx = 0;
}

void DreamGenContext::workToScreenCPP() {
	engine->blit(workspace(), 320, 0, 0, 320, 200);
}

void DreamGenContext::workToScreen() {
	workToScreenCPP();
	uint size = 320 * 200;
	di = si = size;
	cx = 0;
}

void DreamGenContext::printUnderMon() {
	engine->printUnderMonitor();
}

void DreamGenContext::cls() {
	engine->cls();
}

void DreamGenContext::frameOutNm(uint8 *dst, const uint8 *src, uint16 pitch, uint16 width, uint16 height, uint16 x, uint16 y) {
	dst += pitch * y + x;

	for (uint16 j = 0; j < height; ++j) {
		memcpy(dst, src, width);
		dst += pitch;
		src += width;
	}
}

void DreamGenContext::frameOutBh(uint8 *dst, const uint8 *src, uint16 pitch, uint16 width, uint16 height, uint16 x, uint16 y) {
	uint16 stride = pitch - width;
	dst += y * pitch + x;

	for (uint16 i = 0; i < height; ++i) {
		for (uint16 j = 0; j < width; ++j) {
			if (*dst == 0xff) {
				*dst = *src;
			}
			++src;
			++dst;
		}
		dst += stride;
	}
}

void DreamGenContext::frameOutFx(uint8 *dst, const uint8 *src, uint16 pitch, uint16 width, uint16 height, uint16 x, uint16 y) {
	uint16 stride = pitch - width;
	dst += y * pitch + x;
	dst -= width;

	for (uint16 j = 0; j < height; ++j) {
		for (uint16 i = 0; i < width; ++i) {
			uint8 pixel = src[width - i - 1];
			if (pixel)
				*dst = pixel;
			++dst;
		}
		src += width;
		dst += stride;
	}
}

void DreamGenContext::doShake() {
	uint8 &counter = data.byte(kShakecounter);
	if (counter == 48)
		return;

	++counter;
	static const int shakeTable[] = {
		0, -2,  3, -2,  0,  2,  4, -1,
		1, -3,  3,  2,  0, -2,  3, -2,
		0,  2,  4, -1,  1, -3,  3,  2,
		0, -2,  3, -2,  0,  2,  4, -1,

		1, -3,  3,  2,  0, -2,  3, -2,
		0,  2,  4, -1,  1, -3,  3,  2,
		0, -2,  3, -2,  0,  2,  4, -1,
		1, -3,  3,  2,  0, -2,  3, -2,

		0,  2,  4, -1,  1, -3,  3,  2,
		0, -2,  3, -2,  0,  2,  4, -1,
		1, -3,  3,  2,  0, -2,  3, -2,
		0,  2,  4, -1,  1, -3,  3,  2,

		0, -2,  3, -2,  0,  2,  4, -1,
		1, -3,  3,  0,
	};
	int offset = shakeTable[counter];
	engine->setShakePos(offset >= 0 ? offset : -offset);
}

void DreamGenContext::vSync() {
	push(ax);
	push(bx);
	push(cx);
	push(dx);
	push(si);
	push(di);
	push(es);
	push(ds);
	engine->waitForVSync();
	ds = pop();
	es = pop();
	di = pop();
	si = pop();
	dx = pop();
	cx = pop();
	bx = pop();
	ax = pop();
}

void DreamGenContext::setMode() {
	vSync();
	initGraphics(320, 200, false);
}

static Common::String getFilename(Context &context) {
	uint16 name_ptr = context.dx;
	Common::String name;
	uint8 c;
	while((c = context.cs.byte(name_ptr++)) != 0)
		name += (char)c;
	return name;
}

void DreamGenContext::showPCX() {
	Common::String name = getFilename(*this);
	Common::File pcxFile;

	if (!pcxFile.open(name)) {
		warning("showpcx: Could not open '%s'", name.c_str());
		return;
	}

	uint8 *mainGamePal;
	int i, j;

	// Read the 16-color palette into the 'maingamepal' buffer. Note that
	// the color components have to be adjusted from 8 to 6 bits.

	pcxFile.seek(16, SEEK_SET);
	es = data.word(kBuffers);
	mainGamePal = es.ptr(kMaingamepal, 768);
	pcxFile.read(mainGamePal, 48);

	memset(mainGamePal + 48, 0xff, 720);
	for (i = 0; i < 48; i++) {
		mainGamePal[i] >>= 2;
	}

	// Decode the image data.

	Graphics::Surface *s = g_system->lockScreen();
	Common::Rect rect(640, 480);

	s->fillRect(rect, 0);
	pcxFile.seek(128, SEEK_SET);

	for (int y = 0; y < 480; y++) {
		byte *dst = (byte *)s->getBasePtr(0, y);
		int decoded = 0;

		while (decoded < 320) {
			byte col = pcxFile.readByte();
			byte len;

			if ((col & 0xc0) == 0xc0) {
				len = col & 0x3f;
				col = pcxFile.readByte();
			} else {
				len = 1;
			}

			// The image uses 16 colors and is stored as four bit
			// planes, one for each bit of the color, least
			// significant bit plane first.

			for (i = 0; i < len; i++) {
				int plane = decoded / 80;
				int pos = decoded % 80;

				for (j = 0; j < 8; j++) {
					byte bit = (col >> (7 - j)) & 1;
					dst[8 * pos + j] |= (bit << plane);
				}

				decoded++;
			}
		}
	}

	g_system->unlockScreen();
	pcxFile.close();
}

void DreamGenContext::frameOutV(uint8 *dst, const uint8 *src, uint16 pitch, uint16 width, uint16 height, int16 x, int16 y) {
	// NB : These resilience checks were not in the original engine, but did they result in undefined behaviour
	// or was something broken during porting to C++?
	assert(pitch == 320);

	if(x < 0) {
		assert(width >= -x);
		width -= -x;
		src += -x;
		x = 0;
	}
	if(y < 0) {
		assert(height >= -y);
		height -= -y;
		src += (-y) * width;
		y = 0;
	}
	if(x >= 320)
		return;
	if(y >= 200)
		return;
	if(x + width > 320) {
		width = 320 - x;
	}
	if(y + height > 200) {
		height = 200 - y;
	}

	uint16 stride = pitch - width;
	dst += pitch * y + x;

	for (uint16 j = 0; j < height; ++j) {
		for (uint16 i = 0; i < width; ++i) {
			uint8 pixel = *src++;
			if (pixel)
				*dst = pixel;
			++dst;
		}
		dst += stride;
	}
}

void DreamGenContext::showFrame(const Frame *frameData, uint16 x, uint16 y, uint16 frameNumber, uint8 effectsFlag) {
	uint8 width, height;
	showFrame(frameData, x, y, frameNumber, effectsFlag, &width, &height);
}

void DreamGenContext::showFrame(const Frame *frameData, uint16 x, uint16 y, uint16 frameNumber, uint8 effectsFlag, uint8 *width, uint8 *height) {
	const Frame *frame = frameData + frameNumber;
	if ((frame->width == 0) && (frame->height == 0)) {
		*width = 0;
		*height = 0;
		return;
	}

//notBlankShow:
	if ((effectsFlag & 128) == 0) {
		x += frame->x;
		y += frame->y;
	}
//skipOffsets:

	*width = frame->width;
	*height = frame->height;
	const uint8 *pSrc = ((const uint8 *)frameData) + frame->ptr() + 2080;

	if (effectsFlag) {
		if (effectsFlag & 128) { //centred
			x -= *width / 2;
			y -= *height / 2;
		}
		if (effectsFlag & 64) { //diffDest
			frameOutFx(es.ptr(0, dx * *height), pSrc, dx, *width, *height, x, y);
			return;
		}
		if (effectsFlag & 8) { //printList
			/*
			push(ax);
			al = x - data.word(kMapadx);
			ah = y - data.word(kMapady);
			//addToPrintList(); // NB: Commented in the original asm
			ax = pop();
			*/
		}
		if (effectsFlag & 4) { //flippedX
			frameOutFx(workspace(), pSrc, 320, *width, *height, x, y);
			return;
		}
		if (effectsFlag & 2) { //noMask
			frameOutNm(workspace(), pSrc, 320, *width, *height, x, y);
			return;
		}
		if (effectsFlag & 32) {
			frameOutBh(workspace(), pSrc, 320, *width, *height, x, y);
			return;
		}
	}
//noEffects:
	frameOutV(workspace(), pSrc, 320, *width, *height, x, y);
	return;
}

void DreamGenContext::showFrame() {
	uint8 width, height;
	showFrame((Frame *)ds.ptr(0, 0), di, bx, ax & 0x1ff, ah & 0xfe, &width, &height);
	cl = width;
	ch = height;
}

void DreamGenContext::clearWork() {
	memset(workspace(), 0, 320*200);
}

void DreamGenContext::zoom() {
	if (data.word(kWatchingtime) != 0)
		return;
	if (data.byte(kZoomon) != 1)
		return;
	if (data.byte(kCommandtype) >= 199) {
		putUnderZoom();
		return;
	}
	uint16 srcOffset = (data.word(kOldpointery) - 9) * 320 + (data.word(kOldpointerx) - 11);
	uint16 dstOffset = (kZoomy + 4) * 320 + (kZoomx + 5);
	const uint8 *src = workspace() + srcOffset;
	uint8 *dst = workspace() + dstOffset;
	for(size_t i=0; i<20; ++i) {
		for(size_t j=0; j<23; ++j) {
			uint8 v = src[j];
			dst[2*j+0] = v;
			dst[2*j+1] = v; 
			dst[2*j+320] = v;
			dst[2*j+321] = v; 
		}
		src += 320;
		dst += 320*2;
	}
	crosshair();
	data.byte(kDidzoom) = 1;
}

void DreamGenContext::panelToMap() {
	multiGet(getSegment(data.word(kMapstore)).ptr(0, 0), data.word(kMapxstart) + data.word(kMapadx), data.word(kMapystart) + data.word(kMapady), data.byte(kMapxsize), data.byte(kMapysize));
}

void DreamGenContext::mapToPanel() {
	multiPut(getSegment(data.word(kMapstore)).ptr(0, 0), data.word(kMapxstart) + data.word(kMapadx), data.word(kMapystart) + data.word(kMapady), data.byte(kMapxsize), data.byte(kMapysize));
}

void DreamGenContext::dumpMap() {
	multiDump(data.word(kMapxstart) + data.word(kMapadx), data.word(kMapystart) + data.word(kMapady), data.byte(kMapxsize), data.byte(kMapysize));
}

void DreamGenContext::transferInv() {
	const Frame *freeFrames = (const Frame *)getSegment(data.word(kFreeframes)).ptr(kFrframedata, 0);
	const Frame *freeFrame = freeFrames + (3 * data.byte(kItemtotran) + 1);
	Frame *exFrames = (Frame *)getSegment(data.word(kExtras)).ptr(kExframedata, 0);
	Frame *exFrame = exFrames + (3 * data.byte(kExpos) + 1);
	exFrame->width = freeFrame->width;
	exFrame->height = freeFrame->height;
	exFrame->x = freeFrame->x;
	exFrame->y = freeFrame->y;
	uint16 byteCount = freeFrame->width * freeFrame->height;
	const uint8 *src = getSegment(data.word(kFreeframes)).ptr(kFrframes + freeFrame->ptr(), byteCount);
	uint8 *dst = getSegment(data.word(kExtras)).ptr(kExframes + data.word(kExframepos), byteCount);
	memcpy(dst, src, byteCount);
	exFrame->setPtr(data.word(kExframepos));
	data.word(kExframepos) += byteCount;
}

bool DreamGenContext::pixelCheckSet(const ObjPos *pos, uint8 x, uint8 y) {
	x -= pos->xMin;
	y -= pos->yMin;
	SetObject *setObject = getSetAd(pos->index);
	Frame *frame = (Frame *)getSegment(data.word(kSetframes)).ptr(kFramedata, 0) + setObject->index;
	const uint8 *ptr = getSegment(data.word(kSetframes)).ptr(kFrames, 0) + frame->ptr() + y * frame->width + x;
	return *ptr != 0;
}

void DreamGenContext::loadPalFromIFF() {
	dx = kPalettescreen;
	openFile();
	cx = 2000;
	ds = data.word(kMapstore);
	dx = 0;
	readFromFile();
	closeFile();

	const uint8 *src = getSegment(data.word(kMapstore)).ptr(0x30, 0);
	uint8 *dst = mainPalette();
	for (size_t i = 0; i < 256*3; ++i) {
		uint8 c = src[i] / 4;
		if (data.byte(kBrightness) == 1) {
			if (c) {
				c = c + c / 2 + c / 4;
				if (c > 63)
					c = 63;
			}
		}
		dst[i] = c;
	}
}

void DreamGenContext::createPanel() {
	Frame *icons = (Frame *)getSegment(data.word(kIcons2)).ptr(0, 0);
	showFrame(icons, 0, 8, 0, 2);
	showFrame(icons, 160, 8, 0, 2);
	showFrame(icons, 0, 104, 0, 2);
	showFrame(icons, 160, 104, 0, 2);
}

void DreamGenContext::createPanel2() {
	createPanel();
	Frame *icons = (Frame *)getSegment(data.word(kIcons2)).ptr(0, 0);
	showFrame(icons, 0, 0, 5, 2);
	showFrame(icons, 160, 0, 5, 2);
}

} /*namespace dreamgen */