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.
26 #include "../app/globals.h"
27 #include "../app/setup.h"
28 #include "../special/sprite.h"
29 #include "../video/drawing_context.h"
34 Sprite::Sprite(SpriteData& newdata)
37 action = data.actions.begin()->second;
41 Sprite::Sprite(const Sprite& other)
42 : data(other.data), frame(other.frame),
43 animation_loops(other.animation_loops), last_tick(other.last_tick),
44 action(other.action), next_action(other.next_action)
53 Sprite::set_action(std::string name)
55 if(!next_action.empty() && animation_loops > 0) {
59 SpriteData::Action* newaction = data.get_action(name);
67 Sprite::start_animation(int loops)
70 animation_loops = loops;
77 last_tick = SDL_GetTicks();
78 animation_reversed = false;
84 Sprite::check_animation()
86 return animation_loops;
90 Sprite::reverse_animation(bool reverse)
92 animation_reversed = reverse;
94 if(animation_reversed)
95 frame = get_frames()-1;
103 if(animation_loops == 0)
105 if(frame >= get_frames() || frame < 0)
110 float frame_inc = (action->fps/1000.0) * (SDL_GetTicks() - last_tick);
111 last_tick = SDL_GetTicks();
113 if(animation_reversed)
118 if(animation_reversed) {
119 if(frame < 0 || frame >= (float)get_frames()) {
120 // last case can happen when not used reverse_animation()
121 float excedent = frame - 0;
122 frame = get_frames() - 1;
123 if(animation_loops > 0)
126 if(animation_loops == 0 && !next_action.empty())
128 set_action(next_action);
133 if(fabsf(excedent) < get_frames())
139 if(frame >= (float)get_frames())
141 float excedent = frame - get_frames();
143 if(animation_loops > 0)
146 if(animation_loops == 0 && !next_action.empty())
148 set_action(next_action);
153 if(excedent < get_frames())
160 Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
161 Uint32 drawing_effect)
166 if((int)frame >= get_frames() || (int)frame < 0)
167 std::cerr << "Warning: frame out of range: " << (int)frame
168 << "/" << get_frames() << " at " << get_name()
169 << "/" << get_action_name() << std::endl;
171 context.draw_surface(action->surfaces[(int)frame],
172 pos - Vector(action->x_offset, action->y_offset),
173 layer + action->z_order, drawing_effect);
177 Sprite::draw_part(DrawingContext& context, const Vector& source,
178 const Vector& size, const Vector& pos, int layer, Uint32 drawing_effect)
183 if((int)frame >= get_frames() || (int)frame < 0)
184 std::cerr << "Warning: frame out of range: " << (int)frame
185 << "/" << get_frames() << " at sprite: " << get_name()
186 << "/" << get_action_name() << std::endl;
188 context.draw_surface_part(action->surfaces[(int)frame], source, size,
189 pos - Vector(action->x_offset, action->y_offset),
190 layer + action->z_order, drawing_effect);
194 Sprite::get_width() const
196 return action->surfaces[get_frame()]->w;
200 Sprite::get_height() const
202 return action->surfaces[get_frame()]->h;