6d0ed7bac98ce758abcd11d6ec3d70fed3bef765
[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 <cassert>
26 #include <map>
27
28 #include "utils/lispreader.h"
29 #include "video/surface.h"
30 #include "math/vector.h"
31 #include "sprite_data.h"
32
33 namespace SuperTux
34 {
35   class Sprite
36   {
37   public:
38     Sprite(SpriteData& data);
39     Sprite(const Sprite& other);
40     ~Sprite();
41     
42     /** Draw sprite, automatically calculates next frame */
43     void draw(DrawingContext& context, const Vector& pos, int layer,
44         Uint32 drawing_effect = NONE_EFFECT);
45
46     void draw_part(DrawingContext& context, const Vector& source,
47         const Vector& size, const Vector& pos, int layer,
48         Uint32 drawing_effect = NONE_EFFECT);
49
50     /** Set action (or state) */
51     void set_action(std::string act, int loops = -1);
52
53     /* Stop animation */
54     void stop_animation()
55     { animation_loops = 0; }
56     /** Check if animation is stopped or not */
57     bool check_animation();
58
59     float get_fps() const
60     { return action->fps; }
61     /** Get current action total frames */
62     int get_frames() const
63     { return action->surfaces.size(); }
64     /** Get sprite's name */
65     const std::string& get_name() const
66     { return data.name; }
67     /** Get current action name */
68     const std::string& get_action_name() const
69     { return action->name; }
70
71     int get_width() const;
72     int get_height() const;
73
74     /** Get current frame */
75     int get_frame() const
76     { return (int)frame; }
77     /** Set current frame */
78     void set_frame(int frame_)
79     { if(frame_ > get_frames()) frame = 0; else frame = frame_; }
80     Surface* get_frame(unsigned int frame)
81     {
82       assert(frame < action->surfaces.size());
83       return action->surfaces[frame];
84     }    
85
86   private:
87     void update();
88
89     SpriteData& data;
90
91     float frame;
92     int animation_loops;
93     Uint32 last_ticks;
94
95     SpriteData::Action* action;
96   };
97 } //namespace SuperTux
98
99 #endif /*SUPERTUX_SPRITE_H*/
100
101 /* Local Variables: */
102 /* mode:c++ */
103 /* End: */