Revert ca83136
[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       DrawingEffect effect = NO_EFFECT);
39
40   void draw_part(DrawingContext& context, const Vector& source,
41                  const Vector& size, const Vector& pos, int layer);
42
43   /** Set action (or state) */
44   void set_action(const std::string& name, int loops = -1);
45
46   /** Set action (or state), but keep current frame number, loop counter, etc. */
47   void set_action_continued(const std::string& name);
48
49   /** Set number of animation cycles until animation stops */
50   void set_animation_loops(int loops = -1)
51   { animation_loops = loops; }
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   unsigned 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   Rectf 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   unsigned int get_frame() const
101   { return frameidx; }
102   /** Set current frame */
103   void set_frame(int frame_)
104   {
105     this->frame = 0;
106     this->frameidx = frame_ % get_frames();
107   }
108   SurfacePtr get_frame(unsigned int frame_)
109   {
110     assert(frame_ < action->surfaces.size());
111     return action->surfaces[frame_];
112   }
113
114   bool has_action (const std::string& name)
115   {
116     return (data.get_action(name) != NULL);
117   }
118
119 private:
120   void update();
121
122   SpriteData& data;
123
124   // between 0 and 1
125   float frame;
126   // between 0 and get_frames()
127   unsigned int frameidx;
128   int   animation_loops;
129   float last_ticks;
130   float angle;
131   Color color;
132   Blend blend;
133
134   const SpriteData::Action* action;
135
136 private:
137   Sprite(const Sprite& other);
138   Sprite& operator=(const Sprite&);
139 };
140
141 #endif
142
143 /* EOF */