Skip to content

Commit

Permalink
Animation: Fix times_played for single-frame back_forth animations
Browse files Browse the repository at this point in the history
This commit also replaces the `additional_data` variable with one called
`reverse_playback`, which is more descriptive for how it is used.
  • Loading branch information
dorkster committed Oct 1, 2024
1 parent 1469521 commit d512b6e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Engine fixes:
* Fixed potential crash if a hazard was created before the map loaded.
* Fixed infinite loop caused by trying to move an entity before the map loaded.
* Fix case where sounds may be unloaded prematurely if they were playing during certain sitations.
* Fix single-frame "back_forth" animations taking an extra frame to complete.
* Android: Fix 'Flare' directory not being automatically created.
* Android: Added a dialog to direct the player to the wiki page for installing if no game data is found.

Expand Down
31 changes: 14 additions & 17 deletions src/Animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Animation::Animation(const std::string &_name, const std::string &_type, Animati
, cur_frame_duration(0)
, cur_frame_index_f(0)
, max_kinds(0)
, additional_data(0)
, reverse_playback(false)
, times_played(0)
, gfx()
, render_offset()
Expand Down Expand Up @@ -127,21 +127,15 @@ void Animation::setup(unsigned short _frames, unsigned short _duration, unsigned

if (!frames.empty()) number_frames = static_cast<unsigned short>(frames.back()+1);

if (type == ANIMTYPE_PLAY_ONCE) {
additional_data = 0;
}
else if (type == ANIMTYPE_LOOPED) {
additional_data = 0;
}
else if (type == ANIMTYPE_BACK_FORTH) {
if (type == ANIMTYPE_BACK_FORTH) {
number_frames = static_cast<unsigned short>(2 * number_frames);
additional_data = 1;
}
cur_frame = 0;
cur_frame_index = 0;
cur_frame_index_f = 0;
max_kinds = _maxkinds;
times_played = 0;
reverse_playback = false;

active_frames.push_back(static_cast<unsigned short>(number_frames-1)/2);

Expand Down Expand Up @@ -197,21 +191,24 @@ void Animation::advanceFrame() {

case ANIMTYPE_BACK_FORTH:

if (additional_data == 1) {
if (!reverse_playback) {
if (cur_frame_index < last_base_index) {
cur_frame_index_f += speed;
cur_frame_index = static_cast<unsigned short>(cur_frame_index_f);
}
else
additional_data = -1;
else {
reverse_playback = true;
if (frame_count == 1)
times_played++;
}
}
else if (additional_data == -1) {
else if (reverse_playback) {
if (cur_frame_index > 0) {
cur_frame_index_f -= speed;
cur_frame_index = static_cast<unsigned short>(cur_frame_index_f);
}
else {
additional_data = 1;
reverse_playback = false;
times_played++;
}
}
Expand Down Expand Up @@ -250,7 +247,7 @@ void Animation::reset() {
cur_frame_index = 0;
cur_frame_index_f = 0;
times_played = 0;
additional_data = 1;
reverse_playback = false;
elapsed_frames = 0;
active_frame_triggered = false;
}
Expand All @@ -260,7 +257,7 @@ bool Animation::syncTo(const Animation *other) {
cur_frame_index = other->cur_frame_index;
cur_frame_index_f = other->cur_frame_index_f;
times_played = other->times_played;
additional_data = other->additional_data;
reverse_playback = other->reverse_playback;
elapsed_frames = other->elapsed_frames;

if (cur_frame_index >= frames.size()) {
Expand Down Expand Up @@ -356,7 +353,7 @@ bool Animation::isCompleted() {
unsigned short Animation::getLastFrameIndex(const short &frame) {
if (frames.empty() || frame < 0) return 0;

if (type == ANIMTYPE_BACK_FORTH && additional_data == -1) {
if (type == ANIMTYPE_BACK_FORTH && reverse_playback) {
// since the animation is advancing backwards here, the first frame index is actually the last
for (unsigned short i=0; i<frames.size(); i++) {
if (frames[i] == frame) return i;
Expand Down
5 changes: 1 addition & 4 deletions src/Animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ class Animation {

unsigned short max_kinds;

short additional_data; // additional state depending on type:
// if type == BACK_FORTH then it is 1 for advancing, and -1 for going back, 0 at the end
// if type == LOOPED, then it is the number of loops to be played.
// if type == PLAY_ONCE or NONE, this has no meaning.
bool reverse_playback; // only for type == BACK_FORTH

short times_played; // how often this animation was played (loop counter for type LOOPED)

Expand Down
2 changes: 1 addition & 1 deletion src/Version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ FLARE. If not, see http://www.gnu.org/licenses/

#include <SDL.h>

Version VersionInfo::ENGINE(1, 14, 63);
Version VersionInfo::ENGINE(1, 14, 64);
Version VersionInfo::MIN(0, 0, 0);
Version VersionInfo::MAX(USHRT_MAX, USHRT_MAX, USHRT_MAX);

Expand Down

0 comments on commit d512b6e

Please sign in to comment.