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 "supertux/direction.hpp"
23 #include "video/canvas.hpp"
24 #include "video/drawing_context.hpp"
25 
26 class Sprite final
27 {
28 public:
29  enum Loops {
30  LOOPS_CONTINUED = -100
31  };
32 
33 public:
34  Sprite(SpriteData& data);
35  ~Sprite();
36 
37  SpritePtr clone() const;
38 
40  void draw(Canvas& canvas, const Vector& pos, int layer,
41  Flip flip = NO_FLIP);
42  void draw_scaled(Canvas& canvas, const Rectf& dest_rect, int layer,
43  Flip flip = NO_FLIP);
44 
46  void set_action(const std::string& name, int loops = -1);
47 
51  void set_action(const std::string& name, const Direction& dir, int loops = -1);
52 
56  void set_action(const Direction& dir, const std::string& name, int loops = -1);
57 
61  void set_action(const Direction& dir, int loops = -1);
62 
64  void set_animation_loops(int loops = -1) { m_animation_loops = loops; }
65 
66  void set_frame_progress(float frame_progress) { m_frame = frame_progress; }
67 
68  void set_frame(int frame) { m_frameidx = frame; }
69 
70  /* Stop animation */
71  void stop_animation() { m_animation_loops = 0; }
72 
73  void pause_animation() { m_is_paused = true; }
74  void resume_animation() { m_is_paused = false; }
75 
77  bool animation_done() const;
78 
80  int get_frames() const { return static_cast<int>(m_action->surfaces.size()); }
81 
83  int get_current_frame() const { return m_frameidx; }
84 
86  float get_current_frame_progress() const { return m_frame; }
87 
89  const std::string& get_name() const { return m_data.name; }
90 
92  const std::string& get_action() const { return m_action->name; }
93 
94  int get_width() const;
95  int get_height() const;
96 
97  const std::optional<std::vector<SurfacePtr>> get_action_surfaces(const std::string& name) const;
98 
100  bool is_current_hitbox_unisolid() const;
102  float get_current_hitbox_x_offset() const;
104  float get_current_hitbox_y_offset() const;
106  float get_current_hitbox_width() const;
108  float get_current_hitbox_height() const;
110  Rectf get_current_hitbox() const;
111 
113  void set_angle(float angle);
114 
116  float get_angle() const;
117 
118  void set_color(const Color& color);
119  Color get_color() const;
120 
121  void set_alpha(float alpha);
122  float get_alpha() const;
123 
124  void set_blend(const Blend& blend);
125  Blend get_blend() const;
126 
127  bool has_action (const std::string& name) const { return (m_data.get_action(name) != nullptr); }
128  size_t get_actions_count() const { return m_data.actions.size(); }
129 
130 private:
131  void update();
132 
133  SpriteData& m_data;
134 
135  // between 0 and 1
136  float m_frame;
137  // between 0 and get_frames()
138  int m_frameidx;
139  int m_animation_loops;
140  float m_last_ticks;
141  float m_angle;
142  float m_alpha;
143  Color m_color;
144  Blend m_blend;
145  bool m_is_paused;
146 
147  const SpriteData::Action* m_action;
148 
149 private:
150  Sprite(const Sprite& other);
151  Sprite& operator=(const Sprite&) = delete;
152 };
153 
154 #endif
155 
156 /* EOF */
Definition: sprite_data.hpp:28
Definition: sprite.hpp:26
float get_current_hitbox_width() const
return width of current action&#39;s hitbox
Definition: sprite.cpp:246
void set_angle(float angle)
Set the angle of the sprite rotation in degree.
Definition: sprite.cpp:264
bool is_current_hitbox_unisolid() const
Return the "unisolid" property for the current action&#39;s hitbox.
Definition: sprite.cpp:228
void draw(Canvas &canvas, const Vector &pos, int layer, Flip flip=NO_FLIP)
Draw sprite, automatically calculates next frame.
Definition: sprite.cpp:165
const std::string & get_name() const
Get sprite&#39;s name.
Definition: sprite.hpp:89
int get_current_frame() const
Get currently drawn frame.
Definition: sprite.hpp:83
Definition: rectf.hpp:31
void set_action(const std::string &name, int loops=-1)
Set action (or state)
Definition: sprite.cpp:94
Rectf get_current_hitbox() const
return current action&#39;s hitbox, relative to 0,0
Definition: sprite.cpp:258
bool animation_done() const
Check if animation is stopped or not.
Definition: sprite.cpp:130
float get_current_hitbox_height() const
return height of current action&#39;s hitbox
Definition: sprite.cpp:252
void set_animation_loops(int loops=-1)
Set number of animation cycles until animation stops.
Definition: sprite.hpp:64
float get_current_hitbox_x_offset() const
return x-offset of current action&#39;s hitbox, relative to start of image
Definition: sprite.cpp:234
const std::string & get_action() const
Get current action name.
Definition: sprite.hpp:92
Definition: color.hpp:26
int get_frames() const
Get current action total frames.
Definition: sprite.hpp:80
float get_current_hitbox_y_offset() const
return y-offset of current action&#39;s hitbox, relative to start of image
Definition: sprite.cpp:240
Definition: canvas.hpp:43
float get_angle() const
Get the angle of the sprite rotation in degree.
Definition: sprite.cpp:270
float get_current_frame_progress() const
Get current frame progress.
Definition: sprite.hpp:86