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  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef SWORD1_SCREEN_H
24 #define SWORD1_SCREEN_H
25 
26 #include "sword1/sworddefs.h"
27 
28 class OSystem;
29 
30 namespace Sword1 {
31 
32 #define MAX_FORE 20
33 #define MAX_BACK 20
34 #define MAX_SORT 20
35 
36 struct SortSpr {
37 	int32 id, y;
38 };
39 
40 struct RoomDef {
41 	int     totalLayers;
42 	int     sizeX;
43 	int     sizeY;
44 	int     gridWidth;  //number of 16*16 grid blocks across - including off screen edges.
45 	uint32  layers[4];
46 	uint32  grids[3];
47 	uint32  palettes[2];
48 	uint32  parallax[2];
49 };
50 
51 struct PSXDataCache { // Cache for PSX screen, to avoid decompressing background at every screen update
52 	uint8 *decodedBackground;
53 	uint8 *extPlxCache; // If this screen requires an external parallax, save it here
54 };
55 
56 #define SCRNGRID_X 16
57 #define SCRNGRID_Y 8
58 #define SHRINK_BUFFER_SIZE 50000
59 #define RLE_BUFFER_SIZE 50000
60 
61 #define FLASH_RED 0
62 #define FLASH_BLUE 1
63 #define BORDER_YELLOW 2
64 #define BORDER_GREEN 3
65 #define BORDER_PURPLE 4
66 #define BORDER_BLACK 5
67 
68 class ResMan;
69 class ObjectMan;
70 class Text; // Text objects use sprites that are created internally at run-time
71             // the buffer belongs to Text, so we need a reference here.
72 
73 class Screen {
74 public:
75 	Screen(OSystem *system, ResMan *pResMan, ObjectMan *pObjMan);
76 	~Screen();
77 	void clearScreen();
78 	void useTextManager(Text *pTextMan);
79 	void draw();
80 
81 	void quitScreen();
82 	void newScreen(uint32 screen);
83 
84 	void setScrolling(int16 offsetX, int16 offsetY);
85 	void addToGraphicList(uint8 listId, uint32 objId);
86 
87 	void fadeDownPalette();
88 	void fadeUpPalette();
89 	void fnSetPalette(uint8 start, uint16 length, uint32 id, bool fadeUp);
90 	bool stillFading();
91 	void fullRefresh();
92 
93 	bool showScrollFrame();
94 	void updateScreen();
95 	void showFrame(uint16 x, uint16 y, uint32 resId, uint32 frameNo, const byte *fadeMask = NULL, int8 fadeStatus = 0);
96 
97 	void fnSetParallax(uint32 screen, uint32 resId);
98 	void fnFlash(uint8 color);
99 	void fnBorder(uint8 color);
100 
101 	static void decompressHIF(uint8 *src, uint8 *dest);
102 
103 private:
104 	// for router debugging
105 	void drawLine(uint16 x1, uint16 y1, uint16 x2, uint16 y2);
106 	void vline(uint16 x, uint16 y1, uint16 y2);
107 	void hline(uint16 x1, uint16 x2, uint16 y);
108 	void bsubline_1(uint16 x1, uint16 y1, uint16 x2, uint16 y2);
109 	void bsubline_2(uint16 x1, uint16 y1, uint16 x2, uint16 y2);
110 	void bsubline_3(uint16 x1, uint16 y1, uint16 x2, uint16 y2);
111 	void bsubline_4(uint16 x1, uint16 y1, uint16 x2, uint16 y2);
112 
113 	void verticalMask(uint16 x, uint16 y, uint16 bWidth, uint16 bHeight);
114 	void blitBlockClear(uint16 x, uint16 y, uint8 *data);
115 	void renderParallax(uint8 *data);
116 	void processImage(uint32 id);
117 	void spriteClipAndSet(uint16 *pSprX, uint16 *pSprY, uint16 *sprWidth, uint16 *sprHeight, uint16 *incr);
118 	void drawSprite(uint8 *sprData, uint16 sprX, uint16 sprY, uint16 sprWidth, uint16 sprHeight, uint16 sprPitch);
119 	void drawPsxHalfShrinkedSprite(uint8 *sprData, uint16 sprX, uint16 sprY, uint16 sprWidth, uint16 sprHeight, uint16 sprPitch);
120 	void drawPsxFullShrinkedSprite(uint8 *sprData, uint16 sprX, uint16 sprY, uint16 sprWidth, uint16 sprHeight, uint16 sprPitch);
121 	uint8 *psxBackgroundToIndexed(uint8 *psxBackground, uint32 bakXres, uint32 bakYres);
122 	uint8 *psxShrinkedBackgroundToIndexed(uint8 *psxBackground, uint32 bakXres, uint32 bakYres);
123 	void fetchPsxParallaxSize(uint8 *psxParallax, uint16 *paraSizeX, uint16 *paraSizeY);
124 	void drawPsxParallax(uint8 *psxParallax, uint16 paraScrlX, uint16 scrnScrlX, uint16 scrnWidth);
125 	void decompressRLE7(uint8 *src, uint32 compSize, uint8 *dest);
126 	void decompressRLE0(uint8 *src, uint32 compSize, uint8 *dest);
127 	void decompressTony(uint8 *src, uint32 compSize, uint8 *dest);
128 	void fastShrink(uint8 *src, uint32 width, uint32 height, uint32 scale, uint8 *dest);
129 	void fadePalette();
130 
131 	void flushPsxCache();
132 
133 	OSystem *_system;
134 	ResMan *_resMan;
135 	ObjectMan *_objMan;
136 	Text *_textMan;
137 
138 	uint16 _currentScreen;
139 	uint8  *_screenBuf;
140 	uint8  *_screenGrid;
141 	uint16 *_layerGrid[4];
142 	uint8  *_layerBlocks[4];
143 	uint8  *_parallax[2];
144 	uint8  _rleBuffer[RLE_BUFFER_SIZE];
145 	uint8  _shrinkBuffer[SHRINK_BUFFER_SIZE];
146 	bool   _fullRefresh;
147 	bool   _updatePalette;
148 	uint16 _oldScrollX, _oldScrollY; // for drawing additional frames
149 
150 	PSXDataCache _psxCache; // Cache used for PSX backgrounds
151 
152 	uint32  _foreList[MAX_FORE];
153 	uint32  _backList[MAX_BACK];
154 	SortSpr _sortList[MAX_SORT];
155 	uint8   _foreLength, _backLength, _sortLength;
156 	uint16  _scrnSizeX, _scrnSizeY, _gridSizeX, _gridSizeY;
157 
158 	static RoomDef _roomDefTable[TOTAL_ROOMS]; // from ROOMS.C (not const, see fnSetParallax)
159 
160 	uint8 _targetPalette[256 * 3];
161 	uint8 _currentPalette[256 * 3]; // for fading
162 	uint8 _fadingStep;
163 	int8  _fadingDirection; // 1 for fade up, -1 for fade down
164 	bool _isBlack; // if the logic already faded down the palette, this is set to show the
165 	               // mainloop that no further fading is necessary.
166 };
167 
168 } // End of namespace Sword1
169 
170 #endif //BSSCREEN_H
171