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