act->y_hotspot = 0;
act->fps = 10;
- act->animation_loops = 0;
+ animation_loops = -1;
last_tick = 0;
}
void
Sprite::start_animation(int loops)
{
-action->animation_loops = loops;
+animation_loops = loops;
reset();
}
{
frame = 0;
last_tick = SDL_GetTicks();
+animation_reversed = false;
}
bool
Sprite::check_animation()
{
-return action->animation_loops;
+return animation_loops;
+}
+
+void
+Sprite::reverse_animation()
+{
+animation_reversed = !animation_reversed;
+
+if(animation_reversed)
+ frame = get_frames()-1;
+else
+ frame = 0;
}
void
Sprite::update()
{
-frame += (action->fps/1000) * (SDL_GetTicks() - last_tick);
+if(animation_loops == 0)
+ return;
+
+float inc_frame = (action->fps/1000) * (SDL_GetTicks() - last_tick);
+
+if(animation_reversed)
+ frame -= inc_frame;
+else
+ frame += inc_frame;
+
last_tick = SDL_GetTicks();
-if((unsigned int)frame >= action->surfaces.size())
+if(!animation_reversed)
{
- frame = 0;
- if(action->animation_loops > 0)
- action->animation_loops--;
+ if((unsigned int)frame >= action->surfaces.size())
+ {
+ frame = 0;
+ if(animation_loops > 0)
+ animation_loops--;
+ }
+ }
+else
+ {
+ if((unsigned int)frame < 0)
+ {
+ frame = get_frames()-1;
+ if(animation_loops > 0)
+ animation_loops--;
+ }
}
}
{
update();
- context.draw_surface(action->surfaces[(int)frame],
- pos - Vector(action->x_hotspot, action->y_hotspot), layer, drawing_effect);
+ if((int)frame >= get_frames())
+ std::cerr << "Warning: frame higher than total frames!\n";
+ else
+ context.draw_surface(action->surfaces[(int)frame],
+ pos - Vector(action->x_hotspot, action->y_hotspot), layer, drawing_effect);
}
#if 0
int
Sprite::get_width()
{
- return action->surfaces[get_current_frame()]->w;
+ return action->surfaces[get_frame()]->w;
}
int
Sprite::get_height()
{
- return action->surfaces[get_current_frame()]->h;
+ return action->surfaces[get_frame()]->h;
}
/* EOF */
/** Frames per second */
float fps;
- int animation_loops;
-
std::vector<Surface*> surfaces;
};
/** Set action (or state) */
void set_action(std::string act);
- /* Handling animations */
+ /* Start an animation
+ -1 - for infinite
+ 0 - stopped
+ 1,2,3 - one, two, three times... */
void start_animation(int loops);
+ /** Check if animation is stopped or not */
bool check_animation();
+ /** Reverse the animation */
+ void reverse_animation();
float get_fps()
{ return action->fps; }
+ /** Get current action total frames */
int get_frames()
{ return action->surfaces.size(); }
+ /** Get sprite's name */
std::string get_name() const
{ return name; }
+ /** Get current action name */
+ std::string get_action_name() const
+ { return action->name; }
int get_width();
int get_height();
- int get_current_frame()
+ /** Get current frame */
+ int get_frame()
{ return (int)frame; }
+ /** Set current frame */
+ void set_frame(int frame_)
+ { frame = frame_; }
Surface* get_frame(unsigned int frame)
{
if(frame < action->surfaces.size())
std::string name;
float frame;
+ int animation_loops;
+ bool animation_reversed;
float last_tick;
typedef std::map <std::string, Action*> Actions;