Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / sprite / sprite.hpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #ifndef HEADER_SUPERTUX_SPRITE_SPRITE_HPP
18 #define HEADER_SUPERTUX_SPRITE_SPRITE_HPP
19
20 #include "sprite/sprite_data.hpp"
21 #include "video/drawing_context.hpp"
22
23 class Surface;
24 class DrawingContext;
25 class Blend;
26
27 class Sprite
28 {
29 public:
30   Sprite(SpriteData& data);
31   Sprite(const Sprite& other);
32   ~Sprite();
33
34   /** Draw sprite, automatically calculates next frame */
35   void draw(DrawingContext& context, const Vector& pos, int layer);
36
37   void draw_part(DrawingContext& context, const Vector& source,
38                  const Vector& size, const Vector& pos, int layer);
39
40   /** Set action (or state) */
41   void set_action(const std::string& name, int loops = -1);
42
43   /** Set action (or state), but keep current frame number, loop counter, etc. */
44   void set_action_continued(const std::string& name);
45
46   /** Set number of animation cycles until animation stops */
47   void set_animation_loops(int loops = -1)
48   { animation_loops = loops; }
49
50   /** Set framerate */
51   void set_fps(float new_fps);
52
53   /* Stop animation */
54   void stop_animation()
55   { animation_loops = 0; }
56   /** Check if animation is stopped or not */
57   bool animation_done();
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() const
69   { return action->name; }
70
71   int get_width() const;
72   int get_height() const;
73
74   /** return x-offset of current action's hitbox, relative to start of image */
75   float get_current_hitbox_x_offset() const;
76   /** return y-offset of current action's hitbox, relative to start of image */
77   float get_current_hitbox_y_offset() const;
78   /** return width of current action's hitbox */
79   float get_current_hitbox_width() const;
80   /** return height of current action's hitbox */
81   float get_current_hitbox_height() const;
82   /** return current action's hitbox, relative to 0,0 */
83   Rect get_current_hitbox() const;
84
85   /** Set the angle of the sprite rotation in degree */
86   void set_angle(float angle);
87
88   /** Get the angle of the sprite rotation in degree */
89   float get_angle() const;
90
91   void set_color(const Color& color);
92
93   Color get_color() const;
94
95   void set_blend(const Blend& blend);
96
97   Blend get_blend() const;
98
99   /** Get current frame */
100   int get_frame() const
101   { return (int)frame; }
102   /** Set current frame */
103   void set_frame(int frame)
104   {
105     this->frame = (float) (frame % get_frames());
106   }
107   Surface* get_frame(unsigned int frame)
108   {
109     assert(frame < action->surfaces.size());
110     return action->surfaces[frame];
111   }
112
113 private:
114   void update();
115
116   SpriteData& data;
117
118   float frame;
119   int   animation_loops;
120   float last_ticks;
121   float angle;
122   Color color;
123   Blend blend;
124
125   SpriteData::Action* action;
126
127 private:
128   Sprite& operator=(const Sprite&);
129 };
130
131 #endif
132
133 /* EOF */