diff --git a/examples/common/bgfx_utils.cpp b/examples/common/bgfx_utils.cpp index 540fa2df..5f19524e 100644 --- a/examples/common/bgfx_utils.cpp +++ b/examples/common/bgfx_utils.cpp @@ -76,7 +76,6 @@ static void* loadMem(bx::FileReaderI* _reader, bx::ReallocatorI* _allocator, con { *_size = size; } - return data; } @@ -138,8 +137,12 @@ extern "C" stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x bgfx::TextureHandle loadTexture(bx::FileReaderI* _reader, const char* _name, uint32_t _flags, uint8_t _skip, bgfx::TextureInfo* _info) { - char filePath[512]; - strcpy(filePath, "textures/"); + char filePath[512] = { '\0' }; + if (NULL == strchr(_name, '/') ) + { + strcpy(filePath, "textures/"); + } + strcat(filePath, _name); if (NULL != bx::stristr(_name, ".dds") @@ -147,41 +150,55 @@ bgfx::TextureHandle loadTexture(bx::FileReaderI* _reader, const char* _name, uin || NULL != bx::stristr(_name, ".ktx") ) { const bgfx::Memory* mem = loadMem(_reader, filePath); - return bgfx::createTexture(mem, _flags, _skip, _info); + if (NULL != mem) + { + return bgfx::createTexture(mem, _flags, _skip, _info); + } + + bgfx::TextureHandle handle = BGFX_INVALID_HANDLE; + DBG("Failed to load %s.", filePath); + return handle; } + bgfx::TextureHandle handle = BGFX_INVALID_HANDLE; bx::ReallocatorI* allocator = entry::getAllocator(); uint32_t size = 0; void* data = loadMem(_reader, allocator, filePath, &size); - - int width = 0; - int height = 0; - int comp = 0; - - uint8_t* img = NULL; - img = stbi_load_from_memory( (uint8_t*)data, size, &width, &height, &comp, 4); - - BX_FREE(allocator, data); - - bgfx::TextureHandle handle = bgfx::createTexture2D(uint16_t(width), uint16_t(height), 1 - , bgfx::TextureFormat::RGBA8 - , _flags - , bgfx::copy(img, width*height*4) - ); - - free(img); - - if (NULL != _info) + if (NULL != data) { - bgfx::calcTextureSize(*_info - , uint16_t(width) - , uint16_t(height) - , 0 - , false - , 1 - , bgfx::TextureFormat::RGBA8 - ); + int width = 0; + int height = 0; + int comp = 0; + + uint8_t* img = NULL; + img = stbi_load_from_memory( (uint8_t*)data, size, &width, &height, &comp, 4); + + BX_FREE(allocator, data); + + handle = bgfx::createTexture2D(uint16_t(width), uint16_t(height), 1 + , bgfx::TextureFormat::RGBA8 + , _flags + , bgfx::copy(img, width*height*4) + ); + + free(img); + + if (NULL != _info) + { + bgfx::calcTextureSize(*_info + , uint16_t(width) + , uint16_t(height) + , 0 + , false + , 1 + , bgfx::TextureFormat::RGBA8 + ); + } + } + else + { + DBG("Failed to load %s.", filePath); } return handle;