Added SpritePtr
[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   /** Set framerate */
53   void set_fps(float new_fps);
54
55   /* Stop animation */
56   void stop_animation()
57   { animation_loops = 0; }
58   /** Check if animation is stopped or not */
59   bool animation_done();
60
61   float get_fps() const
62   { return action->fps; }
63   /** Get current action total frames */
64   int get_frames() const
65   { return action->surfaces.size(); }
66   /** Get sprite's name */
67   const std::string& get_name() const
68   { return data.name; }
69   /** Get current action name */
70   const std::string& get_action() const
71   { return action->name; }
72
73   int get_width() const;
74   int get_height() const;
75
76   /** return x-offset of current action's hitbox, relative to start of image */
77   float get_current_hitbox_x_offset() const;
78   /** return y-offset of current action's hitbox, relative to start of image */
79   float get_current_hitbox_y_offset() const;
80   /** return width of current action's hitbox */
81   float get_current_hitbox_width() const;
82   /** return height of current action's hitbox */
83   float get_current_hitbox_height() const;
84   /** return current action's hitbox, relative to 0,0 */
85   Rectf get_current_hitbox() const;
86
87   /** Set the angle of the sprite rotation in degree */
88   void set_angle(float angle);
89
90   /** Get the angle of the sprite rotation in degree */
91   float get_angle() const;
92
93   void set_color(const Color& color);
94
95   Color get_color() const;
96
97   void set_blend(const Blend& blend);
98
99   Blend get_blend() const;
100
101   /** Get current frame */
102   int get_frame() const
103   { return (int)frame; }
104   /** Set current frame */
105   void set_frame(int frame_)
106   {
107     this->frame = (float) (frame_ % get_frames());
108   }
109   SurfacePtr get_frame(unsigned int frame_)
110   {
111     assert(frame_ < action->surfaces.size());
112     return action->surfaces[frame_];
113   }
114
115 private:
116   void update();
117
118   SpriteData& data;
119
120   float frame;
121   int   animation_loops;
122   float last_ticks;
123   float angle;
124   Color color;
125   Blend blend;
126
127   SpriteData::Action* action;
128
129 private:
130   Sprite(const Sprite& other);
131   Sprite& operator=(const Sprite&);
132 };
133
134 #endif
135
136 /* EOF */