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