From aa3ffd6917f63899aa0622619e2a029aa0aa9bdc Mon Sep 17 00:00:00 2001 From: Martins Mozeiko Date: Fri, 10 Feb 2017 00:41:31 -0800 Subject: [PATCH] GDI+ simplified image loading, added error checking and image freeing --- demo/gdip/build.bat | 2 +- demo/gdip/nuklear_gdip.h | 45 +++++++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/demo/gdip/build.bat b/demo/gdip/build.bat index 81eef6e..0f24655 100644 --- a/demo/gdip/build.bat +++ b/demo/gdip/build.bat @@ -2,4 +2,4 @@ call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86 -cl /nologo /W3 /O2 /fp:fast /Gm- /Fedemo.exe main.c user32.lib gdiplus.lib ole32.lib /link /incremental:no +cl /nologo /W3 /O2 /fp:fast /Gm- /Fedemo.exe /D_CRT_SECURE_NO_DEPRECATE main.c user32.lib gdiplus.lib shlwapi.lib /link /incremental:no diff --git a/demo/gdip/nuklear_gdip.h b/demo/gdip/nuklear_gdip.h index 5fd475b..7b08571 100644 --- a/demo/gdip/nuklear_gdip.h +++ b/demo/gdip/nuklear_gdip.h @@ -15,6 +15,7 @@ #define WIN32_LEAN_AND_MEAN #include +#include /* font */ typedef struct GdipFont GdipFont; @@ -31,7 +32,8 @@ NK_API void nk_gdip_shutdown(void); /* image */ NK_API struct nk_image nk_gdip_load_image_from_file(const WCHAR* filename); -NK_API struct nk_image nk_gdip_load_image_from_memory(const void* membuf, int membufSize); +NK_API struct nk_image nk_gdip_load_image_from_memory(const void* membuf, nk_uint membufSize); +NK_API void nk_gdip_image_free(struct nk_image image); #endif /* @@ -59,7 +61,6 @@ typedef struct GpStringFormat GpStringFormat; typedef struct GpFont GpFont; typedef struct GpFontFamily GpFontFamily; typedef struct GpFontCollection GpFontCollection; -typedef struct IStream IStream; typedef GpImage GpBitmap; typedef GpBrush GpSolidFill; @@ -602,8 +603,8 @@ nk_gdip_blit(GpGraphics *graphics) GdipDrawImageI(graphics, gdip.bitmap, 0, 0); } -struct nk_image -nk_gdip_image_to_nk(GpImage *image){ +static struct nk_image +nk_gdip_image_to_nk(GpImage *image) { struct nk_image img; UINT uwidth, uheight; img = nk_image_ptr( (void*)image ); @@ -615,30 +616,40 @@ nk_gdip_image_to_nk(GpImage *image){ } struct nk_image -nk_gdip_load_image_from_file(GDIPCONST WCHAR *filename) +nk_gdip_load_image_from_file(const WCHAR *filename) { GpImage *image; - GdipLoadImageFromFile(filename, &image); + if (GdipLoadImageFromFile(filename, &image)) + return nk_image_id(0); return nk_gdip_image_to_nk(image); } struct nk_image -nk_gdip_load_image_from_memory(const void *membuf, int membufSize) +nk_gdip_load_image_from_memory(const void *membuf, nk_uint membufSize) { - GpImage *image = NULL; - IStream *pStream = NULL; - HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, membufSize); - LPVOID pImage = GlobalLock(hMem); - nk_memcopy(pImage, membuf, membufSize); - GlobalUnlock(hMem); + GpImage* image; + GpStatus status; + IStream *stream = SHCreateMemStream((const BYTE*)membuf, membufSize); + if (!stream) + return nk_image_id(0); - /* CreateStreamOnHGlobal needs OLE32 in linked libraries list */ - CreateStreamOnHGlobal(hMem, FALSE, &pStream); - GdipLoadImageFromStream(pStream, &image); - GlobalFree(hMem); + status = GdipLoadImageFromStream(stream, &image); + stream->lpVtbl->Release(stream); + + if (status) + return nk_image_id(0); + return nk_gdip_image_to_nk(image); } +void +nk_gdip_image_free(struct nk_image image) +{ + if (!image.handle.ptr) + return; + GdipDisposeImage(image.handle.ptr); +} + GdipFont* nk_gdipfont_create(const char *name, int size) {