4 // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include "sprite_data.hpp"
27 #include "resources.hpp"
28 #include "video/drawing_context.hpp"
29 #include "lisp/list_iterator.hpp"
32 SpriteData::Action::Action()
40 SpriteData::Action::~Action()
42 for(std::vector<Surface*>::iterator i = surfaces.begin();
43 i != surfaces.end(); ++i)
47 SpriteData::SpriteData(const lisp::Lisp* lisp, const std::string& basedir)
49 lisp::ListIterator iter(lisp);
51 if(iter.item() == "name") {
52 iter.value()->get(name);
53 } else if(iter.item() == "action") {
54 parse_action(iter.lisp(), basedir);
56 msg_warning << "Unknown sprite field: " << iter.item() << std::endl;
60 throw std::runtime_error("Error: Sprite wihtout actions.");
63 SpriteData::~SpriteData()
65 for(Actions::iterator i=actions.begin(); i != actions.end(); ++i)
70 SpriteData::parse_action(const lisp::Lisp* lisp, const std::string& basedir)
72 Action* action = new Action;
74 if(!lisp->get("name", action->name)) {
76 throw std::runtime_error(
77 "If there are more than one action, they need names!");
79 lisp->get("x-offset", action->x_offset);
80 lisp->get("y-offset", action->y_offset);
81 lisp->get("z-order", action->z_order);
82 lisp->get("fps", action->fps);
84 std::string mirror_action;
85 lisp->get("mirror-action", mirror_action);
86 if(!mirror_action.empty()) {
87 Action* act_tmp = get_action(mirror_action);
89 throw std::runtime_error("Could not mirror action. Action not found\n"
90 "Mirror actions must be defined after the real one!");
92 for(int i = 0; static_cast<unsigned int>(i) < act_tmp->surfaces.size();
94 Surface* surface = new Surface(*(act_tmp->surfaces[i]));
96 action->surfaces.push_back(surface);
99 } else { // Load images
100 std::vector<std::string> images;
101 if(!lisp->get_vector("images", images)) {
102 std::stringstream msg;
103 msg << "Sprite '" << name << "' contains no images in action '"
104 << action->name << "'.";
105 throw std::runtime_error(msg.str());
108 for(std::vector<std::string>::size_type i = 0; i < images.size(); i++) {
109 action->surfaces.push_back(new Surface(basedir + images[i]));
112 actions[action->name] = action;
116 SpriteData::get_action(std::string act)
118 Actions::iterator i = actions.find(act);
119 if(i == actions.end()) {