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.
25 #include "screen/drawing_context.h"
27 Sprite::Sprite(lisp_object_t* cur)
31 LispReader reader(cur);
33 if(!reader.read_string("name", name))
34 st_abort("Sprite wihtout name", "");
35 reader.read_int("x-hotspot", x_hotspot);
36 reader.read_int("y-hotspot", y_hotspot);
37 reader.read_float("fps", fps);
39 std::vector<std::string> images;
40 if(!reader.read_string_vector("images", images))
41 st_abort("Sprite contains no images: ", name.c_str());
43 for(std::vector<std::string>::size_type i = 0; i < images.size(); ++i)
46 new Surface(datadir + "/images/" + images[i], USE_ALPHA));
49 frame_delay = 1000.0f/fps;
54 for(std::vector<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
60 Sprite::init_defaults()
66 frame_delay = 1000.0f/fps;
70 Sprite::update(float /*delta*/)
73 //std::cout << "Delta: " << delta << std::endl;
77 Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
78 uint32_t drawing_effect)
80 time = SDL_GetTicks();
81 unsigned int frame = get_current_frame();
83 if (frame < surfaces.size())
85 Surface* surface = surfaces[frame];
87 context.draw_surface(surface, pos - Vector(x_hotspot, y_hotspot), layer, drawing_effect);
93 Sprite::draw_part(float sx, float sy, float x, float y, float w, float h)
95 time = SDL_GetTicks();
96 unsigned int frame = get_current_frame();
98 if (frame < surfaces.size())
99 surfaces[frame]->draw_part(sx, sy, x - x_hotspot, y - y_hotspot, w, h);
110 Sprite::get_current_frame() const
112 unsigned int frame = static_cast<int>(fmodf(time, surfaces.size()*frame_delay)/frame_delay);
113 return frame % surfaces.size();
117 Sprite::get_width() const
119 return surfaces[get_current_frame()]->w;
123 Sprite::get_height() const
125 return surfaces[get_current_frame()]->h;