- More work on scripting interface
[supertux.git] / src / sprite / 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 "sprite.h"
27 #include "video/drawing_context.h"
28
29 Sprite::Sprite(SpriteData& newdata)
30   : data(newdata), frame(0), animation_loops(-1)
31 {
32   action = data.get_action("normal");
33   if(!action)
34     action = data.actions.begin()->second;
35   last_ticks = SDL_GetTicks();
36 }
37
38 Sprite::Sprite(const Sprite& other)
39   : data(other.data), frame(other.frame),
40     animation_loops(other.animation_loops),
41     action(other.action)
42 {
43   last_ticks = SDL_GetTicks();
44 }
45
46 Sprite::~Sprite()
47 {
48 }
49
50 void
51 Sprite::set_action(std::string name, int loops)
52 {
53   if(action && action->name == name)
54     return;
55
56   SpriteData::Action* newaction = data.get_action(name);
57   if(!newaction) {
58 #ifdef DEBUG
59     std::cerr << "Action '" << name << "' not found.\n";
60 #endif
61     return;
62   }
63
64   action = newaction;
65   animation_loops = loops;
66   frame = 0;
67 }
68
69 bool
70 Sprite::check_animation()
71 {
72   return animation_loops == 0;
73 }
74
75 void
76 Sprite::update()
77 {
78   if(animation_loops == 0)
79     return;
80
81   Uint32 ticks = SDL_GetTicks();
82   float frame_inc = action->fps * float(ticks - last_ticks)/1000.0;
83   last_ticks = ticks;
84
85   frame += frame_inc;
86
87   if(frame >= get_frames()) {
88     frame = fmodf(frame+get_frames(), get_frames());
89     
90     animation_loops--;
91     if(animation_loops == 0)
92       frame = 0;
93   }
94 }
95
96 void
97 Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
98     Uint32 drawing_effect)
99 {
100   assert(action != 0);
101   update();
102
103   if((int)frame >= get_frames() || (int)frame < 0)
104     std::cerr << "Warning: frame out of range: " << (int)frame
105               << "/" << get_frames() << " at " << get_name()
106               << "/" << get_action_name() << std::endl;
107   else
108     context.draw_surface(action->surfaces[(int)frame],
109             pos - Vector(action->x_offset, action->y_offset),
110             layer + action->z_order, drawing_effect);
111 }
112
113 void
114 Sprite::draw_part(DrawingContext& context, const Vector& source,
115     const Vector& size, const Vector& pos, int layer, Uint32 drawing_effect)
116 {
117   assert(action != 0);
118   update();
119
120   if((int)frame >= get_frames() || (int)frame < 0)
121     std::cerr << "Warning: frame out of range: " << (int)frame
122               << "/" << get_frames() << " at sprite: " << get_name()
123               << "/" << get_action_name() << std::endl;
124   else
125     context.draw_surface_part(action->surfaces[(int)frame], source, size,
126             pos - Vector(action->x_offset, action->y_offset),
127             layer + action->z_order, drawing_effect);
128 }
129
130 int
131 Sprite::get_width() const
132 {
133   return action->surfaces[get_frame()]->w;
134 }
135
136 int
137 Sprite::get_height() const
138 {
139   return action->surfaces[get_frame()]->h;
140 }
141
142