Check if file to load texture from exists, display error if not

This commit is contained in:
saschawillems 2017-04-13 22:11:00 +02:00
parent ac0a4989bd
commit b399fed33b
3 changed files with 19 additions and 1 deletions

View file

@ -104,6 +104,9 @@ namespace vks
free(textureData); free(textureData);
#else #else
if (!vks::tools::fileExists(filename)) {
vks::tools::exitFatal("Could not load texture from " + filename, "File not found");
}
gli::texture2d tex2D(gli::load(filename.c_str())); gli::texture2d tex2D(gli::load(filename.c_str()));
#endif #endif
assert(!tex2D.empty()); assert(!tex2D.empty());
@ -582,9 +585,11 @@ namespace vks
free(textureData); free(textureData);
#else #else
if (!vks::tools::fileExists(filename)) {
vks::tools::exitFatal("Could not load texture from " + filename, "File not found");
}
gli::texture2d_array tex2DArray(gli::load(filename)); gli::texture2d_array tex2DArray(gli::load(filename));
#endif #endif
assert(!tex2DArray.empty()); assert(!tex2DArray.empty());
this->device = device; this->device = device;
@ -791,6 +796,9 @@ namespace vks
free(textureData); free(textureData);
#else #else
if (!vks::tools::fileExists(filename)) {
vks::tools::exitFatal("Could not load texture from " + filename, "File not found");
}
gli::texture_cube texCube(gli::load(filename)); gli::texture_cube texCube(gli::load(filename));
#endif #endif
assert(!texCube.empty()); assert(!texCube.empty());

View file

@ -375,5 +375,11 @@ namespace vks
return shaderModule; return shaderModule;
} }
bool fileExists(const std::string &filename)
{
std::ifstream f(filename.c_str());
return !f.fail();
}
} }
} }

View file

@ -21,6 +21,7 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <fstream>
#if defined(_WIN32) #if defined(_WIN32)
#include <windows.h> #include <windows.h>
#include <fcntl.h> #include <fcntl.h>
@ -123,5 +124,8 @@ namespace vks
// Load a GLSL shader (text) // Load a GLSL shader (text)
// Note: GLSL support requires vendor-specific extensions to be enabled and is not a core-feature of Vulkan // Note: GLSL support requires vendor-specific extensions to be enabled and is not a core-feature of Vulkan
VkShaderModule loadShaderGLSL(const char *fileName, VkDevice device, VkShaderStageFlagBits stage); VkShaderModule loadShaderGLSL(const char *fileName, VkDevice device, VkShaderStageFlagBits stage);
/** @brief Checks if a file exists */
bool fileExists(const std::string &filename);
} }
} }