SpriteParticle constructor now takes an AnchorPoint for positioning the sprite
[supertux.git] / src / sprite / sprite.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include <iostream>
23 #include <cmath>
24 #include <cassert>
25 #include <stdexcept>
26
27 #include "sprite.hpp"
28 #include "video/drawing_context.hpp"
29 #include "log.hpp"
30 #include "timer.hpp"
31
32 Sprite::Sprite(SpriteData& newdata)
33   : data(newdata), frame(0), animation_loops(-1)
34 {
35   action = data.get_action("normal");
36   if(!action)
37     action = data.actions.begin()->second;
38   last_ticks = real_time;
39 }
40
41 Sprite::Sprite(const Sprite& other)
42   : data(other.data), frame(other.frame),
43     animation_loops(other.animation_loops),
44     action(other.action)
45 {
46   last_ticks = real_time;
47 }
48
49 Sprite::~Sprite()
50 {
51 }
52
53 void
54 Sprite::set_action(const std::string& name, int loops)
55 {
56   if(action && action->name == name)
57     return;
58
59   SpriteData::Action* newaction = data.get_action(name);
60   if(!newaction) {
61     log_debug << "Action '" << name << "' not found." << std::endl;
62     return;
63   }
64
65   action = newaction;
66   animation_loops = loops;
67   frame = 0;
68 }
69
70 bool
71 Sprite::animation_done()
72 {
73   return animation_loops == 0;
74 }
75
76 void
77 Sprite::update()
78 {
79   if(animation_done())
80     return;
81
82   float frame_inc = action->fps * (real_time - last_ticks);
83   last_ticks = real_time;
84
85   frame += frame_inc;
86
87   if(frame >= get_frames()) {
88     frame = fmodf(frame, get_frames());
89       
90     animation_loops--;
91     if(animation_done())
92       frame = get_frames()-1;
93   }
94 }
95
96 void
97 Sprite::draw(DrawingContext& context, const Vector& pos, int layer)
98 {
99   assert(action != 0);
100   update();
101
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() << std::endl;
104   else
105     context.draw_surface(action->surfaces[(int)frame],
106             pos - Vector(action->x_offset, action->y_offset),
107             layer + action->z_order);
108 }
109
110 void
111 Sprite::draw_part(DrawingContext& context, const Vector& source,
112     const Vector& size, const Vector& pos, int layer)
113 {
114   assert(action != 0);
115   update();
116
117   int frameidx = (int) frame;
118   
119   if(frameidx >= get_frames() || frameidx < 0) {
120 #ifndef DEBUG
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() << std::endl;
124 #endif
125     frameidx = get_frames() - 1;
126   }
127     
128   context.draw_surface_part(action->surfaces[frameidx], source, size,
129       pos - Vector(action->x_offset, action->y_offset),
130       layer + action->z_order);
131 }
132
133 int
134 Sprite::get_width() const
135 {
136   return (int) action->surfaces[get_frame()]->get_width();
137 }
138
139 int
140 Sprite::get_height() const
141 {
142   return (int) action->surfaces[get_frame()]->get_height();
143 }
144
145 float
146 Sprite::get_current_hitbox_x_offset() const
147 {
148   return action->x_offset;
149 }
150
151 float
152 Sprite::get_current_hitbox_y_offset() const
153 {
154   return action->y_offset;
155 }
156
157 float
158 Sprite::get_current_hitbox_width() const
159 {
160   return action->hitbox_w;
161 }
162
163 float
164 Sprite::get_current_hitbox_height() const
165 {
166   return action->hitbox_h;
167 }
168
169 Rect
170 Sprite::get_current_hitbox() const
171 {
172   return Rect(action->x_offset, action->y_offset, action->x_offset + action->hitbox_w, action->y_offset + action->hitbox_h);
173 }
174
175 void
176 Sprite::set_fps(float new_fps)
177 {
178   action->fps = new_fps;
179 }
180
181