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
+
15
+ Texture2D& TextureAtlas::SubTextureRef::operator *()
16
+ {
17
+ return parentAtlas->texture ;
18
+ }
19
+
20
+ TextureAtlas* TextureAtlas::SubTextureRef::operator ->()
21
+ {
22
+ return parentAtlas;
23
+ }
24
+
25
+ TextureAtlas::SubTextureRef::operator bool () const
26
+ {
27
+ return parentAtlas;
28
+ }
29
+
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
+
62
+ TextureAtlas::TextureAtlas (const char * name,
63
+ const char * filePath,
64
+ Utils::Extent2DF imageExtents,
65
+ Utils::TextureFilter filter) :
66
+ fixedExtent {imageExtents}
67
+ {
68
+ texture = Texture2D (name, filePath, filter);
69
+ }
70
+
71
+ TextureAtlas::TextureAtlas (TextureAtlas&& other)
72
+ {
73
+ Swap (other);
74
+ }
75
+
76
+ TextureAtlas::~TextureAtlas ()
77
+ {
78
+ fixedExtent = {};
79
+ }
80
+
81
+ TextureAtlas::SubTextureRef TextureAtlas::operator [](size_t index)
82
+ {
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
87
+ size_t elementsInRow = 1 / fixedExtent.width ;
88
+
89
+ return SubTextureRef (this ,
90
+ (index % elementsInRow) * fixedExtent.width , // potentially slow code
91
+ (index / elementsInRow) * fixedExtent.height ,
92
+ fixedExtent.width ,
93
+ fixedExtent.height );
94
+ }
95
+
96
+ TextureAtlas& TextureAtlas::operator =(TextureAtlas&& other)
97
+ {
98
+ Swap (other);
99
+ return *this ;
100
+ }
101
+
102
+ void TextureAtlas::Swap (TextureAtlas& other)
103
+ {
104
+ auto tmpTexture = std::move (texture);
105
+ auto tmpFixedExtent = fixedExtent;
106
+
107
+ texture = std::move (other.texture );
108
+ fixedExtent = other.fixedExtent ;
109
+
110
+ other.texture = std::move (tmpTexture);
111
+ other.fixedExtent = tmpFixedExtent;
112
+ }
113
+
114
+ } // namespace Siege::Vulkan
0 commit comments