The Battle for Wesnoth  1.19.0-dev
animated.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004 - 2024
3  by Philippe Plantier <ayin@anathas.org>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  * Animate units.
19  */
20 
21 #pragma once
22 
23 #include <vector>
24 
25 void new_animation_frame();
27 
28 template<typename T>
29 class animated
30 {
31 public:
32  typedef std::pair<int, T> frame_description;
33  typedef std::vector<frame_description> anim_description;
34 
35  animated(int start_time = 0);
36  explicit animated(const std::vector<frame_description>& cfg, int start_time = 0, bool force_change = false);
37 
38  virtual ~animated() = default;
39 
40  /** Adds a frame to an animation. */
41  void add_frame(int duration, const T& value, bool force_change = false);
42 
43  /**
44  * Starts an animation cycle.
45  *
46  * The first frame of the animation to start may be set to any value by
47  * using a start_time different to 0.
48  */
49  void start_animation(int start_time, bool cycles = false);
51  {
52  started_ = false;
53  }
54 
56  {
57  if(start_tick_)
58  started_ = true;
59  }
60 
61  int get_begin_time() const;
62  int get_end_time() const;
63  void set_begin_time(int new_begin_time);
64 
65  int time_to_tick(int animation_time) const;
66  int tick_to_time(int animation_tick) const;
67 
68  void update_last_draw_time(double acceleration = 0);
69  bool need_update() const;
70 
71  bool cycles() const
72  {
73  return cycles_;
74  }
75 
76  /** Returns true if the current animation was finished. */
77  bool animation_finished() const;
79  int get_animation_time() const;
81  void set_animation_time(int time);
82  void set_max_animation_time(int time);
83 
85  const T& get_current_frame() const;
90  const T& get_first_frame() const;
91  const T& get_frame(std::size_t n) const;
92  const T& get_last_frame() const;
93  std::size_t get_frames_count() const;
94 
95  void force_change()
96  {
97  does_not_change_ = false;
98  }
99 
100  bool does_not_change() const
101  {
102  return does_not_change_;
103  }
104 
105  static const T void_value_; // MSVC: the frame constructor below requires this to be public
106 
107 protected:
108  friend class unit_animation;
109 
110  void remove_frames_until(int starting_time);
111  void set_end_time(int ending_time);
112 
114 
115 private:
116  struct frame
117  {
118  frame(int duration, const T& value, int start_time)
119  : duration_(duration)
120  , value_(value)
121  , start_time_(start_time)
122  {
123  }
124 
126  : duration_(0)
128  , start_time_(0)
129  {
130  }
131 
132  // Represents the timestamp of the frame start
136  };
137 
138  bool does_not_change_; // Optimization for 1-frame permanent animations
139  bool started_;
141  std::vector<frame> frames_;
142 
143  // Can set a maximum animation time so that movement in particular does not exceed potential time
144  // Ignored if has a value of 0
146 
147  // These are only valid when anim is started
148  int start_tick_; // time at which we started
149  bool cycles_;
153 };
154 
155 // NOTE: this needs to be down here or the templates won't build.
156 #include "animated.tpp"
int get_current_animation_tick()
Definition: animated.cpp:36
void new_animation_frame()
Definition: animated.cpp:31
const T & get_first_frame() const
int max_animation_time_
Definition: animated.hpp:145
int get_current_frame_begin_time() const
const T & get_frame(std::size_t n) const
std::vector< frame > frames_
Definition: animated.hpp:141
int time_to_tick(int animation_time) const
int start_tick_
Definition: animated.hpp:148
bool force_next_update_
Definition: animated.hpp:140
virtual ~animated()=default
int get_animation_time_potential() const
void set_max_animation_time(int time)
void set_end_time(int ending_time)
bool started_
Definition: animated.hpp:139
int get_end_time() const
bool animation_finished_potential() const
int get_begin_time() const
animated(int start_time=0)
void pause_animation()
Definition: animated.hpp:50
void update_last_draw_time(double acceleration=0)
void start_animation(int start_time, bool cycles=false)
Starts an animation cycle.
int get_animation_duration() const
std::vector< frame_description > anim_description
Definition: animated.hpp:33
void remove_frames_until(int starting_time)
const T & get_last_frame() const
void force_change()
Definition: animated.hpp:95
int starting_frame_time_
Definition: animated.hpp:113
animated(const std::vector< frame_description > &cfg, int start_time=0, bool force_change=false)
int get_current_frame_time() const
int current_frame_key_
Definition: animated.hpp:152
bool does_not_change() const
Definition: animated.hpp:100
void set_begin_time(int new_begin_time)
int get_current_frame_end_time() const
bool need_update() const
bool animation_finished() const
Returns true if the current animation was finished.
std::pair< int, T > frame_description
Definition: animated.hpp:32
double acceleration_
Definition: animated.hpp:150
int get_current_frame_duration() const
bool cycles_
Definition: animated.hpp:149
const T & get_current_frame() const
void restart_animation()
Definition: animated.hpp:55
int get_animation_time() const
int last_update_tick_
Definition: animated.hpp:151
void set_animation_time(int time)
std::size_t get_frames_count() const
void add_frame(int duration, const T &value, bool force_change=false)
Adds a frame to an animation.
bool does_not_change_
Definition: animated.hpp:138
static const T void_value_
Definition: animated.hpp:105
int tick_to_time(int animation_tick) const
bool cycles() const
Definition: animated.hpp:71
frame(int duration, const T &value, int start_time)
Definition: animated.hpp:118
static map_location::DIRECTION n