summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/common/bgfx_utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/bgfx/examples/common/bgfx_utils.cpp')
-rw-r--r--3rdparty/bgfx/examples/common/bgfx_utils.cpp89
1 files changed, 85 insertions, 4 deletions
diff --git a/3rdparty/bgfx/examples/common/bgfx_utils.cpp b/3rdparty/bgfx/examples/common/bgfx_utils.cpp
index a638e4e7d4b..5f19524e568 100644
--- a/3rdparty/bgfx/examples/common/bgfx_utils.cpp
+++ b/3rdparty/bgfx/examples/common/bgfx_utils.cpp
@@ -15,6 +15,7 @@ namespace stl = tinystl;
#include <bgfx.h>
#include <bx/readerwriter.h>
#include <bx/fpumath.h>
+#include <bx/string.h>
#include "entry/entry.h"
#include <ib-compress/indexbufferdecompression.h>
@@ -62,6 +63,25 @@ static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const char* _filePa
return NULL;
}
+static void* loadMem(bx::FileReaderI* _reader, bx::ReallocatorI* _allocator, const char* _filePath, uint32_t* _size)
+{
+ if (0 == bx::open(_reader, _filePath) )
+ {
+ uint32_t size = (uint32_t)bx::getSize(_reader);
+ void* data = BX_ALLOC(_allocator, size);
+ bx::read(_reader, data, size);
+ bx::close(_reader);
+
+ if (NULL != _size)
+ {
+ *_size = size;
+ }
+ return data;
+ }
+
+ return NULL;
+}
+
static bgfx::ShaderHandle loadShader(bx::FileReaderI* _reader, const char* _name)
{
char filePath[512];
@@ -112,15 +132,76 @@ bgfx::ProgramHandle loadProgram(const char* _vsName, const char* _fsName)
return loadProgram(entry::getFileReader(), _vsName, _fsName);
}
+typedef unsigned char stbi_uc;
+extern "C" stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+
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);
- const bgfx::Memory* mem = loadMem(_reader, filePath);
+ if (NULL != bx::stristr(_name, ".dds")
+ || NULL != bx::stristr(_name, ".pvr")
+ || NULL != bx::stristr(_name, ".ktx") )
+ {
+ const bgfx::Memory* mem = loadMem(_reader, filePath);
+ 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);
+ if (NULL != data)
+ {
+ 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 bgfx::createTexture(mem, _flags, _skip, _info);
+ return handle;
}
bgfx::TextureHandle loadTexture(const char* _name, uint32_t _flags, uint8_t _skip, bgfx::TextureInfo* _info)