GDI+ simplified image loading, added error checking and image freeing

This commit is contained in:
Martins Mozeiko 2017-02-10 00:41:31 -08:00
parent d536d4e89a
commit aa3ffd6917
2 changed files with 29 additions and 18 deletions

View File

@ -2,4 +2,4 @@
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86 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

View File

@ -15,6 +15,7 @@
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
#include <shlwapi.h>
/* font */ /* font */
typedef struct GdipFont GdipFont; typedef struct GdipFont GdipFont;
@ -31,7 +32,8 @@ NK_API void nk_gdip_shutdown(void);
/* image */ /* 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_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 #endif
/* /*
@ -59,7 +61,6 @@ typedef struct GpStringFormat GpStringFormat;
typedef struct GpFont GpFont; typedef struct GpFont GpFont;
typedef struct GpFontFamily GpFontFamily; typedef struct GpFontFamily GpFontFamily;
typedef struct GpFontCollection GpFontCollection; typedef struct GpFontCollection GpFontCollection;
typedef struct IStream IStream;
typedef GpImage GpBitmap; typedef GpImage GpBitmap;
typedef GpBrush GpSolidFill; typedef GpBrush GpSolidFill;
@ -602,8 +603,8 @@ nk_gdip_blit(GpGraphics *graphics)
GdipDrawImageI(graphics, gdip.bitmap, 0, 0); GdipDrawImageI(graphics, gdip.bitmap, 0, 0);
} }
struct nk_image static struct nk_image
nk_gdip_image_to_nk(GpImage *image){ nk_gdip_image_to_nk(GpImage *image) {
struct nk_image img; struct nk_image img;
UINT uwidth, uheight; UINT uwidth, uheight;
img = nk_image_ptr( (void*)image ); img = nk_image_ptr( (void*)image );
@ -615,30 +616,40 @@ nk_gdip_image_to_nk(GpImage *image){
} }
struct nk_image struct nk_image
nk_gdip_load_image_from_file(GDIPCONST WCHAR *filename) nk_gdip_load_image_from_file(const WCHAR *filename)
{ {
GpImage *image; GpImage *image;
GdipLoadImageFromFile(filename, &image); if (GdipLoadImageFromFile(filename, &image))
return nk_image_id(0);
return nk_gdip_image_to_nk(image); return nk_gdip_image_to_nk(image);
} }
struct 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; GpImage* image;
IStream *pStream = NULL; GpStatus status;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, membufSize); IStream *stream = SHCreateMemStream((const BYTE*)membuf, membufSize);
LPVOID pImage = GlobalLock(hMem); if (!stream)
nk_memcopy(pImage, membuf, membufSize); return nk_image_id(0);
GlobalUnlock(hMem);
status = GdipLoadImageFromStream(stream, &image);
stream->lpVtbl->Release(stream);
if (status)
return nk_image_id(0);
/* CreateStreamOnHGlobal needs OLE32 in linked libraries list */
CreateStreamOnHGlobal(hMem, FALSE, &pStream);
GdipLoadImageFromStream(pStream, &image);
GlobalFree(hMem);
return nk_gdip_image_to_nk(image); 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* GdipFont*
nk_gdipfont_create(const char *name, int size) nk_gdipfont_create(const char *name, int size)
{ {