renamed all .h to .hpp
[supertux.git] / src / sprite / sprite_data.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.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 #include <config.h>
20
21 #include <iostream>
22 #include <cmath>
23 #include <sstream>
24 #include <stdexcept>
25
26 #include "sprite_data.hpp"
27 #include "resources.hpp"
28 #include "video/drawing_context.hpp"
29 #include "lisp/list_iterator.hpp"
30
31 SpriteData::Action::Action()
32 {
33   x_offset = 0;
34   y_offset = 0;
35   z_order = 0;   
36   fps = 10;
37 }
38
39 SpriteData::Action::~Action()
40 {
41   for(std::vector<Surface*>::iterator i = surfaces.begin();
42       i != surfaces.end(); ++i)
43     delete *i;
44 }
45
46 SpriteData::SpriteData(const lisp::Lisp* lisp)
47 {
48   lisp::ListIterator iter(lisp);
49   while(iter.next()) {
50     if(iter.item() == "name") {
51       iter.value()->get(name);
52     } else if(iter.item() == "action") {
53       parse_action(iter.lisp());
54     } else {
55       std::cerr << "Unknown sprite field: " << iter.item() << "\n";
56     }
57   }
58   if(name.empty())
59     throw std::runtime_error("Error: Sprite wihtout name.");
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)
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(sdl_surface_from_sdl_surface(
96               act_tmp->surfaces[i]->impl->get_sdl_surface(), true), true);
97         surface->apply_filter(HORIZONTAL_FLIP_FILTER);
98         action->surfaces.push_back(surface);
99       }
100     }
101   } else { // Load images
102     std::vector<std::string> images;
103     if(!lisp->get_vector("images", images)) {
104       std::stringstream msg;
105       msg << "Sprite '" << name << "' contains no images in action '"
106           << action->name << "'.";
107       throw std::runtime_error(msg.str());
108     }
109
110     for(std::vector<std::string>::size_type i = 0; i < images.size(); i++) {
111       action->surfaces.push_back(new Surface("images/" + images[i], true));
112     }
113   }
114   actions[action->name] = action;
115 }
116
117 SpriteData::Action*
118 SpriteData::get_action(std::string act)
119 {
120   Actions::iterator i = actions.find(act);
121   if(i == actions.end()) {
122     return 0;
123   }
124   return i->second;
125 }
126