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
|
/* 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/system.h"
#include "common/str.h"
#include "common/config-manager.h"
#include "graphics/surface.h"
#include "graphics/palette.h"
#include "graphics/thumbnail.h"
#include "engines/util.h"
#include "adl/display.h"
namespace Adl {
// TODO: Implement partial screen updates
#define DISPLAY_PITCH (DISPLAY_WIDTH / 7)
#define COLOR_PALETTE_ENTRIES 6
const byte colorPalette[COLOR_PALETTE_ENTRIES * 3] = {
0x00, 0x00, 0x00,
0xff, 0xff, 0xff,
0xc7, 0x34, 0xff,
0x38, 0xcb, 0x00,
0x0d, 0xa1, 0xff,
0xf2, 0x5e, 0x00
};
#define MONO_PALETTE_ENTRIES 2
const byte monoPalette[MONO_PALETTE_ENTRIES * 3] = {
0x00, 0x00, 0x00,
0x00, 0xc0, 0x01
};
static const byte font[64][5] = {
{ 0x7c, 0x82, 0xba, 0xb2, 0x9c }, { 0xf8, 0x24, 0x22, 0x24, 0xf8 }, // @A
{ 0xfe, 0x92, 0x92, 0x92, 0x6c }, { 0x7c, 0x82, 0x82, 0x82, 0x44 }, // BC
{ 0xfe, 0x82, 0x82, 0x82, 0x7c }, { 0xfe, 0x92, 0x92, 0x92, 0x82 }, // DE
{ 0xfe, 0x12, 0x12, 0x12, 0x02 }, { 0x7c, 0x82, 0x82, 0xa2, 0xe2 }, // FG
{ 0xfe, 0x10, 0x10, 0x10, 0xfe }, { 0x00, 0x82, 0xfe, 0x82, 0x00 }, // HI
{ 0x40, 0x80, 0x80, 0x80, 0x7e }, { 0xfe, 0x10, 0x28, 0x44, 0x82 }, // JK
{ 0xfe, 0x80, 0x80, 0x80, 0x80 }, { 0xfe, 0x04, 0x18, 0x04, 0xfe }, // LM
{ 0xfe, 0x08, 0x10, 0x20, 0xfe }, { 0x7c, 0x82, 0x82, 0x82, 0x7c }, // NO
{ 0xfe, 0x12, 0x12, 0x12, 0x0c }, { 0x7c, 0x82, 0xa2, 0x42, 0xbc }, // PQ
{ 0xfe, 0x12, 0x32, 0x52, 0x8c }, { 0x4c, 0x92, 0x92, 0x92, 0x64 }, // RS
{ 0x02, 0x02, 0xfe, 0x02, 0x02 }, { 0x7e, 0x80, 0x80, 0x80, 0x7e }, // TU
{ 0x3e, 0x40, 0x80, 0x40, 0x3e }, { 0xfe, 0x40, 0x30, 0x40, 0xfe }, // VW
{ 0xc6, 0x28, 0x10, 0x28, 0xc6 }, { 0x06, 0x08, 0xf0, 0x08, 0x06 }, // XY
{ 0xc2, 0xa2, 0x92, 0x8a, 0x86 }, { 0xfe, 0xfe, 0x82, 0x82, 0x82 }, // Z[
{ 0x04, 0x08, 0x10, 0x20, 0x40 }, { 0x82, 0x82, 0x82, 0xfe, 0xfe }, // \]
{ 0x20, 0x10, 0x08, 0x10, 0x20 }, { 0x80, 0x80, 0x80, 0x80, 0x80 }, // ^_
{ 0x00, 0x00, 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0xbe, 0x00, 0x00 }, // !
{ 0x00, 0x0e, 0x00, 0x0e, 0x00 }, { 0x28, 0xfe, 0x28, 0xfe, 0x28 }, // "#
{ 0x48, 0x54, 0xfe, 0x54, 0x24 }, { 0x46, 0x26, 0x10, 0xc8, 0xc4 }, // $%
{ 0x6c, 0x92, 0xac, 0x40, 0xa0 }, { 0x00, 0x00, 0x0e, 0x00, 0x00 }, // &'
{ 0x38, 0x44, 0x82, 0x00, 0x00 }, { 0x00, 0x00, 0x82, 0x44, 0x38 }, // ()
{ 0x44, 0x28, 0xfe, 0x28, 0x44 }, { 0x10, 0x10, 0x7c, 0x10, 0x10 }, // *+
{ 0x00, 0x80, 0x60, 0x00, 0x00 }, { 0x10, 0x10, 0x10, 0x10, 0x10 }, // ,-
{ 0x00, 0x00, 0x80, 0x00, 0x00 }, { 0x40, 0x20, 0x10, 0x08, 0x04 }, // ./
{ 0x7c, 0xa2, 0x92, 0x8a, 0x7c }, { 0x00, 0x84, 0xfe, 0x80, 0x00 }, // 01
{ 0xc4, 0xa2, 0x92, 0x92, 0x8c }, { 0x42, 0x82, 0x92, 0x9a, 0x66 }, // 23
{ 0x30, 0x28, 0x24, 0xfe, 0x20 }, { 0x4e, 0x8a, 0x8a, 0x8a, 0x72 }, // 45
{ 0x78, 0x94, 0x92, 0x92, 0x62 }, { 0x02, 0xe2, 0x12, 0x0a, 0x06 }, // 67
{ 0x6c, 0x92, 0x92, 0x92, 0x6c }, { 0x8c, 0x92, 0x92, 0x52, 0x3c }, // 89
{ 0x00, 0x00, 0x28, 0x00, 0x00 }, { 0x00, 0x80, 0x68, 0x00, 0x00 }, // :;
{ 0x10, 0x28, 0x44, 0x82, 0x00 }, { 0x28, 0x28, 0x28, 0x28, 0x28 }, // <=
{ 0x00, 0x82, 0x44, 0x28, 0x10 }, { 0x04, 0x02, 0xb2, 0x0a, 0x04 } // >?
};
Display::Display() :
_mode(DISPLAY_MODE_TEXT),
_cursorPos(0),
_showCursor(false) {
initGraphics(560, 384, true);
_monochrome = !ConfMan.getBool("color");
_scanlines = ConfMan.getBool("scanlines");
if (_monochrome)
g_system->getPaletteManager()->setPalette(monoPalette, 0, MONO_PALETTE_ENTRIES);
else
g_system->getPaletteManager()->setPalette(colorPalette, 0, COLOR_PALETTE_ENTRIES);
enableScanlines(_scanlines);
_frameBuf = new byte[DISPLAY_PITCH * DISPLAY_HEIGHT];
_frameBufSurface = new Graphics::Surface;
// We need 2x scaling to properly render the half-pixel shift
// of the second palette
_frameBufSurface->create(DISPLAY_WIDTH * 2, DISPLAY_HEIGHT * 2, Graphics::PixelFormat::createFormatCLUT8());
_textBuf = new byte[kTextBufSize];
memset(_textBuf, APPLECHAR(' '), kTextBufSize);
_textBufSurface = new Graphics::Surface;
// For ease of copying, also use 2x scaling here
_textBufSurface->create(DISPLAY_WIDTH * 2, DISPLAY_HEIGHT * 2, Graphics::PixelFormat::createFormatCLUT8());
createFont();
}
Display::~Display() {
delete[] _frameBuf;
_frameBufSurface->free();
delete _frameBufSurface;
delete[] _textBuf;
_textBufSurface->free();
delete _textBufSurface;
_font->free();
delete _font;
}
void Display::setMode(DisplayMode mode) {
_mode = mode;
if (_mode == DISPLAY_MODE_TEXT || _mode == DISPLAY_MODE_MIXED)
updateTextScreen();
if (_mode == DISPLAY_MODE_HIRES || _mode == DISPLAY_MODE_MIXED)
updateHiResScreen();
}
void Display::updateTextScreen() {
updateTextSurface();
if (_mode == DISPLAY_MODE_TEXT)
g_system->copyRectToScreen(_textBufSurface->getPixels(), _textBufSurface->pitch, 0, 0, _textBufSurface->w, _textBufSurface->h);
else if (_mode == DISPLAY_MODE_MIXED)
g_system->copyRectToScreen(_textBufSurface->getBasePtr(0, _textBufSurface->h - 4 * 8 * 2), _textBufSurface->pitch, 0, _textBufSurface->h - 4 * 8 * 2, _textBufSurface->w, 4 * 8 * 2);
g_system->updateScreen();
}
void Display::updateHiResScreen() {
updateHiResSurface();
if (_mode == DISPLAY_MODE_HIRES)
g_system->copyRectToScreen(_frameBufSurface->getPixels(), _frameBufSurface->pitch, 0, 0, _frameBufSurface->w, _frameBufSurface->h);
else if (_mode == DISPLAY_MODE_MIXED)
g_system->copyRectToScreen(_frameBufSurface->getPixels(), _frameBufSurface->pitch, 0, 0, _frameBufSurface->w, _frameBufSurface->h - 4 * 8 * 2);
g_system->updateScreen();
}
bool Display::saveThumbnail(Common::WriteStream &out) {
if (_scanlines) {
enableScanlines(false);
g_system->updateScreen();
}
bool retval = Graphics::saveThumbnail(out);
if (_scanlines) {
enableScanlines(true);
g_system->updateScreen();
}
return retval;
}
void Display::loadFrameBuffer(Common::ReadStream &stream) {
for (uint j = 0; j < 8; ++j) {
for (uint i = 0; i < 8; ++i) {
byte *dst = _frameBuf + DISPLAY_PITCH * (i * 8 + j);
stream.read(dst, DISPLAY_PITCH);
dst += DISPLAY_PITCH * 64;
stream.read(dst, DISPLAY_PITCH);
dst += DISPLAY_PITCH * 64;
stream.read(dst, DISPLAY_PITCH);
stream.readUint32LE();
stream.readUint32LE();
dst += DISPLAY_PITCH * 64;
}
}
}
void Display::putPixel(Common::Point p, byte color) {
byte offset = p.x / 7;
if (offset & 1) {
byte c = color << 1;
if (c >= 0x40 && c < 0xc0)
color ^= 0x7f;
}
byte *b = _frameBuf + p.y * DISPLAY_PITCH + offset;
color ^= *b;
color &= 1 << (p.x % 7);
*b ^= color;
}
void Display::clear(byte color) {
byte val = 0;
byte c = color << 1;
if (c >= 0x40 && c < 0xc0)
val = 0x7f;
for (uint i = 0; i < DISPLAY_PITCH * DISPLAY_HEIGHT; ++i) {
_frameBuf[i] = color;
color ^= val;
}
}
void Display::home() {
memset(_textBuf, APPLECHAR(' '), kTextBufSize);
_cursorPos = 0;
}
void Display::moveCursorForward() {
++_cursorPos;
if (_cursorPos >= kTextBufSize)
scrollUp();
}
void Display::moveCursorBackward() {
--_cursorPos;
if (_cursorPos < 0)
_cursorPos = 0;
}
void Display::moveCursorTo(const Common::Point &pos) {
_cursorPos = pos.y * 40 + pos.x;
if (_cursorPos >= kTextBufSize)
error("Cursor position (%i, %i) out of bounds", pos.x, pos.y);
}
void Display::printString(const Common::String &str) {
Common::String::const_iterator c;
for (c = str.begin(); c != str.end(); ++c) {
byte b = *c;
if (*c == APPLECHAR('\r'))
_cursorPos = (_cursorPos / 40 + 1) * 40;
else if (b < 0x80 || b >= 0xa0) {
setCharAtCursor(b);
++_cursorPos;
}
if (_cursorPos == kTextBufSize)
scrollUp();
}
updateTextScreen();
}
void Display::setCharAtCursor(byte c) {
_textBuf[_cursorPos] = c;
}
void Display::showCursor(bool enable) {
_showCursor = enable;
}
void Display::enableScanlines(bool enable) {
byte pal[COLOR_PALETTE_ENTRIES * 3] = { };
if (enable)
g_system->getPaletteManager()->setPalette(pal, 6, 6);
else {
g_system->getPaletteManager()->grabPalette(pal, 0, 6);
g_system->getPaletteManager()->setPalette(pal, 6, 6);
}
}
void Display::updateHiResSurface() {
byte *src = _frameBuf;
byte *dst = (byte *)_frameBufSurface->getPixels();
for (uint i = 0; i < DISPLAY_HEIGHT; ++i) {
decodeScanline(dst, _frameBufSurface->pitch, src);
src += DISPLAY_PITCH;
dst += _frameBufSurface->pitch * 2;
}
}
void Display::decodeScanlineColor(byte *dst, int pitch, byte *src) const {
// TODO: shift secondPal by half a pixel
bool prevOn = false;
for (uint j = 0; j < 39; ++j) {
bool secondPal = src[j] & 0x80;
byte cur = src[j];
byte next = 0;
if (j != 39)
next = src[j + 1];
for (uint k = 0; k < 7; ++k) {
bool curOn = cur & (1 << k);
bool nextOn;
if (k != 6)
nextOn = cur & (1 << (k + 1));
else
nextOn = next & 1;
byte color;
if (curOn == prevOn || curOn == nextOn)
color = curOn ? 1 : 0;
else {
if (secondPal)
color = (curOn == ((j + k) % 2) ? 5 : 4);
else
color = (curOn == ((j + k) % 2) ? 3 : 2);
}
dst[0] = color;
dst[1] = color;
dst[pitch] = color + 6;
dst[pitch + 1] = color + 6;
dst += 2;
prevOn = curOn;
}
}
}
void Display::decodeScanlineMono(byte *dst, int pitch, byte *src) const {
// TODO: shift secondPal by half a pixel
for (uint j = 0; j < 39; ++j) {
for (uint k = 0; k < 7; ++k) {
byte color = 0;
if (src[j] & (1 << k))
color = 1;
dst[0] = color;
dst[1] = color;
dst[pitch] = color + 6;
dst[pitch + 1] = color + 6;
dst += 2;
}
}
}
void Display::decodeScanline(byte *dst, int pitch, byte *src) const {
if (_monochrome)
decodeScanlineMono(dst, pitch, src);
else
decodeScanlineColor(dst, pitch, src);
}
void Display::updateTextSurface() {
for (uint row = 0; row < 24; ++row)
for (uint col = 0; col < 40; ++col) {
int charPos = row * 40 + col;
char c = _textBuf[row * 40 + col];
if (charPos == _cursorPos && _showCursor)
c = (c & 0x3f) | 0x40;
Common::Rect r(7 * 2, 8 * 2);
r.translate(((c & 0x3f) % 16) * 7 * 2, (c & 0x3f) / 16 * 8 * 2);
if (!(c & 0x80)) {
if (!(c & 0x40) || ((g_system->getMillis() / 270) & 1))
r.translate(0, 4 * 8 * 2);
}
_textBufSurface->copyRectToSurface(*_font, col * 7 * 2, row * 8 * 2, r);
}
}
void Display::drawChar(byte c, int x, int y) {
byte *buf = (byte *)_font->getPixels() + y * _font->pitch + x;
for (uint row = 0; row < 8; ++row) {
if (row & 1) {
buf[_font->pitch] = 6;
buf[_font->pitch + 1] = 6;
buf[_font->pitch + 6 * 2] = 6;
buf[_font->pitch + 6 * 2 + 1] = 6;
}
for (uint col = 1; col < 6; ++col) {
if (font[c][col - 1] & (1 << row)) {
buf[col * 2] = 1;
buf[col * 2 + 1] = 1;
buf[_font->pitch + col * 2] = 1 + 6;
buf[_font->pitch + col * 2 + 1] = 1 + 6;
} else {
buf[_font->pitch + col * 2] = 6;
buf[_font->pitch + col * 2 + 1] = 6;
}
}
buf += 2 * _font->pitch;
}
}
void Display::createFont() {
_font = new Graphics::Surface;
_font->create(16 * 7 * 2, 4 * 8 * 2 * 2, Graphics::PixelFormat::createFormatCLUT8());
for (uint i = 0; i < 4; ++i)
for (uint j = 0; j < 16; ++j)
drawChar(i * 16 + j, j * 7 * 2, i * 8 * 2);
// Create inverted font
byte *buf = (byte *)_font->getPixels();
byte *bufInv = buf + (_font->h / 2) * _font->pitch;
for (uint row = 0; row < _font->h / 2; row += 2) {
for (uint col = 0; col < _font->w; ++col)
bufInv[col] = (buf[col] ? 0 : 1);
buf += _font->pitch;
bufInv += _font->pitch;
for (uint col = 0; col < _font->w; ++col)
bufInv[col] = (buf[col] == 7 ? 6 : 7);
buf += _font->pitch;
bufInv += _font->pitch;
}
}
void Display::scrollUp() {
memmove(_textBuf, _textBuf + 40, kTextBufSize - 40);
memset(_textBuf + kTextBufSize - 40, APPLECHAR(' '), 40);
_cursorPos -= 40;
}
} // End of namespace Adl
|