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/>.
17 #include "sprite/sprite.hpp"
22 #include "supertux/timer.hpp"
24 Sprite::Sprite(SpriteData& newdata) :
31 color(1.0f, 1.0f, 1.0f, 1.0f),
33 action(data.get_action("normal"))
36 action = data.actions.begin()->second;
37 last_ticks = game_time;
40 Sprite::Sprite(const Sprite& other) :
43 frameidx(other.frameidx),
44 animation_loops(other.animation_loops),
45 last_ticks(game_time),
46 angle(0.0f), // FIXME: this can't be right
47 color(1.0f, 1.0f, 1.0f, 1.0f),
60 return SpritePtr(new Sprite(*this));
64 Sprite::set_action(const std::string& name, int loops)
66 if(action && action->name == name)
69 const SpriteData::Action* newaction = data.get_action(name);
71 log_debug << "Action '" << name << "' not found." << std::endl;
76 animation_loops = loops;
82 Sprite::set_action_continued(const std::string& name)
84 if(action && action->name == name)
87 const SpriteData::Action* newaction = data.get_action(name);
89 log_debug << "Action '" << name << "' not found." << std::endl;
98 Sprite::animation_done()
100 return animation_loops == 0;
106 float frame_inc = action->fps * (game_time - last_ticks);
107 last_ticks = game_time;
111 while(frame >= 1.0f) {
116 while(frameidx >= get_frames()) {
117 frameidx -= get_frames();
119 if(animation_done()) {
124 if(animation_done()) {
126 frameidx = get_frames()-1;
129 assert(frameidx < get_frames());
133 Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
134 DrawingEffect effect)
139 context.set_drawing_effect(effect);
140 context.draw_surface(action->surfaces[frameidx],
141 pos - Vector(action->x_offset, action->y_offset),
145 layer + action->z_order);
149 Sprite::draw_part(DrawingContext& context, const Vector& source,
150 const Vector& size, const Vector& pos, int layer)
155 context.draw_surface_part(action->surfaces[frameidx], source, size,
156 pos - Vector(action->x_offset, action->y_offset),
157 layer + action->z_order);
161 Sprite::get_width() const
163 assert(frameidx < get_frames());
164 return (int) action->surfaces[get_frame()]->get_width();
168 Sprite::get_height() const
170 assert(frameidx < get_frames());
171 return (int) action->surfaces[get_frame()]->get_height();
175 Sprite::get_current_hitbox_x_offset() const
177 return action->x_offset;
181 Sprite::get_current_hitbox_y_offset() const
183 return action->y_offset;
187 Sprite::get_current_hitbox_width() const
189 return action->hitbox_w;
193 Sprite::get_current_hitbox_height() const
195 return action->hitbox_h;
199 Sprite::get_current_hitbox() const
201 return Rectf(action->x_offset, action->y_offset, action->x_offset + action->hitbox_w, action->y_offset + action->hitbox_h);
205 Sprite::set_angle(float a)
211 Sprite::get_angle() const
217 Sprite::set_color(const Color& c)
223 Sprite::get_color() const
229 Sprite::set_blend(const Blend& b)
235 Sprite::get_blend() const