The Battle for Wesnoth  1.15.2+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>
30 class animated
31 {
32 public:
33  animated(int start_time = 0);
34  virtual ~animated() {}
35 
36  typedef std::pair<int, T> frame_description;
37  typedef std::vector<frame_description> anim_description;
38  animated(const std::vector<frame_description>& cfg, int start_time = 0, bool force_change = false);
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;
78  bool animation_finished_potential() const;
79  int get_animation_time() const;
80  int get_animation_time_potential() const;
81  void set_animation_time(int time);
82  void set_max_animation_time(int time);
83 
84  int get_animation_duration() const;
85  const T& get_current_frame() const;
86  int get_current_frame_begin_time() const;
87  int get_current_frame_end_time() const;
88  int get_current_frame_duration() const;
89  int get_current_frame_time() 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;
110  void remove_frames_until(int starting_time);
111  void set_end_time(int ending_time);
112 
113 private:
114  struct frame
115  {
116  frame(int duration, const T& value, int start_time)
117  : duration_(duration)
118  , value_(value)
119  , start_time_(start_time)
120  {
121  }
122 
124  : duration_(0)
125  , value_(void_value_)
126  , start_time_(0)
127  {
128  }
129 
130  // Represents the timestamp of the frame start
134  };
135 
136  bool does_not_change_; // Optimization for 1-frame permanent animations
137  bool started_;
139  std::vector<frame> frames_;
140 
141  // Can set a maximum animation time so that movement in particular does not exceed potential time
142  // Ignored if has a value of 0
144 
145  // These are only valid when anim is started
146  int start_tick_; // time at which we started
147  bool cycles_;
151 };
152 
153 // NOTE: this needs to be down here or the templates won't build.
154 #include "animated.tpp"
int get_current_frame_end_time() const
std::size_t get_frames_count() const
double acceleration_
Definition: animated.hpp:148
animated(int start_time=0)
bool cycles() const
Definition: animated.hpp:71
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:150
virtual ~animated()
Definition: animated.hpp:34
int max_animation_time_
Definition: animated.hpp:143
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:37
int starting_frame_time_
Definition: animated.hpp:109
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:136
bool force_next_update_
Definition: animated.hpp:138
bool does_not_change() const
Definition: animated.hpp:100
int start_tick_
Definition: animated.hpp:146
int get_current_animation_tick()
Definition: animated.cpp:35
int time_to_tick(int animation_time) const
bool cycles_
Definition: animated.hpp:147
void restart_animation()
Definition: animated.hpp:55
int last_update_tick_
Definition: animated.hpp:149
void new_animation_frame()
Definition: animated.cpp:30
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:139
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:116
void force_change()
Definition: animated.hpp:95
bool started_
Definition: animated.hpp:137
void pause_animation()
Definition: animated.hpp:50
static const T void_value_
Definition: animated.hpp:105
int get_end_time() const
std::pair< int, T > frame_description
Definition: animated.hpp:36
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