Skip to content

Commit

Permalink
Renamed SPR_getAnimationDone() --> SPR_isAnimationDone() and changed …
Browse files Browse the repository at this point in the history
…its logic + fixed auto_loop
  • Loading branch information
Stephane-D committed Sep 13, 2024
1 parent 8b07ad7 commit d5cf952
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 58 deletions.
12 changes: 6 additions & 6 deletions inc/sprite_eng.h
Original file line number Diff line number Diff line change
Expand Up @@ -844,14 +844,14 @@ bool SPR_getAutoAnimation(Sprite* sprite);
void SPR_setAnimationLoop(Sprite* sprite, bool value);
/**
* \brief
* Return TRUE if animation ended / looped.<br>
* This can be used with the frame change callback (see #SPR_setFrameChangeCallback(..)) to detect
* the end of sprite animation and do appropriate action if required.
* Return TRUE if the sprite reached the last frame of the current animation.<br>
* When auto animation is enabled (see SPR_setAutoAnimation(..)) the function returns TRUE only when we reach
* the last *tick* of the last animation frame.<br>
* When auto animation is disabled the function returns TRUE as soon we are on last animation frame.
*
* \see SPR_FLAG_DISABLE_ANIMATION_LOOP
* \see #SPR_setAnimationLoop(Sprite*, bool)
* \see #SPR_setAutoAnimation(Sprite*, bool)
*/
bool SPR_getAnimationDone(Sprite* sprite);
bool SPR_isAnimationDone(Sprite* sprite);
/**
* \brief
* Set the VRAM tile position reserved for this sprite.
Expand Down
12 changes: 6 additions & 6 deletions inc/sprite_eng_legacy.h
Original file line number Diff line number Diff line change
Expand Up @@ -837,14 +837,14 @@ bool SPR_getAutoAnimation(Sprite* sprite);
void SPR_setAnimationLoop(Sprite* sprite, bool value);
/**
* \brief
* Return TRUE if animation ended / looped.<br>
* This can be used with the frame change callback (see #SPR_setFrameChangeCallback(..)) to detect
* the end of sprite animation and do appropriate action if required.
* Return TRUE if the sprite reached the last frame of the current animation.<br>
* When auto animation is enabled (see SPR_setAutoAnimation(..)) the function returns TRUE only when we reach
* the last *tick* of the last animation frame.<br>
* When auto animation is disabled the function returns TRUE as soon we are on last animation frame.
*
* \see SPR_FLAG_DISABLE_ANIMATION_LOOP
* \see #SPR_setAnimationLoop(Sprite*, bool)
* \see #SPR_setAutoAnimation(Sprite*, bool)
*/
bool SPR_getAnimationDone(Sprite* sprite);
bool SPR_isAnimationDone(Sprite* sprite);
/**
* \brief
* Set the VRAM tile position reserved for this sprite.
Expand Down
Binary file modified lib/libmd.a
Binary file not shown.
Binary file modified lib/libmd_debug.a
Binary file not shown.
32 changes: 14 additions & 18 deletions src/sprite_eng.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@

#define NEED_UPDATE 0x000F

#define STATE_ANIMATION_DONE 0x0010


// shared from vdp_spr.c unit
extern void logVDPSprite(u16 index);
Expand Down Expand Up @@ -1004,7 +1002,7 @@ void SPR_setAnimAndFrame(Sprite* sprite, s16 anim, s16 frame)
KLog_U3("SPR_setAnimAndFrame: #", getSpriteIndex(sprite), " anim=", anim, " frame=", frame);
#endif // SPR_DEBUG

sprite->status = (sprite->status & ~STATE_ANIMATION_DONE) | NEED_FRAME_UPDATE;
sprite->status |= NEED_FRAME_UPDATE;
}

END_PROFIL(PROFIL_SET_ANIM_FRAME)
Expand Down Expand Up @@ -1040,7 +1038,7 @@ void SPR_setAnim(Sprite* sprite, s16 anim)
KLog_U2_("SPR_setAnim: #", getSpriteIndex(sprite), " anim=", anim, " frame=0");
#endif // SPR_DEBUG

sprite->status = (sprite->status & ~STATE_ANIMATION_DONE) | NEED_FRAME_UPDATE;
sprite->status |= NEED_FRAME_UPDATE;
}

END_PROFIL(PROFIL_SET_ANIM_FRAME)
Expand Down Expand Up @@ -1091,19 +1089,9 @@ void SPR_nextFrame(Sprite* sprite)

if (frameInd >= anim->numFrame)
{
// animation done marker
sprite->status |= STATE_ANIMATION_DONE;

// no loop ?
if (sprite->status & SPR_FLAG_DISABLE_ANIMATION_LOOP)
{
// prevent further animation
SPR_setAutoAnimation(sprite, FALSE);

// frame change event handler defined ? --> call it now so we let user now about STATE_ANIMATION_DONE
if (sprite->onFrameChange)
sprite->onFrameChange(sprite);

// can quit now
END_PROFIL(PROFIL_SET_ANIM_FRAME)
return;
Expand All @@ -1126,7 +1114,7 @@ void SPR_setAutoAnimation(Sprite* sprite, bool value)

if (value)
{
// disabled ? --> reset timer to current frame timer
// currently disabled ? --> reset timer to current frame timer
if (sprite->timer == -1)
sprite->timer = sprite->frame->timer;
}
Expand All @@ -1135,6 +1123,10 @@ void SPR_setAutoAnimation(Sprite* sprite, bool value)
// disable it
sprite->timer = -1;
}

#ifdef SPR_DEBUG
KLog_U2("SPR_setAutoAnimation: #", getSpriteIndex(sprite), " AutoAnimation=", value);
#endif // SPR_DEBUG
}

bool SPR_getAutoAnimation(Sprite* sprite)
Expand All @@ -1152,15 +1144,19 @@ void SPR_setAnimationLoop(Sprite* sprite, bool value)

if (value) sprite->status &= ~SPR_FLAG_DISABLE_ANIMATION_LOOP;
else sprite->status |= SPR_FLAG_DISABLE_ANIMATION_LOOP;
}

#ifdef SPR_DEBUG
KLog_U2("SPR_setAnimationLoop: #", getSpriteIndex(sprite), " loop=", value);
#endif // SPR_DEBUG
}

bool SPR_getAnimationDone(Sprite* sprite)
bool SPR_isAnimationDone(Sprite* sprite)
{
// for debug
checkSpriteValid(sprite, "SPR_getAnimationDone");

return (sprite->status & STATE_ANIMATION_DONE)?TRUE:FALSE;
// last tick on last animation frame
return (sprite->timer <= 1) && (sprite->frameInd == (sprite->animation->numFrame - 1));
}

bool SPR_setVRAMTileIndex(Sprite* sprite, s16 value)
Expand Down
46 changes: 18 additions & 28 deletions src/sprite_eng_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@

#define NEED_UPDATE 0x001F

#define STATE_ANIMATION_DONE 0x0020

// shared from vdp_spr.c unit
extern void logVDPSprite(u16 index);
Expand Down Expand Up @@ -1018,8 +1017,7 @@ void SPR_setAnimAndFrame(Sprite* sprite, s16 anim, s16 frame)
KLog_U3("SPR_setAnimAndFrame: #", getSpriteIndex(sprite), " anim=", anim, " frame=", frame);
#endif // SPR_DEBUG

sprite->status = (sprite->status & ~STATE_ANIMATION_DONE) | NEED_FRAME_UPDATE;
//sprite->status |= NEED_FRAME_UPDATE;
sprite->status |= NEED_FRAME_UPDATE;
}

END_PROFIL(PROFIL_SET_ANIM_FRAME)
Expand Down Expand Up @@ -1055,8 +1053,7 @@ void SPR_setAnim(Sprite* sprite, s16 anim)
KLog_U2_("SPR_setAnim: #", getSpriteIndex(sprite), " anim=", anim, " frame=0");
#endif // SPR_DEBUG

sprite->status = (sprite->status & ~STATE_ANIMATION_DONE) | NEED_FRAME_UPDATE;
//sprite->status |= NEED_FRAME_UPDATE;
sprite->status |= NEED_FRAME_UPDATE;
}

END_PROFIL(PROFIL_SET_ANIM_FRAME)
Expand Down Expand Up @@ -1105,21 +1102,11 @@ void SPR_nextFrame(Sprite* sprite)
const Animation *anim = sprite->animation;
u16 frameInd = sprite->frameInd + 1;

if (frameInd >= anim->numFrame)
if (frameInd >= anim->numFrame)
{
// animation done marker
sprite->status |= STATE_ANIMATION_DONE;

// no loop ?
if (sprite->status & SPR_FLAG_DISABLE_ANIMATION_LOOP)
{
// prevent further animation
SPR_setAutoAnimation(sprite, FALSE);

// frame change event handler defined ? --> call it now so we let user now about STATE_ANIMATION_DONE
if (sprite->onFrameChange)
sprite->onFrameChange(sprite);

// can quit now
END_PROFIL(PROFIL_SET_ANIM_FRAME)
return;
Expand All @@ -1137,29 +1124,34 @@ void SPR_nextFrame(Sprite* sprite)

void SPR_setAutoAnimation(Sprite* sprite, bool value)
{
// for debug
if (!isSpriteValid(sprite, "SPR_setAutoAnimation"))
return;

if (value)
{
// disabled ? --> reset timer to current frame timer
// currently disabled ? --> reset timer to current frame timer
if (sprite->timer == -1)
sprite->timer = sprite->frame->timer;
}
else
{
// enabled ? --> disable it
if (sprite->timer != -1)
sprite->timer = -1;
// disable it
sprite->timer = -1;
}

#ifdef SPR_DEBUG
KLog_U2("SPR_setAutoAnimation: #", getSpriteIndex(sprite), " AutoAnimation=", value);
#endif // SPR_DEBUG

}

bool SPR_getAutoAnimation(Sprite* sprite)
{
return (sprite->timer != -1)?TRUE:FALSE;
// for debug
if (!isSpriteValid(sprite, "SPR_getAutoAnimation"))
return FALSE;

return (sprite->timer != -1)?TRUE:FALSE;
}

void SPR_setAnimationLoop(Sprite* sprite, bool value)
Expand All @@ -1174,18 +1166,16 @@ void SPR_setAnimationLoop(Sprite* sprite, bool value)
#ifdef SPR_DEBUG
KLog_U2("SPR_setAnimationLoop: #", getSpriteIndex(sprite), " loop=", value);
#endif // SPR_DEBUG

}


bool SPR_getAnimationDone(Sprite* sprite)
bool SPR_isAnimationDone(Sprite* sprite)
{
// for debug
if (!isSpriteValid(sprite, "SPR_getAnimationDone"))
if (!isSpriteValid(sprite, "SPR_isAnimationDone"))
return FALSE;

return (sprite->status & STATE_ANIMATION_DONE)?TRUE:FALSE;
//return sprite->frameInd >= sprite->animation->numFrame;
// last tick on last animation frame
return (sprite->timer <= 1) && (sprite->frameInd == (sprite->animation->numFrame - 1));
}


Expand Down

0 comments on commit d5cf952

Please sign in to comment.