Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Animation.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_ANIMATION_H
22 #define GF_ANIMATION_H
23 
24 #include <cstddef>
25 #include <vector>
26 
27 #include "Portability.h"
28 #include "Rect.h"
29 
30 namespace gf {
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 inline namespace v1 {
33 #endif
34 
35  class Texture;
36 
37  /**
38  * @ingroup graphics
39  * @brief An animation
40  *
41  * An animation is a collection of frames that are displayed
42  * consecutively during a predefined amount of time.
43  *
44  * To display an animation, you need a gf::AnimatedSprite.
45  *
46  * @sa gf::AnimatedSprite
47  */
48  class GF_API Animation {
49  public:
50  /**
51  * @brief Default constructor
52  */
53  Animation();
54 
55  /**
56  * @brief Add a frame to the animation
57  *
58  * The frame of an animation is defined by the texture of the frame, the
59  * texture rectangle (in texture coordinates) and an amount of time.
60  *
61  * @param texture The texture where the sprite is
62  * @param bounds The texture rectangle where the sprite is
63  * @param duration The amount of time to display the frame
64  */
65  void addFrame(const Texture& texture, const RectF& bounds, float duration);
66 
67  /**
68  * @brief Get the current texture
69  *
70  * @return The current texture or `nullptr` if the animation is empty
71  */
72  const Texture *getCurrentTexture() const;
73 
74  /**
75  * @brief Get the current texture rectangle
76  *
77  * @return The current texture rectangle
78  */
79  RectF getCurrentBounds() const;
80 
81  /**
82  * @brief Update the state of the animation
83  *
84  * @param dt The time (in seconds) since the last update
85  * @return True if the current frame has changed
86  */
87  bool update(float dt);
88 
89  private:
90  struct Frame {
91  const Texture *texture;
92  RectF bounds;
93  float duration;
94  };
95 
96  std::size_t m_currentFrame;
97  float m_currentDurationInFrame;
98  std::vector<Frame> m_frames;
99  };
100 
101 #ifndef DOXYGEN_SHOULD_SKIP_THIS
102 }
103 #endif
104 }
105 
106 #endif // GF_ANIMATION_H
const Texture * getCurrentTexture() const
Get the current texture.
A texture for colored images.
Definition: Texture.h:339
Animation()
Default constructor.
Definition: Action.h:34
bool update(float dt)
Update the state of the animation.
void addFrame(const Texture &texture, const RectF &bounds, float duration)
Add a frame to the animation.
#define GF_API
Definition: Portability.h:35
RectF getCurrentBounds() const
Get the current texture rectangle.
An animation.
Definition: Animation.h:48