From: Ricardo Cruz Date: Fri, 13 Aug 2004 22:32:42 +0000 (+0000) Subject: Added support for one time animations to Sprite. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=72b0d5692f64afdf2d097bf96637f5c55fe4413b;p=supertux.git Added support for one time animations to Sprite. I'll port the one-time animations hacks - if nobody else does - later. SVN-Revision: 1770 --- diff --git a/lib/special/sprite.cpp b/lib/special/sprite.cpp index 5e43dc679..3f28031c9 100644 --- a/lib/special/sprite.cpp +++ b/lib/special/sprite.cpp @@ -82,8 +82,6 @@ Sprite::parse_action(LispReader& lispreader) new Surface(datadir + "/images/" + images[i], true)); } - action->frame_delay = 1000.0f/action->fps; - actions[action->name] = action; } @@ -93,8 +91,9 @@ Sprite::init_defaults(Action* act) act->x_hotspot = 0; act->y_hotspot = 0; act->fps = 10; - act->frame_delay = 1000.0f/act->fps; - time = 0; + + act->animation_loops = 0; + last_tick = 0; } void @@ -105,27 +104,49 @@ action = i->second; } void -Sprite::update(float /*delta*/) +Sprite::start_animation(int loops) { - //time += 10*delta; - //std::cout << "Delta: " << delta << std::endl; +action->animation_loops = loops; +reset(); } void -Sprite::draw(DrawingContext& context, const Vector& pos, int layer, - Uint32 drawing_effect) +Sprite::reset() { - time = SDL_GetTicks(); - unsigned int frame = get_current_frame(); +frame = 0; +last_tick = SDL_GetTicks(); +} - if (frame < action->surfaces.size()) +bool +Sprite::check_animation() +{ +return action->animation_loops; +} + +void +Sprite::update() +{ +frame += (action->fps/1000) * (SDL_GetTicks() - last_tick); +last_tick = SDL_GetTicks(); + +if((unsigned int)frame >= action->surfaces.size()) { - Surface* surface = action->surfaces[frame]; - - context.draw_surface(surface, pos - Vector(action->x_hotspot, action->y_hotspot), layer, drawing_effect); + frame = 0; + if(action->animation_loops > 0) + action->animation_loops--; } } +void +Sprite::draw(DrawingContext& context, const Vector& pos, int layer, + Uint32 drawing_effect) +{ + update(); + + context.draw_surface(action->surfaces[(int)frame], + pos - Vector(action->x_hotspot, action->y_hotspot), layer, drawing_effect); +} + #if 0 void Sprite::draw_part(float sx, float sy, float x, float y, float w, float h) @@ -138,27 +159,14 @@ Sprite::draw_part(float sx, float sy, float x, float y, float w, float h) } #endif -void -Sprite::reset() -{ - time = 0; -} - -int -Sprite::get_current_frame() const -{ - unsigned int frame = static_cast(fmodf(time, action->surfaces.size()*action->frame_delay)/action->frame_delay); - return frame % action->surfaces.size(); -} - int -Sprite::get_width() const +Sprite::get_width() { return action->surfaces[get_current_frame()]->w; } int -Sprite::get_height() const +Sprite::get_height() { return action->surfaces[get_current_frame()]->h; } diff --git a/lib/special/sprite.h b/lib/special/sprite.h index 5837bc63c..05db0c60a 100644 --- a/lib/special/sprite.h +++ b/lib/special/sprite.h @@ -45,9 +45,7 @@ namespace SuperTux /** Frames per second */ float fps; - /** Number of seconds that a frame is displayed until it is switched - to the next frame */ - float frame_delay; + int animation_loops; std::vector surfaces; }; @@ -58,33 +56,28 @@ namespace SuperTux Sprite(lisp_object_t* cur); ~Sprite(); - void reset(); - - /** Update the sprite and process to the next frame */ - void update(float delta); + /** Draw sprite, automatically calculates next frame */ void draw(DrawingContext& context, const Vector& pos, int layer, Uint32 drawing_effect = NONE_EFFECT); - int get_current_frame() const; /** Set action (or state) */ void set_action(std::string act); + /* Handling animations */ + void start_animation(int loops); + bool check_animation(); + float get_fps() - { - return action->fps; - } ; + { return action->fps; } int get_frames() - { - return action->surfaces.size(); - } ; - + { return action->surfaces.size(); } std::string get_name() const - { - return name; - } - int get_width() const; - int get_height() const; + { return name; } + int get_width(); + int get_height(); + int get_current_frame() + { return (int)frame; } Surface* get_frame(unsigned int frame) { if(frame < action->surfaces.size()) @@ -96,9 +89,13 @@ namespace SuperTux void init_defaults(Action* act); void parse_action(LispReader& lispreader); + void update(); + void reset(); + std::string name; - float time; + float frame; + float last_tick; typedef std::map Actions; Actions actions;