X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fsprite%2Fsprite_data.cpp;h=b777cdadbfa705a7304e0d2b361539ab59eae3f0;hb=0289d77aedcb806daaeb5965c34f8d69e8ceed33;hp=4b1d8b4ceae35c272358465b4b66322203d4ba5d;hpb=18c69f4aa24d496f84a088d60c18b180c293dace;p=supertux.git diff --git a/src/sprite/sprite_data.cpp b/src/sprite/sprite_data.cpp index 4b1d8b4ce..b777cdadb 100644 --- a/src/sprite/sprite_data.cpp +++ b/src/sprite/sprite_data.cpp @@ -43,9 +43,6 @@ SpriteData::Action::Action() : SpriteData::Action::~Action() { - for(std::vector::iterator i = surfaces.begin(); - i != surfaces.end(); ++i) - delete *i; } SpriteData::SpriteData(const Reader& lisp, const std::string& basedir) : @@ -84,11 +81,20 @@ SpriteData::parse_action(const Reader& lisp, const std::string& basedir) } std::vector hitbox; if (lisp.get("hitbox", hitbox)) { - if (hitbox.size() != 4) throw std::runtime_error("hitbox must specify exactly 4 coordinates"); - action->x_offset = hitbox[0]; - action->y_offset = hitbox[1]; - action->hitbox_w = hitbox[2]; - action->hitbox_h = hitbox[3]; + switch(hitbox.size()) { + case 4: + action->hitbox_h = hitbox[3]; + action->hitbox_w = hitbox[2]; + + //fall-through + case 2: + action->y_offset = hitbox[1]; + action->x_offset = hitbox[0]; + break; + + default: + throw std::runtime_error("hitbox should specify 2/4 coordinates"); + } } lisp.get("z-order", action->z_order); lisp.get("fps", action->fps); @@ -96,22 +102,22 @@ SpriteData::parse_action(const Reader& lisp, const std::string& basedir) std::string mirror_action; lisp.get("mirror-action", mirror_action); if(!mirror_action.empty()) { - Action* act_tmp = get_action(mirror_action); + const Action* act_tmp = get_action(mirror_action); if(act_tmp == NULL) { - throw std::runtime_error("Could not mirror action. Action not found\n" + throw std::runtime_error("Could not mirror action. Action not found.\n" "Mirror actions must be defined after the real one!"); } else { float max_w = 0; float max_h = 0; for(int i = 0; static_cast(i) < act_tmp->surfaces.size(); i++) { - Surface* surface = new Surface(*(act_tmp->surfaces[i])); + SurfacePtr surface = act_tmp->surfaces[i]->clone(); surface->hflip(); max_w = std::max(max_w, (float) surface->get_width()); max_h = std::max(max_h, (float) surface->get_height()); action->surfaces.push_back(surface); } - if (action->hitbox_w < 1) action->hitbox_w = max_w; - if (action->hitbox_h < 1) action->hitbox_h = max_h; + if (action->hitbox_w < 1) action->hitbox_w = max_w - action->x_offset; + if (action->hitbox_h < 1) action->hitbox_h = max_h - action->y_offset; } } else { // Load images std::vector images; @@ -125,19 +131,19 @@ SpriteData::parse_action(const Reader& lisp, const std::string& basedir) float max_w = 0; float max_h = 0; for(std::vector::size_type i = 0; i < images.size(); i++) { - Surface* surface = new Surface(basedir + images[i]); + SurfacePtr surface = Surface::create(basedir + images[i]); max_w = std::max(max_w, (float) surface->get_width()); max_h = std::max(max_h, (float) surface->get_height()); action->surfaces.push_back(surface); } - if (action->hitbox_w < 1) action->hitbox_w = max_w; - if (action->hitbox_h < 1) action->hitbox_h = max_h; + if (action->hitbox_w < 1) action->hitbox_w = max_w - action->x_offset; + if (action->hitbox_h < 1) action->hitbox_h = max_h - action->y_offset; } actions[action->name] = action; } -SpriteData::Action* -SpriteData::get_action(std::string act) +const SpriteData::Action* +SpriteData::get_action(const std::string act) { Actions::iterator i = actions.find(act); if(i == actions.end()) {