From: Ricardo Cruz Date: Sat, 14 Aug 2004 11:32:51 +0000 (+0000) Subject: Improved Sprites animations. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=70f26d626c6e0f7717be209438341987ae38ddfc;p=supertux.git Improved Sprites animations. SVN-Revision: 1773 --- diff --git a/lib/special/sprite.cpp b/lib/special/sprite.cpp index 3f28031c9..d9134885e 100644 --- a/lib/special/sprite.cpp +++ b/lib/special/sprite.cpp @@ -92,7 +92,7 @@ Sprite::init_defaults(Action* act) act->y_hotspot = 0; act->fps = 10; - act->animation_loops = 0; + animation_loops = -1; last_tick = 0; } @@ -106,7 +106,7 @@ action = i->second; void Sprite::start_animation(int loops) { -action->animation_loops = loops; +animation_loops = loops; reset(); } @@ -115,25 +115,58 @@ Sprite::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--; + } } } @@ -143,8 +176,11 @@ Sprite::draw(DrawingContext& context, const Vector& pos, int layer, { 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 @@ -162,13 +198,13 @@ Sprite::draw_part(float sx, float sy, float x, float y, float w, float h) 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 */ diff --git a/lib/special/sprite.h b/lib/special/sprite.h index 05db0c60a..f84894582 100644 --- a/lib/special/sprite.h +++ b/lib/special/sprite.h @@ -45,8 +45,6 @@ namespace SuperTux /** Frames per second */ float fps; - int animation_loops; - std::vector surfaces; }; @@ -63,21 +61,36 @@ namespace SuperTux /** 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()) @@ -95,6 +108,8 @@ namespace SuperTux std::string name; float frame; + int animation_loops; + bool animation_reversed; float last_tick; typedef std::map Actions;