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