4 // Copyright (C) 2006 Matthias Braun <matze@braunis.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.
28 #include "video/surface.hpp"
30 #include "video/drawing_context.hpp"
34 Sprite::Sprite(SpriteData& newdata)
35 : data(newdata), frame(0), animation_loops(-1), angle(0.0f)
37 action = data.get_action("normal");
39 action = data.actions.begin()->second;
40 last_ticks = real_time;
43 Sprite::Sprite(const Sprite& other)
44 : data(other.data), frame(other.frame),
45 animation_loops(other.animation_loops),
49 last_ticks = real_time;
57 Sprite::set_action(const std::string& name, int loops)
59 if(action && action->name == name)
62 SpriteData::Action* newaction = data.get_action(name);
64 log_debug << "Action '" << name << "' not found." << std::endl;
69 animation_loops = loops;
74 Sprite::animation_done()
76 return animation_loops == 0;
85 float frame_inc = action->fps * (real_time - last_ticks);
86 last_ticks = real_time;
90 if(frame >= get_frames()) {
91 frame = fmodf(frame, get_frames());
95 frame = get_frames()-1;
100 Sprite::draw(DrawingContext& context, const Vector& pos, int layer)
105 if((int)frame >= get_frames() || (int)frame < 0)
106 log_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
108 context.draw_surface(action->surfaces[(int)frame],
109 pos - Vector(action->x_offset, action->y_offset),
111 layer + action->z_order);
115 Sprite::draw_part(DrawingContext& context, const Vector& source,
116 const Vector& size, const Vector& pos, int layer)
121 int frameidx = (int) frame;
123 if(frameidx >= get_frames() || frameidx < 0) {
125 // in optimized mode we get some small rounding errors in floating point
126 // number sometimes...
127 log_warning << "frame out of range: " << frameidx << "/" << get_frames() << " at sprite: " << get_name() << "/" << get_action() << std::endl;
129 frameidx = get_frames() - 1;
132 context.draw_surface_part(action->surfaces[frameidx], source, size,
133 pos - Vector(action->x_offset, action->y_offset),
134 layer + action->z_order);
138 Sprite::get_width() const
140 return (int) action->surfaces[get_frame()]->get_width();
144 Sprite::get_height() const
146 return (int) action->surfaces[get_frame()]->get_height();
150 Sprite::get_current_hitbox_x_offset() const
152 return action->x_offset;
156 Sprite::get_current_hitbox_y_offset() const
158 return action->y_offset;
162 Sprite::get_current_hitbox_width() const
164 return action->hitbox_w;
168 Sprite::get_current_hitbox_height() const
170 return action->hitbox_h;
174 Sprite::get_current_hitbox() const
176 return Rect(action->x_offset, action->y_offset, action->x_offset + action->hitbox_w, action->y_offset + action->hitbox_h);
180 Sprite::set_fps(float new_fps)
182 action->fps = new_fps;
186 Sprite::set_angle(float a)
192 Sprite::get_angle() const