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.cpp
More file actions
executable file
·196 lines (167 loc) · 4.73 KB
/
SpriteComponent.cpp
File metadata and controls
executable file
·196 lines (167 loc) · 4.73 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "SpriteComponent.h"
#include "../../Objects/Object.h"
#include "../../Graphics/SpriteBatch.h"
#include "SpritesheetComponent.h"
#include "TextComponent.h"
namespace star
{
SpriteComponent::SpriteComponent(
const tstring& filepath,
const tstring& spriteName,
uint32 widthSegments,
uint32 heightSegments
)
: BaseComponent()
, m_WidthSegments(widthSegments)
, m_HeightSegments(heightSegments)
, m_CurrentWidthSegment(0)
, m_CurrentHeightSegment(0)
, m_FilePath(filepath)
, m_SpriteName(spriteName)
, m_SpriteInfo(nullptr)
{
m_SpriteInfo = new SpriteInfo();
}
void SpriteComponent::InitializeComponent()
{
if(m_pParentObject->HasComponent<SpriteSheetComponent>(this)
|| m_pParentObject->HasComponent<TextComponent>(this))
{
ASSERT_LOG(false,
_T("Object '") + m_pParentObject->GetName() +
_T("': Can't add a SpriteComponent when already \
having a SpriteSheet- or TextComponent."), STARENGINE_LOG_TAG);
m_pParentObject->RemoveComponent(this);
}
else
{
TextureManager::GetInstance()->LoadTexture(
m_FilePath.GetFullPath(),
m_SpriteName
);
m_Dimensions.x = TextureManager::GetInstance()->
GetTextureDimensions(m_SpriteName).x /
m_WidthSegments;
m_Dimensions.y = TextureManager::GetInstance()->
GetTextureDimensions(m_SpriteName).y /
m_HeightSegments;
GetTransform()->SetDimensionsSafe(m_Dimensions);
CreateUVCoords();
FillSpriteInfo();
}
}
void SpriteComponent::FillSpriteInfo()
{
m_SpriteInfo->textureID =
TextureManager::GetInstance()->GetTextureID(m_SpriteName);
m_SpriteInfo->vertices = vec2(m_Dimensions.x, m_Dimensions.y);
}
SpriteComponent::~SpriteComponent()
{
delete m_SpriteInfo;
}
void SpriteComponent::CreateUVCoords()
{
float32 startX =
float32(m_CurrentWidthSegment) /
float32(m_WidthSegments);
float32 endX = 1.0f / m_WidthSegments;
float32 startY =
float32(m_CurrentHeightSegment) /
float32(m_HeightSegments);
float32 endY = 1.0f / m_HeightSegments;
SetUVCoords(vec4(startX, startY, endX, endY));
}
void SpriteComponent::SetUVCoords(const vec4& coords)
{
m_SpriteInfo->uvCoords = coords;
}
void SpriteComponent::Draw()
{
m_SpriteInfo->transformPtr = GetTransform();
SpriteBatch::GetInstance()->AddSpriteToQueue(m_SpriteInfo);
}
void SpriteComponent::Update(const Context & context)
{
//[COMMENT] Temp hotfix!
#ifdef ANDROID
FillSpriteInfo();
#endif
}
bool SpriteComponent::CheckCulling(
float left,
float right,
float top,
float bottom
) const
{
//Always draw hudObjects
if(m_SpriteInfo->bIsHud)
{
return true;
}
float32 spriteWidth, spriteHeight;
pos objectPos = GetTransform()->GetWorldPosition();
if(m_SpriteInfo->bIsHud)
{
objectPos.x += left;
objectPos.y += bottom;
}
spriteWidth = float32(GetWidth()) * GetTransform()->GetWorldScale().x;
spriteHeight = float32(GetHeight()) * GetTransform()->GetWorldScale().y;
float32 objRight = objectPos.x + spriteWidth;
float32 objTop = objectPos.y + spriteHeight;
return
(objectPos.x <= right && objRight >= left)
&&
(objectPos.y <= top && objTop >= bottom);
}
const tstring& SpriteComponent::GetFilePath() const
{
return m_FilePath.GetPath();
}
const tstring& SpriteComponent::GetName() const
{
return m_SpriteName;
}
void SpriteComponent::SetCurrentSegment(uint32 widthSegment, uint32 heightSegment)
{
m_CurrentWidthSegment = widthSegment;
m_CurrentHeightSegment = m_HeightSegments - heightSegment - 1;
CreateUVCoords();
}
void SpriteComponent::SetColorMultiplier(const Color & color)
{
m_SpriteInfo->colorMultiplier = color;
}
void SpriteComponent::SetHUDOptionEnabled(bool enabled)
{
m_SpriteInfo->bIsHud = enabled;
}
bool SpriteComponent::IsHUDOptionEnabled() const
{
return m_SpriteInfo->bIsHud;
}
void SpriteComponent::SetTexture(
const tstring& filepath,
const tstring& spriteName,
uint32 widthSegments,
uint32 heightSegments
)
{
m_Dimensions.x = 0;
m_WidthSegments = widthSegments;
m_CurrentWidthSegment = 0;
m_Dimensions.y = 0;
m_HeightSegments = heightSegments;
m_CurrentHeightSegment = 0;
m_FilePath = FilePath(filepath);
m_SpriteName = spriteName;
TextureManager::GetInstance()->LoadTexture(m_FilePath.GetFullPath(),m_SpriteName);
m_Dimensions.x = TextureManager::GetInstance()->GetTextureDimensions(m_SpriteName).x / m_WidthSegments;
m_Dimensions.y = TextureManager::GetInstance()->GetTextureDimensions(m_SpriteName).y / m_HeightSegments;
GetTransform()->SetDimensionsSafe(m_Dimensions);
CreateUVCoords();
FillSpriteInfo();
}
}