Skip to content

Commit d1aacea

Browse files
committed
Added documentation to the TextureAtlas class
1 parent 0a97df8 commit d1aacea

File tree

7 files changed

+216
-28
lines changed

7 files changed

+216
-28
lines changed

engine/render/renderer/Renderer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void Renderer::DrawQuad(const Vec2 position,
123123
}
124124

125125
void Renderer::DrawQuad(const Vec2 position,
126-
Vulkan::TextureAtlas::TextureRef texture,
126+
Vulkan::TextureAtlas::SubTextureRef texture,
127127
const Vec2 scale,
128128
const IColour colour,
129129
float rotation,

engine/render/renderer/Renderer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Renderer
5454
const uint8_t zIndex = 0,
5555
Vulkan::Texture2D* texture = nullptr);
5656
void DrawQuad(const Vec2 position,
57-
Vulkan::TextureAtlas::TextureRef texture,
57+
Vulkan::TextureAtlas::SubTextureRef texture,
5858
const Vec2 scale = {1.f, 1.f},
5959
const IColour colour = IColour::White,
6060
float rotation = 0.f,

engine/render/renderer/platform/vulkan/TextureAtlas.cpp

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,54 @@
1111

1212
namespace Siege::Vulkan
1313
{
14-
Texture2D& TextureAtlas::TextureRef::operator*()
14+
15+
Texture2D& TextureAtlas::SubTextureRef::operator*()
1516
{
1617
return parentAtlas->texture;
1718
}
1819

19-
TextureAtlas::TextureRef::operator bool() const
20+
TextureAtlas* TextureAtlas::SubTextureRef::operator->()
21+
{
22+
return parentAtlas;
23+
}
24+
25+
TextureAtlas::SubTextureRef::operator bool() const
2026
{
2127
return parentAtlas;
2228
}
2329

30+
void TextureAtlas::SubTextureRef::Swap(SubTextureRef& other)
31+
{
32+
auto tmpAtlas = parentAtlas;
33+
auto tmpMinX = minX;
34+
auto tmpMinY = minY;
35+
auto tmpWidth = width;
36+
auto tmpHeight = height;
37+
38+
parentAtlas = other.parentAtlas;
39+
minX = other.minX;
40+
minY = other.minY;
41+
width = other.width;
42+
height = other.height;
43+
44+
other.parentAtlas = tmpAtlas;
45+
other.minX = tmpMinX;
46+
other.minY = tmpMinY;
47+
other.width = tmpWidth;
48+
other.height = tmpHeight;
49+
}
50+
TextureAtlas::SubTextureRef& TextureAtlas::SubTextureRef::operator=(
51+
TextureAtlas::SubTextureRef& other)
52+
{
53+
parentAtlas = other.parentAtlas;
54+
minX = other.minX;
55+
minY = other.minY;
56+
width = other.width;
57+
height = other.height;
58+
59+
return *this;
60+
}
61+
2462
TextureAtlas::TextureAtlas(const char* name,
2563
const char* filePath,
2664
Utils::Extent2DF imageExtents,
@@ -40,20 +78,25 @@ TextureAtlas::~TextureAtlas()
4078
fixedExtent = {};
4179
}
4280

43-
TextureAtlas::TextureRef TextureAtlas::operator[](size_t index)
81+
TextureAtlas::SubTextureRef TextureAtlas::operator[](size_t index)
4482
{
83+
// TODO(Aryeh): Add some level of error handling here (assert if index is higher than number of
84+
// textures)
85+
86+
// NOTE(Aryeh): only works for fixed size textures
4587
size_t elementsInRow = 1 / fixedExtent.width;
4688

47-
return TextureRef(this,
48-
(index % elementsInRow) * fixedExtent.width, // potentially slow code
49-
(index / elementsInRow) * fixedExtent.height,
50-
fixedExtent.width,
51-
fixedExtent.height);
89+
return SubTextureRef(this,
90+
(index % elementsInRow) * fixedExtent.width, // potentially slow code
91+
(index / elementsInRow) * fixedExtent.height,
92+
fixedExtent.width,
93+
fixedExtent.height);
5294
}
5395

54-
TextureAtlas* TextureAtlas::TextureRef::operator->()
96+
TextureAtlas& TextureAtlas::operator=(TextureAtlas&& other)
5597
{
56-
return parentAtlas;
98+
Swap(other);
99+
return *this;
57100
}
58101

59102
void TextureAtlas::Swap(TextureAtlas& other)

engine/render/renderer/platform/vulkan/TextureAtlas.h

Lines changed: 154 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,86 +16,232 @@
1616
namespace Siege::Vulkan
1717
{
1818

19+
/**
20+
* A vulkan-based texture atlas class. This class contains a single texture made out of many
21+
* sub-textures. Texture Atlases are a way to store multiple textures in a single space within the
22+
* same memory space. Currently, the TextureAtlas supports fixed size sub-textures
23+
*
24+
* NOTE: Currently only supports fixed size texture atlases
25+
*
26+
* TODO(Aryeh): Add the ability to add sub-textures from png files
27+
* TODO(Aryeh): Add the ability to manually specify the sub-texture extents
28+
*/
1929
class TextureAtlas
2030
{
2131
public:
2232

23-
class TextureRef
33+
/**
34+
* A subTextureRef is a reference to a sub-texture. These are textures which are contained
35+
* within a texture atlas. These can be accessed from the TextureAtlas via indexing
36+
*/
37+
class SubTextureRef
2438
{
2539
public:
2640

2741
// 'Structors
28-
TextureRef(TextureAtlas* parent, float minX, float minY, float width, float height) :
42+
43+
/**
44+
* The SubTextureRef constructor
45+
* @param parent the parent texture atlas this sub-texture belongs to
46+
* @param minX the leftmost normalised x coordinate within the parent texture
47+
* @param minY the top-most normalised y coordinate within the parent texture
48+
* @param width the width of the texture
49+
* @param height the height of the texture
50+
*/
51+
inline SubTextureRef(TextureAtlas* parent,
52+
float minX,
53+
float minY,
54+
float width,
55+
float height) :
2956
parentAtlas {parent},
3057
minX {minX},
3158
width {width},
3259
minY {minY},
3360
height {height}
3461
{}
3562

63+
/**
64+
* The SubTextureRef move constructor
65+
* @param other the SubTextureRef to move information from
66+
*/
67+
inline SubTextureRef(SubTextureRef&& other)
68+
{
69+
Swap(other);
70+
}
71+
72+
/**
73+
* The SubTextureRef copy constructor
74+
* @param other the SubTextureRef to copy information from
75+
*/
76+
inline SubTextureRef(SubTextureRef& other) :
77+
parentAtlas {other.parentAtlas},
78+
minX {other.minX},
79+
width {other.width},
80+
minY {other.minY},
81+
height {other.height}
82+
{}
83+
3684
// Operator Overloads
3785

86+
/**
87+
* The SubTextureRef copy assignment operator
88+
* @param other the SubTextureRef to copy information from
89+
* @return a reference to the SubTextureRef
90+
*/
91+
SubTextureRef& operator=(SubTextureRef& other);
92+
93+
/**
94+
* The SubTextureRef move assignment operator
95+
* @param other the SubTextureRef to move information from
96+
* @return a reference to the SubTextureRef
97+
*/
98+
inline SubTextureRef& operator=(SubTextureRef&& other)
99+
{
100+
Swap(other);
101+
return *this;
102+
}
103+
104+
/**
105+
* The dereference operator
106+
* @return returns a reference to the stored Texture2D within the parent texture
107+
*/
38108
Texture2D& operator*();
39109

110+
/**
111+
* The pointer arrow operator
112+
* @return returns the pointer to the parentTexture for member access
113+
*/
40114
TextureAtlas* operator->();
41115

116+
/**
117+
* A boolean operator for the SubTextureRef
118+
* @return A boolean specifying if the parentAtlas exists (is not a nullptr)
119+
*/
42120
operator bool() const;
43121

44122
// Getters and setters
45123

124+
/**
125+
* Returns the leftmost coordinate of the sub-texture
126+
* @return a float specifying the leftmost extent of the sub-texture
127+
*/
46128
inline float GetMinX() const
47129
{
48130
return minX;
49131
}
132+
/**
133+
* Returns the topmost coordinate of the sub-texture
134+
* @return a float specifying the topmost coordinate of the sub-texture
135+
*/
50136
inline float GetMinY() const
51137
{
52138
return minY;
53139
}
140+
/**
141+
* Returns the width of the sub-texture
142+
* @return the width of the sub-texture
143+
*/
54144
inline float GetWidth() const
55145
{
56146
return width;
57147
}
148+
/**
149+
* Returns the height of the sub-texture
150+
* @return the height of the sub-texture
151+
*/
58152
inline float GetHeight() const
59153
{
60154
return height;
61155
}
62156

63157
private:
64158

65-
// NOTE(Aryeh): Can we always guarantee that the atlas will exist?
159+
// Private methods
160+
161+
/**
162+
* Swaps the contents of two SubTextureRefs
163+
* @param other the SubTextureRef to swap contents with
164+
*/
165+
void Swap(SubTextureRef& other);
166+
167+
// Private member variables
168+
66169
TextureAtlas* parentAtlas {nullptr};
67170
float minX, width, minY, height {0.f};
68171
};
69172

70173
// 'Structors
71174

175+
/**
176+
* The default TextureAtlas constructor (empty constructor)
177+
*/
72178
TextureAtlas() = default;
179+
180+
/**
181+
* The file upload TextureAtlas constructor. Loads texture data from disk and stores it in the
182+
* memory
183+
* @param name the name of the texture
184+
* @param filePath the path to the texture file
185+
* @param imageExtents the width and height of the texture
186+
* @param filter the scaling filter used for the texture
187+
*/
73188
TextureAtlas(const char* name,
74189
const char* filePath,
75190
Utils::Extent2DF imageExtents,
76191
Utils::TextureFilter filter = Utils::TEXTURE_FILTER_LINEAR);
192+
193+
/**
194+
* The TextureAtlas move constructor. Moves the contents of one TextureAtlas to another
195+
* @param other the TextureAtlas to move information from
196+
*/
77197
TextureAtlas(TextureAtlas&& other);
198+
199+
/**
200+
* The TextureAtlas Destructor. Destroys all memory stored by the TextureAtlas
201+
*/
78202
~TextureAtlas();
79203

80204
// Operator Overloads
81205

82-
TextureRef operator[](size_t index);
206+
/**
207+
* The TextureAtlas subscript operator. Indexes into a texture atlas and returns the SubTexture
208+
* stored in that position.
209+
* NOTE: So far TextureAtlases only support fixed-size textures
210+
* @param index the index of the SubTexture
211+
* @return the SubTextureRef stored in the specified location
212+
*/
213+
SubTextureRef operator[](size_t index);
214+
215+
/**
216+
* The TextureAtlas move assignment operator
217+
* @param other the TextureAtlas to move information from
218+
* @return a reference to the TextureAtlas
219+
*/
220+
TextureAtlas& operator=(TextureAtlas&& other);
83221

84222
// Getters & Setters
85223

224+
// TODO(Aryeh): Need a way to add new textures to the Atlas in future
225+
// TODO(Aryeh): Need a way to manually specify each texture coordinate when not using fixed
226+
// textures
227+
228+
/**
229+
* Gets the Texture2D stored within the Atlas
230+
* @return the atlas' texture
231+
*/
86232
inline Texture2D& GetTexture()
87233
{
88234
return texture;
89235
}
90-
inline const Texture2D& GetTexture() const
91-
{
92-
return texture;
93-
}
94236

95237
private:
96238

97239
// Private functions
98240

241+
/**
242+
* Swaps the contents of two TextureAtlases
243+
* @param other the TextureAtlas to swap contents with
244+
*/
99245
void Swap(TextureAtlas& other);
100246

101247
// Private member variables

engine/render/renderer/renderer/Renderer2D.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void Renderer2D::DrawQuad(const Vec2 position,
130130
}
131131

132132
void Renderer2D::DrawQuad(const Vec2 position,
133-
Vulkan::TextureAtlas::TextureRef& texture,
133+
Vulkan::TextureAtlas::SubTextureRef& texture,
134134
const Vec2 scale,
135135
IColour colour,
136136
float rotation,

engine/render/renderer/renderer/Renderer2D.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Renderer2D
3636
const uint8_t zIndex = 0,
3737
Vulkan::Texture2D* texture = nullptr);
3838
void DrawQuad(const Vec2 position,
39-
Vulkan::TextureAtlas::TextureRef& texture,
39+
Vulkan::TextureAtlas::SubTextureRef& texture,
4040
const Vec2 scale = Vec2::One(),
4141
const IColour colour = IColour::White,
4242
float rotation = 0.f,

examples/tilemap/src/main.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,15 @@ int main()
3232

3333
Siege::Camera camera;
3434

35-
camera.projection =
36-
Siege::Orthographic(0.f, window.GetWidth(), window.GetHeight(), 0.f, 0.1f, 1000.f);
37-
camera.view = Siege::LookAt(Siege::Vec3::Zero(), {0.f, 0.f, 1.f});
38-
39-
renderer.SetCamera2D(camera);
40-
4135
while (!window.WindowShouldClose())
4236
{
4337
window.Update();
4438

39+
camera.projection =
40+
Siege::Orthographic(0.f, window.GetWidth(), window.GetHeight(), 0.f, 0.1f, 1000.f);
41+
camera.view = Siege::LookAt(Siege::Vec3::Zero(), Siege::Vec3::Forward());
42+
renderer.SetCamera2D(camera);
43+
4544
if (!renderer.StartFrame()) continue;
4645

4746
renderer.DrawGrid2D(100.f, {.2f, .2f, .2f}, window.GetDPI());

0 commit comments

Comments
 (0)