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  * Based on the original sources
22  * Faery Tale II -- The Halls of the Dead
23  * (c) 1993-1996 The Wyrmkeep Entertainment Co.
24  */
25 
26 #ifndef SAGA2_SPRITE_H
27 #define SAGA2_SPRITE_H
28 
29 #include "saga2/rect.h"
30 
31 namespace Saga2 {
32 
33 class gPort;
34 class gPixelMap;
35 
36 /* ===================================================================== *
37  Basic sprite structures
38  * ===================================================================== */
39 
40 // Sprite: A structure representing a single sprite
41 
42 struct Sprite {
43  Extent16 size; // size of sprite
44  Point16 offset; // sprite origin point
45  byte *data;
46  uint32 dataSize;
47 
49  ~Sprite();
50 
51  // sprite data follows.
52 };
53 
54 // SpriteSet: A bunch of sprites in a single resource
55 
56 struct SpriteSet {
57  uint32 count; // number of images in the range
58  Sprite **sprites;
59  // (variable-length array)
60  // sprite structures follow table
61 
63  ~SpriteSet();
64 
65  // Member function to return a sprite from the set
66  Sprite *sprite(int16 index) {
67  return sprites[index];
68  }
69 
70 // Sprite &operator[]( int32 index )
71 // {
72 // return (Sprite *)( (uint8 *)this + offsets[index] );
73 // }
74 
75 };
76 
77 extern SpriteSet *objectSprites, // object sprites
78  *mentalSprites, // intagible object sprites
79  *weaponSprites[], // weapon sprites
80  *missileSprites; // missile sprites
81 
82 /* ===================================================================== *
83  Describes the facing directions of actor sprites
84  * ===================================================================== */
85 
86 enum spriteFacingDirections {
87  kSprFaceDown = 0,
88  kSprFaceDownLeft,
89  kSprFaceLeft,
90  kSprFaceUpLeft,
91  kSprFaceUp
92 };
93 
94 /* ===================================================================== *
95  ActorPose: Describes an element of a choreographed action
96  * ===================================================================== */
97 
98 const int kNumPoseFacings = 8;
99 
100 // Describes a single entry in an actor sequence
101 
102 struct ActorPose {
103 
104  // Sequence element flags
105 
106  enum {
107  // Indicates which of the sprites should be drawn flipped
108  // left-to-right
109 
110  kActorFlipped = (1 << 0), // actor spr flipped left/right
111  kLeftObjectFlipped = (1 << 1), // left hand object flipped
112  kRightObjectFlipped = (1 << 2), // right hand object flipped
113 
114  // Indicates the front-to-back priority of the objects
115 
116  kLeftObjectInFront = (1 << 3), // left object in front of actor
117  kRightObjectInFront = (1 << 4), // right object in front of actor
118  kLeftOverRight = (1 << 5) // left in front of right
119  };
120 
121  uint16 flags; // sequence element flags
122 
123  uint8 actorFrameIndex, // actor sprite index
124  actorFrameBank; // which bank actor frame is in
125  uint8 leftObjectIndex, // index of obj in left hand
126  rightObjectIndex; // index of obj in right hand
127 
128  Point16 leftObjectOffset, // offset of left-hand obj.
129  rightObjectOffset; // offset of right-hand obj.
130 
131  // 14 bytes
132 
133  ActorPose();
135  void load(Common::SeekableReadStream *stream);
136 
137  void write(Common::MemoryWriteStreamDynamic *out);
138 };
139 
140 // A choreographed sequence of frames
141 
143 
144  // For each facing direction, the offset to the
145  // table of poses for that sequence, and the number of poses
146  // in the sequence.
147 
148  uint16 start[kNumPoseFacings];
149  uint16 count[kNumPoseFacings];
150 
152 
153  // 32 bytes
154 };
155 
156 struct ActorAnimSet {
157  uint32 numAnimations; // number of animations
158  uint32 poseOffset; // offset to poses table
159 
160  ActorAnimation **animations;
161  ActorPose **poses;
162 
163  uint32 numPoses;
164 };
165 
166 /* ===================================================================== *
167  Sprite color lookup tables
168  * ===================================================================== */
169 
170 typedef uint8 ColorTable[256];
171 
172 // List of color schemes for sprites
173 struct ColorScheme {
174  uint8 bank[11];
175  uint8 speechColor;
176  char name[32];
177 
178  ColorScheme() {}
180 };
181 
183 public:
184  int _count;
185  ColorScheme **_schemes;
186 
187  ColorSchemeList(int count, Common::SeekableReadStream *stream);
188  ~ColorSchemeList();
189 };
190 
191 /* ===================================================================== *
192  Composite sprites (characters made from several component sprites)
193  * ===================================================================== */
194 
196  Sprite *sp; // the sprite to draw
197  Point16 offset; // offset from given origin
198  uint8 *colorTable; // color lookup table
199  uint8 flipped; // true if horizontally flipped.
200 };
201 
202 enum spriteEffectFlags {
203  kSprFXGhosted = (1 << 0), // semi-translucent dither
204  kSprFXTerrainMask = (1 << 1), // mask sprite to terrain
205  kSprFXGhostIfObscured = (1 << 2) // apply ghosted effect if
206  // obscured by terrain
207 };
208 
209 
210 /* ===================================================================== *
211  Object sprite information structure
212  * ===================================================================== */
213 
215  Sprite *sp; // object sprite
216  bool flipped; // mirror sprite horizontally
217 };
218 
219 /* ===================================================================== *
220  Actor Appearance
221  * ===================================================================== */
222 
223 // Bits which represent the various "banks" of sprites for
224 // each actor.
225 
226 // REM: I think we want more banks than this...
227 
228 enum spriteBankNums {
229  kSprStandBankNum = 0,
230  kSprWalkBankNum,
231  kSprRunBankNum,
232  kSprKneelBankNum,
233  kSprLeapBankNum,
234  kSprClimbBankNum,
235  kSprTalkBankNum,
236  kSprFight1HBankNum,
237  kSprFight2HBankNum,
238  kSprFireBankNum,
239  kSprPassiveBankNum,
240  kSprUpStairsBankNum,
241  kSprDnStairsBankNum,
242  kSprSitBankNum,
243 
244  kSprBankCount
245 };
246 
247 enum spriteBankBits {
248  kSprStandBank = (1 << kSprStandBankNum),
249  kSprWalkBank = (1 << kSprWalkBankNum),
250  kSprRunBank = (1 << kSprRunBankNum),
251  kSprKneelBank = (1 << kSprKneelBankNum),
252  kSprLeapBank = (1 << kSprLeapBankNum),
253  kSprClimbBank = (1 << kSprClimbBankNum),
254  kSprTalkBank = (1 << kSprTalkBankNum),
255  kSprFight1HBank = (1 << kSprFight1HBankNum),
256  kSprFight2HBank = (1 << kSprFight2HBankNum),
257  kSprFireBank = (1 << kSprFireBankNum),
258  kSprPassiveBank = (1 << kSprPassiveBankNum),
259  kSprUpStairsBank = (1 << kSprUpStairsBankNum),
260  kSprDnStairsBank = (1 << kSprDnStairsBankNum),
261  kSprSitBank = (1 << kSprSitBankNum)
262 };
263 
264 // This structure is used to contain all of the items needed
265 // to draw an actor, including sprite set, frame list, and
266 // wielding offsets.
267 //
268 // There is an LRU cache of these structures maintained by
269 // the sprite coordinator.
270 
272 public:
273  int16 _useCount; // how many actors using this
274  uint32 _id;
275 
276  ActorAnimSet *_poseList; // list of action sequences
277  ColorSchemeList *_schemeList; // color remapping info
278 
279  SpriteSet *_spriteBanks[kSprBankCount];
280 
281  void loadSpriteBanks(int16 banksNeeded);
282 
283  // Determine if this bank is loaded
284  bool isBankLoaded(int16 bank) {
285  return _spriteBanks[bank] != nullptr;
286  }
287 
288  // A request to load a bank.
289  void requestBank(int16 bank) {
290  // Initiate a load of the sprite bank needed.
291  if (!isBankLoaded(bank))
292  loadSpriteBanks((int16)(1 << bank));
293  }
294 
295  ActorAnimation *animation(int num) {
296  if (_poseList == nullptr)
297  return nullptr;
298 
299  if (num >= (int)_poseList->numAnimations) {
300  warning("ActorPose:animation(), animation number is too high, %d >= %d", num, _poseList->numAnimations);
301  return nullptr;
302  }
303 
304  if (_poseList)
305  return _poseList->animations[num];
306 
307  return nullptr;
308  }
309 
310  ActorPose *pose(ActorAnimation *anim, int dir, int num) {
311  if (_poseList == nullptr)
312  return nullptr;
313 
314  if (num < 0 || num >= anim->count[dir])
315  num = 0;
316 
317  num += anim->start[dir];
318 
319  if (num >= (int)_poseList->numPoses) {
320  warning("ActorPose::pose(), pose number is too high, %d >= %d", num, _poseList->numPoses);
321  return nullptr;
322  }
323 
324  return _poseList->poses[num];
325  }
326 };
327 
328 /* ===================================================================== *
329  Prototypes
330  * ===================================================================== */
331 
332 void initSprites();
333 void cleanupSprites();
334 
335 struct TilePoint;
336 
337 // Draw a plain sprite into a gPort, no masking or clipping
338 void DrawSprite(gPort &port, const Point16 &dest, Sprite *sp);
339 
340 // Draw a composite sprite with both masking and color mapping
341 void DrawCompositeMaskedSprite(
342  gPort &port, // destination gPort
343  SpriteComponent *scList, // list of components
344  int16 numParts, // number of components
345  const Point16 &destPoint, // where to render to
346  const TilePoint &loc, // location on map
347  int16 effects, // effects flags
348  bool *obscured = NULL); // set if object is obscured by terrain
349 
350 // Draw a single sprite with color mapping only
351 void DrawColorMappedSprite(
352  gPort &port, // destination gPort
353  const Point16 &destPoint, // where to render to
354  Sprite *sp, // sprite pointer
355  uint8 *colorTable); // color remapping table
356 
357 // Color map a sprite into a gPixelMap.
358 void ExpandColorMappedSprite(
359  gPixelMap &map, // destination gPixelMap
360  Sprite *sp, // sprite pointer
361  uint8 *colorTable); // color remapping table
362 
363 // Return a specific pixel from a sprite for mouse hit test
364 uint8 GetSpritePixel(
365  Sprite *sp, // sprite pointer
366  int16 flipped, // true if sprite was flipped
367  const Point16 &testPoint); // where to render to
368 
369 // Return the number of visible pixels in a sprite after terrain masking
370 uint16 visiblePixelsInSprite(
371  Sprite *sp, // sprite pointer
372  bool flipped, // is sprite flipped
373  ColorTable colors, // sprite's color table
374  Point16 drawPos, // XY position of sprite
375  TilePoint loc, // UVZ coordinates of sprite
376  uint16 roofID); // ID of ripped roof
377 
378 // Assemble a color lookup table
379 void buildColorTable(
380  uint8 *colorTable, // color table to build
381  uint8 *colorOptions, // colors ranges chosen
382  int16 numOptions);
383 
384 // Functions to load and release an actor appearance
385 ActorAppearance *LoadActorAppearance(uint32 id, int16 banksNeeded);
386 void ReleaseActorAppearance(ActorAppearance *aa);
387 
388 } // end of namespace Saga2
389 
390 #endif
Definition: sprite.h:173
Definition: sprite.h:195
Definition: sprite.h:182
void warning(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: sprite.h:156
Definition: actor.h:32
Definition: gdraw.h:56
Definition: gdraw.h:178
Definition: memstream.h:194
Definition: sprite.h:42
Definition: tcoords.h:127
Definition: stream.h:745
Definition: sprite.h:142
Definition: rect.h:42
Definition: sprite.h:271
Definition: sprite.h:214
Definition: sprite.h:102
Definition: sprite.h:56