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")
45 Termination::abort("Error: Sprite wihtout name.", "");
47 Termination::abort("Error: Sprite wihtout actions.", "");
52 for(Actions::iterator i_act = actions.begin(); i_act != actions.end(); ++i_act)
54 for(std::vector<Surface*>::iterator i_sur = i_act->second->surfaces.begin();
55 i_sur != i_act->second->surfaces.end(); ++i_sur)
62 Sprite::parse_action(LispReader& lispreader)
66 init_defaults(action);
68 if(!lispreader.read_string("name", action->name))
70 Termination::abort("Error: If there are more than one action, they need names!", "");
71 lispreader.read_int("x-hotspot", action->x_hotspot);
72 lispreader.read_int("y-hotspot", action->y_hotspot);
73 lispreader.read_float("fps", action->fps);
75 std::vector<std::string> images;
76 if(!lispreader.read_string_vector("images", images))
77 Termination::abort("Sprite contains no images: ", action->name.c_str());
79 for(std::vector<std::string>::size_type i = 0; i < images.size(); ++i)
81 action->surfaces.push_back(
82 new Surface(datadir + "/images/" + images[i], true));
85 action->frame_delay = 1000.0f/action->fps;
87 actions[action->name] = action;
91 Sprite::init_defaults(Action* act)
96 act->frame_delay = 1000.0f/act->fps;
101 Sprite::set_action(std::string& act)
103 Actions::iterator i = actions.find(act);
108 Sprite::update(float /*delta*/)
111 //std::cout << "Delta: " << delta << std::endl;
115 Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
116 Uint32 drawing_effect)
118 time = SDL_GetTicks();
119 unsigned int frame = get_current_frame();
121 if (frame < action->surfaces.size())
123 Surface* surface = action->surfaces[frame];
125 context.draw_surface(surface, pos - Vector(action->x_hotspot, action->y_hotspot), layer, drawing_effect);
131 Sprite::draw_part(float sx, float sy, float x, float y, float w, float h)
133 time = SDL_GetTicks();
134 unsigned int frame = get_current_frame();
136 if (frame < surfaces.size())
137 surfaces[frame]->draw_part(sx, sy, x - x_hotspot, y - y_hotspot, w, h);
148 Sprite::get_current_frame() const
150 unsigned int frame = static_cast<int>(fmodf(time, action->surfaces.size()*action->frame_delay)/action->frame_delay);
151 return frame % action->surfaces.size();
155 Sprite::get_width() const
157 return action->surfaces[get_current_frame()]->w;
161 Sprite::get_height() const
163 return action->surfaces[get_current_frame()]->h;