Improved collision detection by taking movement into account. Fixed long standing...
[supertux.git] / lib / special / sprite.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.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 #include <config.h>
20
21 #include <iostream>
22 #include <cmath>
23 #include <cassert>
24 #include <stdexcept>
25
26 #include "../app/globals.h"
27 #include "../app/setup.h"
28 #include "../special/sprite.h"
29 #include "../video/drawing_context.h"
30
31 namespace SuperTux
32 {
33
34 Sprite::Sprite(SpriteData& newdata)
35   : data(newdata)
36 {
37   action = data.actions.begin()->second;
38   reset();
39 }
40
41 Sprite::Sprite(const Sprite& other)
42   : data(other.data), frame(other.frame),
43     animation_loops(other.animation_loops), last_tick(other.last_tick),
44     action(other.action), next_action(other.next_action)
45 {
46 }
47
48 Sprite::~Sprite()
49 {
50 }
51
52 void
53 Sprite::set_action(std::string name)
54 {
55   if(!next_action.empty() && animation_loops > 0) {
56     next_action = name;
57     return;
58   }
59   SpriteData::Action* newaction = data.get_action(name);
60   if(!action)
61     return;
62
63   action = newaction;
64 }
65
66 void
67 Sprite::start_animation(int loops)
68 {
69   reset();
70   animation_loops = loops;
71 }
72
73 void
74 Sprite::reset()
75 {
76   frame = 0;
77   last_tick = SDL_GetTicks();
78   animation_reversed = false;
79   animation_loops = -1;
80   next_action.clear();
81 }
82
83 bool
84 Sprite::check_animation()
85 {
86   return animation_loops;
87 }
88
89 void
90 Sprite::reverse_animation(bool reverse)
91 {
92   animation_reversed = reverse;
93
94   if(animation_reversed)
95     frame = get_frames()-1;
96   else
97     frame = 0;
98 }
99
100 void
101 Sprite::update()
102 {
103   if(animation_loops == 0)
104   {
105     if(frame >= get_frames() || frame < 0)
106       frame = 0;
107     return;
108   }
109
110   float frame_inc = (action->fps/1000.0) * (SDL_GetTicks() - last_tick);
111   last_tick = SDL_GetTicks();
112
113   if(animation_reversed)
114     frame -= frame_inc;
115   else
116     frame += frame_inc;
117
118   if(animation_reversed) {
119     if(frame < 0 || frame >= (float)get_frames()) {
120       // last case can happen when not used reverse_animation()
121       float excedent = frame - 0;
122       frame = get_frames() - 1;
123       if(animation_loops > 0)
124       {
125         animation_loops--;
126         if(animation_loops == 0 && !next_action.empty())
127         {
128           set_action(next_action);
129           start_animation(-1);
130         }
131       }
132
133       if(fabsf(excedent) < get_frames())
134         frame += excedent;
135     }
136   }
137   else
138   {
139     if(frame >= (float)get_frames())
140     {
141       float excedent = frame - get_frames();
142       frame = 0;
143       if(animation_loops > 0)
144       {
145         animation_loops--;
146         if(animation_loops == 0 && !next_action.empty())
147         {
148           set_action(next_action);
149           start_animation(-1);
150         }
151       }
152
153       if(excedent < get_frames())
154         frame += excedent;
155     }
156   }
157 }
158
159 void
160 Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
161     Uint32 drawing_effect)
162 {
163   assert(action != 0);
164   update();
165
166   if((int)frame >= get_frames() || (int)frame < 0)
167     std::cerr << "Warning: frame out of range: " << (int)frame
168               << "/" << get_frames() << " at " << get_name()
169               << "/" << get_action_name() << std::endl;
170   else
171     context.draw_surface(action->surfaces[(int)frame],
172             pos - Vector(action->x_offset, action->y_offset),
173             layer + action->z_order, drawing_effect);
174 }
175
176 void
177 Sprite::draw_part(DrawingContext& context, const Vector& source,
178     const Vector& size, const Vector& pos, int layer, Uint32 drawing_effect)
179 {
180   assert(action != 0);
181   update();
182
183   if((int)frame >= get_frames() || (int)frame < 0)
184     std::cerr << "Warning: frame out of range: " << (int)frame
185               << "/" << get_frames() << " at sprite: " << get_name()
186               << "/" << get_action_name() << std::endl;
187   else
188     context.draw_surface_part(action->surfaces[(int)frame], source, size,
189             pos - Vector(action->x_offset, action->y_offset),
190             layer + action->z_order, drawing_effect);
191 }
192
193 int
194 Sprite::get_width() const
195 {
196   return action->surfaces[get_frame()]->w;
197 }
198
199 int
200 Sprite::get_height() const
201 {
202   return action->surfaces[get_frame()]->h;
203 }
204
205 }
206
207 /* EOF */