4 // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
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.
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.
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.
23 #include "../app/globals.h"
24 #include "../app/setup.h"
25 #include "../special/sprite.h"
26 #include "../video/drawing_context.h"
28 using namespace SuperTux;
30 Sprite::Sprite(lisp_object_t* cur)
32 for(; !lisp_nil_p(cur); cur = lisp_cdr(cur))
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)));
39 name = lisp_string(data);
40 else if(token == "action")
43 std::cerr << "Warning: Unknown sprite field: " << token << std::endl;
47 Termination::abort("Error: Sprite wihtout name.", "");
49 Termination::abort("Error: Sprite wihtout actions.", "");
54 for(Actions::iterator i_act = actions.begin(); i_act != actions.end(); ++i_act)
56 for(std::vector<Surface*>::iterator i_sur = i_act->second->surfaces.begin();
57 i_sur != i_act->second->surfaces.end(); ++i_sur)
64 Sprite::parse_action(LispReader& lispreader)
68 init_defaults(action);
70 if(!lispreader.read_string("name", action->name))
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);
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());
82 for(std::vector<std::string>::size_type i = 0; i < images.size(); i++)
84 action->surfaces.push_back(
85 new Surface(datadir + "/images/" + images[i], true));
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)
93 for(std::vector<Surface*>::iterator i = action->surfaces.begin();
94 i < action->surfaces.end(); i++)
96 (*i)->apply_mask(Color(mask_color));
100 actions[action->name] = action;
103 /*void Sprite::parse_filter(LispReader& lispreader)
109 Sprite::init_defaults(Action* act)
120 Sprite::set_action(std::string act)
122 if(!next_action.empty() && animation_loops > 0)
127 Actions::iterator i = actions.find(act);
128 if(i == actions.end())
130 std::cerr << "Warning: Action '" << act << "' not found on Sprite '" << name << "'\n";
137 Sprite::start_animation(int loops)
140 animation_loops = loops;
147 last_tick = SDL_GetTicks();
148 animation_reversed = false;
153 Sprite::check_animation()
155 return animation_loops;
159 Sprite::reverse_animation(bool reverse)
161 animation_reversed = reverse;
163 if(animation_reversed)
164 frame = get_frames()-1;
172 if(animation_loops == 0)
175 float frame_inc = (action->fps/1000.0) * (SDL_GetTicks() - last_tick);
176 last_tick = SDL_GetTicks();
178 if(animation_reversed)
183 if(animation_reversed)
185 float excedent = frame - 0;
186 if((int)excedent < 0 || excedent >= get_frames())
187 { // last case can happen when not used reverse_animation()
188 frame = get_frames() - 1;
189 if(animation_loops > 0)
192 if(animation_loops == 0 && !next_action.empty())
194 set_action(next_action);
199 if(fabsf(excedent) < get_frames())
205 float excedent = frame - action->surfaces.size();
206 if((int)excedent >= 0)
209 if(animation_loops > 0)
212 if(animation_loops == 0 && !next_action.empty())
214 set_action(next_action);
219 if(excedent < get_frames())
226 Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
227 Uint32 drawing_effect)
231 if((int)frame >= get_frames() || (int)frame < 0)
232 std::cerr << "Warning: frame out of range: " << (int)frame
233 << "/" << get_frames() << " at sprite: " << get_name()
234 << "/" << get_action_name() << std::endl;
236 context.draw_surface(action->surfaces[(int)frame],
237 pos - Vector(action->x_offset, action->y_offset), layer + action->z_order,
242 Sprite::draw_part(DrawingContext& context, const Vector& source, const Vector& size,
243 const Vector& pos, int layer, Uint32 drawing_effect)
247 if((int)frame >= get_frames() || (int)frame < 0)
248 std::cerr << "Warning: frame out of range: " << (int)frame
249 << "/" << get_frames() << " at sprite: " << get_name()
250 << "/" << get_action_name() << std::endl;
252 context.draw_surface_part(action->surfaces[(int)frame], source, size,
253 pos - Vector(action->x_offset, action->y_offset), layer + action->z_order,
260 return action->surfaces[get_frame()]->w;
266 return action->surfaces[get_frame()]->h;