ManaPlus
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
AnimatedSprite Class Reference

#include <animatedsprite.h>

Inheritance diagram for AnimatedSprite:
Sprite

Public Member Functions

 AnimatedSprite (SpriteDef *const sprite)
 
 ~AnimatedSprite ()
 
bool reset ()
 
bool play (const std::string &spriteAction)
 
bool update (const int time)
 
void draw (Graphics *const graphics, const int posX, const int posY) const
 
void drawRescaled (Graphics *const graphics, const int posX, const int posY, const int dx, const int dy) const
 
void drawRaw (Graphics *const graphics, const int posX, const int posY) const
 
int getWidth () const
 
int getHeight () const
 
const ImagegetImage () const
 
bool setSpriteDirection (const SpriteDirection::Type direction)
 
int getNumberOfLayers () const
 
std::string getIdPath () const
 
unsigned int getCurrentFrame () const
 
unsigned int getFrameCount () const
 
void setAlpha (float alpha)
 
const void * getHash () const
 
bool updateNumber (const unsigned num)
 
void clearDelayLoad ()
 
void setSprite (SpriteDef *const sprite)
 
bool isTerminated () const
 
void setLastTime (const int time)
 
- Public Member Functions inherited from Sprite
virtual ~Sprite ()
 
virtual float getAlpha () const
 
virtual const void * getHash2 () const
 

Static Public Member Functions

static AnimatedSpriteload (const std::string &filename, const int variant=0)
 
static AnimatedSpritedelayedLoad (const std::string &filename, const int variant=0)
 
static AnimatedSpriteclone (const AnimatedSprite *const anim)
 
static void setEnableCache (const bool b)
 

Private Member Functions

bool updateCurrentAnimation (const unsigned int dt)
 
void setDelayLoad (const std::string &filename, const int variant)
 

Private Attributes

SpriteDirection::Type mDirection
 
int mLastTime
 
unsigned int mFrameIndex
 
unsigned int mFrameTime
 
SpriteDefmSprite
 
const ActionmAction
 
const AnimationmAnimation
 
const FramemFrame
 
unsigned mNumber
 
unsigned mNumber1
 
AnimationDelayLoadmDelayLoad
 
bool mTerminated
 

Static Private Attributes

static bool mEnableCache = false
 

Additional Inherited Members

- Protected Member Functions inherited from Sprite
 Sprite ()
 
- Protected Attributes inherited from Sprite
float mAlpha
 

Detailed Description

Animates a sprite by adding playback state.

Definition at line 35 of file animatedsprite.h.

Constructor & Destructor Documentation

◆ AnimatedSprite()

AnimatedSprite::AnimatedSprite ( SpriteDef *const  sprite)
explicit

Constructor.

Parameters
spritethe sprite to animate

Definition at line 50 of file animatedsprite.cpp.

References Resource::incRef(), Sprite::mAlpha, and mSprite.

Referenced by clone(), delayedLoad(), and load().

50  :
52  mLastTime(0),
53  mFrameIndex(0),
54  mFrameTime(0),
55  mSprite(sprite),
56  mAction(nullptr),
57  mAnimation(nullptr),
58  mFrame(nullptr),
59  mNumber(100),
60  mNumber1(100),
61  mDelayLoad(nullptr),
62  mTerminated(false)
63 {
64  mAlpha = 1.0F;
65 
66  // Take possession of the sprite
67  if (mSprite != nullptr)
68  mSprite->incRef();
69 }
const Action * mAction
const Animation * mAnimation
unsigned int mFrameIndex
AnimationDelayLoad * mDelayLoad
SpriteDef * mSprite
virtual void incRef()
Definition: resource.cpp:37
SpriteDirection::Type mDirection
unsigned int mFrameTime
float mAlpha
Definition: sprite.h:129
const Frame * mFrame

◆ ~AnimatedSprite()

AnimatedSprite::~AnimatedSprite ( )

Definition at line 126 of file animatedsprite.cpp.

