hx3d  1
2D/3D Simple Game Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
animation.cpp
1 /*
2  Animated sprite.
3  Copyright (C) 2015 Denis BOURGE
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
7  License as published by the Free Software Foundation; either
8  version 2.1 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 Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/graphics/animation.hpp"
22 
23 namespace hx3d {
24 namespace graphics {
25 
27 Animation::Animation(Ptr<TextureAtlas> atlas, std::vector<std::string> frames, float speed) {
28  initialize(atlas, frames, speed);
29 }
30 
31 void Animation::initialize(Ptr<TextureAtlas> atlas, std::vector<std::string> frames, float speed) {
32  _atlas = atlas;
33  _currentTime = 0;
34  _frames = frames;
35  _delay = speed;
36 
37  setTexture(_atlas->getRegion(_frames[0]));
38 }
39 
40 void Animation::update(float delta) {
41  _currentTime += delta;
42  if (_currentTime >= _delay) {
43  _currentTime -= _delay;
44  }
45 
46  unsigned int framesQty = _frames.size();
47  float limit = _delay / framesQty;
48  unsigned int currentFrame = (unsigned int)(_currentTime / limit);
49  setTexture(_atlas->getRegion(_frames[currentFrame]));
50 }
51 
52 } /* graphics */
53 } /* hx3d */
void initialize(Ptr< TextureAtlas > atlas, std::vector< std::string > frames, float speed)
Initialize an animation with an atlas, frames and a speed.
Definition: animation.cpp:31
void setTexture(const Ptr< Texture > &texture)
Set the sprite texture.
Definition: sprite.cpp:36
hx3d framework namespace
Definition: audio.hpp:26
void update(float delta)
Update the animation.
Definition: animation.cpp:40
Animation()
Create an uninitialized animation.
Definition: animation.cpp:26
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34