Added support for one time animations to Sprite.
[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   for(; !lisp_nil_p(cur); cur = lisp_cdr(cur))
33     {
34     std::string token = lisp_symbol(lisp_car(lisp_car(cur)));
35     lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur)));
36     LispReader reader(lisp_cdr(lisp_car(cur)));
37
38     if(token == "name")
39       name = lisp_string(data);
40     else if(token == "action")
41       parse_action(reader);
42     }
43
44   if(name.empty())
45     Termination::abort("Error: Sprite wihtout name.", "");
46   if(actions.empty())
47     Termination::abort("Error: Sprite wihtout actions.", "");
48 }
49
50 Sprite::~Sprite()
51 {
52   for(Actions::iterator i_act = actions.begin(); i_act != actions.end(); ++i_act)
53     {
54     for(std::vector<Surface*>::iterator i_sur = i_act->second->surfaces.begin();
55         i_sur != i_act->second->surfaces.end(); ++i_sur)
56       delete *i_sur;
57     delete i_act->second;
58     }
59 }
60
61 void
62 Sprite::parse_action(LispReader& lispreader)
63 {
64   action = new Action;
65
66   init_defaults(action);
67
68   if(!lispreader.read_string("name", action->name))
69     if(!actions.empty())
70       Termination::abort("Error: If there are more than one action, they need names!", "");
71   lispreader.read_int("x-hotspot", action->x_hotspot);
72   lispreader.read_int("y-hotspot", action->y_hotspot);
73   lispreader.read_float("fps",     action->fps);
74
75   std::vector<std::string> images;
76   if(!lispreader.read_string_vector("images", images))
77     Termination::abort("Sprite contains no images: ", action->name.c_str());
78
79   for(std::vector<std::string>::size_type i = 0; i < images.size(); ++i)
80     {
81       action->surfaces.push_back(
82           new Surface(datadir + "/images/" + images[i], true));
83     }        
84
85   actions[action->name] = action;
86 }
87
88 void
89 Sprite::init_defaults(Action* act)
90 {
91   act->x_hotspot = 0;
92   act->y_hotspot = 0;
93   act->fps = 10;
94
95   act->animation_loops = 0;
96   last_tick = 0;
97 }
98
99 void
100 Sprite::set_action(std::string act)
101 {
102 Actions::iterator i = actions.find(act);
103 action = i->second;
104 }
105
106 void
107 Sprite::start_animation(int loops)
108 {
109 action->animation_loops = loops;
110 reset();
111 }
112
113 void
114 Sprite::reset()
115 {
116 frame = 0;
117 last_tick = SDL_GetTicks();
118 }
119
120 bool
121 Sprite::check_animation()
122 {
123 return action->animation_loops;
124 }
125
126 void
127 Sprite::update()
128 {
129 frame += (action->fps/1000) * (SDL_GetTicks() - last_tick);
130 last_tick = SDL_GetTicks();
131
132 if((unsigned int)frame >= action->surfaces.size())
133   {
134   frame = 0;
135   if(action->animation_loops > 0)
136     action->animation_loops--;
137   }
138 }
139
140 void
141 Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
142     Uint32 drawing_effect)
143 {
144   update();
145
146   context.draw_surface(action->surfaces[(int)frame],
147           pos - Vector(action->x_hotspot, action->y_hotspot), layer, drawing_effect);
148 }
149
150 #if 0
151 void
152 Sprite::draw_part(float sx, float sy, float x, float y, float w, float h)
153 {
154   time = SDL_GetTicks();
155   unsigned int frame = get_current_frame();
156
157   if (frame < surfaces.size())
158     surfaces[frame]->draw_part(sx, sy, x - x_hotspot, y - y_hotspot, w, h);
159 }
160 #endif
161
162 int
163 Sprite::get_width()
164 {
165   return action->surfaces[get_current_frame()]->w;
166 }
167
168 int
169 Sprite::get_height()
170 {
171   return action->surfaces[get_current_frame()]->h;
172 }
173
174 /* EOF */