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)
39 color(1.0f, 1.0f, 1.0f, 1.0f)
41 action = data.get_action("normal");
43 action = data.actions.begin()->second;
44 last_ticks = real_time;
47 Sprite::Sprite(const Sprite& other)
48 : data(other.data), frame(other.frame),
49 animation_loops(other.animation_loops),
51 color(1.0f, 1.0f, 1.0f, 1.0f),
54 last_ticks = real_time;
62 Sprite::set_action(const std::string& name, int loops)
64 if(action && action->name == name)
67 SpriteData::Action* newaction = data.get_action(name);
69 log_debug << "Action '" << name << "' not found." << std::endl;
74 animation_loops = loops;
79 Sprite::animation_done()
81 return animation_loops == 0;
90 float frame_inc = action->fps * (real_time - last_ticks);
91 last_ticks = real_time;
95 if(frame >= get_frames()) {
96 frame = fmodf(frame, get_frames());
100 frame = get_frames()-1;
105 Sprite::draw(DrawingContext& context, const Vector& pos, int layer)
110 if((int)frame >= get_frames() || (int)frame < 0)
111 log_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
113 context.draw_surface(action->surfaces[(int)frame],
114 pos - Vector(action->x_offset, action->y_offset),
118 layer + action->z_order);
122 Sprite::draw_part(DrawingContext& context, const Vector& source,
123 const Vector& size, const Vector& pos, int layer)
128 int frameidx = (int) frame;
130 if(frameidx >= get_frames() || frameidx < 0) {
132 // in optimized mode we get some small rounding errors in floating point
133 // number sometimes...
134 log_warning << "frame out of range: " << frameidx << "/" << get_frames() << " at sprite: " << get_name() << "/" << get_action() << std::endl;
136 frameidx = get_frames() - 1;
139 context.draw_surface_part(action->surfaces[frameidx], source, size,
140 pos - Vector(action->x_offset, action->y_offset),
141 layer + action->z_order);
145 Sprite::get_width() const
147 return (int) action->surfaces[get_frame()]->get_width();
151 Sprite::get_height() const
153 return (int) action->surfaces[get_frame()]->get_height();
157 Sprite::get_current_hitbox_x_offset() const
159 return action->x_offset;
163 Sprite::get_current_hitbox_y_offset() const
165 return action->y_offset;
169 Sprite::get_current_hitbox_width() const
171 return action->hitbox_w;
175 Sprite::get_current_hitbox_height() const
177 return action->hitbox_h;
181 Sprite::get_current_hitbox() const
183 return Rect(action->x_offset, action->y_offset, action->x_offset + action->hitbox_w, action->y_offset + action->hitbox_h);
187 Sprite::set_fps(float new_fps)
189 action->fps = new_fps;
193 Sprite::set_angle(float a)
199 Sprite::get_angle() const
205 Sprite::set_color(const Color& c)
211 Sprite::get_color() const
217 Sprite::set_blend(const Blend& b)
223 Sprite::get_blend() const