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