b97b65cdecae61a82482a028d0aa2cb52dca28e9
[supertux.git] / src / sprite / sprite.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.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 <assert.h>
25 #include <SDL.h>
26
27 #include "math/vector.hpp"
28 #include "math/rect.hpp"
29 #include "sprite_data.hpp"
30
31 class Surface;
32 class DrawingContext;
33
34 class Sprite
35 {
36 public:
37   Sprite(SpriteData& data);
38   Sprite(const Sprite& other);
39   ~Sprite();
40
41   /** Draw sprite, automatically calculates next frame */
42   void draw(DrawingContext& context, const Vector& pos, int layer);
43
44   void draw_part(DrawingContext& context, const Vector& source,
45       const Vector& size, const Vector& pos, int layer);
46
47   /** Set action (or state) */
48   void set_action(const std::string& act, int loops = -1);
49
50   /** Set number of animation cycles until animation stops */
51   void set_animation_loops(int loops = -1)
52   { animation_loops = loops; }
53
54   /** Set framerate */
55   void set_fps(float new_fps);
56
57   /* Stop animation */
58   void stop_animation()
59   { animation_loops = 0; }
60   /** Check if animation is stopped or not */
61   bool animation_done();
62
63   float get_fps() const
64   { return action->fps; }
65   /** Get current action total frames */
66   int get_frames() const
67   { return action->surfaces.size(); }
68   /** Get sprite's name */
69   const std::string& get_name() const
70   { return data.name; }
71   /** Get current action name */
72   const std::string& get_action() const
73   { return action->name; }
74
75   int get_width() const;
76   int get_height() const;
77
78   /** return x-offset of current action's hitbox, relative to start of image */
79   float get_current_hitbox_x_offset() const;
80   /** return y-offset of current action's hitbox, relative to start of image */
81   float get_current_hitbox_y_offset() const;
82   /** return width of current action's hitbox */
83   float get_current_hitbox_width() const;
84   /** return height of current action's hitbox */
85   float get_current_hitbox_height() const;
86   /** return current action's hitbox, relative to 0,0 */
87   Rect get_current_hitbox() const;
88
89   /** Set the angle of the sprite rotation in degree */
90   void set_angle(float angle);
91
92   /** Get the angle of the sprite rotation in degree */
93   float get_angle() const;
94
95   /** Get current frame */
96   int get_frame() const
97   { return (int)frame; }
98   /** Set current frame */
99   void set_frame(int frame)
100   { 
101     this->frame = (frame % get_frames()); 
102   }
103   Surface* get_frame(unsigned int frame)
104   {
105     assert(frame < action->surfaces.size());
106     return action->surfaces[frame];
107   }    
108
109 private:
110   void update();
111
112   SpriteData& data;
113
114   float frame;
115   int   animation_loops;
116   float last_ticks;
117   float angle;
118   
119   SpriteData::Action* action;
120 };
121
122 #endif
123