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.
27 #include "video/drawing_context.hpp"
30 Sprite::Sprite(SpriteData& newdata)
31 : data(newdata), frame(0), animation_loops(-1)
33 action = data.get_action("normal");
35 action = data.actions.begin()->second;
36 last_ticks = SDL_GetTicks();
39 Sprite::Sprite(const Sprite& other)
40 : data(other.data), frame(other.frame),
41 animation_loops(other.animation_loops),
44 last_ticks = SDL_GetTicks();
52 Sprite::set_action(const std::string& name, int loops)
54 if(action && action->name == name)
57 SpriteData::Action* newaction = data.get_action(name);
59 msg_debug << "Action '" << name << "' not found." << std::endl;
64 animation_loops = loops;
69 Sprite::animation_done()
71 return animation_loops == 0;
80 Uint32 ticks = SDL_GetTicks();
81 float frame_inc = action->fps * float(ticks - last_ticks)/1000.0;
86 if(frame >= get_frames()) {
87 frame = fmodf(frame, get_frames());
91 frame = get_frames()-1;
96 Sprite::draw(DrawingContext& context, const Vector& pos, int layer)
101 if((int)frame >= get_frames() || (int)frame < 0)
102 msg_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at " << get_name() << "/" << get_action_name() << std::endl;
104 context.draw_surface(action->surfaces[(int)frame],
105 pos - Vector(action->x_offset, action->y_offset),
106 layer + action->z_order);
110 Sprite::draw_part(DrawingContext& context, const Vector& source,
111 const Vector& size, const Vector& pos, int layer)
116 if((int)frame >= get_frames() || (int)frame < 0)
117 msg_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at sprite: " << get_name() << "/" << get_action_name() << std::endl;
119 context.draw_surface_part(action->surfaces[(int)frame], source, size,
120 pos - Vector(action->x_offset, action->y_offset),
121 layer + action->z_order);
125 Sprite::get_width() const
127 return (int) action->surfaces[get_frame()]->get_width();
131 Sprite::get_height() const
133 return (int) action->surfaces[get_frame()]->get_height();
137 Sprite::set_fps(float new_fps)
139 action->fps = new_fps;