Skip to content

Commit

Permalink
Animated: simplified template stuff
Browse files Browse the repository at this point in the history
Just... why was T_void_value a thing.
  • Loading branch information
Vultraz committed Oct 19, 2019
1 parent 4a5d34d commit 773cc21
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 70 deletions.
10 changes: 0 additions & 10 deletions src/animated.hpp
Expand Up @@ -27,16 +27,6 @@ void new_animation_frame();
int get_current_animation_tick();

template<typename T>
class void_value
{
public:
const T operator()() const
{
return T();
}
};

template<typename T, typename T_void_value = void_value<T>>
class animated
{
public:
Expand Down
120 changes: 60 additions & 60 deletions src/animated.tpp
Expand Up @@ -17,11 +17,11 @@
* Templates related to animations.
*/

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

template<typename T, typename T_void_value>
inline animated<T, T_void_value>::animated(int start_time)
template<typename T>
inline animated<T>::animated(int start_time)
: starting_frame_time_(start_time)
, does_not_change_(true)
, started_(false)
Expand All @@ -36,8 +36,8 @@ inline animated<T, T_void_value>::animated(int start_time)
{
}

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)
template<typename T>
inline animated<T>::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)
Expand All @@ -56,8 +56,8 @@ inline animated<T, T_void_value>::animated(const std::vector<std::pair<int, T>>&
}
}

template<typename T, typename T_void_value>
inline void animated<T, T_void_value>::add_frame(int duration, const T& value, bool force_change)
template<typename T>
inline void animated<T>::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.
Expand All @@ -70,8 +70,8 @@ inline void animated<T, T_void_value>::add_frame(int duration, const T& value, b
}
}

