This repository was archived by the owner on Feb 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathSpriteComponent.h
More file actions
executable file
·179 lines (156 loc) · 5.16 KB
/
SpriteComponent.h
File metadata and controls
executable file
·179 lines (156 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#pragma once
#include "../../defines.h"
#include "../BaseComponent.h"
#include "../../Helpers/Filepath.h"
#include "../../Graphics/Color.h"
namespace star
{
/// <summary>
/// Information of a sprite.
/// Gets sent to the <see cref="SpriteBatch"> to process it correctly.
/// Contains the width and height of the sprite,
/// the UVs, textureID, pointer to the <see cref="TransformComponent">,
/// color multiplier and a bool to check if the sprite is a HUD element.
/// </summary>
struct SpriteInfo
{
SpriteInfo()
: vertices()
, uvCoords()
, textureID()
, transformPtr(nullptr)
, colorMultiplier(Color::White)
, bIsHud(false)
{
}
vec2 vertices;
vec4 uvCoords;
uint32 textureID;
TransformComponent* transformPtr;
Color colorMultiplier;
bool bIsHud;
};
/// <summary>
/// Graphics component used to draw a texture or part of a texture.
/// </summary>
class SpriteComponent : public BaseComponent
{
public:
/// <summary>
/// Initializes a new instance of the <see cref="SpriteComponent"/> class.
/// </summary>
/// <param name="filepath">
/// Path to the asset, starting from the path defined in <see cref="DirectoryMode::assets"/>.
/// </param>
/// <param name="spriteName">Name of the sprite.</param>
/// <param name="widthSegments">Amount of width segments. Default set to 1</param>
/// <param name="heightSegments">Amount of height segments. Default set to 1</param>
SpriteComponent(
const tstring& filepath,
const tstring& spriteName,
uint32 widthSegments = 1,
uint32 heightSegments = 1
);
/// <summary>
/// Finalizes an instance of the <see cref="SpriteComponent"/> class.
/// </summary>
virtual ~SpriteComponent();
/// <summary>
/// Draws this instance.
/// </summary>
void Draw();
/// <summary>
/// Updates this instance.
/// </summary>
/// <param name="context"><see cref="Context"/> containing usefull information.</param>
virtual void Update(const Context& context);
/// <summary>
/// Culling check. The object will not be drawn if it is out of screen.
/// </summary>
/// <param name="left">Left of the screen</param>
/// <param name="right">Right of the screen</param>
/// <param name="top">Top of the screen</param>
/// <param name="bottom">Bottom of the screen</param>
/// <returns>true if the object should be culled</returns>
virtual bool CheckCulling(
float32 left,
float32 right,
float32 top,
float32 bottom
) const;
/// <summary>
/// Gets the file path of the texture.
/// </summary>
/// <returns>the file path.</returns>
const tstring& GetFilePath() const;
/// <summary>
/// Gets the name of the sprite.
/// </summary>
/// <returns>name of the sprite</returns>
const tstring& GetName() const;
/// <summary>
/// Sets the current segment to draw.
/// </summary>
/// <param name="widthSegment">The width segment.</param>
/// <param name="heightSegment">The height segment.</param>
void SetCurrentSegment(uint32 widthSegment, uint32 heightSegment);
/// <summary>
/// Sets the color multiplier for this sprite.
/// </summary>
/// <param name="color">The color.</param>
void SetColorMultiplier(const Color & color);
/// <summary>
/// Sets this Sprite as a HUD element
/// </summary>
/// <param name="enabled">True to become a HUD element.</param>
void SetHUDOptionEnabled(bool enabled);
/// <summary>
/// Determines whether this Sprite is a HUD element.
/// </summary>
/// <returns></returns>
bool IsHUDOptionEnabled() const;
/// <summary>
/// Sets the texture of this sprite. Usefull if you want to change the texture at runtime.
/// </summary>
/// <param name="filepath">Path to the asset, starting from the path defined in <see cref="DirectoryMode::assets"/>.</param>
/// <param name="spriteName">Name of the sprite.</param>
/// <param name="widthSegments">Amount of width segments. Default set to 1.</param>
/// <param name="heightSegments">Amount of height segments. Default set to 1.</param>
void SetTexture(
const tstring& filepath,
const tstring& spriteName,
uint32 widthSegments = 1,
uint32 heightSegments = 1
);
protected:
/// <summary>
/// Initializes the component.
/// </summary>
virtual void InitializeComponent();
/// <summary>
/// Creates the uv coordinates.
/// </summary>
virtual void CreateUVCoords();
/// <summary>
/// Sets the uv coordinates.
/// </summary>
/// <param name="coords">The uv coordinatess.</param>
void SetUVCoords(const vec4& coords);
/// <summary>
/// Fills the sprite information struct, to send to the <see cref="SpriteBatch"/>
/// </summary>
virtual void FillSpriteInfo();
uint32 m_WidthSegments,
m_HeightSegments,
m_CurrentWidthSegment,
m_CurrentHeightSegment;
private:
FilePath m_FilePath;
tstring m_SpriteName;
SpriteInfo* m_SpriteInfo;
SpriteComponent(const SpriteComponent &);
SpriteComponent(SpriteComponent &&);
SpriteComponent& operator=(const SpriteComponent &);
SpriteComponent& operator=(SpriteComponent &&);
};
}