Improvements.
[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         std::vector<Surface*> surfaces;
49         };
50
51     public:
52       /** cur has to be a pointer to data in the form of ((x-hotspot 5)
53           (y-hotspot 10) ...) */
54       Sprite(lisp_object_t* cur);
55       ~Sprite();
56
57       /** Draw sprite, automatically calculates next frame */
58       void draw(DrawingContext& context, const Vector& pos, int layer,
59                 Uint32 drawing_effect = NONE_EFFECT);
60
61       /** Set action (or state) */
62       void set_action(std::string act);
63
64       /* Start an animation
65           -1 - for infinite
66           0  - stopped
67           1,2,3  - one, two, three times... */
68       void start_animation(int loops);
69       /* Stop animation */
70       void stop_animation()
71         { start_animation(0); }
72       /** Check if animation is stopped or not */
73       bool check_animation();
74       /** Reverse the animation */
75       void reverse_animation(bool reverse);
76
77       float get_fps()
78         { return action->fps; }
79       /** Get current action total frames */
80       int get_frames()
81         { return action->surfaces.size(); }
82       /** Get sprite's name */
83       std::string get_name() const
84         { return name; }
85       /** Get current action name */
86       std::string get_action_name() const
87         { return action->name; }
88       int get_width();
89       int get_height();
90
91       /** Get current frame */
92       int get_frame()
93         { return (int)frame; }
94       /** Set current frame */
95       void set_frame(int frame_)
96         { if(frame_ > get_frames()) frame = 0; else frame = frame_; }
97       Surface* get_frame(unsigned int frame)
98       {
99         if(frame < action->surfaces.size())
100           return action->surfaces[frame];
101         else
102           return action->surfaces[0];
103       }    
104     private:
105       void init_defaults(Action* act);
106       void parse_action(LispReader& lispreader);
107
108       void update();
109       void reset();
110
111       std::string name;
112
113       float frame;
114       int animation_loops;
115       bool animation_reversed;
116       float last_tick;
117
118       typedef std::map <std::string, Action*> Actions;
119       Actions actions;
120
121       Action* action;
122      std::string next_action;
123     };
124
125 } //namespace SuperTux
126
127 #endif /*SUPERTUX_SPRITE_H*/
128
129 /* Local Variables: */
130 /* mode:c++ */
131 /* End: */