ScummVM API documentation
sprite.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 BAGEL_BOFLIB_GFX_SPRITE_H
23 #define BAGEL_BOFLIB_GFX_SPRITE_H
24 
25 #include "bagel/spacebar/boflib/gfx/bitmap.h"
26 #include "bagel/boflib/object.h"
27 #include "bagel/boflib/palette.h"
28 #include "bagel/spacebar/boflib/gui/window.h"
29 
30 namespace Bagel {
31 namespace SpaceBar {
32 
33 #define SPRITE_TOPMOST 0
34 #define SPRITE_FOREGROUND 64
35 #define SPRITE_MIDDLE 128
36 #define SPRITE_BACKGROUND 192
37 #define SPRITE_HINDMOST 255
38 
39 class CBofSprite : public CBofError, public CBofObject, public CLList {
40 public:
41  static void initialize();
42  static void shutdown();
43 
44  // Constructors
45  CBofSprite();
46 
47  // Destructors
48  virtual ~CBofSprite();
49 
51 
52  // Implementation
53  CBofSprite *duplicateSprite();
54  void duplicateSprite(CBofSprite *pSprite);
55 
56  bool loadSprite(const char *pszPathName, int nCels = 1);
57  bool loadSprite(CBofBitmap *pBitmap, int nCels = 1);
58 
59  bool paintSprite(CBofBitmap *pBmp, int x, int y);
60  bool paintSprite(CBofBitmap *pBmp, CBofPoint point) {
61  return paintSprite(pBmp, point.x, point.y);
62  }
63 
64  bool paintSprite(CBofWindow *pWnd, int x, int y);
65  bool paintSprite(CBofWindow *pWnd, CBofPoint point) {
66  return paintSprite(pWnd, point.x, point.y);
67  }
68 
69  bool paintCel(CBofWindow *pWnd, int nCelId, int x, int y);
70  bool paintCel(CBofBitmap *pBmp, int nCelId, int x, int y);
71 
72  void batchPaint(int, int y);
73  void batchErase();
74 
75  bool setupCels(int nCels);
76  void setCel(int nCelID);
77 
78  void nextCel();
79  void prevCel();
80 
81  bool refreshSprite(CBofBitmap *pBmp) {
82  return paintSprite(pBmp, _cPosition.x, _cPosition.y);
83  }
84 
85  bool refreshSprite(CBofWindow *pWnd) {
86  return paintSprite(pWnd, _cPosition.x, _cPosition.y);
87  }
88 
89  bool eraseSprite(CBofWindow *pWnd);
90 
91  // Notice how there is no eraseSprite for a CBofBitmap - that's because
92  // sprites no longer retain their background, so there would be no way
93  // to restore the background, and that's all eraseSprite does.
94 
95  CBofSprite *interception(CBofRect *newRect, CBofSprite *pTestSprite);
96  CBofSprite *interception(CBofSprite *pTestSprite);
97 
98  CBofSprite *interception() {
99  return interception(_pSpriteChain);
100  }
101 
102  CBofSprite *interception(CBofRect *newRect) {
103  return interception(newRect, _pSpriteChain);
104  }
105 
106  bool testInterception(CBofSprite *pTestSprite, CBofPoint *pPoint = nullptr);
107 
108  void setPosition(int x, int y);
109 
110  CBofPoint getPosition() const {
111  return _cPosition;
112  }
113 
114  CBofSize getSize() const {
115  return _cSize;
116  }
117 
118  CBofRect getRect() const {
119  return _cRect;
120  }
121 
122  int height() const {
123  return _cRect.height();
124  }
125 
126  int width() const {
127  return _cRect.width();
128  }
129 
130  void setMaskColor(int nColor) {
131  _nMaskColor = nColor;
132  }
133 
134  int getMaskColor() const {
135  return _nMaskColor;
136  }
137 
138  byte readPixel(int x, int y) const {
139  return _pImage->readPixel(x, y);
140  }
141 
142  void setZOrder(int nValue);
143 
144  int getCelCount() const {
145  return _nCelCount;
146  }
147  int getCelIndex() const {
148  return _nCelID;
149  }
150 
151  void setAnimated(bool bAnimated) {
152  _bAnimated = bAnimated;
153  }
154  bool getAnimated() const {
155  return _bAnimated;
156  }
157 
158  void linkSprite();
159  void unlinkSprite();
160 
161  const char *getFileName() const {
162  return _pImage->getFileName();
163  }
164 
165  static void openLibrary(CBofPalette *pPal);
166  static void closeLibrary();
167 
168  static void setSharedPalette(CBofPalette *pPalette);
169 
170  static CBofSprite *getSpriteChain() {
171  return _pSpriteChain;
172  }
173 
174  static bool updateDirtyRect(CBofWindow *pWnd, CBofSprite *pPrimarySprite = nullptr);
175  static bool updateDirtyRect(CBofBitmap *pBmp, CBofSprite *pPrimarySprite = nullptr);
176  static void addToDirtyRect(CBofRect *pRect);
177  static void clearDirtyRect() {
178  _cDirtyRect->setRectEmpty();
179  }
180 
181  static CBofRect *getDirtyRect() {
182  return _cDirtyRect;
183  }
184 
185  static void flushSpriteChain();
186 
187  static void setupWorkArea(int dx, int dy);
188  static void tearDownWorkArea();
189 
190  // Add a method for allowing callers of this object to block
191  // next cell advancement
192 
193  void setBlockAdvance(bool b = true) {
194  _bBlockAdvance = b;
195  }
196  bool getBlockAdvance() const {
197  return _bBlockAdvance;
198  }
199 
200 private:
201  void clearImage();
202 
203  bool spritesOverlap(CBofSprite *pSprite, CBofPoint *pPoint = nullptr);
204  bool _bBlockAdvance; // Allow block next cell.
205 public:
206  CBofBitmap *_pImage; // Bitmap for the sprite
207 
208 protected:
209  CBofPoint _cPosition; // Upper left corner of sprite on display
210  CBofSize _cSize; // dx/dy size of the sprite bitmap
211  CBofRect _cRect; // Bounding rectangle on display
212  CBofRect _cImageRect; // Bounding rectangle within image bitmap
213 
214  int _nMaskColor; // Transparent color index for this sprite
215  int _nZOrder; // Foreground / background order
216  int _nCelID; // Index of current cel image
217  int _nCelCount; // Number of cels in the animation strip
218 
219  bool _bDuplicated : 1; // Shares bitmaps with some other sprite
220  bool _bPositioned : 1; // Whether sprite has been positioned yet
221  bool _bAnimated : 1; // Whether cel advance occurs when painting
222  bool _bLinked : 1; // Whether sprite is linked into the chain
223  bool _bReadOnly : 1; // Whether image is read only or not
224 
225  static CBofRect *_cDirtyRect;
226  static CBofSprite *_pSpriteChain; // Pointer to linked chain of sprites
227  static CBofSprite *_pTouchedSprite; // Sprite touched during painting operation
228  static CBofBitmap *_pWorkBmp; // Offscreen work area
229  static CBofPalette *_pSharedPalette; // Shared palette for ALL sprites
230  static int _nWorkDX;
231  static int _nWorkDY;
232 };
233 
234 } // namespace SpaceBar
235 } // namespace Bagel
236 
237 #endif
Definition: object.h:28
Definition: size.h:32
Definition: window.h:53
Definition: rect.h:35
Definition: error.h:52
byte readPixel(int x, int y)
Definition: sprite.h:39
Definition: palette.h:64
Definition: afxwin.h:27
Definition: point.h:32
Definition: llist.h:31
Definition: bitmap.h:57