The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
animated.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004 - 2016 by Philippe Plantier <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #ifndef ANIMATED_IMAGE_H_INCLUDED
21 #define ANIMATED_IMAGE_H_INCLUDED
22 
23 #include <string>
24 #include <map>
25 #include <vector>
26 
27 void new_animation_frame();
29 
30 
31 template<typename T>
33 {
34  public:
35  const T operator()() { return T(); }
36 };
37 
38 template<typename T, typename T_void_value=void_value<T> >
39 class animated
40 {
41 public:
42 
43  animated(int start_time=0);
44  virtual ~animated(){}
45 
46 
47  typedef std::pair<int,T> frame_description;
48  typedef std::vector<frame_description> anim_description;
49  animated(const std::vector<frame_description> &cfg, int start_time = 0,bool force_change =false);
50 
51 
52  /** Adds a frame to an animation. */
53  void add_frame(int duration, const T& value,bool force_change =false);
54 
55  /**
56  * Starts an animation cycle.
57  *
58  * The first frame of the animation to start may be set to any value by
59  * using a start_time different to 0.
60  */
61  void start_animation(int start_time, bool cycles=false);
62  void pause_animation(){ started_ =false;}
64 
65  int get_begin_time() const;
66  int get_end_time() const;
67  void set_begin_time(int new_begin_time);
68 
69  int time_to_tick(int animation_time) const;
70  int tick_to_time(int animation_tick) const;
71 
72  void update_last_draw_time(double acceleration = 0);
73  bool need_update() const;
74 
75  bool cycles() const {return cycles_;}
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 
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(size_t n) const;
92  const T& get_last_frame() const;
93  size_t get_frames_count() const;
94  void force_change() {does_not_change_ = false ; }
95  bool does_not_change() const {return does_not_change_;}
96 
97  static const T void_value_; //MSVC: the frame constructor below requires this to be public
98 
99 protected:
100 friend class unit_animation;
102  void remove_frames_until(int starting_time);
103  void set_end_time(int ending_time);
104 
105 private:
106  struct frame
107  {
108 
109  frame(int duration , const T& value,int start_time) :
110  duration_(duration),value_(value),start_time_(start_time)
111  {}
112  frame():
113  duration_(0),value_(void_value_),start_time_(0)
114  {}
115 
116  // Represents the timestamp of the frame start
120  };
121 
122  bool does_not_change_; // Optimization for 1-frame permanent animations
123  bool started_;
125  std::vector<frame> frames_;
126 
127  // These are only valid when anim is started
128  int start_tick_; // time at which we started
129  bool cycles_;
133 
134 };
135 
136 #include "animated.tpp"
137 
138 #endif
139 
static const T void_value_
Definition: animated.hpp:97
bool does_not_change_
Definition: animated.hpp:122
void set_end_time(int ending_time)
std::vector< frame_description > anim_description
Definition: animated.hpp:48
int last_update_tick_
Definition: animated.hpp:131
bool cycles() const
Definition: animated.hpp:75
int time_to_tick(int animation_time) const
const T operator()()
Definition: animated.hpp:35
int tick_to_time(int animation_tick) const
int get_current_frame_begin_time() const
int get_current_frame_time() const
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:132
int get_animation_time() const
size_t get_frames_count() const
void force_change()
Definition: animated.hpp:94
int get_begin_time() const
bool cycles_
Definition: animated.hpp:129
void restart_animation()
Definition: animated.hpp:63
bool started_
Definition: animated.hpp:123
void set_animation_time(int time)
const T & get_last_frame() const
void update_last_draw_time(double acceleration=0)
int get_current_frame_duration() const
GLsizei const GLfloat * value
Definition: glew.h:1817
const T & get_current_frame() const
int starting_frame_time_
Definition: animated.hpp:101
int get_animation_duration() const
int get_current_animation_tick()
bool need_update() const
int get_current_frame_end_time() const
const T & get_frame(size_t n) const
void pause_animation()
Definition: animated.hpp:62
int get_animation_time_potential() const
animated(int start_time=0)
int get_end_time() const
const T & get_first_frame() const
void new_animation_frame()
double acceleration_
Definition: animated.hpp:130
std::vector< frame > frames_
Definition: animated.hpp:125
bool animation_finished() const
Returns true if the current animation was finished.
void set_begin_time(int new_begin_time)
GLclampd n
Definition: glew.h:5903
int start_tick_
Definition: animated.hpp:128
std::pair< int, T > frame_description
Definition: animated.hpp:47
bool does_not_change() const
Definition: animated.hpp:95
frame(int duration, const T &value, int start_time)
Definition: animated.hpp:109
bool animation_finished_potential() const
void remove_frames_until(int starting_time)
bool force_next_update_
Definition: animated.hpp:124
virtual ~animated()
Definition: animated.hpp:44
void start_animation(int start_time, bool cycles=false)
Starts an animation cycle.