DynamicGraphic.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /* GG is a GUI for SDL and OpenGL.
00003    Copyright (C) 2003 T. Zachary Laine
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public License
00007    as published by the Free Software Foundation; either version 2.1
00008    of the License, or (at your option) any later version.
00009    
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014     
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA
00019 
00020    If you do not wish to comply with the terms of the LGPL please
00021    contact the author as other terms are available for a fee.
00022     
00023    Zach Laine
00024    whatwasthataddress@hotmail.com */
00025    
00026 /* This class is based on earlier work with GG by Tony Casale.  Thanks, Tony.*/
00027 
00031 #ifndef _GG_DynamicGraphic_h_
00032 #define _GG_DynamicGraphic_h_
00033 
00034 #include <GG/Control.h>
00035 #include <GG/StaticGraphic.h>
00036 
00037 
00038 namespace GG {
00039 class Texture;
00040 
00057 class GG_API DynamicGraphic : public Control
00058 {
00059 public: 
00061 
00064     typedef boost::signal<void (int)> StoppedSignalType;
00065 
00069     typedef boost::signal<void (int)> EndFrameSignalType;
00071  
00073     typedef StoppedSignalType::slot_type StoppedSlotType;    
00074     typedef EndFrameSignalType::slot_type EndFrameSlotType;  
00075 
00076  
00078 
00082     DynamicGraphic(int x, int y, int w, int h, bool loop, int frame_width, int frame_height, int margin, const std::vector<boost::shared_ptr<Texture> >& textures, Flags<GraphicStyle> style = GRAPHIC_NONE, int frames = -1, Flags<WndFlag> flags = Flags<WndFlag>());
00084  
00086     int     Frames() const;      
00087     bool    Playing() const;     
00088     bool    Looping() const;     
00089     double  FPS() const;         
00090     int     FrameIndex() const;  
00091     int     TimeIndex() const;   
00092 
00094     int     StartFrame() const;
00095 
00097     int     EndFrame() const;
00098 
00099     int     Margin() const;      
00100     int     FrameWidth() const;  
00101     int     FrameHeight() const; 
00102 
00104     Flags<GraphicStyle>  Style() const;
00105 
00106     mutable StoppedSignalType  StoppedSignal;  
00107     mutable EndFrameSignalType EndFrameSignal; 
00108 
00109  
00111     virtual void Render();
00112 
00117     void AddFrames(const Texture* texture, int frames = -1);
00118 
00122     void AddFrames(const boost::shared_ptr<Texture>& texture, int frames = -1);
00123 
00129     void AddFrames(const std::vector<boost::shared_ptr<Texture> >& textures, int frames = -1);
00130 
00131     void  Play();                    
00132     void  Pause();                   
00133     void  NextFrame();               
00134     void  PrevFrame();               
00135     void  Stop();                    
00136     void  Loop(bool b = true);       
00137 
00140     void  SetFPS(double fps);
00141 
00142     void  SetFrameIndex(int idx);    
00143 
00148     void  SetTimeIndex(int idx);
00149 
00152     void  SetStartFrame(int idx);
00153 
00156     void  SetEndFrame(int idx);
00157 
00159     void  SetStyle(Flags<GraphicStyle> style);
00160 
00161     virtual void DefineAttributes(WndEditor* editor);
00163  
00165 
00166     GG_ABSTRACT_EXCEPTION(Exception);
00167 
00169     GG_CONCRETE_EXCEPTION(CannotAddFrame, GG::DynamicGraphic, Exception);
00171 
00172 protected:
00173     struct FrameSet
00174     {
00175         boost::shared_ptr<const Texture> texture; 
00176         int                              frames;  
00177 
00178     private:
00179         friend class boost::serialization::access;
00180         template <class Archive>
00181         void serialize(Archive& ar, const unsigned int version);
00182     };
00183  
00185     DynamicGraphic(); 
00186 
00187  
00189     int FramesInTexture(const Texture* t) const; 
00190 
00191     const std::vector<FrameSet>& Textures() const;    
00192 
00193     int CurrentTexture() const;    
00194     int CurrentSubTexture() const; 
00195     int FirstFrameTime() const;    
00196     int LastFrameTime() const;     
00197 
00198 
00199     const int   m_margin;          
00200     const int   m_frame_width;     
00201     const int   m_frame_height;    
00202 
00203 private:
00204     void ValidateStyle();            
00205 
00206     std::vector<FrameSet> m_textures;
00207 
00208     double      m_FPS;               
00209     bool        m_playing;           
00210     bool        m_looping;           
00211     int         m_curr_texture;      
00212     int         m_curr_subtexture;   
00213     int         m_frames;            
00214     int         m_curr_frame;        
00215     int         m_first_frame_time;  
00216     int         m_last_frame_time;   
00217     int         m_first_frame_idx;   
00218     int         m_last_frame_idx;    
00219 
00220     Flags<GraphicStyle>
00221                 m_style;             
00222 
00223     friend class boost::serialization::access;
00224     template <class Archive>
00225     void serialize(Archive& ar, const unsigned int version);
00226 };
00227 
00228 } // namespace GG
00229 
00230 // template implementations
00231 template <class Archive>
00232 void GG::DynamicGraphic::FrameSet::serialize(Archive& ar, const unsigned int version)
00233 {
00234     boost::shared_ptr<Texture> non_const_texture;
00235     if (Archive::is_saving::value)
00236         non_const_texture = boost::const_pointer_cast<Texture>(texture);
00237 
00238     ar  & boost::serialization::make_nvp("texture", non_const_texture)
00239         & BOOST_SERIALIZATION_NVP(frames);
00240 
00241     if (Archive::is_loading::value)
00242         texture = boost::const_pointer_cast<const Texture>(non_const_texture);
00243 }
00244 
00245 template <class Archive>
00246 void GG::DynamicGraphic::serialize(Archive& ar, const unsigned int version)
00247 {
00248     ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Control)
00249         & boost::serialization::make_nvp("m_margin", const_cast<int&>(m_margin))
00250         & boost::serialization::make_nvp("m_frame_width", const_cast<int&>(m_frame_width))
00251         & boost::serialization::make_nvp("m_frame_height", const_cast<int&>(m_frame_height))
00252         & BOOST_SERIALIZATION_NVP(m_textures)
00253         & BOOST_SERIALIZATION_NVP(m_FPS)
00254         & BOOST_SERIALIZATION_NVP(m_playing)
00255         & BOOST_SERIALIZATION_NVP(m_looping)
00256         & BOOST_SERIALIZATION_NVP(m_curr_texture)
00257         & BOOST_SERIALIZATION_NVP(m_curr_subtexture)
00258         & BOOST_SERIALIZATION_NVP(m_frames)
00259         & BOOST_SERIALIZATION_NVP(m_curr_frame)
00260         & BOOST_SERIALIZATION_NVP(m_first_frame_time)
00261         & BOOST_SERIALIZATION_NVP(m_last_frame_time)
00262         & BOOST_SERIALIZATION_NVP(m_first_frame_idx)
00263         & BOOST_SERIALIZATION_NVP(m_last_frame_idx)
00264         & BOOST_SERIALIZATION_NVP(m_style);
00265 
00266     if (Archive::is_loading::value)
00267         ValidateStyle();
00268 }
00269 
00270 #endif // _GG_DynamicGraphic_h_

Generated on Wed Mar 26 14:35:41 2008 for GG by  doxygen 1.5.2