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.h"
29 Sprite::Sprite(SpriteData& newdata)
30 : data(newdata), frame(0), animation_loops(-1)
32 action = data.get_action("normal");
34 action = data.actions.begin()->second;
35 last_ticks = SDL_GetTicks();
38 Sprite::Sprite(const Sprite& other)
39 : data(other.data), frame(other.frame),
40 animation_loops(other.animation_loops),
43 last_ticks = SDL_GetTicks();
51 Sprite::set_action(std::string name, int loops)
53 if(action && action->name == name)
56 SpriteData::Action* newaction = data.get_action(name);
59 std::cerr << "Action '" << name << "' not found.\n";
65 animation_loops = loops;
70 Sprite::check_animation()
72 return animation_loops == 0;
78 if(animation_loops == 0)
81 Uint32 ticks = SDL_GetTicks();
82 float frame_inc = action->fps * float(ticks - last_ticks)/1000.0;
87 if(frame >= get_frames()) {
88 frame = fmodf(frame+get_frames(), get_frames());
91 if(animation_loops == 0)
97 Sprite::draw(DrawingContext& context, const Vector& pos, int layer)
102 if((int)frame >= get_frames() || (int)frame < 0)
103 std::cerr << "Warning: frame out of range: " << (int)frame
104 << "/" << get_frames() << " at " << get_name()
105 << "/" << get_action_name() << std::endl;
107 context.draw_surface(action->surfaces[(int)frame],
108 pos - Vector(action->x_offset, action->y_offset),
109 layer + action->z_order);
113 Sprite::draw_part(DrawingContext& context, const Vector& source,
114 const Vector& size, const Vector& pos, int layer)
119 if((int)frame >= get_frames() || (int)frame < 0)
120 std::cerr << "Warning: frame out of range: " << (int)frame
121 << "/" << get_frames() << " at sprite: " << get_name()
122 << "/" << get_action_name() << std::endl;
124 context.draw_surface_part(action->surfaces[(int)frame], source, size,
125 pos - Vector(action->x_offset, action->y_offset),
126 layer + action->z_order);
130 Sprite::get_width() const
132 return action->surfaces[get_frame()]->w;
136 Sprite::get_height() const
138 return action->surfaces[get_frame()]->h;