GDI+ simplified image loading, added error checking and image freeing
This commit is contained in:
parent
d536d4e89a
commit
aa3ffd6917
@ -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
|
||||||
|
@ -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);
|
|
||||||
|
|
||||||
/* CreateStreamOnHGlobal needs OLE32 in linked libraries list */
|
status = GdipLoadImageFromStream(stream, &image);
|
||||||
CreateStreamOnHGlobal(hMem, FALSE, &pStream);
|
stream->lpVtbl->Release(stream);
|
||||||
GdipLoadImageFromStream(pStream, &image);
|
|
||||||
GlobalFree(hMem);
|
if (status)
|
||||||
|
return nk_image_id(0);
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user