2 more evil mainloops are gone
[supertux.git] / src / sprite / sprite_data.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include <iostream>
23 #include <cmath>
24 #include <sstream>
25 #include <stdexcept>
26
27 #include "sprite_data.hpp"
28 #include "resources.hpp"
29 #include "video/drawing_context.hpp"
30 #include "lisp/list_iterator.hpp"
31 #include "log.hpp"
32
33 SpriteData::Action::Action()
34 {
35   x_offset = 0;
36   y_offset = 0;
37   z_order = 0;   
38   fps = 10;
39 }
40
41 SpriteData::Action::~Action()
42 {
43   for(std::vector<Surface*>::iterator i = surfaces.begin();
44       i != surfaces.end(); ++i)
45     delete *i;
46 }
47
48 SpriteData::SpriteData(const lisp::Lisp* lisp, const std::string& basedir)
49 {
50   lisp::ListIterator iter(lisp);
51   while(iter.next()) {
52     if(iter.item() == "name") {
53       iter.value()->get(name);
54     } else if(iter.item() == "action") {
55       parse_action(iter.lisp(), basedir);
56     } else {
57       log_warning << "Unknown sprite field: " << iter.item() << std::endl;
58     }
59   }
60   if(actions.empty())
61     throw std::runtime_error("Error: Sprite wihtout actions.");
62 }
63
64 SpriteData::~SpriteData()
65 {
66   for(Actions::iterator i=actions.begin(); i != actions.end(); ++i)
67     delete i->second;
68 }
69
70 void
71 SpriteData::parse_action(const lisp::Lisp* lisp, const std::string& basedir)
72 {
73   Action* action = new Action;
74
75   if(!lisp->get("name", action->name)) {
76     if(!actions.empty())
77       throw std::runtime_error(
78           "If there are more than one action, they need names!");
79   }
80   lisp->get("x-offset", action->x_offset);
81   lisp->get("y-offset", action->y_offset);
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       for(int i = 0; static_cast<unsigned int>(i) < act_tmp->surfaces.size();
94           i++) {
95         Surface* surface = new Surface(*(act_tmp->surfaces[i]));
96         surface->hflip();
97         action->surfaces.push_back(surface);
98       }
99     }
100   } else { // Load images
101     std::vector<std::string> images;
102     if(!lisp->get_vector("images", images)) {
103       std::stringstream msg;
104       msg << "Sprite '" << name << "' contains no images in action '"
105           << action->name << "'.";
106       throw std::runtime_error(msg.str());
107     }
108
109     for(std::vector<std::string>::size_type i = 0; i < images.size(); i++) {
110       action->surfaces.push_back(new Surface(basedir + images[i]));
111     }
112   }
113   actions[action->name] = action;
114 }
115
116 SpriteData::Action*
117 SpriteData::get_action(std::string act)
118 {
119   Actions::iterator i = actions.find(act);
120   if(i == actions.end()) {
121     return 0;
122   }
123   return i->second;
124 }
125