References AnimationDelayLoad::clearSprite(), Resource::decRef(), delete2, mDelayLoad, mSprite, and DelayedManager::removeDelayLoad().

127 {
128  if (mSprite != nullptr)
129  {
130  mSprite->decRef();
131  mSprite = nullptr;
132  }
133  if (mDelayLoad != nullptr)
134  {
138  }
139 }
static void removeDelayLoad(const AnimationDelayLoad *const delayedLoad)
virtual void decRef()
Definition: resource.cpp:49
#define delete2(var)
Definition: delete2.h:24
AnimationDelayLoad * mDelayLoad
SpriteDef * mSprite

Member Function Documentation

◆ clearDelayLoad()

void AnimatedSprite::clearDelayLoad ( )
inline

Definition at line 115 of file animatedsprite.h.

References mDelayLoad.

Referenced by AnimationDelayLoad::~AnimationDelayLoad().

116  { mDelayLoad = nullptr; }
AnimationDelayLoad * mDelayLoad

◆ clone()

AnimatedSprite * AnimatedSprite::clone ( const AnimatedSprite *const  anim)
static

Definition at line 112 of file animatedsprite.cpp.

References AnimatedSprite(), restrict, and SpriteAction::STAND().

Referenced by Being::setEmote().

114 {
115  if (anim == nullptr)
116  return nullptr;
117  AnimatedSprite *restrict const sprite = new AnimatedSprite(anim->mSprite);
118 #ifdef DEBUG_ANIMATIONS
119  sprite->setSpriteName(anim->getSpriteName());
120 #endif // DEBUG_ANIMATIONS
121 
122  sprite->play(SpriteAction::STAND);
123  return sprite;
124 }
static const std::string STAND("stand")
SpriteDef * mSprite
AnimatedSprite(SpriteDef *const sprite)
#define restrict
Definition: localconsts.h:176

◆ delayedLoad()

AnimatedSprite * AnimatedSprite::delayedLoad ( const std::string &  filename,
const int  variant = 0 
)
static

Definition at line 88 of file animatedsprite.cpp.

References AnimatedSprite(), ResourceManager::getFromCache(), load(), mEnableCache, restrict, and SpriteAction::STAND().

Referenced by Being::setGender(), Being::setHairTempSprite(), Being::setSpriteCards(), Being::setSpriteColor(), Being::setSpriteColorId(), Being::setSpriteId(), Being::setTempSprite(), and ActorSprite::setupSpriteDisplay().

91 {
92  if (!mEnableCache)
93  return load(filename, variant);
95  filename, variant);
96  if (res != nullptr)
97  {
98  res->decRef();
99  return load(filename, variant);
100  }
101 
102  AnimatedSprite *restrict const as = new AnimatedSprite(nullptr);
103 #ifdef DEBUG_ANIMATIONS
104  as->setSpriteName(filename);
105 #endif // DEBUG_ANIMATIONS
106 
107  as->play(SpriteAction::STAND);
108  as->setDelayLoad(filename, variant);
109  return as;
110 }
static const std::string STAND("stand")
static AnimatedSprite * load(const std::string &filename, const int variant=0)
static bool mEnableCache
AnimatedSprite(SpriteDef *const sprite)
#define restrict
Definition: localconsts.h:176
Resource * getFromCache(const std::string &filename, const int variant)

◆ draw()

void AnimatedSprite::draw ( Graphics *const  graphics,
const int  posX,
const int  posY 
) const
virtual

Draw the current animation frame at the coordinates given in screen pixels.

Implements Sprite.

Definition at line 327 of file animatedsprite.cpp.

References FUNC_BLOCK, Frame::image, Sprite::mAlpha, mFrame, Frame::offsetX, Frame::offsetY, restrict, and Image::setAlpha().

Referenced by Being::drawBeingCursor(), Being::drawEmotion(), and MiniStatusWindow::drawIcons().

