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