From 261eeeedc433804d335a8a2c5fcb5782147dd33e Mon Sep 17 00:00:00 2001 From: Trevor Hollmann <thollmann@uni-koblenz.de> Date: Tue, 17 Aug 2021 09:52:27 +0200 Subject: [PATCH] [#79] Handle errors in loadTexture(). --- modules/asset_loader/include/vkcv/asset/asset_loader.hpp | 3 +++ modules/asset_loader/src/vkcv/asset/asset_loader.cpp | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/modules/asset_loader/include/vkcv/asset/asset_loader.hpp b/modules/asset_loader/include/vkcv/asset/asset_loader.hpp index 0ae31deb..8d80062b 100644 --- a/modules/asset_loader/include/vkcv/asset/asset_loader.hpp +++ b/modules/asset_loader/include/vkcv/asset/asset_loader.hpp @@ -308,6 +308,9 @@ int loadScene(const std::filesystem::path &path, Scene &scene); * loading a font atlas) and not meant to be used for regular assets. * The sampler is set to -1, signalling that this Texture was loaded * outside the context of a glTF-file. + * If there was an error loading or decoding the image, the returned struct + * will be cleared to all 0 with path and data being empty; make sure to always + * check that !data.empty() before using the struct. * * @param path must be the path to an image file. * @return Texture struct describing the loaded image. diff --git a/modules/asset_loader/src/vkcv/asset/asset_loader.cpp b/modules/asset_loader/src/vkcv/asset/asset_loader.cpp index 5ce21ec8..8042f1b2 100644 --- a/modules/asset_loader/src/vkcv/asset/asset_loader.cpp +++ b/modules/asset_loader/src/vkcv/asset/asset_loader.cpp @@ -760,7 +760,11 @@ namespace vkcv::asset { Texture texture; texture.path = path; texture.sampler = -1; - loadTextureData(texture); + if (loadTextureData(texture) != ASSET_SUCCESS) { + texture.path.clear(); + texture.w = texture.h = texture.channels = 0; + texture.data.clear(); + } return texture; } -- GitLab