GG
DynamicGraphic.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /* GG is a GUI for SDL and OpenGL.
3  Copyright (C) 2003-2008 T. Zachary Laine
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public License
7  as published by the Free Software Foundation; either version 2.1
8  of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free
17  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18  02111-1307 USA
19 
20  If you do not wish to comply with the terms of the LGPL please
21  contact the author as other terms are available for a fee.
22 
23  Zach Laine
24  whatwasthataddress@gmail.com */
25 
26 /* This class is based on earlier work with GG by Tony Casale. Thanks, Tony.*/
27 
31 #ifndef _GG_DynamicGraphic_h_
32 #define _GG_DynamicGraphic_h_
33 
34 #include <GG/Control.h>
35 #include <GG/StaticGraphic.h>
36 
37 
38 namespace GG {
39 class Texture;
40 
68 class GG_API DynamicGraphic : public Control
69 {
70 public:
72 
77  typedef boost::signal<void (std::size_t)> StoppedSignalType;
78 
84  typedef boost::signal<void (std::size_t)> EndFrameSignalType;
86 
88 
95  DynamicGraphic(X x, Y y, X w, Y h, bool loop, X frame_width, Y frame_height, unsigned int margin,
96  const std::vector<boost::shared_ptr<Texture> >& textures,
97  Flags<GraphicStyle> style = GRAPHIC_NONE, std::size_t frames = ALL_FRAMES,
98  Flags<WndFlag> flags = Flags<WndFlag>());
100 
102  std::size_t Frames() const;
103  bool Playing() const;
104  bool Looping() const;
105  double FPS() const;
106  std::size_t FrameIndex() const;
107  unsigned int TimeIndex() const;
108 
111  std::size_t StartFrame() const;
112 
115  std::size_t EndFrame() const;
116 
117  unsigned int Margin() const;
118  X FrameWidth() const;
119  Y FrameHeight() const;
120 
122  Flags<GraphicStyle> Style() const;
123 
126 
127 
129  virtual void Render();
130 
138  void AddFrames(const Texture* texture, std::size_t frames = ALL_FRAMES);
139 
145  void AddFrames(const boost::shared_ptr<Texture>& texture, std::size_t frames = ALL_FRAMES);
146 
154  void AddFrames(const std::vector<boost::shared_ptr<Texture> >& textures, std::size_t frames = ALL_FRAMES);
155 
156  void Play();
157  void Pause();
158  void NextFrame();
159  void PrevFrame();
160  void Stop();
161  void Loop(bool b = true);
162 
166  void SetFPS(double fps);
167 
168  void SetFrameIndex(std::size_t idx);
169 
176  void SetTimeIndex(unsigned int time);
177 
181  void SetStartFrame(std::size_t idx);
182 
186  void SetEndFrame(std::size_t idx);
187 
190  void SetStyle(Flags<GraphicStyle> style);
192 
194 
195  GG_ABSTRACT_EXCEPTION(Exception);
196 
199  GG_CONCRETE_EXCEPTION(CannotAddFrame, GG::DynamicGraphic, Exception);
201 
202  static const std::size_t ALL_FRAMES;
203  static const std::size_t INVALID_INDEX;
204  static const unsigned int INVALID_TIME;
205 
206 protected:
207  struct FrameSet
208  {
209  boost::shared_ptr<const Texture> texture;
210  std::size_t frames;
211  };
212 
214  DynamicGraphic();
215 
216 
218  std::size_t FramesInTexture(const Texture* t) const;
219  const std::vector<FrameSet>& Textures() const;
220 
221  std::size_t CurrentTexture() const;
222  std::size_t CurrentSubTexture() const;
223  unsigned int FirstFrameTime() const;
224  unsigned int LastFrameTime() const;
225 
226 
227  const unsigned int m_margin;
228  const X m_frame_width;
230 
231 private:
232  void ValidateStyle();
233 
234  std::vector<FrameSet> m_textures;
235 
236  double m_FPS;
237  bool m_playing;
238  bool m_looping;
239  std::size_t m_curr_texture;
240  std::size_t m_curr_subtexture;
241  std::size_t m_frames;
242  std::size_t m_curr_frame;
243  unsigned int m_first_frame_time;
244  unsigned int m_last_frame_time;
245  std::size_t m_first_frame_idx;
246  std::size_t m_last_frame_idx;
247 
248  Flags<GraphicStyle> m_style;
249 };
250 
251 } // namespace GG
252 
253 #endif