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