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)
27 color(1.0f, 1.0f, 1.0f, 1.0f)
29 action = data.get_action("normal");
31 action = data.actions.begin()->second;
32 last_ticks = game_time;
35 Sprite::Sprite(const Sprite& other)
36 : data(other.data), frame(other.frame),
37 animation_loops(other.animation_loops),
39 color(1.0f, 1.0f, 1.0f, 1.0f),
42 last_ticks = game_time;
50 Sprite::set_action(const std::string& name, int loops)
52 if(action && action->name == name)
55 SpriteData::Action* newaction = data.get_action(name);
57 log_debug << "Action '" << name << "' not found." << std::endl;
62 animation_loops = loops;
67 Sprite::set_action_continued(const std::string& name)
69 if(action && action->name == name)
72 SpriteData::Action* newaction = data.get_action(name);
74 log_debug << "Action '" << name << "' not found." << std::endl;
79 if(frame >= get_frames()) {
80 frame = fmodf(frame, get_frames());
82 if (animation_loops > 0) animation_loops--;
84 frame = get_frames()-1;
89 Sprite::animation_done()
91 return animation_loops == 0;
100 float frame_inc = action->fps * (game_time - last_ticks);
101 last_ticks = game_time;
105 if(frame >= get_frames()) {
106 frame = fmodf(frame, get_frames());
110 frame = get_frames()-1;
115 Sprite::draw(DrawingContext& context, const Vector& pos, int layer)
120 if((int)frame >= get_frames() || (int)frame < 0)
121 log_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
123 context.draw_surface(action->surfaces[(int)frame],
124 pos - Vector(action->x_offset, action->y_offset),
128 layer + action->z_order);
132 Sprite::draw_part(DrawingContext& context, const Vector& source,
133 const Vector& size, const Vector& pos, int layer)
138 int frameidx = (int) frame;
140 if(frameidx >= get_frames() || frameidx < 0) {
142 // in optimized mode we get some small rounding errors in floating point
143 // number sometimes...
144 log_warning << "frame out of range: " << frameidx << "/" << get_frames() << " at sprite: " << get_name() << "/" << get_action() << std::endl;
146 frameidx = get_frames() - 1;
149 context.draw_surface_part(action->surfaces[frameidx], source, size,
150 pos - Vector(action->x_offset, action->y_offset),
151 layer + action->z_order);
155 Sprite::get_width() const
157 return (int) action->surfaces[get_frame()]->get_width();
161 Sprite::get_height() const
163 return (int) action->surfaces[get_frame()]->get_height();
167 Sprite::get_current_hitbox_x_offset() const
169 return action->x_offset;
173 Sprite::get_current_hitbox_y_offset() const
175 return action->y_offset;
179 Sprite::get_current_hitbox_width() const
181 return action->hitbox_w;
185 Sprite::get_current_hitbox_height() const
187 return action->hitbox_h;
191 Sprite::get_current_hitbox() const
193 return Rect(action->x_offset, action->y_offset, action->x_offset + action->hitbox_w, action->y_offset + action->hitbox_h);
197 Sprite::set_fps(float new_fps)
199 action->fps = new_fps;
203 Sprite::set_angle(float a)
209 Sprite::get_angle() const
215 Sprite::set_color(const Color& c)
221 Sprite::get_color() const
227 Sprite::set_blend(const Blend& b)
233 Sprite::get_blend() const