AnimatedSprite.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/graphics/sprite/AnimatedSprite.hpp"
30 
31 namespace nom {
32 
34 {
35  this->setMaxFrames ( 1 );
36  this->setCurrentFrame ( 0 );
37  this->setFrameIncrement ( 1 );
38  this->setAnimationStyle ( AnimatedSprite::AnimationStyle::NoStyle );
39  this->setAnimationStatus ( AnimatedSprite::AnimationStatus::Stopped );
40  this->fps.setFrameRate ( 100 );
41 }
42 
44 {
45 NOM_LOG_TRACE ( NOM );
46 
47  this->initialize();
48 }
49 
51  SpriteBatch ( sheet )
52 {
53 NOM_LOG_TRACE ( NOM );
54 
55  this->initialize();
56 
57  this->setMaxFrames ( this->sprite_sheet.frames() - 1 ); // We start from zero
58  this->fps.setFrameRate ( 100 );
59 }
60 
61 AnimatedSprite::AnimatedSprite ( const std::string& filename ) :
62  SpriteBatch ( filename )
63 {
64 NOM_LOG_TRACE ( NOM );
65 
66  this->initialize();
67 
68  this->setMaxFrames ( this->sprite_sheet.frames() - 1 ); // We start from zero
69  this->fps.setFrameRate ( 100 );
70 }
71 
73 {
74 NOM_LOG_TRACE ( NOM );
75 }
76 
77 int32 AnimatedSprite::total_frames ( void ) const
78 {
79  return this->max_frames;
80 }
81 
82 int32 AnimatedSprite::frame_inc ( void ) const
83 {
84  return this->frame_increment;
85 }
86 
87 int32 AnimatedSprite::frame ( void ) const
88 {
89  return this->current_frame;
90 }
91 
92 AnimatedSprite::AnimationStyle AnimatedSprite::style ( void ) const
93 {
94  return this->animation_style;
95 }
96 
97 AnimatedSprite::AnimationStatus AnimatedSprite::status ( void ) const
98 {
99  return this->animation_status;
100 }
101 
103 {
104  this->max_frames = max;
105 }
106 
107 void AnimatedSprite::setFrameIncrement ( int32 increment )
108 {
109  this->frame_increment = increment;
110 }
111 
112 void AnimatedSprite::setFrameRate ( int32 rate )
113 {
114  this->fps.setFrameRate ( rate );
115 }
116 
118 {
119  if ( frame < 0 || frame > this->total_frames() )
120  {
121 NOM_LOG_ERR ( NOM, "Could not update animation frame: requested frame value is too low or too high." );
122  return;
123  }
124 
125  this->current_frame = frame;
126 }
127 
128 void AnimatedSprite::setAnimationStyle ( AnimatedSprite::AnimationStyle style )
129 {
130  this->animation_style = style;
131 }
132 
133 void AnimatedSprite::setAnimationStatus ( AnimatedSprite::AnimationStatus status )
134 {
135  this->animation_status = status;
136 }
137 
139 {
140  if ( this->status() != AnimatedSprite::AnimationStatus::Playing ) return;
141 
142  if ( this->fps.ticks() + this->fps.framerate() > SDL_GetTicks() )
143  {
144  return;
145  }
146 
147  this->fps.start();
148 
149  this->setCurrentFrame ( this->current_frame + this->frame_increment );
150 
151  if ( this->style() == AnimatedSprite::AnimationStyle::Oscillate )
152  {
153  if ( this->frame_inc() > 0 )
154  {
155  if ( this->frame() >= this->total_frames() )
156  {
157  this->setFrameIncrement ( -( this->frame_inc() ) );
158  }
159  }
160  else
161  {
162  if ( this->frame() <= 0 )
163  {
164  this->setFrameIncrement ( -( this->frame_inc() ) );
165  }
166  }
167  }
168  else if ( this->style() == AnimatedSprite::AnimationStyle::Blink )
169  {
170  if ( this->frame() >= this->total_frames() )
171  {
172 
173 #if defined ( NOM_DEBUG_ANIMATED_SPRITE )
174  NOM_DUMP_VAR( this->frame() );
175 #endif
176 
177  this->setCurrentFrame ( this->frame() - 2 );
178 
179 #if defined ( NOM_DEBUG_ANIMATED_SPRITE )
180  NOM_DUMP_VAR( this->frame() );
181 #endif
182 
183  }
184  }
185  else
186  {
187  if ( this->frame() >= this->total_frames() )
188  {
189  this->setCurrentFrame ( 0 );
190  }
191  }
192 
193  this->setSheetID ( this->frame() );
195 }
196 
197 void AnimatedSprite::play ( void )
198 {
199  this->setAnimationStatus ( AnimatedSprite::AnimationStatus::Playing );
200  this->update_animation();
201 }
202 
203 void AnimatedSprite::stop ( void )
204 {
205  this->setAnimationStatus ( AnimatedSprite::AnimationStatus::Stopped );
206 }
207 
209 {
210  this->setAnimationStatus ( AnimatedSprite::AnimationStatus::Paused );
211 }
212 
214 {
215  this->setAnimationStatus ( AnimatedSprite::AnimationStatus::Playing );
216 }
217 
218 } // namespace nom
void initialize(void)
Initialize values to their respective defaults.
Extended bitmap object rendering – utilizes sprite sheets.
Definition: SpriteBatch.hpp:44
void play(void)
Play the animation.
int32 frames(void) const
Obtain the number of frames this object contains.
virtual ~AnimatedSprite(void)
Destructor.
int32 max_frames
Total number of animation frames.
int32 frame_inc(void) const
Get the frame increment value of this object.
AnimatedSprite::AnimationStatus status(void) const
Get the current status of this animation.
void setSheetID(int32 id)
Set a new frame ID to render.
void unpause(void)
Un-pause the animation playback.
void setMaxFrames(int32 max)
Set the total number of animated frames for this object.
void setCurrentFrame(int32 frame)
int32 total_frames(void) const
Get the maximum number of animation frames for this object.
int32 current_frame
Current animation frame.
void update_animation(void)
Updates the playback of the animation.
enum AnimatedSprite::AnimationStatus animation_status
Status info.
AnimationTimer fps
Keeps record of our current tick and frame rate (think: frame delay)
int32 frame(void) const
Get the current frame of this object.
void setAnimationStatus(AnimatedSprite::AnimationStatus status)
Set a new status state for this object.
Specialized class container for the creation of sprite sheets via RFC 4627 JSON-compliant file input...
Definition: SpriteSheet.hpp:62
void setAnimationStyle(AnimatedSprite::AnimationStyle style)
Set a new style of animation for this object.
enum AnimatedSprite::AnimationStyle animation_style
Animation playback logic.
void setFrameRate(int32 rate)
SpriteSheet sprite_sheet
Our attached sprite sheet object.
Definition: SpriteBatch.hpp:85
void setFrameIncrement(int32 increment)
Set the increment value of each animated frame of this object.
AnimatedSprite::AnimationStyle style(void) const
Get the current animation style for this object.
void update(void)
int32 frame_increment
Value that we increment the current_frame by.
void pause(void)
Pause the animation playback.
void stop(void)
Stop the animation playback.