05db0c60a6af2faa8079d9bfb4fba5572af63b7a
[supertux.git] / lib / special / sprite.h
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 #ifndef SUPERTUX_SPRITE_H
21 #define SUPERTUX_SPRITE_H
22
23 #include <string>
24 #include <vector>
25 #include <map>
26
27 #include "../utils/lispreader.h"
28 #include "../video/surface.h"
29 #include "../math/vector.h"
30
31 namespace SuperTux
32   {
33
34   class Sprite
35     {
36     private:
37
38       struct Action
39         {
40         std::string name;
41
42         int x_hotspot;
43         int y_hotspot;
44
45         /** Frames per second */
46         float fps;
47
48         int animation_loops;
49
50         std::vector<Surface*> surfaces;
51         };
52
53     public:
54       /** cur has to be a pointer to data in the form of ((x-hotspot 5)
55           (y-hotspot 10) ...) */
56       Sprite(lisp_object_t* cur);
57       ~Sprite();
58
59       /** Draw sprite, automatically calculates next frame */
60       void draw(DrawingContext& context, const Vector& pos, int layer,
61                 Uint32 drawing_effect = NONE_EFFECT);
62
63       /** Set action (or state) */
64       void set_action(std::string act);
65
66       /* Handling animations */
67       void start_animation(int loops);
68       bool check_animation();
69
70       float get_fps()
71         { return action->fps; }
72       int get_frames()
73         { return action->surfaces.size(); }
74       std::string get_name() const
75         { return name; }
76       int get_width();
77       int get_height();
78
79       int get_current_frame()
80         { return (int)frame; }
81       Surface* get_frame(unsigned int frame)
82       {
83         if(frame < action->surfaces.size())
84           return action->surfaces[frame];
85         else
86           return action->surfaces[0];
87       }    
88     private:
89       void init_defaults(Action* act);
90       void parse_action(LispReader& lispreader);
91
92       void update();
93       void reset();
94
95       std::string name;
96
97       float frame;
98       float last_tick;
99
100       typedef std::map <std::string, Action*> Actions;
101       Actions actions;
102
103       Action* action;
104     };
105
106 } //namespace SuperTux
107
108 #endif /*SUPERTUX_SPRITE_H*/
109
110 /* Local Variables: */
111 /* mode:c++ */
112 /* End: */