[Super-tux-commit] supertux/lib/special collision_hit.h,1.2,1.3 sprite.cpp,1.32,1.33 sprite.h,1.18,1
Brought to you by:
wkendrick
From: Matze B. <mat...@us...> - 2004-11-24 23:10:48
|
Update of /cvsroot/super-tux/supertux/lib/special In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16564/lib/special Modified Files: collision_hit.h sprite.cpp sprite.h Log Message: some cleanups in the sprite class, increased delta for collision response Index: collision_hit.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/collision_hit.h,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- collision_hit.h 24 Nov 2004 14:10:23 -0000 1.2 +++ collision_hit.h 24 Nov 2004 23:10:02 -0000 1.3 @@ -30,10 +30,15 @@ */ enum HitResponse { - ABORT_MOVE, // don't move the object - FORCE_MOVE, // do the move ignoring the collision - CONTINUE // move object out of collision and check for collisions again - // if this happens to often then the move will just be aborted + // note: keep the elements in this order + + /// don't move the object + ABORT_MOVE = 0, + /// move object out of collision and check for collisions again + /// if this happens to often then the move will just be aborted + CONTINUE, + /// do the move ignoring the collision + FORCE_MOVE }; /** Index: sprite.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/sprite.cpp,v retrieving revision 1.32 retrieving revision 1.33 diff -u -d -r1.32 -r1.33 --- sprite.cpp 24 Nov 2004 14:10:23 -0000 1.32 +++ sprite.cpp 24 Nov 2004 23:10:02 -0000 1.33 @@ -32,17 +32,18 @@ { Sprite::Sprite(SpriteData& newdata) - : data(newdata) + : data(newdata), frame(0), animation_loops(-1) { action = data.actions.begin()->second; - reset(); + last_ticks = SDL_GetTicks(); } Sprite::Sprite(const Sprite& other) : data(other.data), frame(other.frame), - animation_loops(other.animation_loops), last_tick(other.last_tick), - action(other.action), next_action(other.next_action) + animation_loops(other.animation_loops), + action(other.action) { + last_ticks = SDL_GetTicks(); } Sprite::~Sprite() @@ -50,34 +51,22 @@ } void -Sprite::set_action(std::string name) +Sprite::set_action(std::string name, int loops) { - if(!next_action.empty() && animation_loops > 0) { - next_action = name; + if(action && action->name == name) return; - } + SpriteData::Action* newaction = data.get_action(name); - if(!action) + if(!action) { +#ifdef DEBUG + std::cerr << "Action '" << name << "' not found.\n"; +#endif return; + } action = newaction; -} - -void -Sprite::start_animation(int loops) -{ - reset(); animation_loops = loops; -} - -void -Sprite::reset() -{ frame = 0; - last_tick = SDL_GetTicks(); - animation_reversed = false; - animation_loops = -1; - next_action.clear(); } bool @@ -87,71 +76,24 @@ } void -Sprite::reverse_animation(bool reverse) -{ - animation_reversed = reverse; - - if(animation_reversed) - frame = get_frames()-1; - else - frame = 0; -} - -void Sprite::update() { if(animation_loops == 0) - { - if(frame >= get_frames() || frame < 0) - frame = 0; return; - } - - float frame_inc = (action->fps/1000.0) * (SDL_GetTicks() - last_tick); - last_tick = SDL_GetTicks(); - - if(animation_reversed) - frame -= frame_inc; - else - frame += frame_inc; - if(animation_reversed) { - if(frame < 0 || frame >= (float)get_frames()) { - // last case can happen when not used reverse_animation() - float excedent = frame - 0; - frame = get_frames() - 1; - if(animation_loops > 0) - { - animation_loops--; - if(animation_loops == 0 && !next_action.empty()) - { - set_action(next_action); - start_animation(-1); - } - } + Uint32 ticks = SDL_GetTicks(); + float frame_inc = action->fps * float(ticks - last_ticks)/1000.0; + last_ticks = ticks; - if(fabsf(excedent) < get_frames()) - frame += excedent; - } - } - else - { - if(frame >= (float)get_frames()) - { - float excedent = frame - get_frames(); - frame = 0; - if(animation_loops > 0) - { - animation_loops--; - if(animation_loops == 0 && !next_action.empty()) - { - set_action(next_action); - start_animation(-1); - } - } + frame += frame_inc; - if(excedent < get_frames()) - frame += excedent; + float lastframe = frame; + frame = fmodf(frame+get_frames(), get_frames()); + if(frame != lastframe) { + if(animation_loops > 0) { + animation_loops--; + if(animation_loops == 0) + frame = 0; } } } Index: sprite.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/sprite.h,v retrieving revision 1.18 retrieving revision 1.19 diff -u -d -r1.18 -r1.19 --- sprite.h 24 Nov 2004 14:10:23 -0000 1.18 +++ sprite.h 24 Nov 2004 23:10:02 -0000 1.19 @@ -22,6 +22,7 @@ #include <string> #include <vector> +#include <cassert> #include <map> #include "utils/lispreader.h" @@ -47,20 +48,13 @@ Uint32 drawing_effect = NONE_EFFECT); /** Set action (or state) */ - void set_action(std::string act); + void set_action(std::string act, int loops = -1); - /* Start an animation - -1 - for infinite - 0 - stopped - 1,2,3 - one, two, three times... */ - void start_animation(int loops); /* Stop animation */ void stop_animation() - { start_animation(0); } + { animation_loops = 0; } /** Check if animation is stopped or not */ bool check_animation(); - /** Reverse the animation */ - void reverse_animation(bool reverse); float get_fps() const { return action->fps; } @@ -85,24 +79,20 @@ { if(frame_ > get_frames()) frame = 0; else frame = frame_; } Surface* get_frame(unsigned int frame) { - if(frame < action->surfaces.size()) - return action->surfaces[frame]; - else - return action->surfaces[0]; + assert(frame < action->surfaces.size()); + return action->surfaces[frame]; } + private: void update(); - void reset(); SpriteData& data; float frame; int animation_loops; - bool animation_reversed; - float last_tick; + Uint32 last_ticks; SpriteData::Action* action; - std::string next_action; }; } //namespace SuperTux |