-// $Id$
-//
// SuperTux
-// Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
+// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
//
-// This program is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License
-// as published by the Free Software Foundation; either version 2
-// of the License, or (at your option) any later version.
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-#include <config.h>
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+#include "sprite/sprite_data.hpp"
-#include <iostream>
-#include <cmath>
-#include <sstream>
#include <stdexcept>
+#include <sstream>
-#include "sprite_data.h"
-#include "resources.h"
-#include "video/drawing_context.h"
-#include "lisp/list_iterator.h"
+#include "lisp/list_iterator.hpp"
+#include "util/log.hpp"
+#include "util/reader.hpp"
-SpriteData::Action::Action()
+SpriteData::Action::Action() :
+ name(),
+ x_offset(),
+ y_offset(),
+ hitbox_w(),
+ hitbox_h(),
+ z_order(),
+ fps(),
+ surfaces()
{
x_offset = 0;
y_offset = 0;
- z_order = 0;
+ hitbox_w = 0;
+ hitbox_h = 0;
+ z_order = 0;
fps = 10;
}
SpriteData::Action::~Action()
{
- for(std::vector<Surface*>::iterator i = surfaces.begin();
- i != surfaces.end(); ++i)
- delete *i;
}
-SpriteData::SpriteData(const lisp::Lisp* lisp)
+SpriteData::SpriteData(const Reader& lisp, const std::string& basedir) :
+ actions(),
+ name()
{
- lisp::ListIterator iter(lisp);
+ lisp::ListIterator iter(&lisp);
while(iter.next()) {
if(iter.item() == "name") {
iter.value()->get(name);
} else if(iter.item() == "action") {
- parse_action(iter.lisp());
+ parse_action(*iter.lisp(), basedir);
} else {
- std::cerr << "Unknown sprite field: " << iter.item() << "\n";
+ log_warning << "Unknown sprite field: " << iter.item() << std::endl;
}
}
- if(name.empty())
- throw std::runtime_error("Error: Sprite wihtout name.");
if(actions.empty())
- throw std::runtime_error("Error: Sprite wihtout actions.");
+ throw std::runtime_error("Error: Sprite without actions.");
}
SpriteData::~SpriteData()
}
void
-SpriteData::parse_action(const lisp::Lisp* lisp)
+SpriteData::parse_action(const Reader& lisp, const std::string& basedir)
{
Action* action = new Action;
- if(!lisp->get("name", action->name)) {
+ if(!lisp.get("name", action->name)) {
if(!actions.empty())
throw std::runtime_error(
- "If there are more than one action, they need names!");
+ "If there are more than one action, they need names!");
+ }
+ std::vector<float> hitbox;
+ if (lisp.get("hitbox", hitbox)) {
+ 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("x-offset", action->x_offset);
- lisp->get("y-offset", action->y_offset);
- lisp->get("z-order", action->z_order);
- lisp->get("fps", action->fps);
+ lisp.get("z-order", action->z_order);
+ lisp.get("fps", action->fps);
std::string mirror_action;
- lisp->get("mirror-action", 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"
- "Mirror actions must be defined after the real one!");
+ throw std::runtime_error("Could not mirror action. Action not found.\n"
+ "Mirror actions must be defined after the real one!");
} else {
- for(int i = 0; static_cast<unsigned int>(i) < act_tmp->surfaces.size();
- i++) {
- Surface* surface = new Surface(sdl_surface_from_sdl_surface(
- act_tmp->surfaces[i]->impl->get_sdl_surface(), true), true);
- surface->apply_filter(HORIZONTAL_FLIP_FILTER);
+ float max_w = 0;
+ float max_h = 0;
+ for(int i = 0; static_cast<unsigned int>(i) < act_tmp->surfaces.size(); 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 - action->x_offset;
+ if (action->hitbox_h < 1) action->hitbox_h = max_h - action->y_offset;
}
} else { // Load images
std::vector<std::string> images;
- if(!lisp->get_vector("images", images)) {
+ if(!lisp.get("images", images)) {
std::stringstream msg;
msg << "Sprite '" << name << "' contains no images in action '"
<< action->name << "'.";
throw std::runtime_error(msg.str());
}
+ float max_w = 0;
+ float max_h = 0;
for(std::vector<std::string>::size_type i = 0; i < images.size(); i++) {
- action->surfaces.push_back(new Surface("images/" + images[i], true));
+ 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 - 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()) {
return i->second;
}
+/* EOF */