GG

DynamicGraphic.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /* GG is a GUI for SDL and OpenGL.
00003    Copyright (C) 2003-2008 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@gmail.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 
00068 class GG_API DynamicGraphic : public Control
00069 {
00070 public: 
00072 
00077     typedef boost::signal<void (std::size_t)> StoppedSignalType;
00078 
00084     typedef boost::signal<void (std::size_t)> EndFrameSignalType;
00086  
00088 
00095     DynamicGraphic(X x, Y y, X w, Y h, bool loop, X frame_width, Y frame_height, unsigned int margin,
00096                    const std::vector<boost::shared_ptr<Texture> >& textures,
00097                    Flags<GraphicStyle> style = GRAPHIC_NONE, std::size_t frames = ALL_FRAMES,
00098                    Flags<WndFlag> flags = Flags<WndFlag>());
00100  
00102     std::size_t  Frames() const;      
00103     bool         Playing() const;     
00104     bool         Looping() const;     
00105     double       FPS() const;         
00106     std::size_t  FrameIndex() const;  
00107     unsigned int TimeIndex() const;   
00108 
00111     std::size_t  StartFrame() const;
00112 
00115     std::size_t  EndFrame() const;
00116 
00117     unsigned int Margin() const;      
00118     X            FrameWidth() const;  
00119     Y            FrameHeight() const; 
00120 
00122     Flags<GraphicStyle> Style() const;
00123 
00124     mutable StoppedSignalType  StoppedSignal;  
00125     mutable EndFrameSignalType EndFrameSignal; 
00126 
00127  
00129     virtual void Render();
00130 
00138     void AddFrames(const Texture* texture, std::size_t frames = ALL_FRAMES);
00139 
00145     void AddFrames(const boost::shared_ptr<Texture>& texture, std::size_t frames = ALL_FRAMES);
00146 
00154     void AddFrames(const std::vector<boost::shared_ptr<Texture> >& textures, std::size_t frames = ALL_FRAMES);
00155 
00156     void  Play();                    
00157     void  Pause();                   
00158     void  NextFrame();               
00159     void  PrevFrame();               
00160     void  Stop();                    
00161     void  Loop(bool b = true);       
00162 
00166     void  SetFPS(double fps);
00167 
00168     void  SetFrameIndex(std::size_t idx);    
00169 
00176     void  SetTimeIndex(unsigned int time);
00177 
00181     void  SetStartFrame(std::size_t idx);
00182 
00186     void  SetEndFrame(std::size_t idx);
00187 
00190     void  SetStyle(Flags<GraphicStyle> style);
00191 
00192     virtual void DefineAttributes(WndEditor* editor);
00194  
00196 
00197     GG_ABSTRACT_EXCEPTION(Exception);
00198 
00201     GG_CONCRETE_EXCEPTION(CannotAddFrame, GG::DynamicGraphic, Exception);
00203 
00204     static const std::size_t  ALL_FRAMES;
00205     static const std::size_t  INVALID_INDEX;
00206     static const unsigned int INVALID_TIME;
00207 
00208 protected:
00209     struct FrameSet
00210     {
00211         boost::shared_ptr<const Texture> texture; 
00212         std::size_t                      frames;  
00213 
00214     private:
00215         friend class boost::serialization::access;
00216         template <class Archive>
00217         void serialize(Archive& ar, const unsigned int version);
00218     };
00219  
00221     DynamicGraphic(); 
00222 
00223  
00225     std::size_t FramesInTexture(const Texture* t) const; 
00226     const std::vector<FrameSet>& Textures() const; 
00227 
00228     std::size_t  CurrentTexture() const;    
00229     std::size_t  CurrentSubTexture() const; 
00230     unsigned int FirstFrameTime() const;    
00231     unsigned int LastFrameTime() const;     
00232 
00233 
00234     const unsigned int m_margin;            
00235     const X            m_frame_width;       
00236     const Y            m_frame_height;      
00237 
00238 private:
00239     void ValidateStyle();             
00240 
00241     std::vector<FrameSet> m_textures; 
00242 
00243     double       m_FPS;               
00244     bool         m_playing;           
00245     bool         m_looping;           
00246     std::size_t  m_curr_texture;      
00247     std::size_t  m_curr_subtexture;   
00248     std::size_t  m_frames;            
00249     std::size_t  m_curr_frame;        
00250     unsigned int m_first_frame_time;  
00251     unsigned int m_last_frame_time;   
00252     std::size_t  m_first_frame_idx;   
00253     std::size_t  m_last_frame_idx;    
00254 
00255     Flags<GraphicStyle> m_style;
00256 
00257     friend class boost::serialization::access;
00258     template <class Archive>
00259     void serialize(Archive& ar, const unsigned int version);
00260 };
00261 
00262 } // namespace GG
00263 
00264 // template implementations
00265 template <class Archive>
00266 void GG::DynamicGraphic::FrameSet::serialize(Archive& ar, const unsigned int version)
00267 {
00268     boost::shared_ptr<Texture> non_const_texture;
00269     if (Archive::is_saving::value)
00270         non_const_texture = boost::const_pointer_cast<Texture>(texture);
00271 
00272     ar  & boost::serialization::make_nvp("texture", non_const_texture)
00273         & BOOST_SERIALIZATION_NVP(frames);
00274 
00275     if (Archive::is_loading::value)
00276         texture = boost::const_pointer_cast<const Texture>(non_const_texture);
00277 }
00278 
00279 template <class Archive>
00280 void GG::DynamicGraphic::serialize(Archive& ar, const unsigned int version)
00281 {
00282     ar  & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Control)
00283         & boost::serialization::make_nvp("m_margin", const_cast<unsigned int&>(m_margin))
00284         & boost::serialization::make_nvp("m_frame_width", const_cast<X&>(m_frame_width))
00285         & boost::serialization::make_nvp("m_frame_height", const_cast<Y&>(m_frame_height))
00286         & BOOST_SERIALIZATION_NVP(m_textures)
00287         & BOOST_SERIALIZATION_NVP(m_FPS)
00288         & BOOST_SERIALIZATION_NVP(m_playing)
00289         & BOOST_SERIALIZATION_NVP(m_looping)
00290         & BOOST_SERIALIZATION_NVP(m_curr_texture)
00291         & BOOST_SERIALIZATION_NVP(m_curr_subtexture)
00292         & BOOST_SERIALIZATION_NVP(m_frames)
00293         & BOOST_SERIALIZATION_NVP(m_curr_frame)
00294         & BOOST_SERIALIZATION_NVP(m_first_frame_time)
00295         & BOOST_SERIALIZATION_NVP(m_last_frame_time)
00296         & BOOST_SERIALIZATION_NVP(m_first_frame_idx)
00297         & BOOST_SERIALIZATION_NVP(m_last_frame_idx)
00298         & BOOST_SERIALIZATION_NVP(m_style);
00299 
00300     if (Archive::is_loading::value)
00301         ValidateStyle();
00302 }
00303 
00304 #endif // _GG_DynamicGraphic_h_