Cleaned up animated.tpp/.hpp formatting

This commit is contained in:
Charles Dang 2017-05-11 21:13:36 +11:00
parent 6efc5ae090
commit 69d4b93bef
2 changed files with 216 additions and 172 deletions

View file

@ -19,37 +19,36 @@
#pragma once
#include <string>
#include <map>
#include <string>
#include <vector>
void new_animation_frame();
int get_current_animation_tick();
template<typename T>
class void_value
{
public:
const T operator()() const { return T(); }
public:
const T operator()() const
{
return T();
}
};
template<typename T, typename T_void_value=void_value<T> >
template<typename T, typename T_void_value = void_value<T>>
class animated
{
public:
animated(int start_time = 0);
virtual ~animated() {}
animated(int start_time=0);
virtual ~animated(){}
typedef std::pair<int,T> frame_description;
typedef std::vector<frame_description> anim_description;
animated(const std::vector<frame_description> &cfg, int start_time = 0,bool force_change =false);
typedef std::pair<int, T> frame_description;
typedef std::vector<frame_description> anim_description;
animated(const std::vector<frame_description>& cfg, int start_time = 0, bool force_change = false);
/** Adds a frame to an animation. */
void add_frame(int duration, const T& value,bool force_change =false);
void add_frame(int duration, const T& value, bool force_change = false);
/**
* Starts an animation cycle.
@ -57,13 +56,21 @@ public:
* The first frame of the animation to start may be set to any value by
* using a start_time different to 0.
*/
void start_animation(int start_time, bool cycles=false);
void pause_animation(){ started_ =false;}
void restart_animation(){if(start_tick_) started_ = true;}
void start_animation(int start_time, bool cycles = false);
void pause_animation()
{
started_ = false;
}
void restart_animation()
{
if(start_tick_)
started_ = true;
}
int get_begin_time() const;
int get_end_time() const;
void set_begin_time(int new_begin_time);
void set_begin_time(int new_begin_time);
int time_to_tick(int animation_time) const;
int tick_to_time(int animation_tick) const;
@ -71,7 +78,10 @@ public:
void update_last_draw_time(double acceleration = 0);
bool need_update() const;
bool cycles() const {return cycles_;}
bool cycles() const
{
return cycles_;
}
/** Returns true if the current animation was finished. */
bool animation_finished() const;
@ -90,27 +100,41 @@ public:
const T& get_frame(size_t n) const;
const T& get_last_frame() const;
size_t get_frames_count() const;
void force_change() {does_not_change_ = false ; }
bool does_not_change() const {return does_not_change_;}
static const T void_value_; //MSVC: the frame constructor below requires this to be public
void force_change()
{
does_not_change_ = false;
}
bool does_not_change() const
{
return does_not_change_;
}
static const T void_value_; // MSVC: the frame constructor below requires this to be public
protected:
friend class unit_animation;
friend class unit_animation;
int starting_frame_time_;
void remove_frames_until(int starting_time);
void set_end_time(int ending_time);
void remove_frames_until(int starting_time);
void set_end_time(int ending_time);
private:
struct frame
{
frame(int duration, const T& value, int start_time)
: duration_(duration)
, value_(value)
, start_time_(start_time)
{
}
frame(int duration , const T& value,int start_time) :
duration_(duration),value_(value),start_time_(start_time)
{}
frame():
duration_(0),value_(void_value_),start_time_(0)
{}
frame()
: duration_(0)
, value_(void_value_)
, start_time_(0)
{
}
// Represents the timestamp of the frame start
int duration_;
@ -118,7 +142,7 @@ private:
int start_time_;
};
bool does_not_change_; // Optimization for 1-frame permanent animations
bool does_not_change_; // Optimization for 1-frame permanent animations
bool started_;
bool force_next_update_;
std::vector<frame> frames_;
@ -129,7 +153,7 @@ private:
double acceleration_;
int last_update_tick_;
int current_frame_key_;
};
// NOTE: this needs to be down here or the templates won't build.
#include "animated.tpp"

View file

