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 HODJNPODJ_HNPLIBS_SPRITE_H
23 #define HODJNPODJ_HNPLIBS_SPRITE_H
24 
25 #include "bagel/hodjnpodj/globals.h"
26 #include "bagel/hodjnpodj/hnplibs/dibdoc.h"
27 
28 namespace Bagel {
29 namespace HodjNPodj {
30 
31 enum {
32  COLOR_WHITE = RGB(255, 255, 255)
33 };
34 
35 #define SPRITE_TOPMOST 0
36 #define SPRITE_FOREGROUND 64
37 #define SPRITE_MIDDLE 128
38 #define SPRITE_BACKGROUND 192
39 #define SPRITE_HINDMOST 255
40 
41 class CSprite : public CObject {
42  DECLARE_DYNCREATE(CSprite)
43 
44 public:
45  CSprite(); // use "new" operator to create sprites, then LoadSprite
46  ~CSprite();
47 
48  CSprite *DuplicateSprite(CDC *pDC);
49  bool DuplicateSprite(CDC *pDC, CSprite *pSprite);
50 
51  bool LoadSprite(CDC *pDC, const char* pszPathName);
52  bool LoadSprite(CBitmap *pBitmap, CPalette *pPalette = nullptr);
53  bool LoadResourceSprite(CDC *pDC, const int resId);
54  bool LoadResourceSprite(CDC *pDC, const char* pszName);
55 
56  bool LoadCels(CDC *pDC, const char* pszPathName, const int nCels);
57  bool LoadCels(CBitmap *pBitmap, const int nCels, CPalette *pPalette = nullptr);
58  bool LoadResourceCels(CDC *pDC, const int resId, const int nCels);
59  bool LoadResourceCels(CDC *pDC, const char* pszName, const int nCels);
60 
61  bool SetPalette(CPalette *pPalette);
62  bool SharePalette(CPalette *pPalette);
63  bool GetShared() {
64  return (m_bSharedPalette);
65  }
66 
67  bool PaintSprite(CDC *pDC, const int x, const int y);
68  bool PaintSprite(CDC *pDC, CPoint point) {
69  return (PaintSprite(pDC, point.x, point.y));
70  }
71 
72  bool SetupCels(const int nCels);
73  void SetCel(const int nCelID);
74  void UpdateCel() {
75  SetCel(m_nCelID + 1);
76  }
77 
78  bool CropImage(CDC *pDC, CRect *pRect);
79 
80  bool RefreshSprite(CDC *pDC) {
81  return (PaintSprite(pDC, m_cPosition.x, m_cPosition.y));
82  }
83  bool RefreshBackground(CDC *pDC);
84 
85  bool EraseSprite(CDC *pDC);
86 
87  void ClearBackground();
88 
89  CSprite *Interception(CDC *pDC) {
90  return (Interception(pDC, m_pSpriteChain));
91  }
92  CSprite *Interception(CDC *pDC, CSprite * pTestSprite);
93  CSprite *Interception(CRect *newRect) {
94  return (Interception(newRect, m_pSpriteChain));
95  }
96  CSprite *Interception(CRect *newRect, CSprite * pSprite);
97  bool TestInterception(CDC *pDC, CSprite * pSprite, CPoint *pPoint = nullptr);
98 
99  bool GetVisible() {
100  return (m_bVisible);
101  }
102 
103  void SetIntercepts(bool bValue) {
104  m_bIntercepts = bValue;
105  }
106  bool GetIntercepts() {
107  return (m_bIntercepts);
108  }
109 
110  void SetPosition(int x, int y);
111  void SetPosition(CPoint point) {
112  SetPosition(point.x, point.y);
113  }
114  CPoint GetPosition() {
115  return (m_cPosition);
116  }
117 
118  void SetDelta(int x, int y) {
119  m_cMovementDelta.x = x;
120  m_cMovementDelta.y = y;
121  }
122  CPoint GetDelta() {
123  return (m_cMovementDelta);
124  }
125 
126  void SetHotspot(int x, int y) {
127  m_cHotspot.x = x;
128  m_cHotspot.y = y;
129  }
130  CPoint GetHotspot() {
131  return (m_cHotspot);
132  }
133 
134  CSize GetSize() {
135  return (m_cSize);
136  }
137 
138  CRect GetRect() {
139  return (m_cRect);
140  }
141  CRect GetArea() { // obsolete - use GetRect
142  return (m_cRect);
143  }
144 
145  void SetMasked(bool bValue) {
146  m_bMasked = bValue;
147  }
148  bool GetMasked() {
149  return (m_bMasked);
150  }
151 
152  void SetMobile(bool bValue) {
153  m_bMobile = bValue;
154  }
155  bool GetMobile() {
156  return (m_bMobile);
157  }
158 
159  void SetOptimizeSpeed(bool bValue) {
160  m_bRetainContexts = false & bValue;
161  }
162  bool GetOptimizeSpeed() {
163  return (m_bRetainContexts);
164  }
165 
166  void SetTypeCode(int nValue) {
167  m_nType = nValue;
168  }
169  int GetTypeCode() {
170  return (m_nType);
171  }
172 
173  void SetData(CObject *pData) {
174  m_pData = pData;
175  }
176  CObject *GetData() {
177  return (m_pData);
178  }
179 
180  int GetId() {
181  return (m_nId);
182  }
183 
184  void SetZOrder(int nValue) {
185  m_nZOrder = nValue;
186  m_nZPosition = nValue;
187  }
188  int GetZOrder() {
189  return (m_nZOrder);
190  }
191  int GetZPosition() {
192  return (m_nZPosition);
193  }
194 
195  int GetCelCount() {
196  return (m_nCelCount);
197  }
198  int GetCelIndex() {
199  return (m_nCelID);
200  }
201 
202  void SetAnimated(bool bAnimated) {
203  m_bAnimated = bAnimated;
204  }
205  bool GetAnimated() {
206  return (m_bAnimated);
207  }
208 
209  void SetRetainBackground(bool bValue);
210  bool GetRetainBackground() {
211  return (m_bRetainBackground);
212  }
213 
214  bool IsLinked() {
215  return (m_bLinked);
216  }
217  void LinkSprite();
218  void UnlinkSprite();
219  CSprite *GetNextSprite() {
220  return (m_pNext);
221  }
222  CSprite *GetPrevSprite() {
223  return (m_pPrev);
224  }
225 
226  bool Touching(CPoint myPoint);
227 
228  static CSprite *Touched(CPoint myPoint) {
229  return (Touched(myPoint, m_pSpriteChain));
230  }
231  static CSprite *Touched(CPoint myPoint, CSprite *pSprite);
232  static bool InterceptOccurred() {
233  return (m_bTouchedSprite);
234  }
235  static CSprite *GetInterception() {
236  return (m_pTouchedSprite);
237  }
238 
239  static CSprite *GetSpriteChain() {
240  return (m_pSpriteChain);
241  }
242  static bool EraseSprites(CDC *pDC);
243  static void ClearBackgrounds();
244  static void FlushSpriteChain();
245 
246  static bool HaveBackdrop() {
247  return (m_bHaveBackdrop);
248  }
249  static CDC *GetBackdropDC(CDC *pDC);
250  static void ReleaseBackdropDC();
251  static bool SetBackdrop(CDC *pDC, CPalette *pPalette, CBitmap *pBitmap);
252  static void ClearBackdrop();
253  static bool RefreshBackdrop(CDC *pDC, CPalette *pPalette);
254 
255 private:
256  bool UpdateSprite(CDC *pDC);
257 
258  bool SaveBackground(CDC *pDC);
259 
260  bool CreateBackground(CDC *pDC);
261  bool CreateMask(CDC *pDC);
262 
263  bool SetupImage(CDC *pDC);
264  bool SetupBackground(CDC *pDC);
265  bool SetupMask(CDC *pDC);
266 
267  bool CreateImageContext(CDC *pDC);
268  bool CreateBackgroundContext(CDC *pDC);
269  bool CreateMaskContext(CDC *pDC);
270 
271  void ReleaseImageContext();
272  void ReleaseBackgroundContext();
273  void ReleaseMaskContext();
274 
275  void ClearImage();
276  void ClearMask();
277  void ClearPalette();
278 
279  bool SpritesOverlap(CDC *pDC, CSprite *pSprite, CPoint *pPoint = nullptr);
280 
281  bool DoSpritePainting(CDC *pDC, CPoint cPoint);
282  bool DoOptimizedPainting(CDC *pDC, CRect *pDst);
283  bool DoOverlapPainting(CDC *pDC, CRect *myRect);
284  bool ReconstructBackground(CDC *pDC, CRect *myRect);
285 
286 private:
287  CDC *m_pImageDC; // offscreen bitmap device context for image
288  CBitmap *m_pImage; // bitmap for the sprite
289  CBitmap *m_pImageOld; // previous bitmap mapped in the DC
290  CDC *m_pMaskDC; // offscreen bitmap device context for mask
291  CBitmap *m_pMask; // bitmap for the sprite's mask
292  CBitmap *m_pMaskOld; // previous bitmap mapped in the DC
293  CDC *m_pBackgroundDC; // offscreen bitmap device context for background
294  CBitmap *m_pBackground; // bitmap for the sprite's background
295  CBitmap *m_pBackgroundOld; // previous bitmap mapped in the DC
296  CPalette *m_pPalette; // color palette for the sprite
297  CPalette *m_pPalImageOld; // previous palette mapped to image DC
298  CPalette *m_pPalBackOld; // previous palette mapped to background DC
299  CPoint m_cPosition; // upper left corner of sprite on display
300  CPoint m_cMovementDelta; // dx/dy increments for sprite movement
301  CPoint m_cHotspot; // logical hotspot; e.g. for cursors
302  CSize m_cSize; // dx/dy size of the sprite bitmap
303  CRect m_cRect; // bounding rectangle on display
304  CRect m_cImageRect; // bounding rectangle within image bitmap
305  bool m_bVisible; // sprite has an image to be painted
306  bool m_bIntercepts; // sprite can be detected by interception
307  bool m_bSharedPalette; // palette is shared and not deleteable
308  bool m_bMasked; // sprite is to be masked (ignore white)
309  bool m_bMobile; // sprite is mobile or stationary
310  bool m_bOverlaps; // sprite covers other sprites
311  bool m_bPaintOverlap; // whether to paint sprite for overlaps
312  bool m_bRetainBackground;// retain background for screen updates
313  bool m_bRetainContexts; // retain device contexts across calls
314  bool m_bDuplicated; // shares bitmaps with some other sprite
315  bool m_bOverlapTest; // used for positional testing
316  bool m_bPositioned; // whether sprite has been positioned yet
317  bool m_bAnimated; // whether cel advance occurs when painting
318  bool m_bLinked; // whether sprite is linked into the chain
319  int m_nId; // unique sprite identifier
320  int m_nType; // user defined information
321  CObject *m_pData; // user defined data pointer
322  int m_nZOrder; // foreground / background order
323  int m_nZPosition; // foreground / background placement
324  int m_nCelID; // index of current cel image
325  int m_nCelCount; // number of cels in the animation strip
326  CSprite *m_pNext; // pointer to next sprite in master chain
327  CSprite *m_pPrev; // pointer to previous sprite in master chain
328  CSprite *m_pZNext; // pointer to next sprite in z chain
329  CSprite *m_pZPrev; // pointer to previous sprite in z chain
330 
331  static int m_nIndex; // generator of sprite identifiers
332  static CSprite *m_pSpriteChain; // pointer to linked chain of sprites
333  static bool m_bTouchedSprite; // set when sprite painting intercepts another
334  static CSprite *m_pTouchedSprite; // sprite touched during painting operation
335  static bool m_bHaveBackdrop; // whether we have a backdrop bitmap
336  static CDC *m_pBackdropDC; // context used for backdrop artwork
337  static CBitmap *m_pBackdrop; // pointer to backdrop bitmap for background
338  static CBitmap *m_pBackdropOld; // bitmap previously mapped to backdrop context
339  static CPalette *m_pBackdropPalette;// palette mapped to backdrop context
340  static CPalette *m_pBackdropPalOld; // palette previously mapped to backdrop context
341 };
342 
343 } // namespace HodjNPodj
344 } // namespace Bagel
345 
346 #endif
Definition: afx.h:78
Definition: atltypes.h:79
Definition: afxwin.h:655
Definition: atltypes.h:40
Definition: afxwin.h:27
Definition: afxwin.h:623
Definition: sprite.h:41
Definition: atltypes.h:131
Definition: afxwin.h:708