File: picture_gal.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (267 lines) | stat: -rw-r--r-- 7,042 bytes parent folder | download
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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "agi/agi.h"
#include "agi/graphics.h"
#include "agi/picture.h"

#include "agi/picture_gal.h"

namespace Agi {
	
// PictureMgr_GAL decodes and draws picture resources in early King's Quest 1.
//
// This "Game Adaptation Language" format was used in PC Booters and Apple II.
//
// This format supports lines and flood fills, and visual and priority screens.
//
// As this is the format that evolved into AGI, it is quite similar apart from
// the specific opcodes. The major difference is the line drawing routine;
// it produces different results than AGI. Flood fills implicitly rely on this.
// When KQ1 was ported to AGI, the new lines prevented some fills from working,
// so they just added more. There are still a few white pixels they missed.
//
// As with Troll's Tale, room pictures depend on the game first drawing a frame
// within the entire picture area. When KQ1 was converted to AGI, this frame
// was added to each individual picture resource.

PictureMgr_GAL::PictureMgr_GAL(AgiBase *agi, GfxMgr *gfx) :
	PictureMgr(agi, gfx) {
}

void PictureMgr_GAL::drawPicture() {
	debugC(kDebugLevelPictures, "Drawing picture %d", _resourceNr);

	drawBlackFrame();

	_dataOffset = 0;
	_dataOffsetNibble = false;
	_patCode = 0;
	_patNum = 0;
	_priOn = true; // initially off in AGI
	_scrOn = false;
	_scrColor = 15;
	_priColor = 1;

	// GAL toggles the current screen between visual and priority
	// with opcode F0. This affects opcodes F4-F7, but the rest of
	// the opcodes are explicit about which screen(s) they draw to.
	byte prevScrOn = _scrOn;
	byte prevPriOn = _priOn;

	while (_dataOffset < _dataSize) {
		byte curByte = getNextByte();

		switch (curByte) {
		case 0xf0: // toggle current screen
			draw_SetScreens(!_scrOn, !_priOn);
			break;
		case 0xf1:
			draw_SetColor();
			break;
		case 0xf2:
			draw_SetPriority();
			break;
		case 0xf3:
			draw_SetColor();
			draw_SetPriority();
			break;

		// Line operations drawn to both visual and priority screens
		case 0xf4:
			draw_SetScreens(true, true, prevScrOn, prevPriOn);
			yCorner();
			draw_SetScreens(prevScrOn, prevPriOn);
			break;
		case 0xf5:
			draw_SetScreens(true, true, prevScrOn, prevPriOn);
			xCorner();
			draw_SetScreens(prevScrOn, prevPriOn);
			break;
		case 0xf6:
			draw_SetScreens(true, true, prevScrOn, prevPriOn);
			draw_LineAbsolute();
			draw_SetScreens(prevScrOn, prevPriOn);
			break;
		case 0xf7:
			draw_SetScreens(true, true, prevScrOn, prevPriOn);
			draw_LineShort();
			draw_SetScreens(prevScrOn, prevPriOn);
			break;

		// Line operations drawn to the current screen
		case 0xf8:
			yCorner();
			break;
		case 0xf9:
			xCorner();
			break;
		case 0xfa:
			draw_LineAbsolute();
			break;
		case 0xfb:
			draw_LineShort();
			break;

		// Fill operations drawn to one or both screens
		case 0xfc:
			draw_SetScreens(true, true, prevScrOn, prevPriOn);
			draw_SetColor();
			draw_SetPriority();
			draw_Fill();
			draw_SetScreens(prevScrOn, prevPriOn);
			break;
		case 0xfd:
			draw_SetScreens(false, true, prevScrOn, prevPriOn);
			draw_SetPriority();
			draw_Fill();
			draw_SetScreens(prevScrOn, prevPriOn);
			break;
		case 0xfe:
			draw_SetScreens(true, false, prevScrOn, prevPriOn);
			draw_SetColor();
			draw_Fill();
			draw_SetScreens(prevScrOn, prevPriOn);
			break;

		case 0xff: // end of data
			return;
		default:
			warning("Unknown picture opcode %02x at %04x", curByte, _dataOffset - 1);
			break;
		}
	}
}

/**
 * Sets the status of the visual and priority screens.
 */
void PictureMgr_GAL::draw_SetScreens(byte scrOn, byte priOn) {
	_scrOn = scrOn;
	_priOn = priOn;
}

/**
 * Sets the status of the visual and priority screens,
 * and returns their previous values.
 */
void PictureMgr_GAL::draw_SetScreens(byte scrOn, byte priOn, byte &prevScrOn, byte &prevPriOn) {
	prevScrOn = _scrOn;
	prevPriOn = _priOn;
	_scrOn = scrOn;
	_priOn = priOn;
}

/**
 * Draws a hard-coded black frame in both screens.
 * All room pictures require this to draw correctly.
 *
 * Original data: F3 00 00 F5 00 00 9F A7 00 00 FF
 */
void PictureMgr_GAL::drawBlackFrame() {
	_scrOn = true;
	_scrColor = 0;
	_priOn = true;
	_priColor = 0;
	draw_Line(0, 0, _width - 1, 0);
	draw_Line(_width - 1, 0, _width - 1, _height - 1);
	draw_Line(_width - 1, _height - 1, 0, _height - 1);
	draw_Line(0, _height - 1, 0, 0);
}

/**
 * Draws a horizontal, vertical, or diagonal line using the GAL drawing routine.
 *
 * This routine produces different diagonal lines than the AGI routine.
 */
void PictureMgr_GAL::draw_Line(int16 x1, int16 y1, int16 x2, int16 y2) {
	x1 = CLIP<int16>(x1, 0, _width - 1);
	x2 = CLIP<int16>(x2, 0, _width - 1);
	y1 = CLIP<int16>(y1, 0, _height - 1);
	y2 = CLIP<int16>(y2, 0, _height - 1);

	const byte width  = (x2 > x1) ? (x2 - x1) : (x1 - x2);
	const byte height = (y2 > y1) ? (y2 - y1) : (y1 - y2);

	byte x = 0;
	byte y = 0;
	if (width > height) {
		while (x != width) {
			x++;
			y = (x * height) / width;
			if (((x * height) % width) * 2 > width) {
				y++;
			}

			byte pixelX = (x2 > x1) ? (x1 + x) : (x1 - x);
			byte pixelY = (y2 > y1) ? (y1 + y) : (y1 - y);
			putVirtPixel(pixelX, pixelY);
		}
	} else {
		while (y != height) {
			y++;
			x = (y * width) / height;
			if (((y * width) % height) * 2 > height) {
				x++;
			}

			byte pixelX = (x2 > x1) ? (x1 + x) : (x1 - x);
			byte pixelY = (y2 > y1) ? (y1 + y) : (y1 - y);
			putVirtPixel(pixelX, pixelY);
		}
	}
}

/**
 * Gets the next x coordinate in the current picture instruction,
 * and clip it to the picture width.
 */
bool PictureMgr_GAL::getNextXCoordinate(byte &x) {
	if (!getNextParamByte(x)) {
		return false;
	}

	if (x >= _width) { // 160
		debugC(kDebugLevelPictures, "clipping %c from %d to %d", 'x', x, _width - 1);
		x = _width - 1; // 159
	}

	return true;
}

/**
 * Gets the next y coordinate in the current picture instruction,
 * and clip it to the picture height.
 */
bool PictureMgr_GAL::getNextYCoordinate(byte &y) {
	if (!getNextParamByte(y)) {
		return false;
	}

	if (y >= _height) { // 168
		debugC(kDebugLevelPictures, "clipping %c from %d to %d", 'y', y, _height);
		y = _height - 1; // 167
	}

	return true;
}

} // End of namespace Agi