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),
55 Sprite::set_action(const std::string& name, int loops)
57 if(action && action->name == name)
60 SpriteData::Action* newaction = data.get_action(name);
62 log_debug << "Action '" << name << "' not found." << std::endl;
67 animation_loops = loops;
72 Sprite::set_action_continued(const std::string& name)
74 if(action && action->name == name)
77 SpriteData::Action* newaction = data.get_action(name);
79 log_debug << "Action '" << name << "' not found." << std::endl;
84 if(frame >= get_frames()) {
85 frame = fmodf(frame, get_frames());
87 if (animation_loops > 0) animation_loops--;
89 frame = get_frames()-1;
94 Sprite::animation_done()
96 return animation_loops == 0;
105 float frame_inc = action->fps * (game_time - last_ticks);
106 last_ticks = game_time;
110 if(frame >= get_frames()) {
111 frame = fmodf(frame, get_frames());
115 frame = get_frames()-1;
120 Sprite::draw(DrawingContext& context, const Vector& pos, int layer)
125 if((int)frame >= get_frames() || (int)frame < 0)
126 log_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
128 context.draw_surface(action->surfaces[(int)frame],
129 pos - Vector(action->x_offset, action->y_offset),
133 layer + action->z_order);
137 Sprite::draw_part(DrawingContext& context, const Vector& source,
138 const Vector& size, const Vector& pos, int layer)
143 int frameidx = (int) frame;
145 if(frameidx >= get_frames() || frameidx < 0) {
147 // in optimized mode we get some small rounding errors in floating point
148 // number sometimes...
149 log_warning << "frame out of range: " << frameidx << "/" << get_frames() << " at sprite: " << get_name() << "/" << get_action() << std::endl;
151 frameidx = get_frames() - 1;
154 context.draw_surface_part(action->surfaces[frameidx], source, size,
155 pos - Vector(action->x_offset, action->y_offset),
156 layer + action->z_order);
160 Sprite::get_width() const
162 if((int)frame >= get_frames() || (int)frame < 0)
164 log_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
169 return (int) action->surfaces[get_frame()]->get_width();
174 Sprite::get_height() const
176 if((int)frame >= get_frames() || (int)frame < 0)
178 log_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
183 return (int) action->surfaces[get_frame()]->get_height();
188 Sprite::get_current_hitbox_x_offset() const
190 return action->x_offset;
194 Sprite::get_current_hitbox_y_offset() const
196 return action->y_offset;
200 Sprite::get_current_hitbox_width() const
202 return action->hitbox_w;
206 Sprite::get_current_hitbox_height() const
208 return action->hitbox_h;
212 Sprite::get_current_hitbox() const
214 return Rectf(action->x_offset, action->y_offset, action->x_offset + action->hitbox_w, action->y_offset + action->hitbox_h);
218 Sprite::set_fps(float new_fps)
220 action->fps = new_fps;
224 Sprite::set_angle(float a)
230 Sprite::get_angle() const
236 Sprite::set_color(const Color& c)
242 Sprite::get_color() const
248 Sprite::set_blend(const Blend& b)
254 Sprite::get_blend() const