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