1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * Additional copyright for this file:
8  * Copyright (C) 1994-1998 Revolution Software Ltd.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  */
24 
25 #ifndef	SWORD2_SCREEN_H
26 #define	SWORD2_SCREEN_H
27 
28 #include "common/rect.h"
29 #include "common/stream.h"
30 
31 #define MAX_bgp0_sprites 6
32 #define MAX_bgp1_sprites 6
33 #define MAX_back_sprites 30
34 #define MAX_sort_sprites 30
35 #define MAX_fore_sprites 30
36 #define MAX_fgp0_sprites 6
37 #define MAX_fgp1_sprites 6
38 
39 #define PALTABLESIZE     (64 * 64 * 64)
40 
41 #define BLOCKWIDTH       64
42 #define BLOCKHEIGHT      64
43 #define MAXLAYERS        5
44 
45 #define MENUDEEP         40
46 #define RENDERWIDE       640
47 #define RENDERDEEP       (480 - (MENUDEEP * 2))
48 
49 // Maximum scaled size of a sprite
50 #define SCALE_MAXWIDTH   512
51 #define SCALE_MAXHEIGHT  512
52 
53 // Dirty grid cell size
54 #define CELLWIDE         10
55 #define CELLDEEP         20
56 
57 namespace Sword2 {
58 
59 class Sword2Engine;
60 
61 // Sprite defines
62 
63 enum {
64 	// This is the low byte part of the sprite type.
65 
66 	RDSPR_TRANS			= 0x0001,
67 	RDSPR_BLEND			= 0x0004,
68 	RDSPR_FLIP			= 0x0008,
69 	RDSPR_SHADOW			= 0x0010,
70 	RDSPR_DISPLAYALIGN		= 0x0020,
71 	RDSPR_NOCOMPRESSION		= 0x0040,
72 	RDSPR_EDGEBLEND			= 0x0080,	// Unused
73 
74 	// This is the high byte part of the sprite type, which defines what
75 	// type of compression is used. Unless RDSPR_NOCOMPRESSION is set.
76 
77 	RDSPR_RLE16			= 0x0000,
78 	RDSPR_RLE256			= 0x0100,
79 	RDSPR_RLE256FAST		= 0x0200
80 };
81 
82 // Fading defines
83 
84 enum {
85 	RDFADE_NONE,
86 	RDFADE_UP,
87 	RDFADE_DOWN,
88 	RDFADE_BLACK
89 };
90 
91 // Palette defines
92 
93 enum {
94 	RDPAL_FADE,
95 	RDPAL_INSTANT
96 };
97 
98 // Blitting FX defines
99 
100 enum {
101 	RDBLTFX_SPRITEBLEND		= 0x01,
102 	RDBLTFX_SHADOWBLEND		= 0x02,
103 	RDBLTFX_EDGEBLEND		= 0x04
104 };
105 
106 // Structure filled out by each object to register its graphic printing
107 // requrements
108 
109 struct BuildUnit {
110 	int16 x;
111 	int16 y;
112 	uint16 scaled_width;
113 	uint16 scaled_height;
114 	int16 sort_y;
115 	uint32 anim_resource;
116 	uint16 anim_pc;
117 
118 	// Denotes a scaling sprite at print time - and holds the scaling value
119 	// for the shrink routine
120 
121 	uint16 scale;
122 
123 	// Non-zero means this item is a layer - retrieve from background layer
124 	// and send to special renderer
125 
126 	uint16 layer_number;
127 
128 	// True means we want this frame to be affected by the shading mask
129 
130 	bool shadingFlag;
131 };
132 
133 struct ScreenInfo {
134 	uint16 scroll_offset_x;		// Position x
135 	uint16 scroll_offset_y;		// Position y
136 	uint16 max_scroll_offset_x;	// Calc'ed in fnInitBackground
137 	uint16 max_scroll_offset_y;
138 	int16 player_feet_x;		// Feet coordinates to use - cant just
139 	int16 player_feet_y;		// fetch the player compact anymore
140 	int16 feet_x;			// Special offset-to-player position -
141 	int16 feet_y;			// tweek as desired - always set in
142 					// screen manager object startup
143 	uint16 screen_wide;		// Size of background layer - hence
144 	uint16 screen_deep;		// size of back buffer itself (Paul
145 					// actually malloc's it)
146 	uint32 background_layer_id;	// Id of the normal background layer
147 					// from the header of the main
148 					// background layer
149 	uint16 number_of_layers;
150 	uint8 new_palette;		// Set to non zero to start the
151 					// palette held within layer file
152 					// fading up after a buildDisplay()
153 	uint8 scroll_flag;		// Scroll mode 0 off 1 on
154 	bool mask_flag;			// Using shading mask
155 };
156 
157 // The SpriteInfo structure is used to tell the driver96 code what attributes
158 // are linked to a sprite for drawing.  These include position, scaling and
159 // compression.
160 
161 struct SpriteInfo {
162 	int16 x;		// coords for top-left of sprite
163 	int16 y;
164 	uint16 w;		// dimensions of sprite (before scaling)
165 	uint16 h;
166 	uint16 scale;		// scale at which to draw, given in 256ths ['0' or '256' MEANS DON'T SCALE]
167 	uint16 scaledWidth;	// new dimensions (we calc these for the mouse area, so may as well pass to you to save time)
168 	uint16 scaledHeight;	//
169 	uint16 type;		// mask containing 'RDSPR_' bits specifying compression type, flip, transparency, etc
170 	uint16 blend;		// holds the blending values.
171 	byte *data;		// pointer to the sprite data
172 	byte *colorTable;	// pointer to 16-byte color table, only applicable to 16-col compression type
173 	bool isText;		// It is a engine-generated sprite containing text
174 };
175 
176 struct BlockSurface {
177 	byte data[BLOCKWIDTH * BLOCKHEIGHT];
178 	bool transparent;
179 };
180 
181 struct Parallax {
182 	uint16 w;
183 	uint16 h;
184 
185 	// The dimensions are followed by an offset table, but we don't know in
186 	// advance how big it is. See initializeBackgroundLayer().
187 
sizeParallax188 	static int size() {
189 		return 4;
190 	}
191 
192 	void read(byte *addr);
193 	void write(byte *addr);
194 };
195 
196 class Screen {
197 private:
198 	Sword2Engine *_vm;
199 
200 	// _thisScreen describes the current back buffer and its in-game scroll
201 	// positions, etc.
202 
203 	ScreenInfo _thisScreen;
204 
205 	int32 _renderCaps;
206 	int8 _renderLevel;
207 
208 	byte *_buffer;
209 	byte *_lightMask;
210 
211 	// Game screen metrics
212 	int16 _screenWide;
213 	int16 _screenDeep;
214 
215 	bool _needFullRedraw;
216 
217 	// Scroll variables.  _scrollX and _scrollY hold the current scroll
218 	// position, and _scrollXTarget and _scrollYTarget are the target
219 	// position for the end of the game cycle.
220 
221 	int16 _scrollX;
222 	int16 _scrollY;
223 
224 	int16 _scrollXTarget;
225 	int16 _scrollYTarget;
226 	int16 _scrollXOld;
227 	int16 _scrollYOld;
228 
229 	int16 _parallaxScrollX;	// current x offset to link a sprite to the
230 				// parallax layer
231 	int16 _parallaxScrollY;	// current y offset to link a sprite to the
232 				// parallax layer
233 	int16 _locationWide;
234 	int16 _locationDeep;
235 
236 	// Dirty grid handling
237 	byte *_dirtyGrid;
238 
239 	uint16 _gridWide;
240 	uint16 _gridDeep;
241 
242 	byte _palette[256 * 3];
243 	byte _paletteMatch[PALTABLESIZE];
244 
245 	uint8 _fadeStatus;
246 	int32 _fadeStartTime;
247 	int32 _fadeTotalTime;
248 
249 	// 'frames per second' counting stuff
250 	uint32 _fps;
251 	uint32 _cycleTime;
252 	uint32 _frameCount;
253 
254 	int32 _initialTime;
255 	int32 _startTime;
256 	int32 _totalTime;
257 	int32 _renderAverageTime;
258 	int32 _framesPerGameCycle;
259 	bool _renderTooSlow;
260 
261 	void startNewPalette();
262 
263 	void resetRenderEngine();
264 
265 	void startRenderCycle();
266 	bool endRenderCycle();
267 
268 	// Holds the order of the sort list, i.e. the list stays static and we
269 	// sort this array.
270 
271 	uint16 _sortOrder[MAX_sort_sprites];
272 
273 	BuildUnit _bgp0List[MAX_bgp0_sprites];
274 	BuildUnit _bgp1List[MAX_bgp1_sprites];
275 	BuildUnit _backList[MAX_back_sprites];
276 	BuildUnit _sortList[MAX_sort_sprites];
277 	BuildUnit _foreList[MAX_fore_sprites];
278 	BuildUnit _fgp0List[MAX_fgp0_sprites];
279 	BuildUnit _fgp1List[MAX_fgp1_sprites];
280 
281 	uint32 _curBgp0;
282 	uint32 _curBgp1;
283 	uint32 _curBack;
284 	uint32 _curSort;
285 	uint32 _curFore;
286 	uint32 _curFgp0;
287 	uint32 _curFgp1;
288 
289 	void drawBackPar0Frames();
290 	void drawBackPar1Frames();
291 	void drawBackFrames();
292 	void drawSortFrames(byte *file);
293 	void drawForeFrames();
294 	void drawForePar0Frames();
295 	void drawForePar1Frames();
296 
297 	void processLayer(byte *file, uint32 layer_number);
298 	void processImage(BuildUnit *build_unit);
299 
300 	uint8 _scrollFraction;
301 
302 	// Last palette used - so that we can restore the correct one after a
303 	// pause (which dims the screen) and it's not always the main screen
304 	// palette that we want, eg. during the eclipse
305 
306 	// This flag gets set in startNewPalette() and setFullPalette()
307 
308 	uint32 _lastPaletteRes;
309 
310 	// Debugging stuff
311 	uint32 _largestLayerArea;
312 	uint32 _largestSpriteArea;
313 	char _largestLayerInfo[128];
314 	char _largestSpriteInfo[128];
315 
316 	void registerFrame(byte *ob_mouse, byte *ob_graph, byte *ob_mega, BuildUnit *build_unit);
317 
318 	void mirrorSprite(byte *dst, byte *src, int16 w, int16 h);
319 	int32 decompressRLE256(byte *dst, byte *src, int32 decompSize);
320 	void unwindRaw16(byte *dst, byte *src, uint16 blockSize, byte *colTable);
321 	int32 decompressRLE16(byte *dst, byte *src, int32 decompSize, byte *colTable);
322 	void renderParallax(byte *ptr, int16 layer);
323 
324 
325 	void markAsDirty(int16 x0, int16 y0, int16 x1, int16 y1);
326 
327 	uint8 _xBlocks[MAXLAYERS];
328 	uint8 _yBlocks[MAXLAYERS];
329 
330 	// This is used to cache PSX backgrounds and parallaxes
331 	// data, as they are kept in a file unmanageable from
332 	// resource manager. These gets freed everytime an user
333 	// exits from a room.
334 	byte *_psxScrCache[3];
335 	bool _psxCacheEnabled[3];
336 
337 	// An array of sub-blocks, one for each of the parallax layers.
338 
339 	BlockSurface **_blockSurfaces[MAXLAYERS];
340 
341 	uint16 _xScale[SCALE_MAXWIDTH];
342 	uint16 _yScale[SCALE_MAXHEIGHT];
343 
344 	void blitBlockSurface(BlockSurface *s, Common::Rect *r, Common::Rect *clipRect);
345 
346 	uint16 _layer;
347 
348 	bool _dimPalette;
349 
350 	uint32 _pauseTicks;
351 	uint32 _pauseStartTick;
352 
353 	uint32 getTick();
354 
355 public:
356 	Screen(Sword2Engine *vm, int16 width, int16 height);
357 	~Screen();
358 
359 	void pauseScreen(bool pause);
360 
361 	int8 getRenderLevel();
362 	void setRenderLevel(int8 level);
363 
getScreen()364 	byte *getScreen() { return _buffer; }
getPalette()365 	byte *getPalette() { return _palette; }
getScreenInfo()366 	ScreenInfo *getScreenInfo() { return &_thisScreen; }
367 
getScreenWide()368 	int16 getScreenWide() { return _screenWide; }
getScreenDeep()369 	int16 getScreenDeep() { return _screenDeep; }
370 
getCurBgp0()371 	uint32 getCurBgp0() { return _curBgp0; }
getCurBgp1()372 	uint32 getCurBgp1() { return _curBgp1; }
getCurBack()373 	uint32 getCurBack() { return _curBack; }
getCurSort()374 	uint32 getCurSort() { return _curSort; }
getCurFore()375 	uint32 getCurFore() { return _curFore; }
getCurFgp0()376 	uint32 getCurFgp0() { return _curFgp0; }
getCurFgp1()377 	uint32 getCurFgp1() { return _curFgp1; }
378 
getFps()379 	uint32 getFps() { return _fps; }
380 
getLargestLayerArea()381 	uint32 getLargestLayerArea() { return _largestLayerArea; }
getLargestSpriteArea()382 	uint32 getLargestSpriteArea() { return _largestSpriteArea; }
getLargestLayerInfo()383 	char *getLargestLayerInfo() { return _largestLayerInfo; }
getLargestSpriteInfo()384 	char *getLargestSpriteInfo() { return _largestSpriteInfo; }
385 
386 	void setNeedFullRedraw();
387 
388 	void clearScene();
389 
390 	void resetRenderLists();
391 
392 	void setLocationMetrics(uint16 w, uint16 h);
393 	int32 initializeBackgroundLayer(byte *parallax);
394 	int32 initializePsxParallaxLayer(byte *parallax);   // These are used to initialize psx backgrounds and
395 	int32 initializePsxBackgroundLayer(byte *parallax); // parallaxes, which are different from pc counterparts.
396 	void closeBackgroundLayer();
397 
398 	void initializeRenderCycle();
399 
400 	void initBackground(int32 res, int32 new_palette);
401 	void initPsxBackground(int32 res, int32 new_palette);
402 	void registerFrame(byte *ob_mouse, byte *ob_graph, byte *ob_mega);
403 
setScrollFraction(uint8 f)404 	void setScrollFraction(uint8 f) { _scrollFraction = f; }
405 	void setScrollTarget(int16 x, int16 y);
406 	void setScrolling();
407 
408 	void setFullPalette(int32 palRes);
409 	void setPalette(int16 startEntry, int16 noEntries, byte *palette, uint8 setNow);
410 	void setSystemPalette(const byte *colors, uint start, uint num);
411 	uint8 quickMatch(uint8 r, uint8 g, uint8 b);
412 	int32 fadeUp(float time = 0.75f);
413 	int32 fadeDown(float time = 0.75f);
414 	uint8 getFadeStatus();
415 	void dimPalette(bool dim);
416 	void waitForFade();
417 	void fadeServer();
418 
419 	void updateDisplay(bool redrawScene = true);
420 
421 	void displayMsg(byte *text, int time);
422 
423 	int32 createSurface(SpriteInfo *s, byte **surface);
424 	void drawSurface(SpriteInfo *s, byte *surface, Common::Rect *clipRect = NULL);
425 	void deleteSurface(byte *surface);
426 	int32 drawSprite(SpriteInfo *s);
427 
428 	void scaleImageFast(byte *dst, uint16 dstPitch, uint16 dstWidth,
429 		uint16 dstHeight, byte *src, uint16 srcPitch, uint16 srcWidth,
430 		uint16 srcHeight);
431 	void scaleImageGood(byte *dst, uint16 dstPitch, uint16 dstWidth,
432 		uint16 dstHeight, byte *src, uint16 srcPitch, uint16 srcWidth,
433 		uint16 srcHeight, byte *backBuf, int16 bbXPos, int16 bbYPos);
434 
435 	void updateRect(Common::Rect *r);
436 
437 	int32 openLightMask(SpriteInfo *s);
438 	int32 closeLightMask();
439 
440 	void buildDisplay();
441 
442 	void plotPoint(int x, int y, uint8 color);
443 	void drawLine(int x0, int y0, int x1, int y1, uint8 color);
444 
445 	void rollCredits();
446 	void splashScreen();
447 
448 	// Some sprites are compressed in HIF format
449 	static uint32 decompressHIF(byte *src, byte *dst, uint32 *skipData = NULL);
450 	// This is used to resize psx sprites back to original resolution
451 	static void resizePsxSprite(byte *dst, byte *src, uint16 destW, uint16 destH);
452 	// Some sprites are divided into 254 pixel wide stripes, this recomposes them
453 	// and generates a "normal" sprite.
454 	static void recomposePsxSprite(SpriteInfo *s);
455 	static void recomposeCompPsxSprite(SpriteInfo *s);
456 
457 	// These functions manage the PSX screen cache
458 	void setPsxScrCache(byte *psxScrCache, uint8 level);
459 	byte *getPsxScrCache(uint8 level);
460 	bool getPsxScrCacheStatus(uint8 level);
461 	void flushPsxScrCache();
462 
463 };
464 
465 } // End of namespace Sword2
466 
467 #endif
468