Removed vertical flipping drawing hacks.
[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 #include "setup.h"
25
26 Sprite::Sprite(lisp_object_t* cur)
27 {
28   init_defaults();
29
30   LispReader reader(cur);
31
32   if(!reader.read_string("name",   &name))
33     st_abort("Sprite wihtout name", "");
34   reader.read_int("x-hotspot", &x_hotspot);
35   reader.read_int("y-hotspot", &y_hotspot);
36   reader.read_float("fps",     &fps);
37
38   std::vector<std::string> images;
39   if(!reader.read_string_vector("images", &images))
40     st_abort("Sprite contains no images: ", name.c_str());
41
42   for(std::vector<std::string>::size_type i = 0; i < images.size(); ++i)
43     {
44       surfaces.push_back(
45           new Surface(datadir + "/images/" + images[i], USE_ALPHA));
46     }        
47
48   frame_delay = 1000.0f/fps;
49 }
50
51 Sprite::~Sprite()
52 {
53   for(std::vector<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
54       ++i)
55     delete *i;
56 }
57
58 void
59 Sprite::init_defaults()
60 {
61   x_hotspot = 0;
62   y_hotspot = 0;
63   fps = 10;
64   time = 0;
65   frame_delay = 1000.0f/fps;
66 }
67
68 void
69 Sprite::update(float /*delta*/)
70 {
71   //time += 10*delta;
72   //std::cout << "Delta: " << delta << std::endl;
73 }
74
75 void
76 Sprite::draw(float x, float y, int special_drawing)
77 {
78   time = SDL_GetTicks();
79   unsigned int frame = get_current_frame();
80
81   if (frame < surfaces.size())
82     {
83     if(special_drawing == SD_SEMI_TRANSPARENT)
84       surfaces[frame]->draw(x - x_hotspot, y - y_hotspot, 128);
85     if(special_drawing == SD_VERTICAL_FLIP)
86       surfaces[frame]->draw(x - x_hotspot, y - y_hotspot, 255, true);
87     else
88       surfaces[frame]->draw(x - x_hotspot, y - y_hotspot);
89     }
90 }
91
92 void
93 Sprite::draw_part(float sx, float sy, float x, float y, float w, float h)
94 {
95   time = SDL_GetTicks();
96   unsigned int frame = get_current_frame();
97
98   if (frame < surfaces.size())
99     surfaces[frame]->draw_part(sx, sy, x - x_hotspot, y - y_hotspot, w, h);
100 }
101
102 void
103 Sprite::reset()
104 {
105   time = 0;
106 }
107
108 int
109 Sprite::get_current_frame() const
110 {
111   unsigned int frame = static_cast<int>(fmodf(time, surfaces.size()*frame_delay)/frame_delay);
112   return frame % surfaces.size();
113 }
114
115 int
116 Sprite::get_width() const
117 {
118   return surfaces[get_current_frame()]->w;
119 }
120
121 int
122 Sprite::get_height() const
123 {
124   return surfaces[get_current_frame()]->h;
125 }
126
127 /* EOF */