330 {
331  FUNC_BLOCK("AnimatedSprite::draw", 1)
332  if ((mFrame == nullptr) || (mFrame->image == nullptr))
333  return;
334 
335  Image *restrict const image = mFrame->image;
336  image->setAlpha(mAlpha);
337  graphics->drawImage(image,
338  posX + mFrame->offsetX, posY + mFrame->offsetY);
339 }
if(!vert) return
void drawImage(const Image *restrict const image, int dstX, int dstY) restrict2 override final
#define FUNC_BLOCK(name, id)
Definition: perfomance.h:80
void setAlpha(float alpha)
Definition: image.h:61
#define restrict
Definition: localconsts.h:176
float mAlpha
Definition: sprite.h:129
const Frame * mFrame

◆ drawRaw()

void AnimatedSprite::drawRaw ( Graphics *const  graphics,
const int  posX,
const int  posY 
) const

Definition at line 362 of file animatedsprite.cpp.

References Frame::image, Sprite::mAlpha, mFrame, restrict, and Image::setAlpha().

Referenced by CutInWindow::draw2().

365 {
366  if ((mFrame == nullptr) || (mFrame->image == nullptr))
367  return;
368 
369  Image *restrict const image = mFrame->image;
370  image->setAlpha(mAlpha);
371  graphics->drawImage(image,
372  posX,
373  posY);
374 }
virtual void setAlpha(const float alpha)
Definition: image.cpp:285
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
Definition: image.h:61
#define restrict
Definition: localconsts.h:176
float mAlpha
Definition: sprite.h:129
Image * image
Definition: frame.h:41
const Frame * mFrame

◆ drawRescaled()

void AnimatedSprite::drawRescaled ( Graphics *const  graphics,
const int  posX,
const int  posY,
const int  dx,
const int  dy 
) const

Definition at line 341 of file animatedsprite.cpp.

References Frame::image, Sprite::mAlpha, mFrame, Frame::offsetX, Frame::offsetY, restrict, and Image::setAlpha().

Referenced by CastingEffect::draw().

346 {
347  if (mFrame == nullptr ||
348  mFrame->image == nullptr)
349  {
350  return;
351  }
352 
353  Image *restrict const image = mFrame->image;
354  image->setAlpha(mAlpha);
355  graphics->drawRescaledImage(image,
356  posX + mFrame->offsetX,
357  posY + mFrame->offsetY,
358  dx,
359  dy);
360 }
int offsetX
Definition: frame.h:43
virtual void setAlpha(const float alpha)
Definition: image.cpp:285
virtual void drawRescaledImage(const Image *const image, int dstX, int dstY, const int desiredWidth, const int desiredHeight)=0
int offsetY
Definition: frame.h:44
Definition: image.h:61
#define restrict
Definition: localconsts.h:176
float mAlpha
Definition: sprite.h:129
Image * image
Definition: frame.h:41
const Frame * mFrame

◆ getCurrentFrame()

unsigned int AnimatedSprite::getCurrentFrame ( ) const
inlinevirtual

Returns the current frame number for the sprite.

Implements Sprite.

Definition at line 102 of file animatedsprite.h.

References A_WARN_UNUSED, getFrameCount(), getHash(), mFrameIndex, setAlpha(), and updateNumber().

104  { return mFrameIndex; }
unsigned int mFrameIndex

◆ getFrameCount()

unsigned int AnimatedSprite::getFrameCount ( ) const
virtual

Returns the frame count for the sprite.

Implements Sprite.

Definition at line 403 of file animatedsprite.cpp.

References CAST_U32, Animation::getLength(), and mAnimation.

Referenced by getCurrentFrame().

404 {
405  if (mAnimation != nullptr)
406  return CAST_U32(mAnimation->getLength());
407  return 0;
408 }
#define CAST_U32
Definition: cast.h:30
const Animation * mAnimation
size_t getLength() const
Definition: animation.h:69

◆ getHash()

const void * AnimatedSprite::getHash ( ) const
virtual