@ -23,152 +23,139 @@
#include "animated.hpp"
template<typename T, typename T_void_value>
const T animated<T,T_void_value>::void_value_ = T_void_value()();
const T animated<T, T_void_value>::void_value_ = T_void_value()();
template<typename T, typename T_void_value>
inline animated<T,T_void_value>::animated(int start_time) :
starting_frame_time_(start_time),
does_not_change_(true),
started_(false),
force_next_update_(false),
frames_(),
start_tick_(0),
cycles_(false),
acceleration_(1),
last_update_tick_(0),
current_frame_key_(0)
inline animated<T, T_void_value>::animated(int start_time)
: starting_frame_time_(start_time)
, does_not_change_(true)
, started_(false)
, force_next_update_(false)
, frames_()
, start_tick_(0)
, cycles_(false)
, acceleration_(1)
, last_update_tick_(0)
, current_frame_key_(0)
{
}
template<typename T, typename T_void_value>
inline animated<T,T_void_value>::animated(const std::vector<std::pair<int,T> > &cfg, int start_time, bool force_change ):
starting_frame_time_(start_time),
does_not_change_(true),
started_(false),
force_next_update_(false),
frames_(),
start_tick_(0),
cycles_(false),
acceleration_(1),
last_update_tick_(0),
current_frame_key_(0)
inline animated<T, T_void_value>::animated(const std::vector<std::pair<int, T>>& cfg, int start_time, bool force_change)
: starting_frame_time_(start_time)
, does_not_change_(true)
, started_(false)
, force_next_update_(false)
, frames_()
, start_tick_(0)
, cycles_(false)
, acceleration_(1)
, last_update_tick_(0)
, current_frame_key_(0)
{
typename std::vector< std::pair<int,T> >::const_iterator itor = cfg.begin();
for (; itor != cfg.end(); ++itor) {
add_frame(itor->first,itor->second,force_change);
typename std::vector<std::pair<int, T>>::const_iterator itor = cfg.begin();
for(; itor != cfg.end(); ++itor) {
add_frame(itor->first, itor->second, force_change);
}
}
template<typename T, typename T_void_value>
inline void animated<T,T_void_value>::add_frame(int duration, const T& value,bool force_change)
inline void animated<T, T_void_value>::add_frame(int duration, const T& value, bool force_change)
{
// NOTE: We cannot use emplace_back here, because the value may be a reference into the same vector,
// which case emplace_back could invalidate it before the new frame is constructed.
if (frames_.empty() ) {
does_not_change_=!force_change;
frames_.push_back(frame(duration,value,starting_frame_time_));
if(frames_.empty()) {
does_not_change_ = !force_change;
frames_.push_back(frame(duration, value, starting_frame_time_));
} else {
does_not_change_=false;
frames_.push_back(frame(duration,value,frames_.back().start_time_+frames_.back().duration_));
does_not_change_ = false;
frames_.push_back(frame(duration, value, frames_.back().start_time_ + frames_.back().duration_));
}
}
template<typename T, typename T_void_value>
inline void animated<T,T_void_value>::start_animation(int start_time, bool cycles)
inline void animated<T, T_void_value>::start_animation(int start_time, bool cycles)
{
started_ = true;
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_);
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_);
cycles_ = cycles;
if (acceleration_ <= 0) {
if(acceleration_ <= 0) {
acceleration_ = 1;
}
current_frame_key_= 0;
current_frame_key_ = 0;
force_next_update_ = !frames_.empty();
}
template<typename T, typename T_void_value>
inline void animated<T,T_void_value>::update_last_draw_time(double acceleration)
inline void animated<T, T_void_value>::update_last_draw_time(double acceleration)
{
if (acceleration > 0 && acceleration_ != acceleration) {
if(acceleration > 0 && acceleration_ != acceleration) {
int tmp = tick_to_time(last_update_tick_);
acceleration_ = acceleration;
start_tick_ = last_update_tick_ +
static_cast<int>(( starting_frame_time_ - tmp)/acceleration_);
start_tick_ = last_update_tick_ + static_cast<int>((starting_frame_time_ - tmp) / acceleration_);
}
if (!started_ && start_tick_ != 0) {
if(!started_ && start_tick_ != 0) {
// animation is paused
start_tick_ += get_current_animation_tick() - last_update_tick_;
}
last_update_tick_ = get_current_animation_tick();
if (force_next_update_) {
if(force_next_update_) {
force_next_update_ = false;
return;
}
if (does_not_change_) {
if(does_not_change_) {
return;
}
// Always update last_update_tick_, for the animation_time functions to work.
if (!started_) {
if(!started_) {
return;
}
if (frames_.empty()) {
if(frames_.empty()) {
does_not_change_ = true;
return;
}
if (cycles_) {
while(get_animation_time() > get_end_time()){ // cut extra time
start_tick_ += std::max<int>(static_cast<int>(get_animation_duration()/acceleration_),1);
if(cycles_) {
while(get_animation_time() > get_end_time()) { // cut extra time
start_tick_ += std::max<int>(static_cast<int>(get_animation_duration() / acceleration_), 1);
current_frame_key_ = 0;
}
}
if (get_current_frame_end_time() < get_animation_time() && // catch up
get_current_frame_end_time() < get_end_time()) {// don't go after the end
if(get_current_frame_end_time() < get_animation_time() && // catch up
get_current_frame_end_time() < get_end_time()) { // don't go after the end
current_frame_key_++;
}
}
template<typename T, typename T_void_value>
inline bool animated<T,T_void_value>::need_update() const
inline bool animated<T, T_void_value>::need_update() const
{
if (force_next_update_) {
if(force_next_update_) {
return true;
}
if (does_not_change_) {
return false;
}
if (frames_.empty()) {
return false;
}
if (!started_ && start_tick_ == 0) {
return false;
}
if (get_current_animation_tick() >
static_cast<int>(get_current_frame_end_time() /
acceleration_ + start_tick_)) {
return true;
}
return false;
}
template<typename T, typename T_void_value>
inline bool animated<T,T_void_value>::animation_finished_potential() const
{
if (frames_.empty()) {
return true;
if(does_not_change_) {
return false;
}
if (!started_ && start_tick_ == 0) {
return true;
if(frames_.empty()) {
return false;
}
if (cycles_ ) {
return true;
if(!started_ && start_tick_ == 0) {
return false;
}
if (tick_to_time(get_current_animation_tick()) > get_end_time()) {
if(get_current_animation_tick() > static_cast<int>(get_current_frame_end_time() / acceleration_ + start_tick_)) {
return true;
}
@ -176,18 +163,21 @@ inline bool animated<T,T_void_value>::animation_finished_potential() const
}
template<typename T, typename T_void_value>
inline bool animated<T,T_void_value>::animation_finished() const
inline bool animated<T, T_void_value>::animation_finished_potential() const
{
if (frames_.empty()) {
if(frames_.empty()) {
return true;
}
if (!started_ && start_tick_ == 0) {
if(!started_ && start_tick_ == 0) {
return true;
}
if (cycles_) {
if(cycles_) {
return true;
}
if (get_animation_time() > get_end_time()) {
if(tick_to_time(get_current_animation_tick()) > get_end_time()) {
return true;
}
@ -195,9 +185,31 @@ inline bool animated<T,T_void_value>::animation_finished() const
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_animation_time_potential() const
inline bool animated<T, T_void_value>::animation_finished() const
{
if (!started_ && start_tick_ == 0 ) {
if(frames_.empty()) {
return true;
}
if(!started_ && start_tick_ == 0) {
return true;
}
if(cycles_) {
return true;
}
if(get_animation_time() > get_end_time()) {
return true;
}
return false;
}
template<typename T, typename T_void_value>
inline int animated<T, T_void_value>::get_animation_time_potential() const
{
if(!started_ && start_tick_ == 0) {
return starting_frame_time_;
}
@ -205,9 +217,9 @@ inline int animated<T,T_void_value>::get_animation_time_potential() const
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_animation_time() const
inline int animated<T, T_void_value>::get_animation_time() const
{
if (!started_ && start_tick_ == 0 ) {
if(!started_ && start_tick_ == 0) {
return starting_frame_time_;
}
@ -215,166 +227,174 @@ inline int animated<T,T_void_value>::get_animation_time() const
}
template<typename T, typename T_void_value>
inline void animated<T,T_void_value>::set_animation_time(int time)
inline void animated<T, T_void_value>::set_animation_time(int time)
{
start_tick_ = last_update_tick_ +
static_cast<int>((starting_frame_time_ - time) / acceleration_);
start_tick_ = last_update_tick_ + static_cast<int>((starting_frame_time_ - time) / acceleration_);
current_frame_key_= 0;
current_frame_key_ = 0;
force_next_update_ = true;
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_animation_duration() const
inline int animated<T, T_void_value>::get_animation_duration() const
{
return get_end_time() - get_begin_time();
}
template<typename T, typename T_void_value>
inline const T& animated<T,T_void_value>::get_current_frame() const
inline const T& animated<T, T_void_value>::get_current_frame() const
{
if (frames_.empty()) {
if(frames_.empty()) {
return void_value_;
}
return frames_[current_frame_key_].value_;
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_current_frame_begin_time() const
inline int animated<T, T_void_value>::get_current_frame_begin_time() const
{
if (frames_.empty()) {
if(frames_.empty()) {
return starting_frame_time_;
}
return frames_[current_frame_key_].start_time_;
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_current_frame_end_time() const
inline int animated<T, T_void_value>::get_current_frame_end_time() const
{
if (frames_.empty()) {
if(frames_.empty()) {
return starting_frame_time_;
}
return get_current_frame_begin_time() +get_current_frame_duration();
return get_current_frame_begin_time() + get_current_frame_duration();
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_current_frame_duration() const
inline int animated<T, T_void_value>::get_current_frame_duration() const
{
if (frames_.empty()) {
if(frames_.empty()) {
return 0;
}
return frames_[current_frame_key_].duration_;
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_current_frame_time() const
template<typename T, typename T_void_value>
inline int animated<T, T_void_value>::get_current_frame_time() const
{
if (frames_.empty()) {
if(frames_.empty()) {
return 0;
}
//FIXME: get_animation_time() use acceleration but get_current_frame_begin_time() doesn't ?
return std::max<int>(0,get_animation_time() - get_current_frame_begin_time());
// FIXME: get_animation_time() use acceleration but get_current_frame_begin_time() doesn't ?
return std::max<int>(0, get_animation_time() - get_current_frame_begin_time());
}
template<typename T, typename T_void_value>
inline const T& animated<T,T_void_value>::get_first_frame() const
inline const T& animated<T, T_void_value>::get_first_frame() const
{
if (frames_.empty()) {
if(frames_.empty()) {
return void_value_;
}
return frames_[0].value_;
}
template<typename T, typename T_void_value>
inline const T& animated<T,T_void_value>::get_frame(size_t n) const
inline const T& animated<T, T_void_value>::get_frame(size_t n) const
{
if (n >= frames_.size()) {
if(n >= frames_.size()) {
return void_value_;
}
return frames_[n].value_;
}
template<typename T, typename T_void_value>
inline const T& animated<T,T_void_value>::get_last_frame() const
inline const T& animated<T, T_void_value>::get_last_frame() const
{
if (frames_.empty()) {
if(frames_.empty()) {
return void_value_;
}
return frames_.back().value_;
}
template<typename T, typename T_void_value>
inline size_t animated<T,T_void_value>::get_frames_count() const
inline size_t animated<T, T_void_value>::get_frames_count() const
{
return frames_.size();
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_begin_time() const
inline int animated<T, T_void_value>::get_begin_time() const
{
return starting_frame_time_;
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::time_to_tick(int animation_time) const
inline int animated<T, T_void_value>::time_to_tick(int animation_time) const
{
if (!started_ && start_tick_ == 0) {
if(!started_ && start_tick_ == 0) {
return 0;
}
return start_tick_ + static_cast<int>((animation_time-starting_frame_time_) / acceleration_);
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::tick_to_time(int animation_tick) const
{
if (!started_ && start_tick_ == 0) {
return 0;
}
return static_cast<int>(
(static_cast<double>(animation_tick - start_tick_) *
acceleration_) + starting_frame_time_);
return start_tick_ + static_cast<int>((animation_time - starting_frame_time_) / acceleration_);
}
template<typename T, typename T_void_value>
inline int animated<T,T_void_value>::get_end_time() const
inline int animated<T, T_void_value>::tick_to_time(int animation_tick) const
{
if (frames_.empty()) {
if(!started_ && start_tick_ == 0) {
return 0;
}
return static_cast<int>((static_cast<double>(animation_tick - start_tick_) * acceleration_) + starting_frame_time_);
}
template<typename T, typename T_void_value>
inline int animated<T, T_void_value>::get_end_time() const
{
if(frames_.empty()) {
return starting_frame_time_;
}
return frames_.back().start_time_ + frames_.back().duration_;
}
template<typename T, typename T_void_value>
void animated<T,T_void_value>::remove_frames_until(int new_starting_time)
void animated<T, T_void_value>::remove_frames_until(int new_starting_time)
{
while (starting_frame_time_ < new_starting_time && !frames_.empty()) {
while(starting_frame_time_ < new_starting_time && !frames_.empty()) {
starting_frame_time_ += frames_[0].duration_;
frames_.erase(frames_.begin());
}
}
template<typename T, typename T_void_value>
inline void animated<T,T_void_value>::set_end_time(int new_ending_time)
inline void animated<T, T_void_value>::set_end_time(int new_ending_time)
{
int last_start_time = starting_frame_time_;
typename std::vector<frame>::iterator current_frame = frames_.begin();
while (last_start_time < new_ending_time && current_frame != frames_.end()) {
while(last_start_time < new_ending_time && current_frame != frames_.end()) {
last_start_time += current_frame->duration_;
++current_frame;
}
// at this point last_start_time is set to the beginning of the first frame past the end
// or set to frames_.end()
frames_.erase(current_frame,frames_.end());
frames_.erase(current_frame, frames_.end());
frames_.back().duration_ += new_ending_time - last_start_time;
}
template<typename T, typename T_void_value>
inline void animated<T,T_void_value>::set_begin_time(int new_begin_time)
inline void animated<T, T_void_value>::set_begin_time(int new_begin_time)
{
const int variation = new_begin_time - starting_frame_time_;
starting_frame_time_ += variation;
for (typename std::vector<frame>::iterator itor = frames_.begin(); itor != frames_.end() ; ++itor) {
for(typename std::vector<frame>::iterator itor = frames_.begin(); itor != frames_.end(); ++itor) {
itor->start_time_ += variation;
}
}