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
25class Sprite final
26{
27public:
28 Sprite(SpriteData& data);
29 ~Sprite();
30
31 SpritePtr clone() const;
32
33 /** Draw sprite, automatically calculates next frame */
34 void draw(Canvas& canvas, const Vector& pos, int layer,
35 Flip flip = NO_FLIP);
36
37 /** Set action (or state) */
38 void set_action(const std::string& name, int loops = -1);
39
40 /** Set action (or state), but keep current frame number, loop counter, etc. */
41 void set_action_continued(const std::string& name);
42
43 /** Set number of animation cycles until animation stops */
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
49 /** Check if animation is stopped or not */
50 bool animation_done() const;
51
52 /** Get current action total frames */
53 int get_frames() const { return static_cast<int>(m_action->surfaces.size()); }
54
55 /** Get currently drawn frame */
56 int get_current_frame() const { return m_frameidx; }
57
58 /** Get sprite's name */
59 const std::string& get_name() const { return m_data.name; }
60
61 /** Get current action name */
62 const std::string& get_action() const { return m_action->name; }
63
64 int get_width() const;
65 int get_height() const;
66
67 /** return x-offset of current action's hitbox, relative to start of image */
68 float get_current_hitbox_x_offset() const;
69 /** return y-offset of current action's hitbox, relative to start of image */
70 float get_current_hitbox_y_offset() const;
71 /** return width of current action's hitbox */
72 float get_current_hitbox_width() const;
73 /** return height of current action's hitbox */
74 float get_current_hitbox_height() const;
75 /** return current action's hitbox, relative to 0,0 */
76 Rectf get_current_hitbox() const;
77
78 /** Set the angle of the sprite rotation in degree */
79 void set_angle(float angle);
80
81 /** Get the angle of the sprite rotation in degree */
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
92private:
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
109private:
110 Sprite(const Sprite& other);
111 Sprite& operator=(const Sprite&) = delete;
112};
113
114#endif
115
116/* EOF */
117