Reimplemented from Sprite.

Definition at line 448 of file animatedsprite.cpp.

References mFrame.

Referenced by getCurrentFrame().

449 {
450  if (mFrame != nullptr)
451  return mFrame;
452  return this;
453 }
const Frame * mFrame

◆ getHeight()

int AnimatedSprite::getHeight ( ) const
virtual

Gets the height in pixels of the image of the current frame

Implements Sprite.

Definition at line 417 of file animatedsprite.cpp.

References Frame::image, Image::mBounds, and mFrame.

Referenced by CutInWindow::show().

418 {
419  if ((mFrame != nullptr) && (mFrame->image != nullptr))
420  return mFrame->image->mBounds.h;
421  return 0;
422 }
SDL_Rect mBounds
Definition: image.h:210
Image * image
Definition: frame.h:41
const Frame * mFrame

◆ getIdPath()

std::string AnimatedSprite::getIdPath ( ) const

Definition at line 424 of file animatedsprite.cpp.

References Resource::mIdPath, and mSprite.

Referenced by getNumberOfLayers(), and LocalPlayer::imitateOutfit().

425 {
426  if (mSprite == nullptr)
427  return "";
428  return mSprite->mIdPath;
429 }
SpriteDef * mSprite
std::string mIdPath
Definition: resource.h:83

◆ getImage()

const Image * AnimatedSprite::getImage ( ) const
virtual

Returns a reference to the current image being drawn.

Implements Sprite.

Definition at line 431 of file animatedsprite.cpp.

References Frame::image, and mFrame.

432 {
433  return mFrame != nullptr ? mFrame->image : nullptr;
434 }
Image * image
Definition: frame.h:41
const Frame * mFrame

◆ getNumberOfLayers()

int AnimatedSprite::getNumberOfLayers ( ) const
inline

Definition at line 97 of file animatedsprite.h.

References A_WARN_UNUSED, and getIdPath().

98  { return 1; }

◆ getWidth()

int AnimatedSprite::getWidth ( ) const
virtual

Gets the width in pixels of the image of the current frame

Implements Sprite.

Definition at line 410 of file animatedsprite.cpp.

References Frame::image, Image::mBounds, and mFrame.

Referenced by MiniStatusWindow::drawIcons(), and CutInWindow::show().

411 {
412  if ((mFrame != nullptr) && (mFrame->image != nullptr))
413  return mFrame->image->mBounds.w;
414  return 0;
415 }
SDL_Rect mBounds
Definition: image.h:210
Image * image
Definition: frame.h:41
const Frame * mFrame

◆ isTerminated()

bool AnimatedSprite::isTerminated ( ) const
inline

Definition at line 121 of file animatedsprite.h.

References mTerminated.

Referenced by CastingEffect::isTerminated(), and Being::logic().

122  { return mTerminated; }

◆ load()

AnimatedSprite * AnimatedSprite::load ( const std::string &  filename,
const int  variant = 0 
)
static

An helper function, which will request the sprite to animate from the resource manager.

Parameters
filenamethe file of the sprite to animate
variantthe sprite variant

Definition at line 71 of file animatedsprite.cpp.

References AnimatedSprite(), Loader::getSprite(), restrict, and SpriteAction::STAND().

Referenced by Being::addEffect(), delayedLoad(), StatusEffect::getIcon(), ActorSprite::initTargetCursor(), EmoteDB::load(), EmoteDB::loadSpecialXmlFile(), EmoteDB::loadXmlFile(), Being::setHorse(), Being::setLanguageId(), CutInWindow::show(), Being::showAwayBadge(), Being::showGmBadge(), Being::showGuildBadge(), Being::showInactiveBadge(), Being::showNameBadge(), Being::showPartyBadge(), Being::showShopBadge(), and Being::showTeamBadge().

