Skip to content

Commit

Permalink
Fix animations never terminating
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Mar 20, 2016
1 parent 333a97b commit 8562421
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
26 changes: 6 additions & 20 deletions src/animated.tpp
Expand Up @@ -24,20 +24,6 @@
#include <SDL.h>
#include "animated.hpp"

namespace {
int current_ticks = 0;
}

inline void new_animation_frame()
{
current_ticks = SDL_GetTicks();
}

inline int get_current_animation_tick()
{
return current_ticks;
}

template<typename T, typename T_void_value>
const T animated<T,T_void_value>::void_value_ = T_void_value()();

Expand Down Expand Up @@ -91,7 +77,7 @@ template<typename T, typename T_void_value>
inline void animated<T,T_void_value>::start_animation(int start_time, bool cycles)
{
started_ = true;
last_update_tick_ = current_ticks;
last_update_tick_ = get_current_animation_tick();
acceleration_ = 1.0; //assume acceleration is 1, this will be fixed at first update_last_draw_time
start_tick_ = last_update_tick_ +
static_cast<int>(( starting_frame_time_ - start_time)/acceleration_);
Expand All @@ -115,9 +101,9 @@ inline void animated<T,T_void_value>::update_last_draw_time(double acceleration)
}
if (!started_ && start_tick_ != 0) {
// animation is paused
start_tick_ += current_ticks - last_update_tick_;
start_tick_ += get_current_animation_tick() - last_update_tick_;
}
last_update_tick_ = current_ticks;
last_update_tick_ = get_current_animation_tick();
if (force_next_update_) {
force_next_update_ = false;
return;
Expand Down Expand Up @@ -162,7 +148,7 @@ inline bool animated<T,T_void_value>::need_update() const
if (!started_ && start_tick_ == 0) {
return false;
}
if (current_ticks >
if (get_current_animation_tick() >
static_cast<int>(get_current_frame_end_time() /
acceleration_ + start_tick_)) {
return true;
Expand All @@ -182,7 +168,7 @@ inline bool animated<T,T_void_value>::animation_finished_potential() const
if (cycles_ ) {
return true;
}
if (tick_to_time(current_ticks) > get_end_time()) {
if (tick_to_time(get_current_animation_tick()) > get_end_time()) {
return true;
}

Expand Down Expand Up @@ -215,7 +201,7 @@ inline int animated<T,T_void_value>::get_animation_time_potential() const
return starting_frame_time_;
}

return tick_to_time(current_ticks);
return tick_to_time(get_current_animation_tick());
}

template<typename T, typename T_void_value>
Expand Down
15 changes: 15 additions & 0 deletions src/animated_game.cpp
Expand Up @@ -26,3 +26,18 @@ template class animated< image::locator >;
template class animated< std::string >;
template class animated< unit_frame >;

// Put these here to ensure that there's only
// one instance of the current_ticks variable
namespace {
int current_ticks = 0;
}

void new_animation_frame()
{
current_ticks = SDL_GetTicks();
}

int get_current_animation_tick()
{
return current_ticks;
}

0 comments on commit 8562421

Please sign in to comment.