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/drawing_context.hpp"
32 Sprite::Sprite(SpriteData& newdata)
33 : data(newdata), frame(0), animation_loops(-1)
35 action = data.get_action("normal");
37 action = data.actions.begin()->second;
38 last_ticks = real_time;
41 Sprite::Sprite(const Sprite& other)
42 : data(other.data), frame(other.frame),
43 animation_loops(other.animation_loops),
46 last_ticks = real_time;
54 Sprite::set_action(const std::string& name, int loops)
56 if(action && action->name == name)
59 SpriteData::Action* newaction = data.get_action(name);
61 log_debug << "Action '" << name << "' not found." << std::endl;
66 animation_loops = loops;
71 Sprite::animation_done()
73 return animation_loops == 0;
82 float frame_inc = action->fps * (real_time - last_ticks);
83 last_ticks = real_time;
87 if(frame >= get_frames()) {
88 frame = fmodf(frame, get_frames());
92 frame = get_frames()-1;
97 Sprite::draw(DrawingContext& context, const Vector& pos, int layer)
102 if((int)frame >= get_frames() || (int)frame < 0)
103 log_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at " << get_name() << "/" << get_action_name() << std::endl;
105 context.draw_surface(action->surfaces[(int)frame],
106 pos - Vector(action->x_offset, action->y_offset),
107 layer + action->z_order);
111 Sprite::draw_part(DrawingContext& context, const Vector& source,
112 const Vector& size, const Vector& pos, int layer)
117 int frameidx = (int) frame;
119 if(frameidx >= get_frames() || frameidx < 0) {
121 // in optimized mode we get some small rounding errors in floating point
122 // number sometimes...
123 log_warning << "frame out of range: " << frameidx << "/" << get_frames() << " at sprite: " << get_name() << "/" << get_action_name() << std::endl;
125 frameidx = get_frames() - 1;
128 context.draw_surface_part(action->surfaces[frameidx], source, size,
129 pos - Vector(action->x_offset, action->y_offset),
130 layer + action->z_order);
134 Sprite::get_width() const
136 return (int) action->surfaces[get_frame()]->get_width();
140 Sprite::get_height() const
142 return (int) action->surfaces[get_frame()]->get_height();
146 Sprite::set_fps(float new_fps)
148 action->fps = new_fps;