template<typename T, typename T_void_value>
inline void animated<T, T_void_value>::start_animation(int start_time, bool cycles)
template<typename T>
inline void animated<T>::start_animation(int start_time, bool cycles)
{
started_ = true;
last_update_tick_ = get_current_animation_tick();
Expand All @@ -86,8 +86,8 @@ inline void animated<T, T_void_value>::start_animation(int start_time, bool cycl
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)
template<typename T>
inline void animated<T>::update_last_draw_time(double acceleration)
{
if(acceleration > 0 && acceleration_ != acceleration) {
int tmp = tick_to_time(last_update_tick_);
Expand Down Expand Up @@ -133,8 +133,8 @@ inline void animated<T, T_void_value>::update_last_draw_time(double acceleration
}
}

template<typename T, typename T_void_value>
inline bool animated<T, T_void_value>::need_update() const
template<typename T>
inline bool animated<T>::need_update() const
{
if(force_next_update_) {
return true;
Expand All @@ -159,8 +159,8 @@ inline bool animated<T, T_void_value>::need_update() const
return false;
}

template<typename T, typename T_void_value>
inline bool animated<T, T_void_value>::animation_finished_potential() const
template<typename T>
inline bool animated<T>::animation_finished_potential() const
{
if(frames_.empty()) {
return true;
Expand All @@ -181,8 +181,8 @@ inline bool animated<T, T_void_value>::animation_finished_potential() const
return false;
}

template<typename T, typename T_void_value>
inline bool animated<T, T_void_value>::animation_finished() const
template<typename T>
inline bool animated<T>::animation_finished() const
{
if(frames_.empty()) {
return true;
Expand All @@ -203,8 +203,8 @@ inline bool animated<T, T_void_value>::animation_finished() const
return false;
}

template<typename T, typename T_void_value>
inline int animated<T, T_void_value>::get_animation_time_potential() const
template<typename T>
inline int animated<T>::get_animation_time_potential() const
{
if(!started_ && start_tick_ == 0) {
return starting_frame_time_;
Expand All @@ -213,8 +213,8 @@ inline int animated<T, T_void_value>::get_animation_time_potential() const
return tick_to_time(get_current_animation_tick());
}

template<typename T, typename T_void_value>
inline int animated<T, T_void_value>::get_animation_time() const
template<typename T>
inline int animated<T>::get_animation_time() const
{
if(!started_ && start_tick_ == 0) {
return starting_frame_time_;
Expand All @@ -227,29 +227,29 @@ inline int animated<T, T_void_value>::get_animation_time() const
return time;
}

template<typename T, typename T_void_value>
inline void animated<T, T_void_value>::set_animation_time(int time)
template<typename T>
inline void animated<T>::set_animation_time(int time)
{
start_tick_ = last_update_tick_ + static_cast<int>((starting_frame_time_ - time) / acceleration_);

current_frame_key_ = 0;
force_next_update_ = true;
}

template<typename T, typename T_void_value>
inline void animated<T, T_void_value>::set_max_animation_time(int time)
template<typename T>
inline void animated<T>::set_max_animation_time(int time)
{
max_animation_time_ = time;
}

template<typename T, typename T_void_value>
inline int animated<T, T_void_value>::get_animation_duration() const
template<typename T>
inline int animated<T>::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
template<typename T>
inline const T& animated<T>::get_current_frame() const
{
if(frames_.empty()) {
return void_value_;
Expand All @@ -258,8 +258,8 @@ inline const T& animated<T, T_void_value>::get_current_frame() const
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
template<typename T>
inline int animated<T>::get_current_frame_begin_time() const
{
if(frames_.empty()) {
return starting_frame_time_;
Expand All @@ -268,8 +268,8 @@ inline int animated<T, T_void_value>::get_current_frame_begin_time() const
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
template<typename T>
inline int animated<T>::get_current_frame_end_time() const
{
if(frames_.empty()) {
return starting_frame_time_;
Expand All @@ -278,8 +278,8 @@ inline int animated<T, T_void_value>::get_current_frame_end_time() const
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
template<typename T>
inline int animated<T>::get_current_frame_duration() const
{
if(frames_.empty()) {
return 0;
Expand All @@ -288,8 +288,8 @@ inline int animated<T, T_void_value>::get_current_frame_duration() const
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>
inline int animated<T>::get_current_frame_time() const
{
if(frames_.empty()) {
return 0;
Expand All @@ -299,8 +299,8 @@ inline int animated<T, T_void_value>::get_current_frame_time() const
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
template<typename T>
inline const T& animated<T>::get_first_frame() const
{
if(frames_.empty()) {
return void_value_;
Expand All @@ -309,8 +309,8 @@ inline const T& animated<T, T_void_value>::get_first_frame() const
return frames_[0].value_;
}

template<typename T, typename T_void_value>
inline const T& animated<T, T_void_value>::get_frame(std::size_t n) const
template<typename T>
inline const T& animated<T>::get_frame(std::size_t n) const
{
if(n >= frames_.size()) {
return void_value_;
Expand All @@ -319,8 +319,8 @@ inline const T& animated<T, T_void_value>::get_frame(std::size_t n) const
return frames_[n].value_;
}

template<typename T, typename T_void_value>
inline const T& animated<T, T_void_value>::get_last_frame() const
template<typename T>
inline const T& animated<T>::get_last_frame() const
{
if(frames_.empty()) {
return void_value_;
Expand All @@ -329,20 +329,20 @@ inline const T& animated<T, T_void_value>::get_last_frame() const
return frames_.back().value_;
}

template<typename T, typename T_void_value>
inline std::size_t animated<T, T_void_value>::get_frames_count() const
template<typename T>
inline std::size_t animated<T>::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
template<typename T>
inline int animated<T>::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
template<typename T>
inline int animated<T>::time_to_tick(int animation_time) const
{
if(!started_ && start_tick_ == 0) {
return 0;
Expand All @@ -351,8 +351,8 @@ inline int animated<T, T_void_value>::time_to_tick(int animation_time) const
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
template<typename T>
inline int animated<T>::tick_to_time(int animation_tick) const
{
if(!started_ && start_tick_ == 0) {
return 0;
Expand All @@ -361,8 +361,8 @@ inline int animated<T, T_void_value>::tick_to_time(int animation_tick) const
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
template<typename T>
inline int animated<T>::get_end_time() const
{
if(frames_.empty()) {
return starting_frame_time_;
Expand All @@ -371,17 +371,17 @@ inline int animated<T, T_void_value>::get_end_time() const
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)
template<typename T>
void animated<T>::remove_frames_until(int new_starting_time)
{
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)
template<typename T>
inline void animated<T>::set_end_time(int new_ending_time)
{
int last_start_time = starting_frame_time_;
typename std::vector<frame>::iterator current_frame = frames_.begin();
Expand All @@ -396,8 +396,8 @@ inline void animated<T, T_void_value>::set_end_time(int new_ending_time)
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)
template<typename T>
inline void animated<T>::set_begin_time(int new_begin_time)
{
const int variation = new_begin_time - starting_frame_time_;
starting_frame_time_ += variation;
Expand Down

0 comments on commit 773cc21

Please sign in to comment.