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