From 9128c4f3747a1ef68ebe6465fe92df0fdab6bab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Wed, 12 Apr 2017 20:53:58 -0700 Subject: [PATCH] Fixed NanoVG. --- examples/20-nanovg/nanovg.cpp | 41 +++++++++++++++- examples/common/nanovg/nanovg.cpp | 65 -------------------------- examples/common/nanovg/nanovg_bgfx.cpp | 3 +- 3 files changed, 41 insertions(+), 68 deletions(-) diff --git a/examples/20-nanovg/nanovg.cpp b/examples/20-nanovg/nanovg.cpp index e6f89ce7c..6451d3b7b 100644 --- a/examples/20-nanovg/nanovg.cpp +++ b/examples/20-nanovg/nanovg.cpp @@ -29,6 +29,8 @@ #include #include +#include + #include "entry/entry.h" #include "imgui/imgui.h" #include "nanovg/nanovg.h" @@ -939,13 +941,48 @@ struct DemoData int images[12]; }; +int createImage(struct NVGcontext* _ctx, const char* _filePath, int _imageFlags) +{ + uint32_t size; + void* data = load(_filePath, &size); + if (NULL == data) + { + return 0; + } + + bimg::ImageContainer* imageContainer = bimg::imageParse( + entry::getAllocator() + , data + , size + , bimg::TextureFormat::RGBA8 + ); + unload(data); + + if (NULL == imageContainer) + { + return 0; + } + + int texId = nvgCreateImageRGBA( + _ctx + , imageContainer->m_width + , imageContainer->m_height + , _imageFlags + , (const uint8_t*)imageContainer->m_data + ); + + bimg::imageFree(imageContainer); + + return texId; +} + int loadDemoData(struct NVGcontext* vg, struct DemoData* data) { for (uint32_t ii = 0; ii < 12; ++ii) { char file[128]; bx::snprintf(file, 128, "images/image%d.jpg", ii+1); - data->images[ii] = nvgCreateImage(vg, file, 0); + data->images[ii] = createImage(vg, file, 0); if (data->images[ii] == 0) { printf("Could not load %s.\n", file); @@ -1234,7 +1271,7 @@ class ExampleNanoVG : public entry::AppI loadDemoData(m_nvg, &m_data); bndSetFont(nvgCreateFont(m_nvg, "droidsans", "font/droidsans.ttf") ); - bndSetIconImage(nvgCreateImage(m_nvg, "images/blender_icons16.png", 0) ); + bndSetIconImage(createImage(m_nvg, "images/blender_icons16.png", 0) ); m_timeOffset = bx::getHPCounter(); } diff --git a/examples/common/nanovg/nanovg.cpp b/examples/common/nanovg/nanovg.cpp index c7687282d..dcd652c8c 100644 --- a/examples/common/nanovg/nanovg.cpp +++ b/examples/common/nanovg/nanovg.cpp @@ -23,10 +23,6 @@ #include "nanovg.h" -#ifndef NANOVG_HAS_STB_IMAGE -# define NANOVG_HAS_STB_IMAGE 0 -#endif // NANOVG_HAS_STB_IMAGE - #include BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4701) // error C4701: potentially uninitialized local variable 'cint' used @@ -41,28 +37,6 @@ BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wunused-result"); #include "fontstash.h" BX_PRAGMA_DIAGNOSTIC_POP(); -#if NANOVG_HAS_STB_IMAGE -#define LODEPNG_NO_COMPILE_ENCODER -#define LODEPNG_NO_COMPILE_DISK -#define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS -#define LODEPNG_NO_COMPILE_ERROR_TEXT -#define LODEPNG_NO_COMPILE_ALLOCATORS -#define LODEPNG_NO_COMPILE_CPP -#include - -BX_PRAGMA_DIAGNOSTIC_PUSH(); -BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wmissing-field-initializers"); -BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wshadow"); -BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wint-to-pointer-cast") -#if BX_COMPILER_GCC >= 60000 -BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wmisleading-indentation"); -BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wshift-negative-value"); -#endif // BX_COMPILER_GCC >= 60000_ - -#include -BX_PRAGMA_DIAGNOSTIC_POP(); -#endif // NANOVG_HAS_STB_IMAGE - #ifdef _MSC_VER #pragma warning(disable: 4100) // unreferenced formal parameter #pragma warning(disable: 4127) // conditional expression is constant @@ -816,45 +790,6 @@ void nvgFillPaint(NVGcontext* ctx, NVGpaint paint) nvgTransformMultiply(state->fill.xform, state->xform); } -int nvgCreateImage(NVGcontext* ctx, const char* filename, int imageFlags) -{ -#if NANOVG_HAS_STB_IMAGE - int w, h, n, image; - unsigned char* img; - stbi_set_unpremultiply_on_load(1); - stbi_convert_iphone_png_to_rgb(1); - img = stbi_load(filename, &w, &h, &n, 4); - if (img == NULL) { -// printf("Failed to load %s - %s\n", filename, stbi_failure_reason()); - return 0; - } - image = nvgCreateImageRGBA(ctx, w, h, imageFlags, img); - stbi_image_free(img); - return image; -#else - BX_UNUSED(ctx, filename, imageFlags); - return 0; -#endif // NANOVG_HAS_STB_IMAGE -} - -int nvgCreateImageMem(NVGcontext* ctx, int imageFlags, unsigned char* data, int ndata) -{ -#if NANOVG_HAS_STB_IMAGE - int w, h, n, image; - unsigned char* img = stbi_load_from_memory(data, ndata, &w, &h, &n, 4); - if (img == NULL) { -// printf("Failed to load %s - %s\n", filename, stbi_failure_reason()); - return 0; - } - image = nvgCreateImageRGBA(ctx, w, h, imageFlags, img); - stbi_image_free(img); - return image; -#else - BX_UNUSED(ctx, imageFlags, data, ndata); - return 0; -#endif // NANOVG_HAS_STB_IMAGE -} - int nvgCreateImageRGBA(NVGcontext* ctx, int w, int h, int imageFlags, const unsigned char* data) { return ctx->params.renderCreateTexture(ctx->params.userPtr, NVG_TEXTURE_RGBA, w, h, imageFlags, data); diff --git a/examples/common/nanovg/nanovg_bgfx.cpp b/examples/common/nanovg/nanovg_bgfx.cpp index 78d854bfa..09ba3ddfc 100644 --- a/examples/common/nanovg/nanovg_bgfx.cpp +++ b/examples/common/nanovg/nanovg_bgfx.cpp @@ -386,7 +386,8 @@ namespace struct GLNVGcontext* gl = (struct GLNVGcontext*)_userPtr; struct GLNVGtexture* tex = glnvg__findTexture(gl, image); - if (!bgfx::isValid(tex->id) ) + if (NULL == tex + || !bgfx::isValid(tex->id) ) { return 0; }