2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "sprite/sprite.hpp"
20 #include "supertux/timer.hpp"
22 Sprite::Sprite(SpriteData& newdata) :
28 color(1.0f, 1.0f, 1.0f, 1.0f),
32 action = data.get_action("normal");
34 action = data.actions.begin()->second;
35 last_ticks = game_time;
38 Sprite::Sprite(const Sprite& other) :
41 animation_loops(other.animation_loops),
42 last_ticks(game_time),
43 angle(0.0f), // FIXME: this can't be right
44 color(1.0f, 1.0f, 1.0f, 1.0f),
57 return SpritePtr(new Sprite(*this));
61 Sprite::set_action(const std::string& name, int loops)
63 if(action && action->name == name)
66 const SpriteData::Action* newaction = data.get_action(name);
68 log_debug << "Action '" << name << "' not found." << std::endl;
73 animation_loops = loops;
78 Sprite::set_action_continued(const std::string& name)
80 if(action && action->name == name)
83 const SpriteData::Action* newaction = data.get_action(name);
85 log_debug << "Action '" << name << "' not found." << std::endl;
90 while(frame >= get_frames()) {
91 frame -= get_frames();
94 frame = get_frames()-1;
100 Sprite::animation_done()
102 return animation_loops == 0;
108 if(animation_done()) {
109 frame = get_frames()-1;
113 float frame_inc = action->fps * (game_time - last_ticks);
114 last_ticks = game_time;
118 while(frame >= get_frames()) {
119 frame -= get_frames();
122 frame = get_frames()-1;
128 Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
129 DrawingEffect effect)
134 if((int)frame >= get_frames() || (int)frame < 0)
135 log_warning << "frame out of range: " << frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
137 context.set_drawing_effect(effect);
138 context.draw_surface(action->surfaces[(int)frame],
139 pos - Vector(action->x_offset, action->y_offset),
143 layer + action->z_order);
148 Sprite::draw_part(DrawingContext& context, const Vector& source,
149 const Vector& size, const Vector& pos, int layer)
154 int frameidx = (int) frame;
156 if(frameidx >= get_frames() || frameidx < 0) {
157 // in optimized mode we get some small rounding errors in floating point
158 // number sometimes...
159 frameidx = get_frames() - 1;
162 context.draw_surface_part(action->surfaces[frameidx], source, size,
163 pos - Vector(action->x_offset, action->y_offset),
164 layer + action->z_order);
168 Sprite::get_width() const
170 if((int)frame >= get_frames() || (int)frame < 0)
172 log_warning << "frame out of range: " << frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
177 return (int) action->surfaces[get_frame()]->get_width();
182 Sprite::get_height() const
184 if((int)frame >= get_frames() || (int)frame < 0)
186 log_warning << "frame out of range: " << frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
191 return (int) action->surfaces[get_frame()]->get_height();
196 Sprite::get_current_hitbox_x_offset() const
198 return action->x_offset;
202 Sprite::get_current_hitbox_y_offset() const
204 return action->y_offset;
208 Sprite::get_current_hitbox_width() const
210 return action->hitbox_w;
214 Sprite::get_current_hitbox_height() const
216 return action->hitbox_h;
220 Sprite::get_current_hitbox() const
222 return Rectf(action->x_offset, action->y_offset, action->x_offset + action->hitbox_w, action->y_offset + action->hitbox_h);
226 Sprite::set_angle(float a)
232 Sprite::get_angle() const
238 Sprite::set_color(const Color& c)
244 Sprite::get_color() const
250 Sprite::set_blend(const Blend& b)
256 Sprite::get_blend() const