Skip to content

Commit f2bba80

Browse files
committed
Created TextureAtlas class and implemented basic texture atlas functions
1 parent 8f92f40 commit f2bba80

File tree

7 files changed

+228
-7
lines changed

7 files changed

+228
-7
lines changed

engine/render/renderer/Renderer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ void Renderer::DrawQuad(const Vec2 position,
122122
renderer2D.DrawQuad(position, scale, colour, rotation, zIndex, texture);
123123
}
124124

125+
void Renderer::DrawQuad(const Vec2 position,
126+
Vulkan::TextureAtlas::TextureRef texture,
127+
const Vec2 scale,
128+
const IColour colour,
129+
float rotation,
130+
const uint8_t zIndex)
131+
{
132+
renderer2D.DrawQuad(position, texture, scale, colour, rotation, zIndex);
133+
}
134+
125135
void Renderer::DrawText2D(const char* text,
126136
Vulkan::Font& font,
127137
const Vec2 position,

engine/render/renderer/Renderer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ class Renderer
5353
float rotation = 0.f,
5454
const uint8_t zIndex = 0,
5555
Vulkan::Texture2D* texture = nullptr);
56+
void DrawQuad(const Vec2 position,
57+
Vulkan::TextureAtlas::TextureRef texture,
58+
const Vec2 scale = {1.f, 1.f},
59+
const IColour colour = IColour::White,
60+
float rotation = 0.f,
61+
const uint8_t zIndex = 0);
5662
void DrawText2D(const char* text,
5763
Vulkan::Font& font,
5864
const Vec2 position,
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// Copyright (c) 2020-present Caps Collective & contributors
3+
// Originally authored by Jonathan Moallem (@jonjondev) & Aryeh Zinn (@Raelr)
4+
//
5+
// This code is released under an unmodified zlib license.
6+
// For conditions of distribution and use, please see:
7+
// https://opensource.org/licenses/Zlib
8+
//
9+
10+
#include "TextureAtlas.h"
11+
12+
namespace Siege::Vulkan
13+
{
14+
Texture2D& TextureAtlas::TextureRef::operator*()
15+
{
16+
return parentAtlas.texture;
17+
}
18+
19+
TextureAtlas::TextureAtlas(const char* name,
20+
const char* filePath,
21+
Utils::Extent2DF imageExtents,
22+
Utils::TextureFilter filter) :
23+
fixedExtent {imageExtents}
24+
{
25+
texture = Texture2D(name, filePath, filter);
26+
}
27+
28+
TextureAtlas::TextureAtlas(TextureAtlas&& other)
29+
{
30+
Swap(other);
31+
}
32+
33+
TextureAtlas::~TextureAtlas() {}
34+
35+
void TextureAtlas::Swap(TextureAtlas& other)
36+
{
37+
auto tmpTexture = std::move(texture);
38+
auto tmpFixedExtent = fixedExtent;
39+
40+
texture = std::move(other.texture);
41+
fixedExtent = other.fixedExtent;
42+
43+
other.texture = std::move(tmpTexture);
44+
other.fixedExtent = tmpFixedExtent;
45+
}
46+
47+
TextureAtlas::TextureRef TextureAtlas::operator[](size_t index)
48+
{
49+
size_t elementsInRow = 1 / fixedExtent.width;
50+
51+
return TextureRef(*this,
52+
(index % elementsInRow) * fixedExtent.width, // potentially slow code
53+
(index / elementsInRow) * fixedExtent.height,
54+
fixedExtent.width,
55+
fixedExtent.height);
56+
}
57+
58+
} // namespace Siege::Vulkan
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// Copyright (c) 2020-present Caps Collective & contributors
3+
// Originally authored by Jonathan Moallem (@jonjondev) & Aryeh Zinn (@Raelr)
4+
//
5+
// This code is released under an unmodified zlib license.
6+
// For conditions of distribution and use, please see:
7+
// https://opensource.org/licenses/Zlib
8+
//
9+
10+
#ifndef SIEGE_ENGINE_VULKAN_TEXTURE_ATLAS_H
11+
#define SIEGE_ENGINE_VULKAN_TEXTURE_ATLAS_H
12+
13+
#include "Texture2D.h"
14+
#include "utils/Types.h"
15+
16+
namespace Siege::Vulkan
17+
{
18+
19+
class TextureAtlas
20+
{
21+
public:
22+
23+
class TextureRef
24+
{
25+
public:
26+
27+
TextureRef(TextureAtlas& parent, float minX, float minY, float width, float height) :
28+
parentAtlas {parent},
29+
minX {minX},
30+
width {width},
31+
minY {minY},
32+
height {height}
33+
{}
34+
Texture2D& operator*();
35+
36+
inline float GetMinX() const
37+
{
38+
return minX;
39+
}
40+
inline float GetMinY() const
41+
{
42+
return minY;
43+
}
44+
45+
inline float GetWidth() const
46+
{
47+
return width;
48+
}
49+
inline float GetHeight() const
50+
{
51+
return height;
52+
}
53+
54+
private:
55+
56+
// NOTE(Aryeh): Can we always guarantee that the atlas will exist?
57+
TextureAtlas& parentAtlas;
58+
float minX, width, minY, height {0.f};
59+
};
60+
61+
TextureAtlas() = default;
62+
TextureAtlas(const char* name,
63+
const char* filePath,
64+
Utils::Extent2DF imageExtents,
65+
Utils::TextureFilter filter = Utils::TEXTURE_FILTER_LINEAR);
66+
TextureAtlas(TextureAtlas&& other);
67+
~TextureAtlas();
68+
69+
TextureRef operator[](size_t index);
70+
71+
private:
72+
73+
void Swap(TextureAtlas& other);
74+
Texture2D texture;
75+
Utils::Extent2DF fixedExtent {};
76+
};
77+
78+
} // namespace Siege::Vulkan
79+
80+
#endif // SIEGE_ENGINE_VULKAN_TEXTURE_ATLAS_H

engine/render/renderer/renderer/Renderer2D.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,30 @@ void Renderer2D::DrawQuad(const Vec2 position,
108108
{0.f, 0.f, 1.f, 1.f}});
109109
}
110110

