e14621f80c5f78bbd5d177f7f2f28071111739bb
[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), frame(0), animation_loops(-1), angle(0.0f)
36 {
37   action = data.get_action("normal");
38   if(!action)
39     action = data.actions.begin()->second;
40   last_ticks = real_time;
41 }
42
43 Sprite::Sprite(const Sprite& other)
44   : data(other.data), frame(other.frame),
45     animation_loops(other.animation_loops),
46     action(other.action)
47 {
48   last_ticks = real_time;
49 }
50
51 Sprite::~Sprite()
52 {
53 }
54
55 void
56 Sprite::set_action(const std::string& name, int loops)
57 {
58   if(action && action->name == name)
59     return;
60
61   SpriteData::Action* newaction = data.get_action(name);
62   if(!newaction) {
63     log_debug << "Action '" << name << "' not found." << std::endl;
64     return;
65   }
66
67   action = newaction;
68   animation_loops = loops;
69   frame = 0;
70 }
71
72 bool
73 Sprite::animation_done()
74 {
75   return animation_loops == 0;
76 }
77
78 void
79 Sprite::update()
80 {
81   if(animation_done())
82     return;
83
84   float frame_inc = action->fps * (real_time - last_ticks);
85   last_ticks = real_time;
86
87   frame += frame_inc;
88
89   if(frame >= get_frames()) {
90     frame = fmodf(frame, get_frames());
91       
92     animation_loops--;
93     if(animation_done())
94       frame = get_frames()-1;
95   }
96 }
97
98 void
99 Sprite::draw(DrawingContext& context, const Vector& pos, int layer)
100 {
101   assert(action != 0);
102   update();
103
104   if((int)frame >= get_frames() || (int)frame < 0)
105     log_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at " << get_name() << "/" << get_action() << std::endl;
106   else
107     context.draw_surface(action->surfaces[(int)frame],
108                          pos - Vector(action->x_offset, action->y_offset),
109                          angle,
110                          layer + action->z_order);
111 }
112
113 void
114 Sprite::draw_part(DrawingContext& context, const Vector& source,
115     const Vector& size, const Vector& pos, int layer)
116 {
117   assert(action != 0);
118   update();
119
120   int frameidx = (int) frame;
121   
122   if(frameidx >= get_frames() || frameidx < 0) {
123 #ifndef DEBUG
124     // in optimized mode we get some small rounding errors in floating point
125     // number sometimes...
126     log_warning << "frame out of range: " << frameidx << "/" << get_frames() << " at sprite: " << get_name() << "/" << get_action() << std::endl;
127 #endif
128     frameidx = get_frames() - 1;
129   }
130     
131   context.draw_surface_part(action->surfaces[frameidx], source, size,
132       pos - Vector(action->x_offset, action->y_offset),
133       layer + action->z_order);
134 }
135
136 int
137 Sprite::get_width() const
138 {
139   return (int) action->surfaces[get_frame()]->get_width();
140 }
141
142 int
143 Sprite::get_height() const
144 {
145   return (int) action->surfaces[get_frame()]->get_height();
146 }
147
148 float
149 Sprite::get_current_hitbox_x_offset() const
150 {
151   return action->x_offset;
152 }
153
154 float
155 Sprite::get_current_hitbox_y_offset() const
156 {
157   return action->y_offset;
158 }
159
160 float
161 Sprite::get_current_hitbox_width() const
162 {
163   return action->hitbox_w;
164 }
165
166 float
167 Sprite::get_current_hitbox_height() const
168 {
169   return action->hitbox_h;
170 }
171
172 Rect
173 Sprite::get_current_hitbox() const
174 {
175   return Rect(action->x_offset, action->y_offset, action->x_offset + action->hitbox_w, action->y_offset + action->hitbox_h);
176 }
177
178 void
179 Sprite::set_fps(float new_fps)
180 {
181   action->fps = new_fps;
182 }
183
184 void
185 Sprite::set_angle(float a)
186 {
187   angle = a;
188 }
189
190 float
191 Sprite::get_angle() const
192 {
193   return angle;
194 }
195
196 /* EOF */