Fix frame out of range error for -O2 release build (may or may not fix on other syste...
[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   while(frame >= get_frames()) {
91     frame -= get_frames();
92     animation_loops--;
93     if(animation_done())
94       frame = get_frames()-1;
95       break;
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     frame = get_frames()-1;
110     return;
111   }
112
113   float frame_inc = action->fps * (game_time - last_ticks);
114   last_ticks = game_time;
115
116   frame += frame_inc;
117
118   while(frame >= get_frames()) {
119     frame -= get_frames();
120     animation_loops--;
121     if(animation_done())
122       frame = get_frames()-1;
123       break;
124   }
125 }
126
127 void
128 Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
129              DrawingEffect effect)
130 {
131   assert(action != 0);
132   update();
133
134   if((int)frame >= get_frames() || (int)frame < 0)
135     log_warning << "frame out of range: " << frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
136   else {
137     context.set_drawing_effect(effect);
138     context.draw_surface(action->surfaces[(int)frame],
139                          pos - Vector(action->x_offset, action->y_offset),
140                          angle,
141                          color,
142                          blend,
143                          layer + action->z_order);
144   }
145 }
146
147 void
148 Sprite::draw_part(DrawingContext& context, const Vector& source,
149                   const Vector& size, const Vector& pos, int layer)
150 {
151   assert(action != 0);
152   update();
153
154   int frameidx = (int) frame;
155
156   if(frameidx >= get_frames() || frameidx < 0) {
157     // in optimized mode we get some small rounding errors in floating point
158     // number sometimes...
159     frameidx = get_frames() - 1;
160   }
161
162   context.draw_surface_part(action->surfaces[frameidx], source, size,
163                             pos - Vector(action->x_offset, action->y_offset),
164                             layer + action->z_order);
165 }
166
167 int
168 Sprite::get_width() const
169 {
170   if((int)frame >= get_frames() || (int)frame < 0)
171   {
172     log_warning << "frame out of range: " << frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
173     return 0;
174   }
175   else
176   {
177     return (int) action->surfaces[get_frame()]->get_width();
178   }
179 }
180
181 int
182 Sprite::get_height() const
183 {
184   if((int)frame >= get_frames() || (int)frame < 0)
185   {
186     log_warning << "frame out of range: " << frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
187     return 0;
188   }
189   else
190   {
191   return (int) action->surfaces[get_frame()]->get_height();
192   }
193 }
194
195 float
196 Sprite::get_current_hitbox_x_offset() const
197 {
198   return action->x_offset;
199 }
200
201 float
202 Sprite::get_current_hitbox_y_offset() const
203 {
204   return action->y_offset;
205 }
206
207 float
208 Sprite::get_current_hitbox_width() const
209 {
210   return action->hitbox_w;
211 }
212
213 float
214 Sprite::get_current_hitbox_height() const
215 {
216   return action->hitbox_h;
217 }
218
219 Rectf
220 Sprite::get_current_hitbox() const
221 {
222   return Rectf(action->x_offset, action->y_offset, action->x_offset + action->hitbox_w, action->y_offset + action->hitbox_h);
223 }
224
225 void
226 Sprite::set_angle(float a)
227 {
228   angle = a;
229 }
230
231 float
232 Sprite::get_angle() const
233 {
234   return angle;
235 }
236
237 void
238 Sprite::set_color(const Color& c)
239 {
240   color = c;
241 }
242
243 Color
244 Sprite::get_color() const
245 {
246   return color;
247 }
248
249 void
250 Sprite::set_blend(const Blend& b)
251 {
252   blend = b;
253 }
254
255 Blend
256 Sprite::get_blend() const
257 {
258   return blend;
259 }
260
261 /* EOF */