73 {
75  filename, variant);
76  if (s == nullptr)
77  return nullptr;
78  AnimatedSprite *restrict const as = new AnimatedSprite(s);
79 #ifdef DEBUG_ANIMATIONS
80  as->setSpriteName(filename);
81 #endif // DEBUG_ANIMATIONS
82 
83  as->play(SpriteAction::STAND);
84  s->decRef();
85  return as;
86 }
static const std::string STAND("stand")
SpriteDef * getSprite(const std::string &path, const int variant=0)
AnimatedSprite(SpriteDef *const sprite)
#define restrict
Definition: localconsts.h:176

◆ play()

bool AnimatedSprite::play ( const std::string &  action)
virtual

Plays an action using the current direction.

Returns
true if the sprite changed, false otherwise

Implements Sprite.

Definition at line 158 of file animatedsprite.cpp.

References SpriteDef::getAction(), Action::getAnimation(), Animation::getLength(), mAction, mAnimation, mDelayLoad, mDirection, mNumber, mSprite, reset(), and AnimationDelayLoad::setAction().

Referenced by AnimationDelayLoad::load(), Being::setAction(), Being::setEmote(), Being::setHorse(), update(), and updateCurrentAnimation().

159 {
160  if (mSprite == nullptr)
161  {
162  if (mDelayLoad == nullptr)
163  return false;
164  mDelayLoad->setAction(spriteAction);
165  return true;
166  }
167 
168  const Action *const action = mSprite->getAction(spriteAction, mNumber);
169  if (action == nullptr)
170  return false;
171 
172  mAction = action;
173  const Animation *const animation = mAction->getAnimation(mDirection);
174 
175  if ((animation != nullptr) &&
176  animation != mAnimation &&
177  animation->getLength() > 0)
178  {
179  mAnimation = animation;
180  reset();
181 
182  return true;
183  }
184 
185  return false;
186 }
const Action * mAction
void setAction(const std::string &action)
const Animation * getAnimation(SpriteDirection::Type direction) const
Definition: action.cpp:45
const Animation * mAnimation
Definition: action.h:39
const Action * getAction(const std::string &action, const unsigned num) const
Definition: spritedef.cpp:51
AnimationDelayLoad * mDelayLoad
size_t getLength() const
Definition: animation.h:69
SpriteDef * mSprite
SpriteDirection::Type mDirection

◆ reset()

bool AnimatedSprite::reset ( )
virtual

Resets the sprite.

Returns
true if the sprite changed, false otherwise

Implements Sprite.

Definition at line 141 of file animatedsprite.cpp.

References mAnimation, mFrame, mFrameIndex, Animation::mFrames, mFrameTime, and mLastTime.

Referenced by play(), and setSpriteDirection().

142 {
143  const bool ret = mFrameIndex !=0 ||
144  mFrameTime != 0 ||
145  mLastTime != 0;
146 
147  mFrameIndex = 0;
148  mFrameTime = 0;
149  mLastTime = 0;
150 
151  if (mAnimation != nullptr)
152  mFrame = &mAnimation->mFrames[0];
153  else
154  mFrame = nullptr;
155  return ret;
156 }
const Animation * mAnimation
unsigned int mFrameIndex
Frames mFrames
Definition: animation.h:98
unsigned int mFrameTime
const Frame * mFrame

◆ setAlpha()

void AnimatedSprite::setAlpha ( float  alpha)
virtual

Sets the alpha value of the animated sprite

Reimplemented from Sprite.

Definition at line 436 of file animatedsprite.cpp.

References Frame::image, Sprite::mAlpha, mFrame, restrict, and Image::setAlpha().

Referenced by getCurrentFrame().

437 {
438  mAlpha = alpha;
439 
440  if (mFrame != nullptr)
441  {
442  Image *restrict const image = mFrame->image;
443  if (image != nullptr)
444  image->setAlpha(mAlpha);
445  }
446 }
virtual void setAlpha(const float alpha)
Definition: image.cpp:285
Definition: image.h:61
#define restrict
Definition: localconsts.h:176
float mAlpha
Definition: sprite.h:129
Image * image
Definition: frame.h:41
const Frame * mFrame

