This should fix animations. (bugs in the line of bomb explosions)
[supertux.git] / lib / special / sprite.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
20 #include <iostream>
21 #include <cmath>
22
23 #include "../app/globals.h"
24 #include "../app/setup.h"
25 #include "../special/sprite.h"
26 #include "../video/drawing_context.h"
27
28 using namespace SuperTux;
29
30 Sprite::Sprite(lisp_object_t* cur)
31 {
32   for(; !lisp_nil_p(cur); cur = lisp_cdr(cur))
33     {
34     std::string token = lisp_symbol(lisp_car(lisp_car(cur)));
35     lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur)));
36     LispReader reader(lisp_cdr(lisp_car(cur)));
37
38     if(token == "name")
39       name = lisp_string(data);
40     else if(token == "action")
41       parse_action(reader);
42     else
43       std::cerr << "Warning: Unknown sprite field: " << token << std::endl;
44     }
45
46   if(name.empty())
47     Termination::abort("Error: Sprite wihtout name.", "");
48   if(actions.empty())
49     Termination::abort("Error: Sprite wihtout actions.", "");
50 }
51
52 Sprite::~Sprite()
53 {
54   for(Actions::iterator i_act = actions.begin(); i_act != actions.end(); ++i_act)
55     {
56     for(std::vector<Surface*>::iterator i_sur = i_act->second->surfaces.begin();
57         i_sur != i_act->second->surfaces.end(); ++i_sur)
58       delete *i_sur;
59     delete i_act->second;
60     }
61 }
62
63 void
64 Sprite::parse_action(LispReader& lispreader)
65 {
66   action = new Action;
67
68   init_defaults(action);
69
70   if(!lispreader.read_string("name", action->name))
71     if(!actions.empty())
72       Termination::abort("Error: If there are more than one action, they need names!", "");
73   lispreader.read_int("x-offset", action->x_offset);
74   lispreader.read_int("y-offset", action->y_offset);
75   lispreader.read_int("z-order", action->z_order);
76   lispreader.read_float("fps",     action->fps);
77
78   std::vector<std::string> images;
79   if(!lispreader.read_string_vector("images", images))
80     Termination::abort("Sprite contains no images: ", action->name.c_str());
81
82   for(std::vector<std::string>::size_type i = 0; i < images.size(); i++)
83     {
84       action->surfaces.push_back(
85           new Surface(datadir + "/images/" + images[i], true));
86     }
87
88   // TODO: add a top filter entry
89   std::vector <int> mask_color;
90   lispreader.read_int_vector("apply-mask", mask_color);
91   if(mask_color.size() == 4)
92     {
93     for(std::vector<Surface*>::iterator i = action->surfaces.begin();
94         i < action->surfaces.end(); i++)
95       {
96         (*i)->apply_mask(Color(mask_color));
97       }
98     }
99
100   actions[action->name] = action;
101 }
102
103 /*void Sprite::parse_filter(LispReader& lispreader)
104 {
105
106 }*/
107
108 void
109 Sprite::init_defaults(Action* act)
110 {
111   act->x_offset = 0;
112   act->y_offset = 0;
113   act->z_order = 0;
114   act->fps = 10;
115
116   start_animation(-1);
117 }
118
119 void
120 Sprite::set_action(std::string act)
121 {
122 if(!next_action.empty() && animation_loops > 0)
123   {
124   next_action = act;
125   return;
126   }
127 Actions::iterator i = actions.find(act);
128 if(i == actions.end())
129   {
130   std::cerr << "Warning: Action '" << act << "' not found on Sprite '" << name << "'\n";
131   return;
132   }
133 action = i->second;
134
135 if((int)frame >= get_frames())
136   frame = 0;
137 }
138
139 void
140 Sprite::start_animation(int loops)
141 {
142 reset();
143 animation_loops = loops;
144 }
145
146 void
147 Sprite::reset()
148 {
149 frame = 0;
150 last_tick = SDL_GetTicks();
151 animation_reversed = false;
152 next_action.clear();
153 }
154
155 bool
156 Sprite::check_animation()
157 {
158 return animation_loops;
159 }
160
161 void
162 Sprite::reverse_animation(bool reverse)
163 {
164 animation_reversed = reverse;
165
166 if(animation_reversed)
167   frame = get_frames()-1;
168 else
169   frame = 0;
170 }
171
172 void
173 Sprite::update()
174 {
175 if(animation_loops == 0)
176   return;
177
178 float frame_inc = (action->fps/1000.0) * (SDL_GetTicks() - last_tick);
179 last_tick = SDL_GetTicks();
180
181 if(animation_reversed)
182   frame -= frame_inc;
183 else
184   frame += frame_inc;
185
186 if(animation_reversed)
187   {
188   float excedent = frame - 0;
189   if((int)excedent < 0 || excedent >= get_frames())
190     {  // last case can happen when not used reverse_animation()
191     frame = get_frames() - 1;
192     if(animation_loops > 0)
193       {
194       animation_loops--;
195       if(animation_loops == 0 && !next_action.empty())
196         {
197         set_action(next_action);
198         start_animation(-1);
199         }
200       }
201
202     if(fabsf(excedent) < get_frames())
203       frame += excedent;
204     }
205   }
206 else
207   {
208   float excedent = frame - (get_frames()+1);
209   if((int)excedent >= 0)
210     {
211     frame = 0;
212     if(animation_loops > 0)
213       {
214       animation_loops--;
215       if(animation_loops == 0 && !next_action.empty())
216         {
217         set_action(next_action);
218         start_animation(-1);
219         }
220       }
221
222     if(excedent < get_frames())
223       frame += excedent;
224     }
225   }
226 }
227
228 void
229 Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
230     Uint32 drawing_effect)
231 {
232   update();
233
234   if((int)frame >= get_frames() || (int)frame < 0)
235     std::cerr << "Warning: frame out of range: " << (int)frame
236               << "/" << get_frames() << " at " << get_name()
237               << "/" << get_action_name() << std::endl;
238   else
239     context.draw_surface(action->surfaces[(int)frame],
240             pos - Vector(action->x_offset, action->y_offset), layer + action->z_order,
241             drawing_effect);
242 }
243
244 void
245 Sprite::draw_part(DrawingContext& context, const Vector& source, const Vector& size,
246                   const Vector& pos, int layer, Uint32 drawing_effect)
247 {
248   update();
249
250   if((int)frame >= get_frames() || (int)frame < 0)
251     std::cerr << "Warning: frame out of range: " << (int)frame
252               << "/" << get_frames() << " at sprite: " << get_name()
253               << "/" << get_action_name() << std::endl;
254   else
255     context.draw_surface_part(action->surfaces[(int)frame], source, size,
256             pos - Vector(action->x_offset, action->y_offset), layer + action->z_order,
257             drawing_effect);
258 }
259
260 int
261 Sprite::get_width()
262 {
263   return action->surfaces[get_frame()]->w;
264 }
265
266 int
267 Sprite::get_height()
268 {
269   return action->surfaces[get_frame()]->h;
270 }
271
272 /* EOF */