111+
void Renderer2D::DrawQuad(const Vec2 position,
112+
Vulkan::TextureAtlas::TextureRef& texture,
113+
const Vec2 scale,
114+
IColour colour,
115+
float rotation,
116+
uint8_t zIndex)
117+
{
118+
CC_ASSERT(zIndex < MAX_LAYERS, "zIndex provided is larger than the maximum number of layers")
119+
120+
auto texIndex = quadMaterial.SetTexture(textureId, &(*texture));
121+
122+
auto& layerQuads = quads[zIndex];
123+
124+
if (texIndex >= layerQuads.Count())
125+
layerQuads[texIndex] = MHArray<QuadVertex>(MAX_QUADS_PER_LAYER);
126+
127+
layerQuads[texIndex].Append(
128+
{Transform3D({position.x + scale.x, position.y + scale.y, 1.f},
129+
{0.f, 0.f, rotation},
130+
scale),
131+
ToFColour(colour),
132+
{texture.GetMinX(), texture.GetMinY(), texture.GetWidth(), texture.GetHeight()}});
133+
}
134+
111135
void Renderer2D::DrawText2D(const char* const text,
112136
const Vec2 position,
113137
const Vec2 scale,

engine/render/renderer/renderer/Renderer2D.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "render/renderer/platform/vulkan/Font.h"
1818
#include "render/renderer/platform/vulkan/IndexBuffer.h"
1919
#include "render/renderer/platform/vulkan/Material.h"
20+
#include "render/renderer/platform/vulkan/TextureAtlas.h"
2021
#include "render/renderer/platform/vulkan/VertexBuffer.h"
2122

2223
namespace Siege
@@ -33,6 +34,12 @@ class Renderer2D
3334
float rotation = 0.f,
3435
const uint8_t zIndex = 0,
3536
Vulkan::Texture2D* texture = nullptr);
37+
void DrawQuad(const Vec2 position,
38+
Vulkan::TextureAtlas::TextureRef& texture,
39+
const Vec2 scale = Vec2::One(),
40+
const IColour colour = IColour::White,
41+
float rotation = 0.f,
42+
const uint8_t zIndex = 0);
3643
void DrawText2D(const char* const text,
3744
const Vec2 position,
3845
const Vec2 scale,

examples/tilemap/src/main.cpp

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <render/renderer/Renderer.h>
1111
#include <render/renderer/camera/Camera.h>
12+
#include <render/renderer/platform/vulkan/TextureAtlas.h>
1213
#include <utils/math/Projection.h>
1314
#include <utils/math/Transform.h>
1415
#include <utils/math/vec/Vec3.h>
@@ -24,9 +25,11 @@ int main()
2425
Siege::Renderer renderer(window);
2526

2627
auto pixel = Siege::Vulkan::Font("assets/fonts/PublicPixel.ttf");
27-
auto tilemap = Siege::Vulkan::Texture2D("tilemap",
28-
"assets/textures/tilemap.png",
29-
Siege::Vulkan::Utils::TEXTURE_FILTER_NEAREST);
28+
auto tilemap = Siege::Vulkan::TextureAtlas("tilemap",
29+
"assets/textures/tilemap.png",
30+
{.5f, .5f},
31+
Siege::Vulkan::Utils::TEXTURE_FILTER_NEAREST);
32+
auto fullTile = Siege::Vulkan::Texture2D("full tilemap", "assets/textures/tilemap.png", Siege::Vulkan::Utils::TEXTURE_FILTER_NEAREST);
3033

3134
Siege::Camera camera;
3235

@@ -44,14 +47,47 @@ int main()
4447

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

47-
renderer.DrawText2D("Hello World!",
50+
renderer.DrawText2D("Full Texture",
4851
pixel,
49-
{10.f, 10.f},
50-
{64.f, 64.f},
52+
{window.GetWidth() / 4.f, 20.f},
53+
{32.f, 32.f},
5154
0.f,
5255
Siege::IColour::Black);
5356

54-
renderer.DrawQuad({400.f, 300.f}, {64.f, 64.f}, Siege::IColour::White, 0.f, 0, &tilemap);
57+
renderer.DrawQuad({(window.GetWidth() / 2.f) - 64.f, 100.f}, {64.f, 64.f}, Siege::IColour::White, 0.f, 0, &fullTile);
58+
59+
renderer.DrawText2D("Texture 0",
60+
pixel,
61+
{(window.GetWidth() / 2.f) * 0.1f, 275.f},
62+
{16.f, 16.f},
63+
0.f,
64+
Siege::IColour::Black);
65+
66+
renderer.DrawText2D("Texture 1",
67+
pixel,
68+
{(window.GetWidth() / 2.f) * 0.6f, 275.f},
69+
{16.f, 16.f},
70+
0.f,
71+
Siege::IColour::Black);
72+
73+
renderer.DrawText2D("Texture 2",
74+
pixel,
75+
{(window.GetWidth() / 2.f) * 1.1f, 275.f},
76+
{16.f, 16.f},
77+
0.f,
78+
Siege::IColour::Black);
79+
80+
renderer.DrawText2D("Texture 3",
81+
pixel,
82+
{(window.GetWidth() / 2.f) * 1.6f, 275.f},
83+
{16.f, 16.f},
84+
0.f,
85+
Siege::IColour::Black);
86+
87+
renderer.DrawQuad({(window.GetWidth() / 2.f) * 0.1f, 325.f}, tilemap[0], {64.f, 64.f}, Siege::IColour::White, 0.f, 0);
88+
renderer.DrawQuad({(window.GetWidth() / 2.f) * 0.6f, 325.f}, tilemap[1], {64.f, 64.f}, Siege::IColour::White, 0.f, 0);
89+
renderer.DrawQuad({(window.GetWidth() / 2.f) * 1.1f, 325.f}, tilemap[2], {64.f, 64.f}, Siege::IColour::White, 0.f, 0);
90+
renderer.DrawQuad({(window.GetWidth() / 2.f) * 1.6f, 325.f}, tilemap[3], {64.f, 64.f}, Siege::IColour::White, 0.f, 0);
5591

5692
renderer.EndFrame();
5793
}

0 commit comments

Comments
 (0)