1212
1313const static std::filesystem::path COMPRESSED_TEX_DIR{ std::filesystem::current_path () / " Data/cache/textures" };
1414
15+ using ImageBuffer = std::unique_ptr<unsigned char []>;
16+
1517struct CompressedImageDesc {
1618 GLint width{ -1 };
1719 GLint height{ -1 };
1820 GLint size{ -1 };
1921 GLint format{ -1 };
20- unsigned char * data{ nullptr } ;
22+ ImageBuffer data;
2123};
2224
2325/* **********************************************************************************/
@@ -98,14 +100,34 @@ void saveCompressedImageToDisk(const std::filesystem::path& target, const Compre
98100
99101 std::ofstream out (target, std::ios::binary);
100102 if (out) {
101- out.write ((char *)(&desc.size ), sizeof (GLint ));
102- out.write ((char *)(&desc.width ), sizeof (GLint ));
103- out.write ((char *)(&desc.height ), sizeof (GLint ));
104- out.write ((char *)(&desc.format ), sizeof (GLint ));
105- out.write ((char *)(desc.data ), desc.size );
103+ out.write ((char *)(&desc.size ), sizeof (CompressedImageDesc::size ));
104+ out.write ((char *)(&desc.width ), sizeof (CompressedImageDesc::width ));
105+ out.write ((char *)(&desc.height ), sizeof (CompressedImageDesc::height ));
106+ out.write ((char *)(&desc.format ), sizeof (CompressedImageDesc::format ));
107+ out.write ((char *)(desc.data . get () ), desc.size );
106108 }
107109}
108110
111+ /* **********************************************************************************/
112+ std::optional<CompressedImageDesc> loadCompressedImageFromDisk (const std::filesystem::path& target) {
113+ CompressedImageDesc desc;
114+
115+ std::ifstream in (target, std::ios::binary);
116+ if (!in) {
117+ return std::nullopt ;
118+ }
119+
120+ in.read (reinterpret_cast <char *>(&desc.size ), sizeof (CompressedImageDesc::size));
121+ in.read (reinterpret_cast <char *>(&desc.width ), sizeof (CompressedImageDesc::width));
122+ in.read (reinterpret_cast <char *>(&desc.height ), sizeof (CompressedImageDesc::height));
123+ in.read (reinterpret_cast <char *>(&desc.format ), sizeof (CompressedImageDesc::format));
124+
125+ desc.data = std::make_unique<unsigned char []>(desc.size );
126+ in.read (reinterpret_cast <char *>(desc.data .get ()), desc.size );
127+
128+ return std::make_optional (std::move (desc));
129+ }
130+
109131/* **********************************************************************************/
110132unsigned int ResourceManager::LoadTexture (const std::filesystem::path& path, const bool useMipMaps, const bool useUnalignedUnpack) {
111133
@@ -132,31 +154,23 @@ unsigned int ResourceManager::LoadTexture(const std::filesystem::path& path, con
132154 glGenTextures (1 , &textureID);
133155
134156 if (compressedImageExists) {
135- std::ifstream in (compressedFilePath, std::ios::binary);
136- in.exceptions (std::ifstream::failbit | std::ifstream::badbit);
137- if (!in) {
138- return 0 ;
139- }
140-
141- GLint size{ -1 };
142- GLint width{ -1 };
143- GLint height{ -1 };
144- GLint format{ -1 };
145-
146- in.read ((char *)&size, sizeof (GLint));
147- if (in.fail () || size == -1 ) {
157+
158+ const auto desc{ loadCompressedImageFromDisk (compressedFilePath) };
159+ if (!desc) {
148160 return 0 ;
149161 }
150162
151- in.read ((char *)&width, sizeof (GLint));
152- in.read ((char *)&height, sizeof (GLint));
153- in.read ((char *)&format, sizeof (GLint));
154-
155- auto compressedData = std::make_unique<unsigned char []>(size);
156- in.read ((char *)compressedData.get (), size);
157-
158163 glBindTexture (GL_TEXTURE_2D, textureID);
159- glCompressedTexImage2D (GL_TEXTURE_2D, 0 , format, width, height, 0 , size, compressedData.get ());
164+ glCompressedTexImage2D (
165+ GL_TEXTURE_2D,
166+ 0 ,
167+ desc.value ().format ,
168+ desc.value ().width ,
169+ desc.value ().height ,
170+ 0 ,
171+ desc.value ().size ,
172+ desc.value ().data .get ()
173+ );
160174
161175 } else {
162176 int width = 0 , height = 0 , nrComponents = 0 ;
@@ -195,23 +209,17 @@ unsigned int ResourceManager::LoadTexture(const std::filesystem::path& path, con
195209 GLint compressed = GL_FALSE;
196210 glGetTexLevelParameteriv (GL_TEXTURE_2D, 0 , GL_TEXTURE_COMPRESSED, &compressed);
197211 if (compressed == GL_TRUE) {
198- GLint compressedSize = -1 ;
199- glGetTexLevelParameteriv (GL_TEXTURE_2D, 0 , GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &compressedSize);
200-
201- GLint internalFormat = -1 ;
202- glGetTexLevelParameteriv (GL_TEXTURE_2D, 0 , GL_TEXTURE_INTERNAL_FORMAT, &internalFormat);
203-
204- auto compressedData = std::make_unique<unsigned char []>(compressedSize);
205- glGetCompressedTexImage (GL_TEXTURE_2D, 0 , (GLvoid*)compressedData.get ());
206-
207- const CompressedImageDesc desc {
212+ CompressedImageDesc desc{
208213 .width = width,
209- .height = height,
210- .size = compressedSize,
211- .format = internalFormat,
212- .data = compressedData.get ()
214+ .height = height
213215 };
214216
217+ glGetTexLevelParameteriv (GL_TEXTURE_2D, 0 , GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &desc.size );
218+ glGetTexLevelParameteriv (GL_TEXTURE_2D, 0 , GL_TEXTURE_INTERNAL_FORMAT, &desc.format );
219+
220+ desc.data = std::make_unique<unsigned char []>(desc.size );
221+ glGetCompressedTexImage (GL_TEXTURE_2D, 0 , (GLvoid*)desc.data .get ());
222+
215223 saveCompressedImageToDisk (compressedFilePath, desc);
216224 }
217225 }
0 commit comments