supertux
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/canvas.hpp"
23 #include "video/drawing_context.hpp"
24 
25 class Sprite final
26 {
27 public:
28  Sprite(SpriteData& data);
29  ~Sprite();
30 
31  SpritePtr clone() const;
32 
34  void draw(Canvas& canvas, const Vector& pos, int layer,
35  Flip flip = NO_FLIP);
36 
38  void set_action(const std::string& name, int loops = -1);
39 
41  void set_action_continued(const std::string& name);
42 
44  void set_animation_loops(int loops = -1) { m_animation_loops = loops; }
45 
46  /* Stop animation */
47  void stop_animation() { m_animation_loops = 0; }
48 
50  bool animation_done() const;
51 
53  int get_frames() const { return static_cast<int>(m_action->surfaces.size()); }
54 
56  int get_current_frame() const { return m_frameidx; }
57 
59  const std::string& get_name() const { return m_data.name; }
60 
62  const std::string& get_action() const { return m_action->name; }
63 
64  int get_width() const;
65  int get_height() const;
66 
68  float get_current_hitbox_x_offset() const;
70  float get_current_hitbox_y_offset() const;
72  float get_current_hitbox_width() const;
74  float get_current_hitbox_height() const;
76  Rectf get_current_hitbox() const;
77 
79  void set_angle(float angle);
80 
82  float get_angle() const;
83 
84  void set_color(const Color& color);
85  Color get_color() const;
86 
87  void set_blend(const Blend& blend);
88  Blend get_blend() const;
89 
90  bool has_action (const std::string& name) const { return (m_data.get_action(name) != nullptr); }
91 
92 private:
93  void update();
94 
95  SpriteData& m_data;
96 
97  // between 0 and 1
98  float m_frame;
99  // between 0 and get_frames()
100  int m_frameidx;
101  int m_animation_loops;
102  float m_last_ticks;
103  float m_angle;
104  Color m_color;
105  Blend m_blend;
106 
107  const SpriteData::Action* m_action;
108 
109 private:
110  Sprite(const Sprite& other);
111  Sprite& operator=(const Sprite&) = delete;
112 };
113 
114 #endif
115 
116 /* EOF */
Definition: sprite_data.hpp:28
Simple two dimensional vector.
Definition: vector.hpp:24
Definition: sprite.hpp:25
float get_current_hitbox_width() const
return width of current action&#39;s hitbox
Definition: sprite.cpp:185
void set_angle(float angle)
Set the angle of the sprite rotation in degree.
Definition: sprite.cpp:203
void draw(Canvas &canvas, const Vector &pos, int layer, Flip flip=NO_FLIP)
Draw sprite, automatically calculates next frame.
Definition: sprite.cpp:136
const std::string & get_name() const
Get sprite&#39;s name.
Definition: sprite.hpp:59
int get_current_frame() const
Get currently drawn frame.
Definition: sprite.hpp:56
Definition: rectf.hpp:29
void set_action_continued(const std::string &name)
Set action (or state), but keep current frame number, loop counter, etc.
Definition: sprite.cpp:85
void set_action(const std::string &name, int loops=-1)
Set action (or state)
Definition: sprite.cpp:65
Rectf get_current_hitbox() const
return current action&#39;s hitbox, relative to 0,0
Definition: sprite.cpp:197
bool animation_done() const
Check if animation is stopped or not.
Definition: sprite.cpp:101
float get_current_hitbox_height() const
return height of current action&#39;s hitbox
Definition: sprite.cpp:191
void set_animation_loops(int loops=-1)
Set number of animation cycles until animation stops.
Definition: sprite.hpp:44
float get_current_hitbox_x_offset() const
return x-offset of current action&#39;s hitbox, relative to start of image
Definition: sprite.cpp:173
const std::string & get_action() const
Get current action name.
Definition: sprite.hpp:62
Definition: color.hpp:25
int get_frames() const
Get current action total frames.
Definition: sprite.hpp:53
float get_current_hitbox_y_offset() const
return y-offset of current action&#39;s hitbox, relative to start of image
Definition: sprite.cpp:179
Definition: canvas.hpp:43
float get_angle() const
Get the angle of the sprite rotation in degree.
Definition: sprite.cpp:209