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