Skip to content

Commit de4c620

Browse files
committed
Fixed SPR_isAnimationDone(.)
1 parent 4475553 commit de4c620

File tree

6 files changed

+35
-30
lines changed

6 files changed

+35
-30
lines changed

inc/sprite_eng.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
#define SPR_FLAG_INSERT_HEAD 0x4000
4545
/**
4646
* \brief
47-
* Disable animation auto loop..<br>
48-
* By default animation always restart fater the last frame has been played.
47+
* Disable animation auto loop.<br>
48+
* By default animation always restart after the last frame has been played.
4949
* This flag prevent the animation to restart and so the animation end on the last frame forever (see #SPR_getAnimationDone(..))
5050
*/
5151
#define SPR_FLAG_DISABLE_ANIMATION_LOOP 0x2000
@@ -844,8 +844,8 @@ bool SPR_getAutoAnimation(Sprite* sprite);
844844
void SPR_setAnimationLoop(Sprite* sprite, bool value);
845845
/**
846846
* \brief
847-
* Return TRUE if the sprite reached the last frame of the current animation.<br>
848-
* When auto animation is enabled (see SPR_setAutoAnimation(..)) the function returns TRUE only when we reach
847+
* Returns TRUE if the sprite reached the end of the current animation.<br>
848+
* When auto animation is enabled (see SPR_setAutoAnimation(..)) the function returns TRUE only when we reached
849849
* the last *tick* of the last animation frame.<br>
850850
* When auto animation is disabled the function returns TRUE as soon we are on last animation frame.
851851
*

inc/sprite_eng_legacy.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
#define SPR_FLAG_INSERT_HEAD 0x4000
4545
/**
4646
* \brief
47-
* Disable animation auto loop..<br>
48-
* By default animation always restart fater the last frame has been played.
47+
* Disable animation auto loop.<br>
48+
* By default animation always restart after the last frame has been played.
4949
* This flag prevent the animation to restart and so the animation end on the last frame forever (see #SPR_getAnimationDone(..))
5050
*/
5151
#define SPR_FLAG_DISABLE_ANIMATION_LOOP 0x2000
@@ -837,8 +837,8 @@ bool SPR_getAutoAnimation(Sprite* sprite);
837837
void SPR_setAnimationLoop(Sprite* sprite, bool value);
838838
/**
839839
* \brief
840-
* Return TRUE if the sprite reached the last frame of the current animation.<br>
841-
* When auto animation is enabled (see SPR_setAutoAnimation(..)) the function returns TRUE only when we reach
840+
* Returns TRUE if the sprite reached the end of the current animation.<br>
841+
* When auto animation is enabled (see SPR_setAutoAnimation(..)) the function returns TRUE only when we reached
842842
* the last *tick* of the last animation frame.<br>
843843
* When auto animation is disabled the function returns TRUE as soon we are on last animation frame.
844844
*

lib/libmd.a

-1000 KB
Binary file not shown.

lib/libmd_debug.a

7.35 KB
Binary file not shown.

src/sprite_eng.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747

4848
#define NEED_UPDATE 0x000F
4949

50+
#define STATE_ANIMATION_DONE 0x0010
51+
52+
5053

5154
// shared from vdp_spr.c unit
5255
extern void logVDPSprite(u16 index);
@@ -1147,6 +1150,8 @@ void SPR_nextFrame(Sprite* sprite)
11471150

11481151
// loop
11491152
frameInd = anim->loop;
1153+
// set animation done state (allows SPR_isAnimationDone(..) to correctly report state when called from frame change callback)
1154+
sprite->status |= STATE_ANIMATION_DONE;
11501155
}
11511156

11521157
// set new frame
@@ -1203,8 +1208,10 @@ bool SPR_isAnimationDone(Sprite* sprite)
12031208
// for debug
12041209
checkSpriteValid(sprite, "SPR_isAnimationDone");
12051210

1206-
// last tick on last animation frame (use <= 1 so it also works when AutoAnimation is disabled)
1207-
return (sprite->timer <= 1) && (sprite->frameInd == (sprite->animation->numFrame - 1));
1211+
// check for animation done state (mainly used for frame change callback) or
1212+
return (sprite->status & STATE_ANIMATION_DONE) ||
1213+
// if we are on last tick from last frame (if auto animation is disabled then only test for last frame)
1214+
((sprite->frameInd == (sprite->animation->numFrame - 1)) && ((sprite->timer == 1) || (sprite->timer == -1)));
12081215
}
12091216

12101217
bool SPR_setVRAMTileIndex(Sprite* sprite, s16 value)
@@ -1937,6 +1944,9 @@ static u16 updateFrame(Sprite* sprite, u16 status)
19371944

19381945
// set frame
19391946
sprite->frame = frame;
1947+
// init timer for this frame *before* frame change callback so it can modify change it if needed.
1948+
if (SPR_getAutoAnimation(sprite))
1949+
sprite->timer = frame->timer;
19401950

19411951
// frame change event handler defined ? --> call it
19421952
if (sprite->onFrameChange)
@@ -1945,16 +1955,6 @@ static u16 updateFrame(Sprite* sprite, u16 status)
19451955
sprite->status = status;
19461956
sprite->onFrameChange(sprite);
19471957
status = sprite->status;
1948-
1949-
// init timer for this frame *after* callback call and only if auto animation is enabled and timer was not manually changed in callback
1950-
if (sprite->timer == 0)
1951-
sprite->timer = frame->timer;
1952-
}
1953-
else
1954-
{
1955-
// init timer for this frame *after* callback call to allow SPR_isAnimationDone(..) to correctly report TRUE when animation is done in the callbacnk
1956-
if (SPR_getAutoAnimation(sprite))
1957-
sprite->timer = frame->timer;
19581958
}
19591959

19601960
// require tile data upload
@@ -1964,8 +1964,8 @@ static u16 updateFrame(Sprite* sprite, u16 status)
19641964
if (status & SPR_FLAG_AUTO_VISIBILITY)
19651965
status |= NEED_VISIBILITY_UPDATE;
19661966

1967-
// frame update done
1968-
status &= ~NEED_FRAME_UPDATE;
1967+
// frame update done, also clear ANIMATION_DONE state
1968+
status &= ~(NEED_FRAME_UPDATE | STATE_ANIMATION_DONE);
19691969

19701970
END_PROFIL(PROFIL_UPDATE_FRAME)
19711971

src/sprite_eng_legacy.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949

5050
#define NEED_UPDATE 0x001F
5151

52+
#define STATE_ANIMATION_DONE 0x0020
53+
5254

5355
// shared from vdp_spr.c unit
5456
extern void logVDPSprite(u16 index);
@@ -1161,6 +1163,8 @@ void SPR_nextFrame(Sprite* sprite)
11611163

11621164
// loop
11631165
frameInd = anim->loop;
1166+
// set animation done state (allows SPR_isAnimationDone(..) to correctly report state when called from frame change callback)
1167+
sprite->status |= STATE_ANIMATION_DONE;
11641168
}
11651169

11661170
// set new frame
@@ -1221,8 +1225,10 @@ bool SPR_isAnimationDone(Sprite* sprite)
12211225
if (!isSpriteValid(sprite, "SPR_isAnimationDone"))
12221226
return FALSE;
12231227

1224-
// last tick on last animation frame (use <= 1 so it also works when AutoAnimation is disabled)
1225-
return (sprite->timer <= 1) && (sprite->frameInd == (sprite->animation->numFrame - 1));
1228+
// check for animation done state (mainly used for frame change callback) or
1229+
return (sprite->status & STATE_ANIMATION_DONE) ||
1230+
// if we are on last tick from last frame (if auto animation is disabled then only test for last frame)
1231+
((sprite->frameInd == (sprite->animation->numFrame - 1)) && ((sprite->timer == 1) || (sprite->timer == -1)));
12261232
}
12271233

12281234

@@ -1895,6 +1901,9 @@ static u16 updateFrame(Sprite* sprite, u16 status)
18951901
sprite->lastNumSprite = currentNumSprite;
18961902
// set frame
18971903
sprite->frame = frame;
1904+
// init timer for this frame *before* frame change callback so it can modify change it if needed.
1905+
if (SPR_getAutoAnimation(sprite))
1906+
sprite->timer = frame->timer;
18981907

18991908
// frame change event handler defined ? --> call it
19001909
if (sprite->onFrameChange)
@@ -1905,19 +1914,15 @@ static u16 updateFrame(Sprite* sprite, u16 status)
19051914
status = sprite->status;
19061915
}
19071916

1908-
// init timer for this frame *after* calling callback (this allows SPR_isAnimationDone(..) to correctly report TRUE when animation is done)
1909-
if (SPR_getAutoAnimation(sprite))
1910-
sprite->timer = frame->timer;
1911-
19121917
// require tile data upload
19131918
if (status & SPR_FLAG_AUTO_TILE_UPLOAD)
19141919
status |= NEED_TILES_UPLOAD;
19151920
// require visibility update
19161921
if (status & SPR_FLAG_AUTO_VISIBILITY)
19171922
status |= NEED_VISIBILITY_UPDATE;
19181923

1919-
// frame update done
1920-
status &= ~NEED_FRAME_UPDATE;
1924+
// frame update done, also clear ANIMATION_DONE state
1925+
status &= ~(NEED_FRAME_UPDATE | STATE_ANIMATION_DONE);
19211926

19221927
END_PROFIL(PROFIL_UPDATE_FRAME)
19231928

0 commit comments

Comments
 (0)