01c41cfe87b287a82e2de79f48e5dc2387503f4a
[supertux.git] / src / sprite / sprite.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include <math.h>
18
19 #include "sprite/sprite.hpp"
20 #include "supertux/timer.hpp"
21
22 Sprite::Sprite(SpriteData& newdata) :
23   data(newdata),
24   frame(0),
25   animation_loops(-1),
26   last_ticks(),
27   angle(0.0f),
28   color(1.0f, 1.0f, 1.0f, 1.0f),
29   blend(),
30   action()
31 {
32   action = data.get_action("normal");
33   if(!action)
34     action = data.actions.begin()->second;
35   last_ticks = game_time;
36 }
37
38 Sprite::Sprite(const Sprite& other) :
39   data(other.data), 
40   frame(other.frame),
41   animation_loops(other.animation_loops),
42   last_ticks(game_time),
43   angle(0.0f), // FIXME: this can't be right
44   color(1.0f, 1.0f, 1.0f, 1.0f),
45   blend(),
46   action(other.action)
47 {
48 }
49
50 Sprite::~Sprite()
51 {
52 }
53
54 SpritePtr
55 Sprite::clone() const
56 {
57   return SpritePtr(new Sprite(*this));
58 }
59
60 void
61 Sprite::set_action(const std::string& name, int loops)
62 {
63   if(action && action->name == name)
64     return;
65
66   const SpriteData::Action* newaction = data.get_action(name);
67   if(!newaction) {
68     log_debug << "Action '" << name << "' not found." << std::endl;
69     return;
70   }
71
72   action = newaction;
73   animation_loops = loops;
74   frame = 0;
75 }
76
77 void
78 Sprite::set_action_continued(const std::string& name)
79 {
80   if(action && action->name == name)
81     return;
82
83   const SpriteData::Action* newaction = data.get_action(name);
84   if(!newaction) {
85     log_debug << "Action '" << name << "' not found." << std::endl;
86     return;
87   }
88
89   action = newaction;
90   if(frame >= get_frames()) {
91     frame = fmodf(frame, get_frames());
92
93     if (animation_loops > 0) animation_loops--;
94     if(animation_done())
95       frame = get_frames()-1;
96   }
97 }
98
99 bool
100 Sprite::animation_done()
101 {
102   return animation_loops == 0;
103 }
104
105 void
106 Sprite::update()
107 {
108   if(animation_done())
109     return;
110
111   float frame_inc = action->fps * (game_time - last_ticks);
112   last_ticks = game_time;
113
114   frame += frame_inc;
115
116   if(frame >= get_frames()) {
117     frame = fmodf(frame, get_frames());
118
119     animation_loops--;
120     if(animation_done())
121       frame = get_frames()-1;
122   }
123 }
124
125 void
126 Sprite::draw(DrawingContext& context, const Vector& pos, int layer)
127 {
128   assert(action != 0);
129   update();
130
131   if((int)frame >= get_frames() || (int)frame < 0)
132     log_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
133   else
134     context.draw_surface(action->surfaces[(int)frame],
135                          pos - Vector(action->x_offset, action->y_offset),
136                          angle,
137                          color,
138                          blend,
139                          layer + action->z_order);
140 }
141
142 void
143 Sprite::draw_part(DrawingContext& context, const Vector& source,
144                   const Vector& size, const Vector& pos, int layer)
145 {
146   assert(action != 0);
147   update();
148
149   int frameidx = (int) frame;
150
151   if(frameidx >= get_frames() || frameidx < 0) {
152     // in optimized mode we get some small rounding errors in floating point
153     // number sometimes...
154     frameidx = get_frames() - 1;
155   }
156
157   context.draw_surface_part(action->surfaces[frameidx], source, size,
158                             pos - Vector(action->x_offset, action->y_offset),
159                             layer + action->z_order);
160 }
161
162 int
163 Sprite::get_width() const
164 {
165   if((int)frame >= get_frames() || (int)frame < 0)
166   {
167     log_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
168     return 0;
169   }
170   else
171   {
172     return (int) action->surfaces[get_frame()]->get_width();
173   }
174 }
175
176 int
177 Sprite::get_height() const
178 {
179   if((int)frame >= get_frames() || (int)frame < 0)
180   {
181     log_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
182     return 0;
183   }
184   else
185   {
186   return (int) action->surfaces[get_frame()]->get_height();
187   }
188 }
189
190 float
191 Sprite::get_current_hitbox_x_offset() const
192 {
193   return action->x_offset;
194 }
195
196 float
197 Sprite::get_current_hitbox_y_offset() const
198 {
199   return action->y_offset;
200 }
201
202 float
203 Sprite::get_current_hitbox_width() const
204 {
205   return action->hitbox_w;
206 }
207
208 float
209 Sprite::get_current_hitbox_height() const
210 {
211   return action->hitbox_h;
212 }
213
214 Rectf
215 Sprite::get_current_hitbox() const
216 {
217   return Rectf(action->x_offset, action->y_offset, action->x_offset + action->hitbox_w, action->y_offset + action->hitbox_h);
218 }
219
220 void
221 Sprite::set_angle(float a)
222 {
223   angle = a;
224 }
225
226 float
227 Sprite::get_angle() const
228 {
229   return angle;
230 }
231
232 void
233 Sprite::set_color(const Color& c)
234 {
235   color = c;
236 }
237
238 Color
239 Sprite::get_color() const
240 {
241   return color;
242 }
243
244 void
245 Sprite::set_blend(const Blend& b)
246 {
247   blend = b;
248 }
249
250 Blend
251 Sprite::get_blend() const
252 {
253   return blend;
254 }
255
256 /* EOF */