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