◆ setDelayLoad()

void AnimatedSprite::setDelayLoad ( const std::string &  filename,
const int  variant 
)
private

Definition at line 474 of file animatedsprite.cpp.

References DelayedManager::addDelayedAnimation(), AnimationDelayLoad::clearSprite(), mDelayLoad, and DelayedManager::removeDelayLoad().

Referenced by setLastTime().

476 {
477  if (mDelayLoad != nullptr)
478  {
481  delete mDelayLoad;
482  }
483  mDelayLoad = new AnimationDelayLoad(filename, variant, this);
485 }
static void removeDelayLoad(const AnimationDelayLoad *const delayedLoad)
AnimationDelayLoad * mDelayLoad
static void addDelayedAnimation(AnimationDelayLoad *const animation)

◆ setEnableCache()

static void AnimatedSprite::setEnableCache ( const bool  b)
inlinestatic

Definition at line 124 of file animatedsprite.h.

References mEnableCache.

Referenced by Game::Game(), and Game::~Game().

125  { mEnableCache = b; }
static bool mEnableCache

◆ setLastTime()

void AnimatedSprite::setLastTime ( const int  time)
inline

◆ setSprite()

void AnimatedSprite::setSprite ( SpriteDef *const  sprite)
inline

Definition at line 118 of file animatedsprite.h.

References mSprite.

Referenced by AnimationDelayLoad::load().

119  { mSprite = sprite; }
SpriteDef * mSprite

◆ setSpriteDirection()

bool AnimatedSprite::setSpriteDirection ( const SpriteDirection::Type  direction)
virtual

Sets the direction.

Returns
true if the sprite changed, false otherwise

Implements Sprite.

Definition at line 376 of file animatedsprite.cpp.

References Action::getAnimation(), Animation::getLength(), mAction, mAnimation, mDirection, reset(), and restrict.

Referenced by Being::setDirection(), Being::setEmote(), and Being::setHorse().

378 {
379  if (mDirection != direction)
380  {
381  mDirection = direction;
382 
383  if (mAction == nullptr)
384  return false;
385 
386  const Animation *restrict const animation =
388 
389  if ((animation != nullptr) &&
390  animation != mAnimation &&
391  animation->getLength() > 0)
392  {
393  mAnimation = animation;
394  reset();
395  }
396 
397  return true;
398  }
399 
400  return false;
401 }
const Action * mAction
const Animation * getAnimation(SpriteDirection::Type direction) const
Definition: action.cpp:45
const Animation * mAnimation
size_t getLength() const
Definition: animation.h:69
SpriteDirection::Type mDirection
#define restrict
Definition: localconsts.h:176

◆ update()

bool AnimatedSprite::update ( const int  time)
virtual

Inform the animation of the passed time so that it can output the correct animation frame.

Returns
true if the sprite changed, false otherwise

Implements Sprite.

Definition at line 188 of file animatedsprite.cpp.

References A_UNLIKELY, mAnimation, mFrame, mLastTime, mTerminated, play(), restrict, SpriteAction::STAND(), ServerCommandType::time, and updateCurrentAnimation().

Referenced by Being::drawBeingCursor(), CutInWindow::logic(), MiniStatusWindow::logic(), Being::logic(), CutInWindow::show(), and CastingEffect::update().

189 {
190  // Avoid freaking out at first frame or when tick_time overflows
191  if (A_UNLIKELY(time < mLastTime || mLastTime == 0))
192  mLastTime = time;
193 
194  // If not enough time has passed yet, do nothing
195  if (time <= mLastTime || (mAnimation == nullptr))
196  return false;
197 
198  const unsigned int dt = time - mLastTime;
199  mLastTime = time;
200 
201  const Animation *restrict const animation = mAnimation;
202  const Frame *restrict const frame = mFrame;
203 
205  {
206  // Animation finished, reset to default
208  mTerminated = true;
209  }
210 
211  // Make sure something actually changed
212  return animation != mAnimation || frame != mFrame;
213 }
Definition: frame.h:37
static const std::string STAND("stand")
#define A_UNLIKELY(x)
Definition: likely.h:29
const Animation * mAnimation
bool updateCurrentAnimation(const unsigned int dt)
#define restrict
Definition: localconsts.h:176
bool play(const std::string &spriteAction)
const Frame * mFrame

