[Super-tux-commit] supertux/lib/special sprite.cpp,1.7,1.8 sprite.h,1.7,1.8
Brought to you by:
wkendrick
From: Ricardo C. <rm...@us...> - 2004-08-14 11:32:59
|
Update of /cvsroot/super-tux/supertux/lib/special In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27688/lib/special Modified Files: sprite.cpp sprite.h Log Message: Improved Sprites animations. Index: sprite.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/sprite.cpp,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- sprite.cpp 13 Aug 2004 22:32:42 -0000 1.7 +++ sprite.cpp 14 Aug 2004 11:32:50 -0000 1.8 @@ -92,7 +92,7 @@ act->y_hotspot = 0; act->fps = 10; - act->animation_loops = 0; + animation_loops = -1; last_tick = 0; } @@ -106,7 +106,7 @@ void Sprite::start_animation(int loops) { -action->animation_loops = loops; +animation_loops = loops; reset(); } @@ -115,25 +115,58 @@ { 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 @@ { 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 @@ 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 */ Index: sprite.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/sprite.h,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- sprite.h 13 Aug 2004 22:32:42 -0000 1.7 +++ sprite.h 14 Aug 2004 11:32:51 -0000 1.8 @@ -45,8 +45,6 @@ /** Frames per second */ float fps; - int animation_loops; - std::vector<Surface*> surfaces; }; @@ -63,21 +61,36 @@ /** 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 @@ std::string name; float frame; + int animation_loops; + bool animation_reversed; float last_tick; typedef std::map <std::string, Action*> Actions; |