aboutsummaryrefslogtreecommitdiff
path: root/engines/adl/graphics_v2.cpp
blob: 289f43e8de300637de15e936b1577bddff718199 (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
/* 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 "common/stream.h"
#include "common/rect.h"
#include "common/textconsole.h"

#include "adl/display.h"
#include "adl/graphics.h"

namespace Adl {

#define NUM_PATTERNS 22
#define PATTERN_LEN 4
static const byte fillPatterns[NUM_PATTERNS][PATTERN_LEN] = {
	{ 0x00, 0x00, 0x00, 0x00 },
	{ 0x80, 0x80, 0x80, 0x80 },
	{ 0xff, 0xff, 0xff, 0xff },
	{ 0x7f, 0x7f, 0x7f, 0x7f },
	{ 0x2a, 0x55, 0x2a, 0x55 },
	{ 0xaa, 0xd5, 0xaa, 0xd5 },
	{ 0x55, 0x2a, 0x55, 0x2a },
	{ 0xd5, 0xaa, 0xd5, 0xaa },
	{ 0x33, 0x66, 0x4c, 0x19 },
	{ 0xb3, 0xe6, 0xcc, 0x99 },
	{ 0x22, 0x44, 0x08, 0x11 },
	{ 0xa2, 0xc4, 0x88, 0x91 },
	{ 0x11, 0x22, 0x44, 0x08 },
	{ 0x91, 0xa2, 0xc4, 0x88 },
	{ 0x6e, 0x5d, 0x3b, 0x77 },
	{ 0xee, 0xdd, 0xbb, 0xf7 },
	{ 0x5d, 0x3b, 0x77, 0x6e },
	{ 0xdd, 0xbb, 0xf7, 0xee },
	{ 0x66, 0x4c, 0x19, 0x33 },
	{ 0xe6, 0xcc, 0x99, 0xb3 },
	{ 0x33, 0x66, 0x4c, 0x19 },
	{ 0xb3, 0xe6, 0xcc, 0x99 }
};

#define MIN_COMMAND 0xe0

#define CHECK_COMMAND(X) \
	do { \
		if ((X) >= MIN_COMMAND) { \
			pic.seek(-1, SEEK_CUR); \
			return; \
		} \
	} while (0)

#define READ_BYTE(b) \
	do { \
		b = pic.readByte(); \
		if (pic.eos() || pic.err()) \
			error("Error reading picture"); \
		CHECK_COMMAND(b); \
	} while (0)

#define READ_POINT(p) \
	do { \
		READ_BYTE(p.x); \
		p.x <<= 1; \
		READ_BYTE(p.y); \
	} while (0)

void Graphics_v2::clear() {
	_display.clear(0xff);
	_color = 0;
}

void Graphics_v2::drawCorners(Common::SeekableReadStream &pic, bool yFirst) {
	Common::Point p;

	READ_POINT(p);

	if (yFirst)
		goto doYStep;

	while (true) {
		int16 n;

		READ_BYTE(n);

		_display.putPixel(p, _color);

		n <<= 1;
		drawLine(p, Common::Point(n, p.y), _color);
		p.x = n;

doYStep:
		READ_BYTE(n);

		_display.putPixel(p, _color);
		drawLine(p, Common::Point(p.x, n), _color);

		_display.putPixel(Common::Point(p.x + 1, p.y), _color);
		drawLine(Common::Point(p.x + 1, p.y), Common::Point(p.x + 1, n), _color);

		p.y = n;
	}
}

void Graphics_v2::drawRelativeLines(Common::SeekableReadStream &pic) {
	Common::Point p1;

	READ_POINT(p1);
	_display.putPixel(p1, _color);

	while (true) {
		Common::Point p2(p1);

		byte n;
		READ_BYTE(n);

		byte h = (n & 0x70) >> 4;
		byte l = n & 7;

		if (n & 0x80)
			p2.x -= (h << 1);
		else
			p2.x += (h << 1);

		if (n & 8)
			p2.y -= l;
		else
			p2.y += l;

		drawLine(p1, p2, _color);
		p1 = p2;
	}
}

void Graphics_v2::drawAbsoluteLines(Common::SeekableReadStream &pic) {
	Common::Point p1;

	READ_POINT(p1);
	_display.putPixel(p1, _color);

	while (true) {
		Common::Point p2;

		READ_POINT(p2);
		drawLine(p1, p2, _color);
		p1 = p2;
	}
}

static byte getPatternColor(const Common::Point &p, byte pattern) {
	if (pattern >= NUM_PATTERNS)
		error("Invalid fill pattern %i encountered in picture", pattern);

	byte offset = (p.y & 1) << 1;
	offset += (p.x / 7) & 3;

	return fillPatterns[pattern][offset % PATTERN_LEN];
}

void Graphics_v2::fillRow(const Common::Point &p, bool stopBit, byte pattern) {
	const byte color = getPatternColor(p, pattern);
	_display.putPixelRaw(p, color);

	Common::Point q(p);
	byte c = color;

	while (++q.x < DISPLAY_WIDTH && _display.getPixelBit(q) != stopBit) {
		if ((q.x % 7) == 0)
			c = getPatternColor(q, pattern);
		_display.putPixelRaw(q, c);
	}

	q = p;
	c = color;
	while (--q.x >= 0 && _display.getPixelBit(q) != stopBit) {
		if ((q.x % 7) == 6)
			c = getPatternColor(q, pattern);
		_display.putPixelRaw(q, c);
	}
}

// Basic flood fill
void Graphics_v2::fill(Common::SeekableReadStream &pic) {
	byte pattern;
	READ_BYTE(pattern);

	while (true) {
		Common::Point p;
		READ_POINT(p);

		bool stopBit = !_display.getPixelBit(p);

		while (--p.y >= 0) {
			if (_display.getPixelBit(p) == stopBit)
				break;
			if (_display.getPixelBit(Common::Point(p.x + 1, p.y)) == stopBit)
				break;
		}

		while (++p.y < DISPLAY_HEIGHT) {
			if (_display.getPixelBit(p) == stopBit)
				break;
			if (_display.getPixelBit(Common::Point(p.x + 1, p.y)) == stopBit)
				break;
			fillRow(p, stopBit, pattern);
		}
	}
}

void Graphics_v2::drawPic(Common::SeekableReadStream &pic, const Common::Point &pos, byte color) {
	while (true) {
		byte opcode = pic.readByte();

		if (pic.eos() || pic.err())
			error("Error reading picture");

		switch (opcode) {
		case 0xe0:
			drawCorners(pic, false);
			break;
		case 0xe1:
			drawCorners(pic, true);
			break;
		case 0xe2:
			drawRelativeLines(pic);
			break;
		case 0xe3:
			drawAbsoluteLines(pic);
			break;
		case 0xe4:
			fill(pic);
			break;
		case 0xe5:
			clear();
			break;
		case 0xff:
			return;
		default:
			error("Invalid pic opcode %02x", opcode);
		}
	}
}

} // End of namespace Adl