animated.hpp

Go to the documentation of this file.
00001 /* $Id: animated.hpp 25602 2008-04-05 21:16:10Z boucman $ */
00002 /*
00003    Copyright (C) 2004 - 2008 by Philippe Plantier <ayin@anathas.org>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 //! @file animated.hpp
00016 //! Animate units.
00017 
00018 #ifndef ANIMATED_IMAGE_H_INCLUDED
00019 #define ANIMATED_IMAGE_H_INCLUDED
00020 
00021 #include <string>
00022 #include <map>
00023 #include <vector>
00024 
00025 void new_animation_frame();
00026 int get_current_animation_tick();
00027 
00028 
00029 template<typename T>
00030 class void_value
00031 {
00032     public:
00033         const T operator()() { return T(); }
00034 };
00035 
00036 template<typename T, typename T_void_value=void_value<T> >
00037 class animated
00038 {
00039 public:
00040 
00041     animated(int start_time=0);
00042     virtual ~animated(){};
00043 
00044 
00045     typedef  std::pair<int,T> frame_description;
00046     typedef  std::vector<frame_description> anim_description;
00047     animated(const std::vector<frame_description> &cfg, int start_time = 0,bool force_change =false);
00048 
00049 
00050     //! Adds a frame to an animation.
00051     void add_frame(int duration, const T& value,bool force_change =false);
00052 
00053     //! Starts an animation cycle.
00054     //! The first frame of the animation to start may be set
00055     //! to any value by using a start_time different to 0.
00056     void start_animation(int start_time, bool cycles=false);
00057     void pause_animation(){ started_ =false;};
00058     void restart_animation(){if(start_tick_) started_ = true;};
00059 
00060     int get_begin_time() const;
00061     int get_end_time() const;
00062         void set_begin_time(int new_begin_time);
00063 
00064     int time_to_tick(int animation_time) const;
00065     int tick_to_time(int animation_tick) const;
00066 
00067     void update_last_draw_time(double acceleration = 0);
00068     bool need_update() const;
00069 
00070     bool cycles() const {return cycles_;};
00071 
00072     //! Returns true if the current animation was finished.
00073     bool animation_finished() const;
00074     bool animation_finished_potential() const;
00075     int get_animation_time() const;
00076     int get_animation_time_potential() const;
00077 
00078     int get_animation_duration() const;
00079     const T& get_current_frame() const;
00080     int get_current_frame_begin_time() const;
00081     int get_current_frame_end_time() const;
00082     int get_current_frame_duration() const;
00083     int get_current_frame_time() const;
00084     const T& get_first_frame() const;
00085     const T& get_last_frame() const;
00086     int get_frames_count() const;
00087     void force_change() {does_not_change_ = false ; }
00088     bool does_not_change() const {return does_not_change_;}
00089 
00090     static const T void_value_; //MSVC: the frame constructor below requires this to be public
00091 
00092 protected:
00093 friend class unit_animation;
00094     int starting_frame_time_;
00095         // backward compatibility for teleport anims
00096         void remove_frames_until(int starting_time); 
00097         void remove_frames_after(int ending_time); 
00098 
00099 private:
00100     struct frame
00101     {
00102 
00103         frame(int duration , const T& value,int start_time) :
00104             duration_(duration),value_(value),start_time_(start_time)
00105         {};
00106         frame():
00107             duration_(0),value_(void_value_),start_time_(0)
00108         {};
00109 
00110         // Represents the timestamp of the frame start
00111         int duration_;
00112         T value_;
00113         int start_time_;
00114     };
00115 
00116     bool does_not_change_;  // Optimization for 1-frame permanent animations
00117     bool started_;
00118     bool need_first_update_;
00119     std::vector<frame> frames_;
00120 
00121     // These are only valid when anim is started
00122     int start_tick_; // time at which we started
00123     bool cycles_;
00124     double acceleration_;
00125     int last_update_tick_;
00126     int current_frame_key_;
00127 
00128 };
00129 
00130 #endif
00131 

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs