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 = game_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 = game_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::set_action_continued(const std::string& name)
81 if(action && action->name == name)
84 SpriteData::Action* newaction = data.get_action(name);
86 log_debug << "Action '" << name << "' not found." << std::endl;
91 if(frame >= get_frames()) {
92 frame = fmodf(frame, get_frames());
94 if (animation_loops > 0) animation_loops--;
96 frame = get_frames()-1;
101 Sprite::animation_done()
103 return animation_loops == 0;
112 float frame_inc = action->fps * (game_time - last_ticks);
113 last_ticks = game_time;
117 if(frame >= get_frames()) {
118 frame = fmodf(frame, get_frames());
122 frame = get_frames()-1;
127 Sprite::draw(DrawingContext& context, const Vector& pos, int layer)
132 if((int)frame >= get_frames() || (int)frame < 0)
133 log_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
135 context.draw_surface(action->surfaces[(int)frame],
136 pos - Vector(action->x_offset, action->y_offset),
140 layer + action->z_order);
144 Sprite::draw_part(DrawingContext& context, const Vector& source,
145 const Vector& size, const Vector& pos, int layer)
150 int frameidx = (int) frame;
152 if(frameidx >= get_frames() || frameidx < 0) {
154 // in optimized mode we get some small rounding errors in floating point
155 // number sometimes...
156 log_warning << "frame out of range: " << frameidx << "/" << get_frames() << " at sprite: " << get_name() << "/" << get_action() << std::endl;
158 frameidx = get_frames() - 1;
161 context.draw_surface_part(action->surfaces[frameidx], source, size,
162 pos - Vector(action->x_offset, action->y_offset),
163 layer + action->z_order);
167 Sprite::get_width() const
169 return (int) action->surfaces[get_frame()]->get_width();
173 Sprite::get_height() const
175 return (int) action->surfaces[get_frame()]->get_height();
179 Sprite::get_current_hitbox_x_offset() const
181 return action->x_offset;
185 Sprite::get_current_hitbox_y_offset() const
187 return action->y_offset;
191 Sprite::get_current_hitbox_width() const
193 return action->hitbox_w;
197 Sprite::get_current_hitbox_height() const
199 return action->hitbox_h;
203 Sprite::get_current_hitbox() const
205 return Rect(action->x_offset, action->y_offset, action->x_offset + action->hitbox_w, action->y_offset + action->hitbox_h);
209 Sprite::set_fps(float new_fps)
211 action->fps = new_fps;
215 Sprite::set_angle(float a)
221 Sprite::get_angle() const
227 Sprite::set_color(const Color& c)
233 Sprite::get_color() const
239 Sprite::set_blend(const Blend& b)
245 Sprite::get_blend() const