[Super-tux-commit] supertux/lib/special sprite.cpp,1.4,1.5 sprite.h,1.4,1.5
Brought to you by:
wkendrick
From: Ricardo C. <rm...@us...> - 2004-08-12 20:55:15
|
Update of /cvsroot/super-tux/supertux/lib/special In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2888/lib/special Modified Files: sprite.cpp sprite.h Log Message: Added what I called of actions to Sprite. Instead of putting "left", "right", "jump-right"... as different sprites, we can now put them as one, and name them with different actions. To change to an action, just do set_action("jump-left"); , for instance. Suggested by Ryan. Index: sprite.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/sprite.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- sprite.cpp 25 Jul 2004 19:03:34 -0000 1.4 +++ sprite.cpp 12 Aug 2004 20:55:05 -0000 1.5 @@ -29,44 +29,79 @@ Sprite::Sprite(lisp_object_t* cur) { - init_defaults(); + for(; !lisp_nil_p(cur); cur = lisp_cdr(cur)) + { + std::string token = lisp_symbol(lisp_car(lisp_car(cur))); + lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur))); + LispReader reader(lisp_cdr(lisp_car(cur))); - LispReader reader(cur); + if(token == "name") + name = lisp_string(data); + else if(token == "action") + parse_action(reader); + } - if(!reader.read_string("name", name)) - Termination::abort("Sprite wihtout name", ""); - reader.read_int("x-hotspot", x_hotspot); - reader.read_int("y-hotspot", y_hotspot); - reader.read_float("fps", fps); + if(name.empty()) + Termination::abort("Error: Sprite wihtout name.", ""); + if(actions.empty()) + Termination::abort("Error: Sprite wihtout actions.", ""); +} + +Sprite::~Sprite() +{ + for(Actions::iterator i_act = actions.begin(); i_act != actions.end(); ++i_act) + { + for(std::vector<Surface*>::iterator i_sur = i_act->second->surfaces.begin(); + i_sur != i_act->second->surfaces.end(); ++i_sur) + delete *i_sur; + delete i_act->second; + } +} + +void +Sprite::parse_action(LispReader& lispreader) +{ + action = new Action; + + init_defaults(action); + + if(!lispreader.read_string("name", action->name)) + if(!actions.empty()) + Termination::abort("Error: If there are more than one action, they need names!", ""); + lispreader.read_int("x-hotspot", action->x_hotspot); + lispreader.read_int("y-hotspot", action->y_hotspot); + lispreader.read_float("fps", action->fps); std::vector<std::string> images; - if(!reader.read_string_vector("images", images)) - Termination::abort("Sprite contains no images: ", name.c_str()); + if(!lispreader.read_string_vector("images", images)) + Termination::abort("Sprite contains no images: ", action->name.c_str()); for(std::vector<std::string>::size_type i = 0; i < images.size(); ++i) { - surfaces.push_back( + action->surfaces.push_back( new Surface(datadir + "/images/" + images[i], true)); } - frame_delay = 1000.0f/fps; + action->frame_delay = 1000.0f/action->fps; + + actions[action->name] = action; } -Sprite::~Sprite() +void +Sprite::init_defaults(Action* act) { - for(std::vector<Surface*>::iterator i = surfaces.begin(); i != surfaces.end(); - ++i) - delete *i; + act->x_hotspot = 0; + act->y_hotspot = 0; + act->fps = 10; + act->frame_delay = 1000.0f/act->fps; + time = 0; } void -Sprite::init_defaults() +Sprite::set_action(std::string& act) { - x_hotspot = 0; - y_hotspot = 0; - fps = 10; - time = 0; - frame_delay = 1000.0f/fps; +Actions::iterator i = actions.find(act); +action = i->second; } void @@ -83,11 +118,11 @@ time = SDL_GetTicks(); unsigned int frame = get_current_frame(); - if (frame < surfaces.size()) + if (frame < action->surfaces.size()) { - Surface* surface = surfaces[frame]; + Surface* surface = action->surfaces[frame]; - context.draw_surface(surface, pos - Vector(x_hotspot, y_hotspot), layer, drawing_effect); + context.draw_surface(surface, pos - Vector(action->x_hotspot, action->y_hotspot), layer, drawing_effect); } } @@ -112,20 +147,20 @@ int Sprite::get_current_frame() const { - unsigned int frame = static_cast<int>(fmodf(time, surfaces.size()*frame_delay)/frame_delay); - return frame % surfaces.size(); + unsigned int frame = static_cast<int>(fmodf(time, action->surfaces.size()*action->frame_delay)/action->frame_delay); + return frame % action->surfaces.size(); } int Sprite::get_width() const { - return surfaces[get_current_frame()]->w; + return action->surfaces[get_current_frame()]->w; } int Sprite::get_height() const { - return surfaces[get_current_frame()]->h; + return action->surfaces[get_current_frame()]->h; } /* EOF */ Index: sprite.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/sprite.h,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- sprite.h 25 Jul 2004 19:03:34 -0000 1.4 +++ sprite.h 12 Aug 2004 20:55:05 -0000 1.5 @@ -22,6 +22,7 @@ #include <string> #include <vector> +#include <map> #include "../utils/lispreader.h" #include "../video/surface.h" @@ -32,6 +33,25 @@ class Sprite { + private: + + struct Action + { + std::string name; + + int x_hotspot; + int y_hotspot; + + /** Frames per second */ + float fps; + + /** Number of seconds that a frame is displayed until it is switched + to the next frame */ + float frame_delay; + + std::vector<Surface*> surfaces; + }; + public: /** cur has to be a pointer to data in the form of ((x-hotspot 5) (y-hotspot 10) ...) */ @@ -46,13 +66,16 @@ Uint32 drawing_effect = NONE_EFFECT); int get_current_frame() const; + /** Set action (or state) */ + void set_action(std::string& act); + float get_fps() { - return fps; + return action->fps; } ; int get_frames() { - return surfaces.size(); + return action->surfaces.size(); } ; std::string get_name() const @@ -64,29 +87,23 @@ Surface* get_frame(unsigned int frame) { - if(frame < surfaces.size()) - return surfaces[frame]; + if(frame < action->surfaces.size()) + return action->surfaces[frame]; else - return surfaces[0]; + return action->surfaces[0]; } private: - std::string name; - - int x_hotspot; - int y_hotspot; - - /** Frames per second */ - float fps; + void init_defaults(Action* act); + void parse_action(LispReader& lispreader); - /** Number of seconds that a frame is displayed until it is switched - to the next frame */ - float frame_delay; + std::string name; float time; - std::vector<Surface*> surfaces; + typedef std::map <std::string, Action*> Actions; + Actions actions; - void init_defaults(); + Action* action; }; } //namespace SuperTux |