2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "sprite/sprite_data.hpp"
22 #include "lisp/list_iterator.hpp"
23 #include "util/log.hpp"
24 #include "util/reader.hpp"
26 SpriteData::Action::Action() :
44 SpriteData::Action::~Action()
48 SpriteData::SpriteData(const Reader& lisp, const std::string& basedir) :
52 lisp::ListIterator iter(&lisp);
54 if(iter.item() == "name") {
55 iter.value()->get(name);
56 } else if(iter.item() == "action") {
57 parse_action(*iter.lisp(), basedir);
59 log_warning << "Unknown sprite field: " << iter.item() << std::endl;
63 throw std::runtime_error("Error: Sprite without actions.");
66 SpriteData::~SpriteData()
68 for(Actions::iterator i=actions.begin(); i != actions.end(); ++i)
73 SpriteData::parse_action(const Reader& lisp, const std::string& basedir)
75 Action* action = new Action;
77 if(!lisp.get("name", action->name)) {
79 throw std::runtime_error(
80 "If there are more than one action, they need names!");
82 std::vector<float> hitbox;
83 if (lisp.get("hitbox", hitbox)) {
84 switch(hitbox.size()) {
86 action->hitbox_h = hitbox[3];
87 action->hitbox_w = hitbox[2];
91 action->y_offset = hitbox[1];
92 action->x_offset = hitbox[0];
96 throw std::runtime_error("hitbox should specify 2/4 coordinates");
99 lisp.get("z-order", action->z_order);
100 lisp.get("fps", action->fps);
102 std::string mirror_action;
103 lisp.get("mirror-action", mirror_action);
104 if(!mirror_action.empty()) {
105 const Action* act_tmp = get_action(mirror_action);
106 if(act_tmp == NULL) {
107 throw std::runtime_error("Could not mirror action. Action not found.\n"
108 "Mirror actions must be defined after the real one!");
112 for(int i = 0; static_cast<unsigned int>(i) < act_tmp->surfaces.size(); i++) {
113 SurfacePtr surface = act_tmp->surfaces[i]->clone();
115 max_w = std::max(max_w, (float) surface->get_width());
116 max_h = std::max(max_h, (float) surface->get_height());
117 action->surfaces.push_back(surface);
119 if (action->hitbox_w < 1) action->hitbox_w = max_w - action->x_offset;
120 if (action->hitbox_h < 1) action->hitbox_h = max_h - action->y_offset;
122 } else { // Load images
123 std::vector<std::string> images;
124 if(!lisp.get("images", images)) {
125 std::stringstream msg;
126 msg << "Sprite '" << name << "' contains no images in action '"
127 << action->name << "'.";
128 throw std::runtime_error(msg.str());
133 for(std::vector<std::string>::size_type i = 0; i < images.size(); i++) {
134 SurfacePtr surface = Surface::create(basedir + images[i]);
135 max_w = std::max(max_w, (float) surface->get_width());
136 max_h = std::max(max_h, (float) surface->get_height());
137 action->surfaces.push_back(surface);
139 if (action->hitbox_w < 1) action->hitbox_w = max_w - action->x_offset;
140 if (action->hitbox_h < 1) action->hitbox_h = max_h - action->y_offset;
142 actions[action->name] = action;
145 const SpriteData::Action*
146 SpriteData::get_action(const std::string act)
148 Actions::iterator i = actions.find(act);
149 if(i == actions.end()) {