aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/graphics/screen.h
blob: 65416252f6904d41f20dc84bb05d9d8ab9f266c6 (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
/* 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.
 *
 */

#ifndef SCI_GRAPHICS_SCREEN_H
#define SCI_GRAPHICS_SCREEN_H

#include "sci/sci.h"
#include "sci/graphics/helpers.h"
#include "sci/graphics/view.h"

#include "graphics/sjis.h"

namespace Sci {

#define SCI_SCREEN_UPSCALEDMAXHEIGHT 200
#define SCI_SCREEN_UPSCALEDMAXWIDTH  320

enum GfxScreenUpscaledMode {
	GFX_SCREEN_UPSCALED_DISABLED	= 0,
	GFX_SCREEN_UPSCALED_480x300     = 1,
	GFX_SCREEN_UPSCALED_640x400		= 2,
	GFX_SCREEN_UPSCALED_640x440		= 3,
	GFX_SCREEN_UPSCALED_640x480		= 4
};

enum GfxScreenMasks {
	GFX_SCREEN_MASK_VISUAL		= 1,
	GFX_SCREEN_MASK_PRIORITY	= 2,
	GFX_SCREEN_MASK_CONTROL		= 4,
	GFX_SCREEN_MASK_DISPLAY		= 8, // not official sierra sci, only used internally
	GFX_SCREEN_MASK_ALL			= GFX_SCREEN_MASK_VISUAL|GFX_SCREEN_MASK_PRIORITY|GFX_SCREEN_MASK_CONTROL
};

enum {
	DITHERED_BG_COLORS_SIZE = 256
};

/**
 * Screen class, actually creates 3 (4) screens internally:
 * - visual/display (for the user),
 * - priority (contains priority information) and
 * - control (contains control information).
 * Handles all operations to it and copies parts of visual/display screen to
 * the actual screen, so the user can really see it.
 */
class GfxScreen {
public:
	GfxScreen(ResourceManager *resMan);
	~GfxScreen();

	uint16 getWidth() { return _width; }
	uint16 getHeight() { return _height; }
	uint16 getScriptWidth() { return _scriptWidth; }
	uint16 getScriptHeight() { return _scriptHeight; }
	uint16 getDisplayWidth() { return _displayWidth; }
	uint16 getDisplayHeight() { return _displayHeight; }
	byte getColorWhite() { return _colorWhite; }
	byte getColorDefaultVectorData() { return _colorDefaultVectorData; }

#ifdef ENABLE_SCI32
	byte *getDisplayScreen() { return _displayScreen; }
	byte *getPriorityScreen() { return _priorityScreen; }
#endif

	void clearForRestoreGame();
	void copyToScreen();
	void copyFromScreen(byte *buffer);
	void kernelSyncWithFramebuffer();
	void copyRectToScreen(const Common::Rect &rect);
	void copyDisplayRectToScreen(const Common::Rect &rect);
	void copyRectToScreen(const Common::Rect &rect, int16 x, int16 y);

	// Vector drawing
private:
	void vectorPutLinePixel(int16 x, int16 y, byte drawMask, byte color, byte priority, byte control);
	void vectorPutLinePixel480x300(int16 x, int16 y, byte drawMask, byte color, byte priority, byte control);

public:
	void vectorAdjustLineCoordinates(int16 *left, int16 *top, int16 *right, int16 *bottom, byte drawMask, byte color, byte priority, byte control);
	byte vectorIsFillMatch(int16 x, int16 y, byte screenMask, byte checkForColor, byte checkForPriority, byte checkForControl, bool isEGA);

	byte getDrawingMask(byte color, byte prio, byte control);
	void drawLine(Common::Point startPoint, Common::Point endPoint, byte color, byte prio, byte control);
	void drawLine(int16 left, int16 top, int16 right, int16 bottom, byte color, byte prio, byte control) {
		drawLine(Common::Point(left, top), Common::Point(right, bottom), color, prio, control);
	}

	GfxScreenUpscaledMode getUpscaledHires() const {
		return _upscaledHires;
	}

	bool isUnditheringEnabled() const {
		return _unditheringEnabled;
	}
	void enableUndithering(bool flag);

	void putKanjiChar(Graphics::FontSJIS *commonFont, int16 x, int16 y, uint16 chr, byte color);

	int bitsGetDataSize(Common::Rect rect, byte mask);
	void bitsSave(Common::Rect rect, byte mask, byte *memoryPtr);
	void bitsGetRect(byte *memoryPtr, Common::Rect *destRect);
	void bitsRestore(byte *memoryPtr);

	void scale2x(const byte *src, byte *dst, int16 srcWidth, int16 srcHeight, byte bytesPerPixel = 1);

	void adjustToUpscaledCoordinates(int16 &y, int16 &x, Sci32ViewNativeResolution viewScalingType = SCI_VIEW_NATIVERES_NONE);
	void adjustBackUpscaledCoordinates(int16 &y, int16 &x, Sci32ViewNativeResolution viewScalingType = SCI_VIEW_NATIVERES_NONE);

	void dither(bool addToFlag);

	// Force a color combination as a dithered color
	void ditherForceDitheredColor(byte color);
	int16 *unditherGetDitheredBgColors();

	void debugShowMap(int mapNo);

	int _picNotValid; // possible values 0, 1 and 2
	int _picNotValidSci11; // another variable that is used by kPicNotValid in sci1.1

	int16 kernelPicNotValid(int16 newPicNotValid);
	void kernelShakeScreen(uint16 shakeCount, uint16 direction);

	void setFontIsUpscaled(bool isUpscaled) { _fontIsUpscaled = isUpscaled; }
	bool fontIsUpscaled() const { return _fontIsUpscaled; }

private:
	uint16 _width;
	uint16 _height;
	uint _pixels;
	uint16 _scriptWidth;
	uint16 _scriptHeight;
	uint16 _displayWidth;
	uint16 _displayHeight;
	uint _displayPixels;

	byte _colorWhite;
	byte _colorDefaultVectorData;

	void bitsRestoreScreen(Common::Rect rect, byte *&memoryPtr, byte *screen, uint16 screenWidth);
	void bitsRestoreDisplayScreen(Common::Rect rect, byte *&memoryPtr);
	void bitsSaveScreen(Common::Rect rect, byte *screen, uint16 screenWidth, byte *&memoryPtr);
	void bitsSaveDisplayScreen(Common::Rect rect, byte *&memoryPtr);

	void setVerticalShakePos(uint16 shakePos);

	/**
	 * If this flag is true, undithering is enabled, otherwise disabled.
	 */
	bool _unditheringEnabled;
	int16 _ditheredPicColors[DITHERED_BG_COLORS_SIZE];

	// These screens have the real resolution of the game engine (320x200 for
	// SCI0/SCI1/SCI11 games, 640x480 for SCI2 games). SCI0 games will be
	// dithered in here at any time.
	byte *_visualScreen;
	byte *_priorityScreen;
	byte *_controlScreen;

	/**
	 * This screen is the one, where pixels are copied out of into the frame buffer.
	 * It may be 640x400 for japanese SCI1 games. SCI0 games may be undithered in here.
	 * Only read from this buffer for Save/ShowBits usage.
	 */
	byte *_displayScreen;

	ResourceManager *_resMan;

	/**
	 * Pointer to the currently active screen (changing only required for
	 * debug purposes, to show for example the priority screen).
	 */
	byte *_activeScreen;

	/**
	 * This variable defines, if upscaled hires is active and what upscaled mode
	 * is used.
	 */
	GfxScreenUpscaledMode _upscaledHires;

	/**
	 * This here holds a translation for vertical+horizontal coordinates between native
	 * (visual) and actual (display) screen.
	 */
	int16 _upscaledHeightMapping[SCI_SCREEN_UPSCALEDMAXHEIGHT + 1];
	int16 _upscaledWidthMapping[SCI_SCREEN_UPSCALEDMAXWIDTH + 1];

	/**
	 * This defines whether or not the font we're drawing is already scaled
	 * to the screen size (and we therefore should not upscale it ourselves).
	 */
	bool _fontIsUpscaled;


	// pixel related code, in header so that it can be inlined for performance
public:
	void putPixel(int16 x, int16 y, byte drawMask, byte color, byte priority, byte control) {
		if (_upscaledHires == GFX_SCREEN_UPSCALED_480x300) {
			putPixel480x300(x, y, drawMask, color, priority, control);
			return;
		}

		// Set pixel for visual, priority and control map directly, those are not upscaled
		int offset = y * _width + x;

		if (drawMask & GFX_SCREEN_MASK_VISUAL) {
			_visualScreen[offset] = color;

			int displayOffset = 0;

			switch (_upscaledHires) {
			case GFX_SCREEN_UPSCALED_DISABLED:
				displayOffset = offset;
				_displayScreen[displayOffset] = color;
				break;

			case GFX_SCREEN_UPSCALED_640x400:
			case GFX_SCREEN_UPSCALED_640x440:
			case GFX_SCREEN_UPSCALED_640x480:
				putScaledPixelOnDisplay(x, y, color);
				break;
			default:
				break;
			}
		}
		if (drawMask & GFX_SCREEN_MASK_PRIORITY) {
			_priorityScreen[offset] = priority;
		}
		if (drawMask & GFX_SCREEN_MASK_CONTROL) {
			_controlScreen[offset] = control;
		}
	}

	void putPixel480x300(int16 x, int16 y, byte drawMask, byte color, byte priority, byte control) {
		int offset = ((y * 3) / 2 * _width) + ((x * 3) / 2);

		// All maps are upscaled
		// TODO: figure out, what Sierra exactly did on Mac for these games
		if (drawMask & GFX_SCREEN_MASK_VISUAL) {
			putPixel480x300Worker(x, y, offset, _visualScreen, color);
			putPixel480x300Worker(x, y, offset, _displayScreen, color);
		}
		if (drawMask & GFX_SCREEN_MASK_PRIORITY) {
			putPixel480x300Worker(x, y, offset, _priorityScreen, priority);
		}
		if (drawMask & GFX_SCREEN_MASK_CONTROL) {
			putPixel480x300Worker(x, y, offset, _controlScreen, control);
		}
	}
	void putPixel480x300Worker(int16 x, int16 y, int offset, byte *screen, byte byteToSet) {
		screen[offset] = byteToSet;
		if (x & 1)
			screen[offset + 1] = byteToSet;
		if (y & 1)
			screen[offset + _width] = byteToSet;
		if ((x & 1) && (y & 1))
			screen[offset + _width + 1] = byteToSet;
	}

	// This is called from vector drawing to put a pixel at a certain location
	void vectorPutPixel(int16 x, int16 y, byte drawMask, byte color, byte priority, byte control) {
		switch (_upscaledHires) {
		case GFX_SCREEN_UPSCALED_640x400:
		case GFX_SCREEN_UPSCALED_640x440:
		case GFX_SCREEN_UPSCALED_640x480:
			// For regular upscaled modes forward to the regular putPixel
			putPixel(x, y, drawMask, color, priority, control);
			return;
			break;

		default:
			break;
		}
		
		// For non-upscaled mode and 480x300 Mac put pixels directly
		int offset = y * _width + x;

		if (drawMask & GFX_SCREEN_MASK_VISUAL) {
			_visualScreen[offset] = color;
			_displayScreen[offset] = color;
		}
		if (drawMask & GFX_SCREEN_MASK_PRIORITY) {
			_priorityScreen[offset] = priority;
		}
		if (drawMask & GFX_SCREEN_MASK_CONTROL) {
			_controlScreen[offset] = control;
		}
	}

	/**
	 * This will just change a pixel directly on displayscreen. It is supposed to be
	 * only used on upscaled-Hires games where hires content needs to get drawn ONTO
	 * the upscaled display screen (like japanese fonts, hires portraits, etc.).
	 */
	void putPixelOnDisplay(int16 x, int16 y, byte color) {
		int offset = y * _displayWidth + x;
		_displayScreen[offset] = color;
	}

	// Upscales a pixel and puts it on display screen only
	void putScaledPixelOnDisplay(int16 x, int16 y, byte color) {
		int displayOffset = 0;

		switch (_upscaledHires) {
		case GFX_SCREEN_UPSCALED_640x400:
			displayOffset = (y * 2) * _displayWidth + x * 2; // straight 1 pixel -> 2 mapping

			_displayScreen[displayOffset] = color;
			_displayScreen[displayOffset + 1] = color;
			_displayScreen[displayOffset + _displayWidth] = color;
			_displayScreen[displayOffset + _displayWidth + 1] = color;
			break;

		case GFX_SCREEN_UPSCALED_640x440: {
			int16 startY = (y * 11) / 5;
			int16 endY = ((y + 1) * 11) / 5;
			displayOffset = (startY * _displayWidth) + x * 2;

			for (int16 curY = startY; curY < endY; curY++) {
				_displayScreen[displayOffset] = color;
				_displayScreen[displayOffset + 1] = color;
				displayOffset += _displayWidth;
			}
			break;
		}
		case GFX_SCREEN_UPSCALED_640x480: {
			int16 startY = (y * 12) / 5;
			int16 endY = ((y + 1) * 12) / 5;
			displayOffset = (startY * _displayWidth) + x * 2;

			for (int16 curY = startY; curY < endY; curY++) {
				_displayScreen[displayOffset] = color;
				_displayScreen[displayOffset + 1] = color;
				displayOffset += _displayWidth;
			}
			break;
		}
		default:
			break;
		}
	}

	/**
	 * This is used to put font pixels onto the screen - we adjust differently, so that we won't
	 *  do triple pixel lines in any case on upscaled hires. That way the font will not get distorted
	 *  Sierra SCI didn't do this
	 */
	void putFontPixel(int16 startingY, int16 x, int16 y, byte color) {
		int16 actualY = startingY + y;
		if (_fontIsUpscaled) {
			// Do not scale ourselves, but put it on the display directly
			putPixelOnDisplay(x, actualY, color);
		} else {
			int offset = actualY * _width + x;

			_visualScreen[offset] = color;
			switch (_upscaledHires) {
			case GFX_SCREEN_UPSCALED_DISABLED:
				_displayScreen[offset] = color;
				break;
			case GFX_SCREEN_UPSCALED_640x400:
			case GFX_SCREEN_UPSCALED_640x440:
			case GFX_SCREEN_UPSCALED_640x480: {
				// to 1-> 4 pixels upscaling for all of those, so that fonts won't look weird
				int displayOffset = (_upscaledHeightMapping[startingY] + y * 2) * _displayWidth + x * 2;
				_displayScreen[displayOffset] = color;
				_displayScreen[displayOffset + 1] = color;
				displayOffset += _displayWidth;
				_displayScreen[displayOffset] = color;
				_displayScreen[displayOffset + 1] = color;
				break;
			}
			default:
				putScaledPixelOnDisplay(x, actualY, color);
				break;
			}
		}
	}

	byte getPixel(byte *screen, int16 x, int16 y) {
		switch (_upscaledHires) {
		case GFX_SCREEN_UPSCALED_480x300: {
			int offset = ((y * 3) / 2) * _width + ((y * 3) / 2);

			return screen[offset];
			break;
		}
		default:
			break;
		}
		return screen[y * _width + x];
	}

	byte getVisual(int16 x, int16 y) {
		return getPixel(_visualScreen, x, y);
	}
	byte getPriority(int16 x, int16 y) {
		return getPixel(_priorityScreen, x, y);
	}
	byte getControl(int16 x, int16 y) {
		return getPixel(_controlScreen, x, y);
	}

	// Vector related public code - in here, so that it can be inlined
	byte vectorGetPixel(byte *screen, int16 x, int16 y) {
		return screen[y * _width + x];
	}

	byte vectorGetVisual(int16 x, int16 y) {
		return vectorGetPixel(_visualScreen, x, y);
	}
	byte vectorGetPriority(int16 x, int16 y) {
		return vectorGetPixel(_priorityScreen, x, y);
	}
	byte vectorGetControl(int16 x, int16 y) {
		return vectorGetPixel(_controlScreen, x, y);
	}

	void vectorAdjustCoordinate(int16 *x, int16 *y) {
		switch (_upscaledHires) {
		case GFX_SCREEN_UPSCALED_480x300:
			*x = (*x * 3) / 2;
			*y = (*y * 3) / 2;
			break;
		default:
			break;
		}
	}
};

} // End of namespace Sci

#endif