- tweaked bullet and endsequence
[supertux.git] / src / 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
20 #include <iostream>
21 #include <math.h>
22 #include "globals.h"
23 #include "sprite.h"
24
25 Sprite::Sprite(lisp_object_t* cur)
26 {
27   init_defaults();
28
29   LispReader reader(cur);
30
31   reader.read_string("name",   &name);
32   reader.read_int("x-hotspot", &x_hotspot);
33   reader.read_int("y-hotspot", &y_hotspot);
34   reader.read_float("fps",     &fps);
35   std::vector<std::string> images;
36   reader.read_string_vector("images", &images);
37   surfaces.resize(images.size());
38
39   for(std::vector<std::string>::size_type i = 0; i < images.size(); ++i)
40     {
41       surfaces[i] = new Surface(datadir + "/images/" + images[i], USE_ALPHA);
42     }        
43
44   frame_delay = 1000.0f/fps;
45 }
46
47 void
48 Sprite::init_defaults()
49 {
50   x_hotspot = 0;
51   y_hotspot = 0;
52   fps = 10;
53   time = 0;
54   frame_delay = 1000.0f/fps;
55 }
56
57 void
58 Sprite::update(float /*delta*/)
59 {
60   //time += 10*delta;
61   //std::cout << "Delta: " << delta << std::endl;
62 }
63
64 void
65 Sprite::draw(float x, float y)
66 {
67   time = SDL_GetTicks();
68   unsigned int frame = get_current_frame();
69
70   if (frame < surfaces.size())
71     surfaces[frame]->draw(x - x_hotspot, y - y_hotspot);
72 }
73
74 void
75 Sprite::draw_part(float sx, float sy, float x, float y, float w, float h)
76 {
77   time = SDL_GetTicks();
78   unsigned int frame = get_current_frame();
79
80   if (frame < surfaces.size())
81     surfaces[frame]->draw_part(sx, sy, x - x_hotspot, y - y_hotspot, w, h);
82 }
83
84 void
85 Sprite::reset()
86 {
87   time = 0;
88 }
89
90 int
91 Sprite::get_current_frame() const
92 {
93   unsigned int frame = static_cast<int>(fmodf(time, surfaces.size()*frame_delay)/frame_delay);
94   return frame % surfaces.size();
95 }
96
97 int
98 Sprite::get_width() const
99 {
100   return surfaces[get_current_frame()]->w;
101 }
102
103 int
104 Sprite::get_height() const
105 {
106   return surfaces[get_current_frame()]->h;
107 }
108
109 /* EOF */