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 "../app/globals.h"
26 #include "../app/setup.h"
27 #include "../special/sprite.h"
28 #include "../video/drawing_context.h"
33 Sprite::Sprite(SpriteData& newdata)
36 action = data.actions.begin()->second;
40 Sprite::Sprite(const Sprite& other)
41 : data(other.data), frame(other.frame),
42 animation_loops(other.animation_loops), last_tick(other.last_tick),
43 action(other.action), next_action(other.next_action)
52 Sprite::set_action(std::string name)
54 if(!next_action.empty() && animation_loops > 0) {
58 SpriteData::Action* newaction = data.get_action(name);
66 Sprite::start_animation(int loops)
69 animation_loops = loops;
76 last_tick = SDL_GetTicks();
77 animation_reversed = false;
83 Sprite::check_animation()
85 return animation_loops;
89 Sprite::reverse_animation(bool reverse)
91 animation_reversed = reverse;
93 if(animation_reversed)
94 frame = get_frames()-1;
102 if(animation_loops == 0)
104 if(frame >= get_frames() || frame < 0)
109 float frame_inc = (action->fps/1000.0) * (SDL_GetTicks() - last_tick);
110 last_tick = SDL_GetTicks();
112 if(animation_reversed)
117 if(animation_reversed) {
118 if(frame < 0 || frame >= (float)get_frames()) {
119 // last case can happen when not used reverse_animation()
120 float excedent = frame - 0;
121 frame = get_frames() - 1;
122 if(animation_loops > 0)
125 if(animation_loops == 0 && !next_action.empty())
127 set_action(next_action);
132 if(fabsf(excedent) < get_frames())
138 if(frame >= (float)get_frames())
140 float excedent = frame - get_frames();
142 if(animation_loops > 0)
145 if(animation_loops == 0 && !next_action.empty())
147 set_action(next_action);
152 if(excedent < get_frames())
159 Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
160 Uint32 drawing_effect)
164 if((int)frame >= get_frames() || (int)frame < 0)
165 std::cerr << "Warning: frame out of range: " << (int)frame
166 << "/" << get_frames() << " at " << get_name()
167 << "/" << get_action_name() << std::endl;
169 context.draw_surface(action->surfaces[(int)frame],
170 pos - Vector(action->x_offset, action->y_offset),
171 layer + action->z_order, drawing_effect);
175 Sprite::draw_part(DrawingContext& context, const Vector& source,
176 const Vector& size, const Vector& pos, int layer, Uint32 drawing_effect)
180 if((int)frame >= get_frames() || (int)frame < 0)
181 std::cerr << "Warning: frame out of range: " << (int)frame
182 << "/" << get_frames() << " at sprite: " << get_name()
183 << "/" << get_action_name() << std::endl;
185 context.draw_surface_part(action->surfaces[(int)frame], source, size,
186 pos - Vector(action->x_offset, action->y_offset),
187 layer + action->z_order, drawing_effect);
191 Sprite::get_width() const
193 return action->surfaces[get_frame()]->w;
197 Sprite::get_height() const
199 return action->surfaces[get_frame()]->h;