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 
324  GfxObj(uint type, Frames *frames, const char *name = NULL);
325  virtual ~GfxObj();
326 
327  const char *getName() const;
328 
329  uint getNum();
330  void getRect(uint frame, Common::Rect &r);
331  byte *getData(uint frame);
332  uint getRawSize(uint frame);
333  uint getSize(uint frame);
334 
335 
336  void setFlags(uint32 flags);
337  void clearFlags(uint32 flags);
338  bool isVisible() {
339  return (_flags & kGfxObjVisible) == kGfxObjVisible;
340  }
341 
342  void release();
343 };
344 
345 #define LAYER_FOREGROUND 3
346 
347 /*
348  BackgroundInfo keeps information about the background bitmap that can be seen in the game.
349  These bitmaps can be of any size, smaller or larger than the visible screen, the latter
350  being the most common options.
351 */
353 protected:
355  MaskPatches _maskPatches;
356  MaskBuffer _maskBackup;
357  void clearMaskData();
358 
360  PathPatches _pathPatches;
361  PathBuffer _pathBackup;
362  void clearPathData();
363 
364 public:
365  int _x, _y; // used to display bitmaps smaller than the screen
366  int width;
367  int height;
368 
370  MaskBuffer *_mask;
371  PathBuffer *_path;
372 
373  Palette palette;
374 
375  int layers[4];
376  PaletteFxRange ranges[6];
377 
378 
379  BackgroundInfo();
380  ~BackgroundInfo();
381 
382  void setPaletteRange(int index, const PaletteFxRange& range);
383 
384  // mask management
385  bool hasMask();
386  uint addMaskPatch(MaskBuffer *patch);
387  void toggleMaskPatch(uint id, int x, int y, bool apply);
388  uint16 getMaskLayer(uint16 z) const;
389  void finalizeMask();
390  void loadGfxObjMask(Parallaction *vm, const char *name, GfxObj *obj);
391 
392  // path management
393  bool hasPath();
394  uint addPathPatch(PathBuffer *patch);
395  void togglePathPatch(uint id, int x, int y, bool apply);
396  void finalizePath();
397  void loadGfxObjPath(Parallaction *vm, const char *name, GfxObj *obj);
398 };
399 
400 
401 
402 
403 enum {
404  kBackgroundLocation = 1,
405  kBackgroundSlide = 2
406 };
407 
408 
410 public:
411  enum TextColor {
412  kSelectedColor = 0,
413  kUnselectedColor = 1,
414  kNormalColor = 2
415  };
416 
417  virtual ~BalloonManager() { }
418 
419  virtual void reset() = 0;
420  virtual int setLocationBalloon(const Common::String &text, bool endGame) = 0;
421  virtual int setDialogueBalloon(const Common::String &text, uint16 winding, TextColor textColor) = 0;
422  virtual int setSingleBalloon(const Common::String &text, uint16 x, uint16 y, uint16 winding, TextColor textColor) = 0;
423  virtual void setBalloonText(uint id, const Common::String &text, TextColor textColor) = 0;
424  virtual int hitTestDialogueBalloon(int x, int y) = 0;
425 };
426 
427 
429 #define SCENE_DRAWLIST_SIZE 100
430 
431 class Gfx {
432 
433 protected:
434  Parallaction* _vm;
435  void resetSceneDrawList();
436 
437 public:
438  Disk *_disk;
439 
440  void beginFrame();
441  void addObjectToScene(GfxObj *obj);
442  GfxObjArray _sceneObjects;
443  GfxObj* loadAnim(const char *name);
444  GfxObj* loadGet(const char *name);
445  GfxObj* loadDoor(const char *name);
446  GfxObj* loadCharacterAnim(const char *name);
447  void sortScene();
448  void freeCharacterObjects();
449  void freeLocationObjects();
450  void showGfxObj(GfxObj* obj, bool visible);
451  void blt(const Common::Rect& r, byte *data, Graphics::Surface *surf, uint16 z, uint scale, byte transparentColor);
452  void unpackBlt(const Common::Rect& r, byte *data, uint size, Graphics::Surface *surf, uint16 z, uint scale, byte transparentColor);
453 
454  // labels
455  void showFloatingLabel(GfxObj *label);
456  void hideFloatingLabel();
457 
458  GfxObj *renderFloatingLabel(Font *font, char *text);
459  GfxObj *createLabel(Font *font, const char *text, byte color);
460  void showLabel(GfxObj *label, int16 x, int16 y);
461  void hideLabel(GfxObj *label);
462  void freeLabels();
463  void unregisterLabel(GfxObj *label);
464 
465  // dialogue handling
466  GfxObj* registerBalloon(Frames *frames, const char *text);
467  int setItem(GfxObj* obj, uint16 x, uint16 y, byte transparentColor = 0);
468  void setItemFrame(uint item, uint16 f);
469  void freeDialogueObjects();
470 
471  // background surface
472  BackgroundInfo *_backgroundInfo;
473  void setBackground(uint type, BackgroundInfo *info);
474  void patchBackground(Graphics::Surface &surf, int16 x, int16 y, bool mask = false);
475  void grabBackground(const Common::Rect& r, Graphics::Surface &dst);
476  void fillBackground(const Common::Rect& r, byte color);
477  void invertBackground(const Common::Rect& r);
478 
479  // palette
480  void setPalette(Palette &palette);
481  void setBlackPalette();
482  void animatePalette();
483 
484  // amiga specific
485  void applyHalfbriteEffect_NS(Graphics::Surface &surf);
486  void setHalfbriteMode(bool enable);
487  void setProjectorPos(int x, int y);
488  void setProjectorProgram(int16 *data);
489  int16 *_nextProjectorPos;
490 
491  // start programmatic relative scroll
492  void initiateScroll(int deltaX, int deltaY);
493  // immediate and absolute x,y scroll
494  void setScrollPosX(int scrollX);
495  void setScrollPosY(int scrollY);
496  // return current scroll position
497  void getScrollPos(Common::Point &p);
498 
499  // init
500  Gfx(Parallaction* vm);
501  virtual ~Gfx();
502 
503  void clearScreen();
504  void updateScreen();
505 
506 public:
507  Palette _palette;
508 
509  byte *_unpackedBitmap;
510 
511 protected:
512  bool _halfbrite;
513 
514  Common::Point _hbCirclePos;
515  int _hbCircleRadius;
516 
517  // BRA specific
518  Palette _backupPal;
519 
520 
521  Graphics::Surface *lockScreen();
522  void unlockScreen();
523  void updateScreenIntern();
524 
525  bool _doubleBuffering;
526  int _gameType;
527  Graphics::Surface _backBuffer;
528  void copyRectToScreen(const byte *buf, int pitch, int x, int y, int w, int h);
529 
530  int _scrollPosX, _scrollPosY;
531  int _minScrollX, _maxScrollX, _minScrollY, _maxScrollY;
532 
533  uint32 _requestedHScrollSteps;
534  uint32 _requestedVScrollSteps;
535  int32 _requestedHScrollDir;
536  int32 _requestedVScrollDir;
537  void scroll();
538  #define NO_FLOATING_LABEL 1000
539 
540  struct Label {
541  Common::String _text;
542  int _x, _y;
543  int color;
544  bool _floating;
545  };
546 
547  GfxObjArray _labels;
548  GfxObjArray _balloons;
549  GfxObjArray _items;
550 
551  GfxObj *_floatingLabel;
552 
553  // overlay mode enables drawing of graphics with automatic screen-to-game coordinate translation
554  bool _overlayMode;
555  void drawOverlay(Graphics::Surface &surf);
556  void drawInventory();
557 
558  void drawList(Graphics::Surface &surface, GfxObjArray &list);
559  void updateFloatingLabel();
560  void copyRect(const Common::Rect &r, Graphics::Surface &src, Graphics::Surface &dst);
561  void drawText(Font *font, Graphics::Surface* surf, uint16 x, uint16 y, const char *text, byte color);
562  void drawGfxObject(GfxObj *obj, Graphics::Surface &surf);
563  void bltMaskScale(const Common::Rect& r, byte *data, Graphics::Surface *surf, uint16 z, uint scale, byte transparentColor);
564  void bltMaskNoScale(const Common::Rect& r, byte *data, Graphics::Surface *surf, uint16 z, byte transparentColor);
565  void bltNoMaskNoScale(const Common::Rect& r, byte *data, Graphics::Surface *surf, byte transparentColor);
566 };
567 
568 
569 } // Parallaction
570 
571 
572 #endif
Definition: graphics.h:244
void setHeight(int16 aHeight)
Definition: rect.h:198
Definition: str.h:59
Definition: surface.h:66
int16 h
Definition: surface.h:75
Definition: graphics.h:431
Definition: rect.h:144
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:137
Definition: graphics.h:41
void setWidth(int16 aWidth)
Definition: rect.h:194
Definition: graphics.h:86
Definition: rect.h:45
int16 left
Definition: rect.h:145
Definition: graphics.h:352
Definition: graphics.h:220
Definition: graphics.h:124
int16 w
Definition: surface.h:70
Definition: graphics.h:53
Definition: graphics.h:540
Definition: graphics.h:409
Definition: graphics.h:300
Definition: graphics.h:73
Definition: label.h:25