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