testing Yeti image version, feel free to revert
[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.h"
27 #include "app/globals.h"
28 #include "app/setup.h"
29 #include "video/drawing_context.h"
30 #include "lisp/list_iterator.h"
31
32 SpriteData::Action::Action()
33 {
34   x_offset = 0;
35   y_offset = 0;
36   z_order = 0;   
37   fps = 10;
38 }
39
40 SpriteData::Action::~Action()
41 {
42   for(std::vector<Surface*>::iterator i = surfaces.begin();
43       i != surfaces.end(); ++i)
44     delete *i;
45 }
46
47 SpriteData::SpriteData(const lisp::Lisp* lisp)
48 {
49   lisp::ListIterator iter(lisp);
50   while(iter.next()) {
51     if(iter.item() == "name") {
52       iter.value()->get(name);
53     } else if(iter.item() == "action") {
54       parse_action(iter.lisp());
55     } else {
56       std::cerr << "Unknown sprite field: " << iter.item() << "\n";
57     }
58   }
59   if(name.empty())
60     throw std::runtime_error("Error: Sprite wihtout name.");
61   if(actions.empty())
62     throw std::runtime_error("Error: Sprite wihtout actions.");
63 }
64
65 SpriteData::~SpriteData()
66 {
67   for(Actions::iterator i=actions.begin(); i != actions.end(); ++i)
68     delete i->second;
69 }
70
71 void
72 SpriteData::parse_action(const lisp::Lisp* lisp)
73 {
74   Action* action = new Action;
75
76   if(!lisp->get("name", action->name)) {
77     if(!actions.empty())
78       throw std::runtime_error(
79           "If there are more than one action, they need names!");
80   }
81   lisp->get("x-offset", action->x_offset);
82   lisp->get("y-offset", action->y_offset);
83   lisp->get("z-order", action->z_order);
84   lisp->get("fps", action->fps);
85
86   std::string mirror_action;
87   lisp->get("mirror-action", mirror_action);
88   if(!mirror_action.empty()) {
89     Action* act_tmp = get_action(mirror_action);
90     if(act_tmp == NULL) {
91       throw std::runtime_error("Could not mirror action. Action not found\n"
92                    "Mirror actions must be defined after the real one!");
93     } else {
94       for(int i = 0; static_cast<unsigned int>(i) < act_tmp->surfaces.size();
95           i++) {
96         Surface* surface = new Surface(sdl_surface_from_sdl_surface(
97               act_tmp->surfaces[i]->impl->get_sdl_surface(), true), true);
98         surface->apply_filter(HORIZONTAL_FLIP_FILTER);
99         action->surfaces.push_back(surface);
100       }
101     }
102   } else { // Load images
103     std::vector<std::string> images;
104     if(!lisp->get_vector("images", images)) {
105       std::stringstream msg;
106       msg << "Sprite '" << name << "' contains no images in action '"
107           << action->name << "'.";
108       throw std::runtime_error(msg.str());
109     }
110
111     for(std::vector<std::string>::size_type i = 0; i < images.size(); i++) {
112       action->surfaces.push_back(
113           new Surface(datadir + "/images/" + images[i], true));
114     }
115   }
116   actions[action->name] = action;
117 }
118
119 SpriteData::Action*
120 SpriteData::get_action(std::string act)
121 {
122   Actions::iterator i = actions.find(act);
123   if(i == actions.end()) {
124     return 0;
125   }
126   return i->second;
127 }
128