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