raylibstarter 0.1.0
sprite.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4
5#include <raylib.h>
6
7#include "texture2d.h"
8
9namespace game::core {
10 /**
11 * Besides the position and rotation angle, a sprite object is further defined by its rotation origin,
12 * its tint and its visibility attribute. In addition, the visible section ("frame") of the associated
13 * VRAM texture can be specified. This is especially important for the SpriteAnimated class derived from this class.
14 *
15 * Sprites can be drawn using the game::core::Renderer::DrawTexture function. However, the preferred way is to
16 * create Actor objects. These contain a sprite and are automatically drawn if they are referenced in the actors map
17 * of a Scene object.
18 *
19 * @brief The Sprite class specifies position and degree of rotation on the screen for an associated VRAM texture.
20 */
21 class Sprite {
22 protected:
23 /// The sprites Texture2D
24 std::shared_ptr<game::core::Texture2D> texture_;
25
26 /// The section of the associated VRAM texture to be displayed as Raylib Rectangle structure.
27 Rectangle frame_;
28
29 public:
30 /// Visibility of the Sprite.
31 bool visible = true;
32
33 /// The Raylib tint applied to the VRAM texture when drawing. Defulat ist Raylib WHITE.
34 Color tint = WHITE;
35
36 /// The sprites rotation origin, centered by default.
38
39 /// The sprites rotation angle. Default is 0.
40 float rotation = 0.0f;
41
42 /// The sprites x position
43 int pos_x = 0;
44
45 /// The sprites y position
46 int pos_y = 0;
47
48 Sprite() = delete;
49
50 /**
51 * This constructor sets the section of the texture to be displayed to its full size.
52 * In addition, the rotation origin is set to its center and rotation angle = 0. The sprites default position is {0, 0}.
53 * @brief Constructor
54 * @param texture Shared pointer to a Texture2D object.
55 */
56 explicit Sprite(std::shared_ptr<game::core::Texture2D> texture);
57
58 /**
59 * This constructor sets the section of the texture to be displayed to its full size.
60 * In addition, the rotation origin is set to its center and rotation angle = 0. The sprites position can be specified.
61 * @brief Constructor
62 * @param texture Shared pointer to a Texture2D object.
63 * @param pos_x The sprites x position.
64 * @param pos_y The sprites y position.
65 */
66 Sprite(std::shared_ptr<game::core::Texture2D> texture, int pos_x, int pos_y);
67
68 /**
69 * This constructor sets the section of the texture to be displayed to its full size.
70 * In addition, the rotation origin is set to its center. The sprites position and rotation angle can be specified.
71 * @brief Constructor
72 * @param texture Shared pointer to a Texture2D object.
73 * @param pos_x The sprites x position.
74 * @param pos_y The sprites y position.
75 * @param rotation The sprites rotation angle.
76 */
77 Sprite(std::shared_ptr<game::core::Texture2D> texture, int pos_x, int pos_y, float rotation);
78
79 /**
80 * Using this constructor the sprites position, rotation angle and rotation and a Raylib Rectangle,
81 * which describes the texture section to be displayed can be specified.
82 * @brief Constructor
83 * @param texture Shared pointer to a Texture2D object.
84 * @param pos_x The sprites x position.
85 * @param pos_y The sprites y position.
86 * @param rotation_origin The rotation origin.
87 * @param rotation The sprites rotation angle.
88 * @param frame The section of the associated VRAM texture to be displayed.
89 */
90 Sprite(std::shared_ptr<game::core::Texture2D> texture, int pos_x, int pos_y, Vector2 rotation_origin, float rotation, Rectangle frame);
91
92 virtual ~Sprite();
93
94 /**
95 * The section of the texture to be displayed is set to its full size. In addition, the rotation origin is set to its center.
96 * @brief Replaces the Texture2D object.
97 * @param texture Shared pointer to a Texture2D object.
98 */
99 void texture(const std::shared_ptr<game::core::Texture2D> &texture);
100
101 /**
102 * @return Returns a shared pointer to the objects Texture2D member.
103 */
104 [[maybe_unused]] [[nodiscard]] const std::shared_ptr<game::core::Texture2D> &texture_object() const;
105
106 /**
107 * @return Returns a reference to the Raylib Texture2D structure associated with this object.
108 */
109 [[nodiscard]] const ::Texture2D &texture() const;
110
111 /**
112 * @return Returns the objects coordinates as Raylib Vector structure.
113 */
114 [[nodiscard]] Vector2 position() const;
115
116 /**
117 * @return The section of the associated VRAM texture to be displayed as Raylib Rectangle structure.
118 */
119 [[nodiscard]] const Rectangle &frame() const;
120
121 /**
122 * @brief Sets the section of the associated VRAM texture to be displayed.
123 * @param frame Raylib Rectangle, which describes the texture section to be displayed.
124 */
125 void frame(const Rectangle &frame);
126
127 /**
128 * Only relevant in derived classes, like SpiteAnimated.
129 */
130 virtual void Update() { };
131 };
132}
The Sprite class specifies position and degree of rotation on the screen for an associated VRAM textu...
Definition: sprite.h:21
Vector2 position() const
Definition: sprite.cpp:45
std::shared_ptr< game::core::Texture2D > texture_
The sprites Texture2D.
Definition: sprite.h:24
Vector2 rotation_origin
The sprites rotation origin, centered by default.
Definition: sprite.h:37
const ::Texture2D & texture() const
Definition: sprite.cpp:63
bool visible
Visibility of the Sprite.
Definition: sprite.h:31
float rotation
The sprites rotation angle. Default is 0.
Definition: sprite.h:40
virtual ~Sprite()
Definition: sprite.cpp:41
Rectangle frame_
The section of the associated VRAM texture to be displayed as Raylib Rectangle structure.
Definition: sprite.h:27
virtual void Update()
Definition: sprite.h:130
const std::shared_ptr< game::core::Texture2D > & texture_object() const
Definition: sprite.cpp:49
const Rectangle & frame() const
Definition: sprite.cpp:67
int pos_x
The sprites x position.
Definition: sprite.h:43
Color tint
The Raylib tint applied to the VRAM texture when drawing. Defulat ist Raylib WHITE.
Definition: sprite.h:34
int pos_y
The sprites y position.
Definition: sprite.h:46