Resolves issue 0000306: Tux' graphic won't change direction while he is growing.
[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
28 #include "video/surface.hpp"
29 #include "sprite.hpp"
30 #include "video/drawing_context.hpp"
31 #include "log.hpp"
32 #include "timer.hpp"
33
34 Sprite::Sprite(SpriteData& newdata)
35   : data(newdata),
36     frame(0),
37     animation_loops(-1),
38     angle(0.0f),
39     color(1.0f, 1.0f, 1.0f, 1.0f)
40 {
41   action = data.get_action("normal");
42   if(!action)
43     action = data.actions.begin()->second;
44   last_ticks = game_time;
45 }
46
47 Sprite::Sprite(const Sprite& other)
48   : data(other.data), frame(other.frame),
49     animation_loops(other.animation_loops),
50     angle(0.0f),
51     color(1.0f, 1.0f, 1.0f, 1.0f),
52     action(other.action)
53 {
54   last_ticks = game_time;
55 }
56
57 Sprite::~Sprite()
58 {
59 }
60
61 void
62 Sprite::set_action(const std::string& name, int loops)
63 {
64   if(action && action->name == name)
65     return;
66
67   SpriteData::Action* newaction = data.get_action(name);
68   if(!newaction) {
69     log_debug << "Action '" << name << "' not found." << std::endl;
70     return;
71   }
72
73   action = newaction;
74   animation_loops = loops;
75   frame = 0;
76 }
77
78 void
79 Sprite::set_action_continued(const std::string& name)
80 {
81   if(action && action->name == name)
82     return;
83
84   SpriteData::Action* newaction = data.get_action(name);
85   if(!newaction) {
86     log_debug << "Action '" << name << "' not found." << std::endl;
87     return;
88   }
89
90   action = newaction;
91   if(frame >= get_frames()) {
92     frame = fmodf(frame, get_frames());
93
94     if (animation_loops > 0) animation_loops--;
95     if(animation_done())
96       frame = get_frames()-1;
97   }
98 }
99
100 bool
101 Sprite::animation_done()
102 {
103   return animation_loops == 0;
104 }
105
106 void
107 Sprite::update()
108 {
109   if(animation_done())
110     return;
111
112   float frame_inc = action->fps * (game_time - last_ticks);
113   last_ticks = game_time;
114
115   frame += frame_inc;
116
117   if(frame >= get_frames()) {
118     frame = fmodf(frame, get_frames());
119
120     animation_loops--;
121     if(animation_done())
122       frame = get_frames()-1;
123   }
124 }
125
126 void
127 Sprite::draw(DrawingContext& context, const Vector& pos, int layer)
128 {
129   assert(action != 0);
130   update();
131
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;
134   else
135     context.draw_surface(action->surfaces[(int)frame],
136                          pos - Vector(action->x_offset, action->y_offset),
137                          angle,
138                          color,
139                          blend,
140                          layer + action->z_order);
141 }
142
143 void
144 Sprite::draw_part(DrawingContext& context, const Vector& source,
145     const Vector& size, const Vector& pos, int layer)
146 {
147   assert(action != 0);
148   update();
149
150   int frameidx = (int) frame;
151
152   if(frameidx >= get_frames() || frameidx < 0) {
153 #ifndef DEBUG
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;
157 #endif
158     frameidx = get_frames() - 1;
159   }
160
161   context.draw_surface_part(action->surfaces[frameidx], source, size,
162       pos - Vector(action->x_offset, action->y_offset),
163       layer + action->z_order);
164 }
165
166 int
167 Sprite::get_width() const
168 {
169   return (int) action->surfaces[get_frame()]->get_width();
170 }
171
172 int
173 Sprite::get_height() const
174 {
175   return (int) action->surfaces[get_frame()]->get_height();
176 }
177
178 float
179 Sprite::get_current_hitbox_x_offset() const
180 {
181   return action->x_offset;
182 }
183
184 float
185 Sprite::get_current_hitbox_y_offset() const
186 {
187   return action->y_offset;
188 }
189
190 float
191 Sprite::get_current_hitbox_width() const
192 {
193   return action->hitbox_w;
194 }
195
196 float
197 Sprite::get_current_hitbox_height() const
198 {
199   return action->hitbox_h;
200 }
201
202 Rect
203 Sprite::get_current_hitbox() const
204 {
205   return Rect(action->x_offset, action->y_offset, action->x_offset + action->hitbox_w, action->y_offset + action->hitbox_h);
206 }
207
208 void
209 Sprite::set_fps(float new_fps)
210 {
211   action->fps = new_fps;
212 }
213
214 void
215 Sprite::set_angle(float a)
216 {
217   angle = a;
218 }
219
220 float
221 Sprite::get_angle() const
222 {
223   return angle;
224 }
225
226 void
227 Sprite::set_color(const Color& c)
228 {
229   color = c;
230 }
231
232 Color
233 Sprite::get_color() const
234 {
235   return color;
236 }
237
238 void
239 Sprite::set_blend(const Blend& b)
240 {
241   blend = b;
242 }
243
244 Blend
245 Sprite::get_blend() const
246 {
247   return blend;
248 }
249
250 /* EOF */