animated.hpp

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

Generated by doxygen 1.5.6 on Thu Feb 10 01:01:19 2011 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs