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