animated.hpp

Go to the documentation of this file.
00001 /* $Id: animated.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2004 - 2012 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_frame(size_t n) const;
00093     const T& get_last_frame() const;
00094     size_t get_frames_count() const;
00095     void force_change() {does_not_change_ = false ; }
00096     bool does_not_change() const {return does_not_change_;}
00097 
00098     static const T void_value_; //MSVC: the frame constructor below requires this to be public
00099 
00100 protected:
00101 friend class unit_animation;
00102     int starting_frame_time_;
00103         void remove_frames_until(int starting_time);
00104         void set_end_time(int ending_time);
00105 
00106 private:
00107     struct frame
00108     {
00109 
00110         frame(int duration , const T& value,int start_time) :
00111             duration_(duration),value_(value),start_time_(start_time)
00112         {};
00113         frame():
00114             duration_(0),value_(void_value_),start_time_(0)
00115         {};
00116 
00117         // Represents the timestamp of the frame start
00118         int duration_;
00119         T value_;
00120         int start_time_;
00121     };
00122 
00123     bool does_not_change_;  // Optimization for 1-frame permanent animations
00124     bool started_;
00125     bool force_next_update_;
00126     std::vector<frame> frames_;
00127 
00128     // These are only valid when anim is started
00129     int start_tick_; // time at which we started
00130     bool cycles_;
00131     double acceleration_;
00132     int last_update_tick_;
00133     int current_frame_key_;
00134 
00135 };
00136 
00137 #endif
00138 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Tue May 22 2012 01:03:40 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs