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