The Battle for Wesnoth  1.17.12+dev
animated.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004 - 2022
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 <map>
24 #include <string>
25 #include <vector>
26 
27 void new_animation_frame();
29 
30 template<typename T>
31 class animated
32 {
33 public:
34  typedef std::pair<int, T> frame_description;
35  typedef std::vector<frame_description> anim_description;
36 
37  animated(int start_time = 0);
38  explicit animated(const std::vector<frame_description>& cfg, int start_time = 0, bool force_change = false);
39 
40  virtual ~animated() = default;
41 
42  /** Adds a frame to an animation. */
43  void add_frame(int duration, const T& value, bool force_change = false);
44 
45  /**
46  * Starts an animation cycle.
47  *
48  * The first frame of the animation to start may be set to any value by
49  * using a start_time different to 0.
50  */
51  void start_animation(int start_time, bool cycles = false);
53  {
54  started_ = false;
55  }
56 
58  {
59  if(start_tick_)
60  started_ = true;
61  }
62 
63  int get_begin_time() const;
64  int get_end_time() const;
65  void set_begin_time(int new_begin_time);
66 
67  int time_to_tick(int animation_time) const;
68  int tick_to_time(int animation_tick) const;
69 
70  void update_last_draw_time(double acceleration = 0);
71  bool need_update() const;
72 
73  bool cycles() const
74  {
75  return cycles_;
76  }
77 
78  /** Returns true if the current animation was finished. */
79  bool animation_finished() const;
80  bool animation_finished_potential() const;
81  int get_animation_time() const;
82  int get_animation_time_potential() const;
83  void set_animation_time(int time);
84  void set_max_animation_time(int time);
85 
86  int get_animation_duration() const;
87  const T& get_current_frame() const;
88  int get_current_frame_begin_time() const;
89  int get_current_frame_end_time() const;
90  int get_current_frame_duration() const;
91  int get_current_frame_time() const;
92  const T& get_first_frame() const;
93  const T& get_frame(std::size_t n) const;
94  const T& get_last_frame() const;
95  std::size_t get_frames_count() const;
96 
97  void force_change()
98  {
99  does_not_change_ = false;
100  }
101 
102  bool does_not_change() const
103  {
104  return does_not_change_;
105  }
106 
107  static const T void_value_; // MSVC: the frame constructor below requires this to be public
108 
109 protected:
110  friend class unit_animation;
111 
112  void remove_frames_until(int starting_time);
113  void set_end_time(int ending_time);
114 
116 
117 private:
118  struct frame
119  {
120  frame(int duration, const T& value, int start_time)
121  : duration_(duration)
122  , value_(value)
123  , start_time_(start_time)
124  {
125  }
126 
128  : duration_(0)
129  , value_(void_value_)
130  , start_time_(0)
131  {
132  }
133 
134  // Represents the timestamp of the frame start
138  };
139 
140  bool does_not_change_; // Optimization for 1-frame permanent animations
141  bool started_;
143  std::vector<frame> frames_;
144 
145  // Can set a maximum animation time so that movement in particular does not exceed potential time
146  // Ignored if has a value of 0
148 
149  // These are only valid when anim is started
150  int start_tick_; // time at which we started
151  bool cycles_;
155 };
156 
157 // NOTE: this needs to be down here or the templates won't build.
158 #include "animated.tpp"
int get_current_frame_end_time() const
std::size_t get_frames_count() const
double acceleration_
Definition: animated.hpp:152
animated(int start_time=0)
bool cycles() const
Definition: animated.hpp:73
int get_current_frame_begin_time() const
const T & get_last_frame() const
void set_max_animation_time(int time)
bool need_update() const
bool animation_finished_potential() const
void set_animation_time(int time)
void add_frame(int duration, const T &value, bool force_change=false)
Adds a frame to an animation.
int current_frame_key_
Definition: animated.hpp:154
int max_animation_time_
Definition: animated.hpp:147
int get_animation_duration() const
int get_begin_time() const
int get_current_frame_time() const
std::vector< frame_description > anim_description
Definition: animated.hpp:35
int starting_frame_time_
Definition: animated.hpp:115
int get_animation_time_potential() const
int get_current_frame_duration() const
const T & get_frame(std::size_t n) const
bool does_not_change_
Definition: animated.hpp:140
bool force_next_update_
Definition: animated.hpp:142
bool does_not_change() const
Definition: animated.hpp:102
int start_tick_
Definition: animated.hpp:150
int get_current_animation_tick()
Definition: animated.cpp:36
int time_to_tick(int animation_time) const
bool cycles_
Definition: animated.hpp:151
void restart_animation()
Definition: animated.hpp:57
int last_update_tick_
Definition: animated.hpp:153
virtual ~animated()=default
void new_animation_frame()
Definition: animated.cpp:31
const T & get_current_frame() const
void update_last_draw_time(double acceleration=0)
void start_animation(int start_time, bool cycles=false)
Starts an animation cycle.
std::vector< frame > frames_
Definition: animated.hpp:143
void set_begin_time(int new_begin_time)
void set_end_time(int ending_time)
frame(int duration, const T &value, int start_time)
Definition: animated.hpp:120
void force_change()
Definition: animated.hpp:97
bool started_
Definition: animated.hpp:141
void pause_animation()
Definition: animated.hpp:52
static const T void_value_
Definition: animated.hpp:107
int get_end_time() const
std::pair< int, T > frame_description
Definition: animated.hpp:34
int tick_to_time(int animation_tick) const
const T & get_first_frame() const
static map_location::DIRECTION n
bool animation_finished() const
Returns true if the current animation was finished.
void remove_frames_until(int starting_time)
int get_animation_time() const