◆ updateCurrentAnimation()

bool AnimatedSprite::updateCurrentAnimation ( const unsigned int  dt)
private

Definition at line 215 of file animatedsprite.cpp.

References FrameType::ANIMATION, CAST_U32, Frame::delay, Animation::getLength(), FrameType::GOTO, Frame::image, FrameType::JUMP, FrameType::LABEL, mAnimation, mFrame, mFrameIndex, Animation::mFrames, mFrameTime, mrand(), Frame::nextAction, FrameType::PAUSE, play(), Frame::rand, restrict, ServerCommandType::time, and Frame::type.

Referenced by setLastTime(), and update().

216 {
217  // move code from Animation::isTerminator(*mFrame)
218  if (mFrame == nullptr ||
219  mAnimation == nullptr ||
220  (mFrame->image == nullptr && mFrame->type == FrameType::ANIMATION))
221  {
222  return false;
223  }
224 
225  mFrameTime += time;
226 
227  while ((mFrameTime > CAST_U32(mFrame->delay) &&
228  mFrame->delay > 0) ||
231  {
232  bool fail(true);
234  mFrameIndex++;
235 
237  mFrameIndex = 0;
238 
240  if (mFrame->type == FrameType::LABEL &&
241  !mFrame->nextAction.empty())
242  {
243  fail = false;
244  }
245  else if (mFrame->type == FrameType::GOTO &&
246  !mFrame->nextAction.empty())
247  {
248  const int rand = mFrame->rand;
249  if (rand == 100 ||
250  ((rand != 0) && rand >= mrand() % 100))
251  {
252  for (size_t i = 0; i < mAnimation->getLength(); i ++)
253  {
254  const Frame *restrict const frame =
255  &mAnimation->mFrames[i];
256  if (frame->type == FrameType::LABEL &&
257  mFrame->nextAction == frame->nextAction)
258  {
259  mFrameIndex = CAST_U32(i);
260  if (mFrameIndex >= CAST_U32(
261  mAnimation->getLength()))
262  {
263  mFrameIndex = 0;
264  }
265 
267 
268  fail = false;
269  break;
270  }
271  }
272  }
273  else
274  {
275  fail = false;
276  }
277  }
278  else if (mFrame->type == FrameType::JUMP &&
279  !mFrame->nextAction.empty())
280  {
281  const int rand = mFrame->rand;
282  if (rand == 100 ||
283  ((rand != 0) && rand >= mrand() % 100))
284  {
286  return true;
287  }
288  }
289  // copy code from Animation::isTerminator(*mFrame)
290  else if ((mFrame->image == nullptr) &&
292  {
293  const int rand = mFrame->rand;
294  if (rand == 100 ||
295  ((rand != 0) && rand >= mrand() % 100))
296  {
297  mAnimation = nullptr;
298  mFrame = nullptr;
299  return false;
300  }
301  }
302  else
303  {
304  const int rand = mFrame->rand;
305  if (rand == 100 ||
307  {
308  fail = false;
309  }
310  else
311  {
312  if ((rand != 0) && mrand() % 100 <= rand)
313  fail = false;
314  }
315  }
316  if (fail)
317  {
318  if (mFrame != nullptr)
319  mFrameTime = mFrame->delay + 1;
320  else
321  mFrameTime ++;
322  }
323  }
324  return true;
325 }
#define CAST_U32
Definition: cast.h:30
std::string nextAction
Definition: frame.h:47
Definition: frame.h:37
const Animation * mAnimation
unsigned int mFrameIndex
int delay
Definition: frame.h:42
size_t getLength() const
Definition: animation.h:69
Frames mFrames
Definition: animation.h:98
int mrand()
Definition: mrand.cpp:40
unsigned int mFrameTime
#define restrict
Definition: localconsts.h:176
int rand
Definition: frame.h:45
bool play(const std::string &spriteAction)
Image * image
Definition: frame.h:41
const Frame * mFrame
FrameTypeT type
Definition: frame.h:46

◆ updateNumber()

bool AnimatedSprite::updateNumber ( const unsigned  num)
virtual

Implements Sprite.

Definition at line 455 of file animatedsprite.cpp.

References SpriteDef::findNumber(), mNumber, mNumber1, and mSprite.

Referenced by getCurrentFrame().

456 {
457  if (mSprite == nullptr)
458  return false;
459 
460  if (mNumber1 != num)
461  {
462  mNumber1 = num;
463  mNumber = mSprite->findNumber(num);
464  if (mNumber == 0u)
465  {
466  mNumber = 100;
467  return false;
468  }
469  return true;
470  }
471  return false;
472 }
SpriteDef * mSprite
unsigned findNumber(const unsigned num) const
Definition: spritedef.cpp:75

Field Documentation

◆ mAction

const Action* AnimatedSprite::mAction
private

The currently active animation.

Definition at line 174 of file animatedsprite.h.

Referenced by play(), and setSpriteDirection().

◆ mAnimation

const Animation* AnimatedSprite::mAnimation
private

The currently active frame.

Definition at line 176 of file animatedsprite.h.

Referenced by getFrameCount(), play(), reset(), setLastTime(), setSpriteDirection(), update(), and updateCurrentAnimation().

◆ mDelayLoad

AnimationDelayLoad* AnimatedSprite::mDelayLoad
private

Definition at line 181 of file animatedsprite.h.

Referenced by clearDelayLoad(), play(), setDelayLoad(), and ~AnimatedSprite().

◆ mDirection

SpriteDirection::Type AnimatedSprite::mDirection
private

The sprite direction.

Definition at line 165 of file animatedsprite.h.

Referenced by play(), and setSpriteDirection().

◆ mEnableCache

bool AnimatedSprite::mEnableCache = false
staticprivate

Definition at line 183 of file animatedsprite.h.

Referenced by delayedLoad(), and setEnableCache().

◆ mFrame

const Frame* AnimatedSprite::mFrame
private

◆ mFrameIndex

unsigned int AnimatedSprite::mFrameIndex
private

The index of the current frame.

Definition at line 168 of file animatedsprite.h.

Referenced by getCurrentFrame(), reset(), setLastTime(), and updateCurrentAnimation().

◆ mFrameTime

unsigned int AnimatedSprite::mFrameTime
private

The time since start of frame. The sprite definition.

Definition at line 169 of file animatedsprite.h.

Referenced by reset(), setLastTime(), and updateCurrentAnimation().

◆ mLastTime

int AnimatedSprite::mLastTime
private

The last time update was called.

Definition at line 166 of file animatedsprite.h.

Referenced by reset(), setLastTime(), and update().

◆ mNumber

unsigned AnimatedSprite::mNumber
private

Definition at line 179 of file animatedsprite.h.

Referenced by play(), and updateNumber().

◆ mNumber1

unsigned AnimatedSprite::mNumber1
private

Definition at line 180 of file animatedsprite.h.

Referenced by updateNumber().

◆ mSprite

SpriteDef* AnimatedSprite::mSprite
private

The currently active action.

Definition at line 172 of file animatedsprite.h.

Referenced by AnimatedSprite(), getIdPath(), play(), setLastTime(), setSprite(), updateNumber(), and ~AnimatedSprite().

◆ mTerminated

bool AnimatedSprite::mTerminated
private

Definition at line 182 of file animatedsprite.h.

Referenced by isTerminated(), and update().


The documentation for this class was generated from the following files: