aboutsummaryrefslogtreecommitdiff
path: root/engines/sherlock/screen.cpp
blob: 349bf4d024a2542af1a2ed571d7a54c0137a288a (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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/* 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 "sherlock/screen.h"
#include "sherlock/sherlock.h"
#include "common/system.h"
#include "common/util.h"
#include "graphics/palette.h"

namespace Sherlock {

Screen::Screen(SherlockEngine *vm) : Surface(g_system->getWidth(), g_system->getHeight()), _vm(vm),
		_backBuffer1(g_system->getWidth(), g_system->getHeight()),
		_backBuffer2(g_system->getWidth(), g_system->getHeight()),
		_backBuffer(&_backBuffer1) {
	_transitionSeed = 1;
	_fadeStyle = false;
	_font = nullptr;
	_fontHeight = 0;
	Common::fill(&_cMap[0], &_cMap[PALETTE_SIZE], 0);
	Common::fill(&_sMap[0], &_sMap[PALETTE_SIZE], 0);
	Common::fill(&_tMap[0], &_tMap[PALETTE_SIZE], 0);
	setFont(1);

	// Set dummy surface used for restricted scene drawing
	_sceneSurface.format = Graphics::PixelFormat::createFormatCLUT8();
	_sceneSurface.pitch = pitch;

	// Rose Tattoo specific fields
	_fadeBytesRead = _fadeBytesToRead = 0;
	_oldFadePercent = 0;
	_scrollSize = 0;
	_currentScroll = 0;
	_targetScroll = 0;
}

Screen::~Screen() {
	delete _font;
}

/**
 * Set the font to use for writing text on the screen
 */
void Screen::setFont(int fontNumb) {
	_fontNumber = fontNumb;
	Common::String fname = Common::String::format("FONT%d.VGS", fontNumb + 1);

	// Discard any previous font and read in new one
	delete _font;
	_font = new ImageFile(fname);

	// Iterate through the frames to find the tallest font character
	_fontHeight = 0;
	for (uint idx = 0; idx < _font->size(); ++idx)
		_fontHeight = MAX((uint16)_fontHeight, (*_font)[idx]._frame.h);
}

/**
 * Handles updating any dirty areas of the screen Surface object to the physical screen
 */
void Screen::update() {
	// Merge the dirty rects
	mergeDirtyRects();

	// Loop through copying dirty areas to the physical screen
	Common::List<Common::Rect>::iterator i;
	for (i = _dirtyRects.begin(); i != _dirtyRects.end(); ++i) {
		const Common::Rect &r = *i;
		const byte *srcP = (const byte *)getBasePtr(r.left, r.top);
		g_system->copyRectToScreen(srcP, this->pitch, r.left, r.top,
			r.width(), r.height());
	}

	// Signal the physical screen to update
	g_system->updateScreen();
	_dirtyRects.clear();
}

/**
 * Return the currently active palette
 */
void Screen::getPalette(byte palette[PALETTE_SIZE]) {
	g_system->getPaletteManager()->grabPalette(palette, 0, PALETTE_COUNT);
}

/**
 * Set the palette
 */
void Screen::setPalette(const byte palette[PALETTE_SIZE]) {
	g_system->getPaletteManager()->setPalette(palette, 0, PALETTE_COUNT);
}

/**
 * Fades from the currently active palette to the passed palette
 */
int Screen::equalizePalette(const byte palette[PALETTE_SIZE]) {
	int total = 0;
	byte tempPalette[PALETTE_SIZE];
	getPalette(tempPalette);

	// For any palette component that doesn't already match the given destination
	// palette, change by 1 towards the reference palette component
	for (int idx = 0; idx < PALETTE_SIZE; ++idx) {
		if (tempPalette[idx] > palette[idx])
		{
			tempPalette[idx] = MAX((int)palette[idx], (int)tempPalette[idx] - 4);
			++total;
		} else if (tempPalette[idx] < palette[idx]) {
			tempPalette[idx] = MIN((int)palette[idx], (int)tempPalette[idx] + 4);
			++total;
		}
	}

	if (total > 0)
		// Palette changed, so reload it
		setPalette(tempPalette);

	return total;
}

/**
 * Fade out the palette to black
 */
void Screen::fadeToBlack(int speed) {
	byte tempPalette[PALETTE_SIZE];
	Common::fill(&tempPalette[0], &tempPalette[PALETTE_SIZE], 0);

	while (equalizePalette(tempPalette)) {
		_vm->_events->delay(15 * speed);
	}

	setPalette(tempPalette);
	fillRect(Common::Rect(0, 0, this->w, this->h), 0);
}

/**
 * Fade in a given palette
 */
void Screen::fadeIn(const byte palette[PALETTE_SIZE], int speed) {
	int count = 50;
	while (equalizePalette(palette) && --count) {
		_vm->_events->delay(15 * speed);
	}

	setPalette(palette);
}

/**
 * Adds a rectangle to the list of modified areas of the screen during the
 * current frame
 */
void Screen::addDirtyRect(const Common::Rect &r) {
	_dirtyRects.push_back(r);
	assert(r.width() > 0 && r.height() > 0);
}

/**
 * Merges together overlapping dirty areas of the screen
 */
void Screen::mergeDirtyRects() {
	Common::List<Common::Rect>::iterator rOuter, rInner;

	// Process the dirty rect list to find any rects to merge
	for (rOuter = _dirtyRects.begin(); rOuter != _dirtyRects.end(); ++rOuter) {
		rInner = rOuter;
		while (++rInner != _dirtyRects.end()) {

			if ((*rOuter).intersects(*rInner)) {
				// these two rectangles overlap or
				// are next to each other - merge them

				unionRectangle(*rOuter, *rOuter, *rInner);

				// remove the inner rect from the list
				_dirtyRects.erase(rInner);

				// move back to beginning of list
				rInner = rOuter;
			}
		}
	}
}

/**
 * Returns the union of two dirty area rectangles
 */
bool Screen::unionRectangle(Common::Rect &destRect, const Common::Rect &src1, const Common::Rect &src2) {
	destRect = src1;
	destRect.extend(src2);

	return !destRect.isEmpty();
}

/**
 * Do a random pixel transition in from _backBuffer surface to the screen
 */
void Screen::randomTransition() {
	Events &events = *_vm->_events;
	const int TRANSITION_MULTIPLIER = 0x15a4e35;
	_dirtyRects.clear();

	for (int idx = 0; idx <= 65535 && !_vm->shouldQuit(); ++idx) {
		_transitionSeed = _transitionSeed * TRANSITION_MULTIPLIER + 1;
		int offset = _transitionSeed & 65535;

		if (offset < (this->w * this->h))
			*((byte *)getPixels() + offset) = *((const byte *)_backBuffer->getPixels() + offset);

		if (idx != 0 && (idx % 300) == 0) {
			// Ensure there's a full screen dirty rect for the next frame update
			if (_dirtyRects.empty())
				addDirtyRect(Common::Rect(0, 0, this->w, this->h));

			events.pollEvents();
			events.delay(1);
		}
	}

	// Make sure everything has been transferred
	blitFrom(*_backBuffer);
}

/**
 * Transition to the surface from _backBuffer using a vertical transition
 */
void Screen::verticalTransition() {
	Events &events = *_vm->_events;

	byte table[640];
	Common::fill(&table[0], &table[640], 0);

	for (int yp = 0; yp < this->h; ++yp) {
		for (int xp = 0; xp < this->w; ++xp) {
			int temp = (table[xp] >= (this->h - 3)) ? this->h - table[xp] :
				_vm->getRandomNumber(3) + 1;

			if (temp) {
				blitFrom(_backBuffer1, Common::Point(xp, table[xp]),
					Common::Rect(xp, table[xp], xp + 1, table[xp] + temp));
				table[xp] += temp;
			}
		}

		events.delay(10);
	}
}

/**
 * Copies a section of the second back buffer into the main back buffer
 */
void Screen::restoreBackground(const Common::Rect &r) {
	if (r.width() > 0 && r.height() > 0) {
		Common::Rect tempRect = r;
		tempRect.clip(Common::Rect(0, 0, this->w, SHERLOCK_SCENE_HEIGHT));

		if (tempRect.isValidRect())
			_backBuffer1.blitFrom(_backBuffer2, Common::Point(tempRect.left, tempRect.top), tempRect);
	}
}

/**
 * Copies a given area to the screen
 */
void Screen::slamArea(int16 xp, int16 yp, int16 width, int16 height) {
	slamRect(Common::Rect(xp, yp, xp + width, yp + height));
}

/**
 * Copies a given area to the screen
 */
void Screen::slamRect(const Common::Rect &r) {
	if (r.width() && r.height() > 0) {
		Common::Rect tempRect = r;
		tempRect.clip(Common::Rect(0, 0, this->w, this->h));

		if (tempRect.isValidRect())
			blitFrom(*_backBuffer, Common::Point(tempRect.left, tempRect.top), tempRect);
	}
}

/**
 * Copy an image from the back buffer to the screen, taking care of both the
 * new area covered by the shape as well as the old area, which must be restored
 */
void Screen::flushImage(ImageFrame *frame, const Common::Point &pt,
		int16 *xp, int16 *yp, int16 *width, int16 *height) {
	Common::Point imgPos = pt + frame->_offset;
	Common::Rect newBounds(imgPos.x, imgPos.y, imgPos.x + frame->_frame.w, imgPos.y + frame->_frame.h);
	Common::Rect oldBounds(*xp, *yp, *xp + *width, *yp + *height);

	// See if the areas of the old and new overlap, and if so combine the areas
	if (newBounds.intersects(oldBounds)) {
		Common::Rect mergedBounds = newBounds;
		mergedBounds.extend(oldBounds);
		mergedBounds.right += 1;
		mergedBounds.bottom += 1;

		slamRect(mergedBounds);
	} else {
		// The two areas are independent, so copy them both
		slamRect(newBounds);
		slamRect(oldBounds);
	}

	*xp = newBounds.left;
	*yp = newBounds.top;
	*width = newBounds.width();
	*height = newBounds.height();
}

/**
 * Prints the text passed onto the back buffer at the given position and color.
 * The string is then blitted to the screen
 */
void Screen::print(const Common::Point &pt, byte color, const char *formatStr, ...) {
	// Create the string to display
	char buffer[100];
	va_list args;

	va_start(args, formatStr);
	vsprintf(buffer, formatStr, args);
	va_end(args);
	Common::String str(buffer);

	// Figure out area to draw text in
	Common::Point pos = pt;
	int width = stringWidth(str);
	pos.y--;		// Font is always drawing one line higher
	if (!pos.x)
		// Center text horizontally
		pos.x = (this->w - width) / 2;

	Common::Rect textBounds(pos.x, pos.y, pos.x + width, pos.y + _fontHeight);
	if (textBounds.right > this->w)
		textBounds.moveTo(this->w - width, textBounds.top);
	if (textBounds.bottom > this->h)
		textBounds.moveTo(textBounds.left, this->h - _fontHeight);

	// Write out the string at the given position
	writeString(str, Common::Point(textBounds.left, textBounds.top), color);

	// Copy the affected area to the screen
	slamRect(textBounds);
}

/**
 * Print a strings onto the back buffer without blitting it to the screen
 */
void Screen::gPrint(const Common::Point &pt, byte color, const char *formatStr, ...) {
	// Create the string to display
	char buffer[100];
	va_list args;

	va_start(args, formatStr);
	vsprintf(buffer, formatStr, args);
	va_end(args);
	Common::String str(buffer);

	// Print the text
	writeString(str, pt, color);
}


/**
 * Returns the width of a string in pixels
 */
int Screen::stringWidth(const Common::String &str) {
	int width = 0;

	for (const char *c = str.c_str(); *c; ++c)
		width += charWidth(*c);

	return width;
}

/**
 * Returns the width of a character in pixels
 */
int Screen::charWidth(char c) {
	if (c == ' ')
		return 5;
	else if (c > ' ' && c <= '~')
		return (*_font)[c - 33]._frame.w + 1;
	else
		return 0;
}

/**
 * Draws the given string into the back buffer using the images stored in _font
 */
void Screen::writeString(const Common::String &str, const Common::Point &pt, byte color) {
	Common::Point charPos = pt;

	for (const char *c = str.c_str(); *c; ++c) {
		if (*c == ' ')
			charPos.x += 5;
		else {
			assert(*c > ' ' && *c <= '~');
			ImageFrame &frame = (*_font)[*c - 33];
			_backBuffer->transBlitFrom(frame, charPos, false, color);
			charPos.x += frame._frame.w + 1;
		}
	}
}

/**
 * Fills an area on the back buffer, and then copies it to the screen
 */
void Screen::vgaBar(const Common::Rect &r, int color) {
	_backBuffer->fillRect(r, color);
	slamRect(r);
}

/**
 * Draws a button for use in the inventory, talk, and examine dialogs.
 */
void Screen::makeButton(const Common::Rect &bounds, int textX,
		const Common::String &str) {

	Surface &bb = *_backBuffer;
	bb.fillRect(Common::Rect(bounds.left, bounds.top, bounds.right, bounds.top + 1), BUTTON_TOP);
	bb.fillRect(Common::Rect(bounds.left, bounds.top, bounds.left + 1, bounds.bottom), BUTTON_TOP);
	bb.fillRect(Common::Rect(bounds.right - 1, bounds.top, bounds.right, bounds.bottom), BUTTON_BOTTOM);
	bb.fillRect(Common::Rect(bounds.left + 1, bounds.bottom - 1, bounds.right, bounds.bottom), BUTTON_BOTTOM);
	bb.fillRect(Common::Rect(bounds.left + 1, bounds.top + 1, bounds.right - 1, bounds.bottom - 1), BUTTON_MIDDLE);

	gPrint(Common::Point(textX, bounds.top), COMMAND_HIGHLIGHTED, "%c", str[0]);
	gPrint(Common::Point(textX + charWidth(str[0]), bounds.top),
		COMMAND_FOREGROUND, "%s", str.c_str() + 1);
}

/**
 * Prints an interface command with the first letter highlighted to indicate
 * what keyboard shortcut is associated with it
 */
void Screen::buttonPrint(const Common::Point &pt, byte color, bool slamIt,
		const Common::String &str) {
	int xStart = pt.x - stringWidth(str) / 2;

	if (color == COMMAND_FOREGROUND) {
		// First character needs to be highlighted
		if (slamIt) {
			print(Common::Point(xStart, pt.y + 1), COMMAND_HIGHLIGHTED, "%c", str[0]);
			print(Common::Point(xStart + charWidth(str[0]), pt.y + 1),
				COMMAND_FOREGROUND, str.c_str() + 1);
		} else {
			gPrint(Common::Point(xStart, pt.y), COMMAND_HIGHLIGHTED, "%c", str[0]);
			gPrint(Common::Point(xStart + charWidth(str[0]), pt.y),
				COMMAND_FOREGROUND, str.c_str() + 1);
		}
	} else if (slamIt) {
		print(Common::Point(xStart, pt.y + 1), color, str.c_str());
	} else {
		gPrint(Common::Point(xStart, pt.y), color, str.c_str());
	}
}

/**
 * Draw a panel in th eback buffer with a raised area effect around the edges
 */
void Screen::makePanel(const Common::Rect &r) {
	_backBuffer->fillRect(r, BUTTON_MIDDLE);
	_backBuffer->hLine(r.left, r.top, r.right - 2, BUTTON_TOP);
	_backBuffer->hLine(r.left + 1, r.top + 1, r.right - 3, BUTTON_TOP);
	_backBuffer->vLine(r.left, r.top, r.bottom - 1, BUTTON_TOP);
	_backBuffer->vLine(r.left + 1, r.top + 1, r.bottom - 2, BUTTON_TOP);

	_backBuffer->vLine(r.right - 1, r.top, r.bottom - 1, BUTTON_BOTTOM);
	_backBuffer->vLine(r.right - 2, r.top + 1, r.bottom - 2, BUTTON_BOTTOM);
	_backBuffer->hLine(r.left, r.bottom - 1, r.right - 1, BUTTON_BOTTOM);
	_backBuffer->hLine(r.left + 1, r.bottom - 2, r.right - 1, BUTTON_BOTTOM);
}

/**
 * Sets the active back buffer pointer to a restricted sub-area of the first back buffer
 */
void Screen::setDisplayBounds(const Common::Rect &r) {
	assert(r.left == 0 && r.top == 0);
	_sceneSurface.setPixels(_backBuffer1.getPixels());
	_sceneSurface.w = r.width();
	_sceneSurface.h = r.height();

	_backBuffer = &_sceneSurface;
}

/**
 * Resets the active buffer pointer to point back to the full first back buffer
 */
void Screen::resetDisplayBounds() {
	_backBuffer = &_backBuffer1;
}

/**
 * Return the size of the current display window
 */
Common::Rect Screen::getDisplayBounds() {
	return (_backBuffer == &_sceneSurface) ? Common::Rect(0, 0, _sceneSurface.w, _sceneSurface.h) :
		Common::Rect(0, 0, this->w, this->h);
}

/**
 * Synchronize the data for a savegame
 */
void Screen::synchronize(Common::Serializer &s) {
	int fontNumb = _fontNumber;
	s.syncAsByte(fontNumb);
	if (s.isLoading())
		setFont(fontNumb);
}

void Screen::initPaletteFade(int bytesToRead) {
	Common::copy(&_cMap[0], &_cMap[PALETTE_SIZE], &_sMap[0]);
	Common::copy(&_cMap[0], &_cMap[PALETTE_SIZE], &_tMap[0]);

	// Set how many bytes need to be read / have been read
	_fadeBytesRead = 0;
	_fadeBytesToRead = bytesToRead;
	_oldFadePercent = 0;
}

int Screen::fadeRead(Common::SeekableReadStream &stream, byte *buf, int totalSize) {
	warning("TODO: fadeRead");
	stream.read(buf, totalSize);
	return totalSize;
}

/**
 * Creates a grey-scale version of the passed palette
 */
void Screen::setupBGArea(const byte cMap[PALETTE_SIZE]) {
	warning("TODO");
}

/**
 * Initializes scroll variables
 */
void Screen::initScrollVars() {
	_scrollSize = 0;
	_currentScroll = 0;
	_targetScroll = 0;
}

} // End of namespace Sherlock