Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / sprite / sprite_data.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "sprite/sprite_data.hpp"
18
19 #include <stdexcept>
20 #include <sstream>
21
22 #include "lisp/list_iterator.hpp"
23 #include "util/log.hpp"
24
25 SpriteData::Action::Action()
26 {
27   x_offset = 0;
28   y_offset = 0;
29   hitbox_w = 0;
30   hitbox_h = 0;
31   z_order = 0;
32   fps = 10;
33 }
34
35 SpriteData::Action::~Action()
36 {
37   for(std::vector<Surface*>::iterator i = surfaces.begin();
38       i != surfaces.end(); ++i)
39     delete *i;
40 }
41
42 SpriteData::SpriteData(const lisp::Lisp* lisp, const std::string& basedir)
43 {
44   lisp::ListIterator iter(lisp);
45   while(iter.next()) {
46     if(iter.item() == "name") {
47       iter.value()->get(name);
48     } else if(iter.item() == "action") {
49       parse_action(iter.lisp(), basedir);
50     } else {
51       log_warning << "Unknown sprite field: " << iter.item() << std::endl;
52     }
53   }
54   if(actions.empty())
55     throw std::runtime_error("Error: Sprite without actions.");
56 }
57
58 SpriteData::~SpriteData()
59 {
60   for(Actions::iterator i=actions.begin(); i != actions.end(); ++i)
61     delete i->second;
62 }
63
64 void
65 SpriteData::parse_action(const lisp::Lisp* lisp, const std::string& basedir)
66 {
67   Action* action = new Action;
68
69   if(!lisp->get("name", action->name)) {
70     if(!actions.empty())
71       throw std::runtime_error(
72         "If there are more than one action, they need names!");
73   }
74   std::vector<float> hitbox;
75   if (lisp->get("hitbox", hitbox)) {
76     if (hitbox.size() != 4) throw std::runtime_error("hitbox must specify exactly 4 coordinates");
77     action->x_offset = hitbox[0];
78     action->y_offset = hitbox[1];
79     action->hitbox_w = hitbox[2];
80     action->hitbox_h = hitbox[3];
81   }
82   lisp->get("z-order", action->z_order);
83   lisp->get("fps", action->fps);
84
85   std::string mirror_action;
86   lisp->get("mirror-action", mirror_action);
87   if(!mirror_action.empty()) {
88     Action* act_tmp = get_action(mirror_action);
89     if(act_tmp == NULL) {
90       throw std::runtime_error("Could not mirror action. Action not found\n"
91                                "Mirror actions must be defined after the real one!");
92     } else {
93       float max_w = 0;
94       float max_h = 0;
95       for(int i = 0; static_cast<unsigned int>(i) < act_tmp->surfaces.size(); i++) {
96         Surface* surface = new Surface(*(act_tmp->surfaces[i]));
97         surface->hflip();
98         max_w = std::max(max_w, (float) surface->get_width());
99         max_h = std::max(max_h, (float) surface->get_height());
100         action->surfaces.push_back(surface);
101       }
102       if (action->hitbox_w < 1) action->hitbox_w = max_w;
103       if (action->hitbox_h < 1) action->hitbox_h = max_h;
104     }
105   } else { // Load images
106     std::vector<std::string> images;
107     if(!lisp->get("images", images)) {
108       std::stringstream msg;
109       msg << "Sprite '" << name << "' contains no images in action '"
110           << action->name << "'.";
111       throw std::runtime_error(msg.str());
112     }
113
114     float max_w = 0;
115     float max_h = 0;
116     for(std::vector<std::string>::size_type i = 0; i < images.size(); i++) {
117       Surface* surface = new Surface(basedir + images[i]);
118       max_w = std::max(max_w, (float) surface->get_width());
119       max_h = std::max(max_h, (float) surface->get_height());
120       action->surfaces.push_back(surface);
121     }
122     if (action->hitbox_w < 1) action->hitbox_w = max_w;
123     if (action->hitbox_h < 1) action->hitbox_h = max_h;
124   }
125   actions[action->name] = action;
126 }
127
128 SpriteData::Action*
129 SpriteData::get_action(std::string act)
130 {
131   Actions::iterator i = actions.find(act);
132   if(i == actions.end()) {
133     return 0;
134   }
135   return i->second;
136 }
137
138 /* EOF */