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