ScummVM API documentation
graphics.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  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef PARALLACTION_GRAPHICS_H
23 #define PARALLACTION_GRAPHICS_H
24 
25 #include "common/list.h"
26 #include "common/rect.h"
27 #include "common/hashmap.h"
28 #include "common/hash-str.h"
29 #include "common/str.h"
30 #include "common/stream.h"
31 #include "common/array.h"
32 
33 #include "graphics/surface.h"
34 
35 
36 namespace Parallaction {
37 
38 
39 #include "common/pack-start.h" // START STRUCT PACKING
40 
42 
43  uint16 _timer;
44  uint16 _step;
45  uint16 _flags;
46  byte _first;
47  byte _last;
48 
49 } PACKED_STRUCT;
50 
51 #include "common/pack-end.h" // END STRUCT PACKING
52 
53 class Font {
54 
55 protected:
56  byte _color;
57 
58 
59 public:
60  Font() : _color(0) {}
61  virtual ~Font() {}
62 
63  virtual void setColor(byte color) {
64  _color = color;
65  }
66  virtual uint32 getStringWidth(const char *s) = 0;
67  virtual uint16 height() = 0;
68 
69  virtual void drawString(Graphics::Surface *dst, int x, int y, const char *s) = 0;
70 };
71 
72 
73 struct Frames {
74 
75  virtual uint16 getNum() = 0;
76  virtual byte* getData(uint16 index) = 0;
77  virtual void getRect(uint16 index, Common::Rect &r) = 0;
78  virtual uint getRawSize(uint16 index) = 0;
79  virtual uint getSize(uint16 index) = 0;
80 
81  virtual ~Frames() { }
82 
83 };
84 
85 
86 struct SurfaceToFrames : public Frames {
87 
88  Graphics::Surface *_surf;
89 
90 public:
91  SurfaceToFrames(Graphics::Surface *surf) : _surf(surf) {
92  }
93 
94  ~SurfaceToFrames() override {
95  _surf->free();
96  delete _surf;
97  }
98 
99  uint16 getNum() override {
100  return 1;
101  }
102  byte* getData(uint16 index) override {
103  assert(index == 0);
104  return (byte *)_surf->getBasePtr(0,0);
105  }
106  void getRect(uint16 index, Common::Rect &r) override {
107  assert(index == 0);
108  r.left = 0;
109  r.top = 0;
110  r.setWidth(_surf->w);
111  r.setHeight(_surf->h);
112  }
113  uint getRawSize(uint16 index) override {
114  assert(index == 0);
115  return getSize(index);
116  }
117  uint getSize(uint16 index) override {
118  assert(index == 0);
119  return _surf->w * _surf->h;
120  }
121 
122 };
123 
124 struct Cnv : public Frames {
125  uint16 _count; // # of frames
126  uint16 _width; //
127  uint16 _height; //
128  byte** field_8; // unused
129  byte* _data;
130  bool _freeData;
131  Graphics::Surface *_surf;
132 
133 public:
134  Cnv() {
135  _width = _height = _count = 0;
136  _data = NULL;
137  _surf = NULL;
138  _freeData = false;
139  field_8 = 0;
140  }
141 
142  Cnv(uint16 numFrames, uint16 width, uint16 height, byte *data, bool freeData = false)
143  : _count(numFrames), _width(width), _height(height), _data(data), _freeData(freeData), field_8(0), _surf(NULL) {
144  }
145 
146  Cnv(uint16 numFrames, uint16 width, uint16 height, Graphics::Surface *surf)
147  : _count(numFrames), _width(width), _height(height), _data(NULL), _freeData(true), field_8(0), _surf(surf) {
148  _data = (byte *)_surf->getBasePtr(0, 0);
149  }
150 
151  ~Cnv() override {
152  if (_freeData) {
153  if (_surf) {
154  _surf->free();
155  delete _surf;
156  } else {
157  delete[] _data;
158  }
159  }
160  }
161 
162  byte* getFramePtr(uint16 index) {
163  if (index >= _count)
164  return NULL;
165  return &_data[index * _width * _height];
166  }
167 
168  uint16 getNum() override {
169  return _count;
170  }
171 
172  byte *getData(uint16 index) override {
173  return getFramePtr(index);
174  }
175 
176  void getRect(uint16 index, Common::Rect &r) override {
177  r.left = 0;
178  r.top = 0;
179  r.setWidth(_width);
180  r.setHeight(_height);
181  }
182  uint getRawSize(uint16 index) override {
183  assert(index < _count);
184  return getSize(index);
185  }
186  uint getSize(uint16 index) override {
187  assert(index < _count);
188  return _width * _height;
189  }
190 
191 };
192 
193 
194 struct MaskBuffer {
195  // handles a 2-bit depth buffer used for z-buffering
196 
197  uint16 w;
198  uint16 internalWidth;
199  uint16 h;
200  uint size;
201  byte *data;
202  bool bigEndian;
203 
204  byte* getPtr(uint16 x, uint16 y) const;
205  void bltOr(uint16 dx, uint16 dy, const MaskBuffer &src, uint16 sx, uint16 sy, uint width, uint height);
206  void bltCopy(uint16 dx, uint16 dy, const MaskBuffer &src, uint16 sx, uint16 sy, uint width, uint height);
207 
208 public:
209  MaskBuffer();
210  ~MaskBuffer();
211 
212  void clone(const MaskBuffer &buf);
213  void create(uint16 width, uint16 height);
214  void free();
215 
216  byte getValue(uint16 x, uint16 y) const;
217 };
218 
219 
220 struct PathBuffer {
221  // handles a 1-bit depth buffer used for masking non-walkable areas
222 
223  uint16 w;
224  uint16 internalWidth;
225  uint16 h;
226  uint size;
227  byte *data;
228  bool bigEndian;
229 
230  byte* getPtr(uint16 x, uint16 y) const;
231  void bltCopy(uint16 dx, uint16 dy, const PathBuffer &src, uint16 sx, uint16 sy, uint width, uint height);
232 
233 public:
234  PathBuffer();
235  ~PathBuffer();
236 
237  void clone(const PathBuffer &buf);
238  void create(uint16 width, uint16 height);
239  void free();
240  byte getValue(uint16 x, uint16 y) const;
241 };
242 
243 
244 class Palette {
245 
246  byte _data[768];
247  uint _colors;
248  uint _size;
249  bool _hb;
250 
251 public:
252  Palette();
253  Palette(const Palette &pal);
254 
255  void clone(const Palette &pal);
256 
257  void makeBlack();
258  void setEntries(byte* data, uint first, uint num);
259  void getEntry(uint index, int &red, int &green, int &blue);
260  void setEntry(uint index, int red, int green, int blue);
261  void makeGrayscale();
262  void fadeTo(const Palette& target, uint step);
263  uint fillRGB(byte *rgb);
264 
265  void rotate(uint first, uint last, bool forward);
266 };
267 
268 
269 #define CENTER_LABEL_HORIZONTAL -1
270 #define CENTER_LABEL_VERTICAL -1
271 
272 
273 
274 #define MAX_BALLOON_WIDTH 130
275 
276 class Parallaction;
277 
278 struct DoorData;
279 struct GetData;
280 struct Label;
281 class Disk;
282 
283 enum {
284  kGfxObjVisible = 1,
285 
286  kGfxObjTypeDoor = 0,
287  kGfxObjTypeGet = 1,
288  kGfxObjTypeAnim = 2,
289  kGfxObjTypeLabel = 3,
290  kGfxObjTypeBalloon = 4,
291  kGfxObjTypeCharacter = 8,
292  kGfxObjTypeMenu = 16
293 };
294 
295 enum {
296  kGfxObjDoorZ = -200,
297  kGfxObjGetZ = -100
298 };
299 
300 class GfxObj {
301  Common::String _name;
302  Frames *_frames;
303 
304 public:
305  int16 x, y;
306 
307  int32 z;
308  uint32 _prog; // this value is used when sorting, in case that comparing z is not enough to tell which object goes on front
309 
310  uint32 _flags;
311 
312  uint type;
313  uint frame;
314  uint layer;
315  uint transparentKey;
316  uint scale;
317 
318  int _maskId;
319  bool _hasMask;
320  int _pathId;
321  bool _hasPath;
322 
323  Common::String _text;
324 
325 
326  GfxObj(uint type, Frames *frames, const char *name = NULL);
327  virtual ~GfxObj();
328 
329  const char *getName() const;
330 
331  uint getNum();
332  void getRect(uint frame, Common::Rect &r);
333  byte *getData(uint frame);
334  uint getRawSize(uint frame);
335  uint getSize(uint frame);
336 
337 
338  void setFlags(uint32 flags);
339  void clearFlags(uint32 flags);
340  bool isVisible() {
341  return (_flags & kGfxObjVisible) == kGfxObjVisible;
342  }
343 
344  void release();
345 };
346 
347 #define LAYER_FOREGROUND 3
348 
349 /*
350  BackgroundInfo keeps information about the background bitmap that can be seen in the game.
351  These bitmaps can be of any size, smaller or larger than the visible screen, the latter
352  being the most common options.
353 */
355 protected:
357  MaskPatches _maskPatches;
358  MaskBuffer _maskBackup;
359  void clearMaskData();
360 
362  PathPatches _pathPatches;
363  PathBuffer _pathBackup;
364  void clearPathData();
365 
366 public:
367  int _x, _y; // used to display bitmaps smaller than the screen
368  int width;
369  int height;
370 
372  MaskBuffer *_mask;
373  PathBuffer *_path;
374 
375  Palette palette;
376 
377  int layers[4];
378  PaletteFxRange ranges[6];
379 
380 
381  BackgroundInfo();
382  ~BackgroundInfo();
383 
384  void setPaletteRange(int index, const PaletteFxRange& range);
385 
386  // mask management
387  bool hasMask();
388  uint addMaskPatch(MaskBuffer *patch);
389  void toggleMaskPatch(uint id, int x, int y, bool apply);
390  uint16 getMaskLayer(uint16 z) const;
391  void finalizeMask();
392  void loadGfxObjMask(Parallaction *vm, const char *name, GfxObj *obj);
393 
394  // path management
395  bool hasPath();
396  uint addPathPatch(PathBuffer *patch);
397  void togglePathPatch(uint id, int x, int y, bool apply);
398  void finalizePath();
399  void loadGfxObjPath(Parallaction *vm, const char *name, GfxObj *obj);
400 };
401 
402 
403 
404 
405 enum {
406  kBackgroundLocation = 1,
407  kBackgroundSlide = 2
408 };
409 
410 
412 public:
413  enum TextColor {
414  kSelectedColor = 0,
415  kUnselectedColor = 1,
416  kNormalColor = 2
417  };
418 
419  virtual ~BalloonManager() { }
420 
421  virtual void reset() = 0;
422  virtual int setLocationBalloon(const Common::String &text, bool endGame) = 0;
423  virtual int setDialogueBalloon(const Common::String &text, uint16 winding, TextColor textColor) = 0;
424  virtual int setSingleBalloon(const Common::String &text, uint16 x, uint16 y, uint16 winding, TextColor textColor) = 0;
425  virtual void setBalloonText(uint id, const Common::String &text, TextColor textColor) = 0;
426  virtual int hitTestDialogueBalloon(int x, int y) = 0;
427 };
428 
429 
431 #define SCENE_DRAWLIST_SIZE 100
432 
433 class Gfx {
434 
435 protected:
436  Parallaction* _vm;
437  void resetSceneDrawList();
438 
439 public:
440  Disk *_disk;
441 
442  void beginFrame();
443  void addObjectToScene(GfxObj *obj);
444  GfxObjArray _sceneObjects;
445  GfxObj* loadAnim(const char *name);
446  GfxObj* loadGet(const char *name);
447  GfxObj* loadDoor(const char *name);
448  GfxObj* loadCharacterAnim(const char *name);
449  void sortScene();
450  void freeCharacterObjects();
451  void freeLocationObjects();
452  void showGfxObj(GfxObj* obj, bool visible);
453  void blt(const Common::Rect& r, byte *data, Graphics::Surface *surf, uint16 z, uint scale, byte transparentColor);
454  void unpackBlt(const Common::Rect& r, byte *data, uint size, Graphics::Surface *surf, uint16 z, uint scale, byte transparentColor);
455 
456  // labels
457  void showFloatingLabel(GfxObj *label);
458  void hideFloatingLabel();
459 
460  GfxObj *renderFloatingLabel(Font *font, char *text);
461  GfxObj *createLabel(Font *font, const char *text, byte color);
462  void showLabel(GfxObj *label, int16 x, int16 y, bool queueTTS = true, bool voiceText = true);
463  void hideLabel(GfxObj *label);
464  void freeLabels();
465  void unregisterLabel(GfxObj *label);
466 
467  // dialogue handling
468  GfxObj* registerBalloon(Frames *frames, const char *text);
469  int setItem(GfxObj* obj, uint16 x, uint16 y, byte transparentColor = 0);
470  void setItemFrame(uint item, uint16 f);
471  void freeDialogueObjects();
472 
473  // background surface
474  BackgroundInfo *_backgroundInfo;
475  void setBackground(uint type, BackgroundInfo *info);
476  void patchBackground(Graphics::Surface &surf, int16 x, int16 y, bool mask = false);
477  void grabBackground(const Common::Rect& r, Graphics::Surface &dst);
478  void fillBackground(const Common::Rect& r, byte color);
479  void invertBackground(const Common::Rect& r);
480 
481  // palette
482  void setPalette(Palette &palette);
483  void setBlackPalette();
484  void animatePalette();
485 
486  // amiga specific
487  void applyHalfbriteEffect_NS(Graphics::Surface &surf);
488  void setHalfbriteMode(bool enable);
489  void setProjectorPos(int x, int y);
490  void setProjectorProgram(int16 *data);
491  int16 *_nextProjectorPos;
492 
493  // start programmatic relative scroll
494  void initiateScroll(int deltaX, int deltaY);
495  // immediate and absolute x,y scroll
496  void setScrollPosX(int scrollX);
497  void setScrollPosY(int scrollY);
498  // return current scroll position
499  void getScrollPos(Common::Point &p);
500 
501  // init
502  Gfx(Parallaction* vm);
503  virtual ~Gfx();
504 
505  void clearScreen();
506  void updateScreen();
507 
508 public:
509  Palette _palette;
510 
511  byte *_unpackedBitmap;
512 
513 protected:
514  bool _halfbrite;
515 
516  Common::Point _hbCirclePos;
517  int _hbCircleRadius;
518 
519  // BRA specific
520  Palette _backupPal;
521 
522 
523  Graphics::Surface *lockScreen();
524  void unlockScreen();
525  void updateScreenIntern();
526 
527  bool _doubleBuffering;
528  int _gameType;
529  Graphics::Surface _backBuffer;
530  void copyRectToScreen(const byte *buf, int pitch, int x, int y, int w, int h);
531 
532  int _scrollPosX, _scrollPosY;
533  int _minScrollX, _maxScrollX, _minScrollY, _maxScrollY;
534 
535  uint32 _requestedHScrollSteps;
536  uint32 _requestedVScrollSteps;
537  int32 _requestedHScrollDir;
538  int32 _requestedVScrollDir;
539  void scroll();
540  #define NO_FLOATING_LABEL 1000
541 
542  struct Label {
543  Common::String _text;
544  int _x, _y;
545  int color;
546  bool _floating;
547  };
548 
549  GfxObjArray _labels;
550  GfxObjArray _balloons;
551  GfxObjArray _items;
552 
553  GfxObj *_floatingLabel;
554 
555  // overlay mode enables drawing of graphics with automatic screen-to-game coordinate translation
556  bool _overlayMode;
557  void drawOverlay(Graphics::Surface &surf);
558  void drawInventory();
559 
560  void drawList(Graphics::Surface &surface, GfxObjArray &list);
561  void updateFloatingLabel();
562  void copyRect(const Common::Rect &r, Graphics::Surface &src, Graphics::Surface &dst);
563  void drawText(Font *font, Graphics::Surface* surf, uint16 x, uint16 y, const char *text, byte color);
564  void drawGfxObject(GfxObj *obj, Graphics::Surface &surf);
565  void bltMaskScale(const Common::Rect& r, byte *data, Graphics::Surface *surf, uint16 z, uint scale, byte transparentColor);
566  void bltMaskNoScale(const Common::Rect& r, byte *data, Graphics::Surface *surf, uint16 z, byte transparentColor);
567  void bltNoMaskNoScale(const Common::Rect& r, byte *data, Graphics::Surface *surf, byte transparentColor);
568 };
569 
570 
571 } // Parallaction
572 
573 
574 #endif
Definition: graphics.h:244
Definition: str.h:59
Definition: surface.h:67
T left
Definition: rect.h:170
int16 h
Definition: surface.h:76
Definition: graphics.h:433
Definition: rect.h:524
void setHeight(T aHeight)
Definition: rect.h:224
void setWidth(T aWidth)
Definition: rect.h:220
Definition: disk.h:56
Definition: graphics.h:194
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Definition: debug.h:8
const void * getBasePtr(int x, int y) const
Definition: surface.h:138
Definition: graphics.h:41
Definition: graphics.h:86
Definition: rect.h:144
Definition: graphics.h:354
Definition: graphics.h:220
Definition: graphics.h:124
int16 w
Definition: surface.h:71
Definition: graphics.h:53
Definition: graphics.h:542
Definition: graphics.h:411
Definition: graphics.h:300
Definition: graphics.h:73
Definition: label.h:25