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