Skip to content
This repository has been archived by the owner on Dec 2, 2019. It is now read-only.

Pointer size for MinGW 64Bit / Castings #867

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 45 additions & 24 deletions demo/gdi/nuklear_gdi.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
#ifndef NK_GDI_H_
#define NK_GDI_H_

// suppress compiler warnings for changes C89 -> C++11
#ifdef __GNUC__
#if ( __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) )
#pragma GCC diagnostic ignored "-Wpadded"
#pragma GCC diagnostic ignored "-Wold-style-cast"
#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#pragma GCC diagnostic ignored "-Wcast-align"
#pragma GCC diagnostic ignored "-Wcast-"
#endif
#endif

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

Expand All @@ -29,6 +40,7 @@ NK_API void nk_gdi_set_font(GdiFont *font);

#endif


/*
* ==============================================================
*
Expand Down Expand Up @@ -62,28 +74,32 @@ nk_create_image(struct nk_image * image, const char * frame_buffer, const int wi
{
if (image && frame_buffer && (width > 0) && (height > 0))
{
image->w = width;
image->h = height;
image->w = (unsigned short)width;
image->h = (unsigned short)height;
image->region[0] = 0;
image->region[1] = 0;
image->region[2] = width;
image->region[3] = height;
image->region[2] = (unsigned short)width;
image->region[3] = (unsigned short)height;

INT row = ((width * 3 + 3) & ~3);
BITMAPINFO bi = { 0 };
BITMAPINFO bi;
memset( &bi, 0, sizeof(bi) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required? It's less efficient and slightly "cumbersome" compared to the previous version.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not required.
The compiler got warnings because "0" is not equal "unsinged long".
If you changed the "0" to "0ul" the compiler said "0ul" is not equal to "int".
I think the notation "{ 0 }" is not really designed for structs with different data types?

If you thinks its better to reverse this. Just do it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that's really weird. According to C99 and newer, the notation { 0 } denotes the minimal case for partial struct assignment which in this case means assign 0 to the first struct member and all following struct members disregarding their type (which might be potentially harmful as e.g. NULL can be something else than 0 on some platforms or some IEEE 754 implementations could behave incorrectly if some flags are zero, but programmers for such platforms would know what to do with this source 😉).

Are you using the provided build.bat script? Or some other compiler? What are all the options?

I would like to find out what's the issue in this case as it sounds really suspicious (might be also an issue with Microsoft Visual Studio compiler as used in build.bat).

bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = width;
bi.bmiHeader.biHeight = height;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 24;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = row * height;
bi.bmiHeader.biSizeImage = (unsigned long)(row * height);

LPBYTE lpBuf, pb = NULL;
HBITMAP hbm = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, (void**)&lpBuf, NULL, 0);

pb = lpBuf + row * height;

// cast from "const char *" to "unsigned char *" to iterate over the buffer
unsigned char * src = (unsigned char *)frame_buffer;

for (int v = 0; v<height; v++)
{
pb -= row;
Expand All @@ -95,7 +111,7 @@ nk_create_image(struct nk_image * image, const char * frame_buffer, const int wi
src += 3;
}
}
SetDIBits(NULL, hbm, 0, height, lpBuf, &bi, DIB_RGB_COLORS);
SetDIBits(NULL, hbm, 0, (unsigned int)height, lpBuf, &bi, DIB_RGB_COLORS);
image->handle.ptr = hbm;
}
}
Expand All @@ -115,6 +131,8 @@ static void
nk_gdi_draw_image(short x, short y, unsigned short w, unsigned short h,
struct nk_image img, struct nk_color col)
{
NK_UNUSED(col);

HBITMAP hbm = (HBITMAP)img.handle.ptr;
HDC hDCBits;
BITMAP bitmap;
Expand All @@ -132,7 +150,7 @@ nk_gdi_draw_image(short x, short y, unsigned short w, unsigned short h,
static COLORREF
convert_color(struct nk_color c)
{
return c.r | (c.g << 8) | (c.b << 16);
return ((DWORD)c.r | ((DWORD)c.g << 8) | ((DWORD)c.b << 16));
}

static void
Expand All @@ -152,7 +170,7 @@ nk_gdi_stroke_line(HDC dc, short x0, short y0, short x1,
if (line_thickness == 1) {
SetDCPenColor(dc, color);
} else {
pen = CreatePen(PS_SOLID, line_thickness, color);
pen = CreatePen(PS_SOLID, (int)line_thickness, color);
SelectObject(dc, pen);
}

Expand Down Expand Up @@ -212,9 +230,9 @@ nk_gdi_fill_rect(HDC dc, short x, short y, unsigned short w,
static void
nk_gdi_set_vertexColor(PTRIVERTEX tri, struct nk_color col)
{
tri->Red = col.r << 8;
tri->Green = col.g << 8;
tri->Blue = col.b << 8;
tri->Red = (unsigned short)(col.r << 8);
tri->Green = (unsigned short)(col.g << 8);
tri->Blue = (unsigned short)(col.b << 8);
tri->Alpha = 0xff << 8;
}

Expand All @@ -224,7 +242,6 @@ nk_gdi_rect_multi_color(HDC dc, short x, short y, unsigned short w,
struct nk_color right, struct nk_color bottom)
{
BLENDFUNCTION alphaFunction;
GRADIENT_RECT gRect;
GRADIENT_TRIANGLE gTri[2];
TRIVERTEX vt[4];
alphaFunction.BlendOp = AC_SRC_OVER;
Expand Down Expand Up @@ -442,27 +459,29 @@ static void
nk_gdi_draw_text(HDC dc, short x, short y, unsigned short w, unsigned short h,
const char *text, int len, GdiFont *font, struct nk_color cbg, struct nk_color cfg)
{
NK_UNUSED(w);
NK_UNUSED(h);
int wsize;
WCHAR* wstr;

if(!text || !font || !len) return;

wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
wstr = (WCHAR*)_alloca(wsize * sizeof(wchar_t));
wstr = (WCHAR*)_alloca((unsigned long long)wsize * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);

SetBkColor(dc, convert_color(cbg));
SetTextColor(dc, convert_color(cfg));

SelectObject(dc, font->handle);
ExtTextOutW(dc, x, y, ETO_OPAQUE, NULL, wstr, wsize, NULL);
ExtTextOutW(dc, x, y, ETO_OPAQUE, NULL, wstr, (unsigned int)wsize, NULL);
}

static void
nk_gdi_clear(HDC dc, struct nk_color col)
{
COLORREF color = convert_color(col);
RECT rect = { 0, 0, gdi.width, gdi.height };
RECT rect = { 0, 0, (LONG)gdi.width, (LONG)gdi.height };
SetBkColor(dc, color);

ExtTextOutW(dc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
Expand All @@ -471,7 +490,7 @@ nk_gdi_clear(HDC dc, struct nk_color col)
static void
nk_gdi_blit(HDC dc)
{
BitBlt(dc, 0, 0, gdi.width, gdi.height, gdi.memory_dc, 0, 0, SRCCOPY);
BitBlt(dc, 0, 0, (LONG)gdi.width, (LONG)gdi.height, gdi.memory_dc, 0, 0, SRCCOPY);

}

Expand All @@ -493,6 +512,7 @@ nk_gdifont_create(const char *name, int size)
static float
nk_gdifont_get_text_width(nk_handle handle, float height, const char *text, int len)
{
NK_UNUSED(height);
GdiFont *font = (GdiFont*)handle.ptr;
SIZE size;
int wsize;
Expand All @@ -501,7 +521,7 @@ nk_gdifont_get_text_width(nk_handle handle, float height, const char *text, int
return 0;

wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
wstr = (WCHAR*)_alloca(wsize * sizeof(wchar_t));
wstr = (WCHAR*)_alloca((SIZE_T)wsize * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, text, len, wstr, wsize);
if (GetTextExtentPoint32W(font->dc, wstr, wsize, &size))
return (float)size.cx;
Expand Down Expand Up @@ -535,7 +555,7 @@ nk_gdi_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
int utf8size = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)(size / sizeof(wchar_t)), NULL, 0, NULL, NULL);
if (utf8size)
{
char* utf8 = (char*)malloc(utf8size);
char* utf8 = (char*)malloc((unsigned int)utf8size);
if (utf8)
{
WideCharToMultiByte(CP_UTF8, 0, wstr, (int)(size / sizeof(wchar_t)), utf8, utf8size, NULL, NULL);
Expand All @@ -554,12 +574,13 @@ nk_gdi_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
static void
nk_gdi_clipboard_copy(nk_handle usr, const char *text, int len)
{
NK_UNUSED(usr);
if (OpenClipboard(NULL))
{
int wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
if (wsize)
{
HGLOBAL mem = (HGLOBAL)GlobalAlloc(GMEM_MOVEABLE, (wsize + 1) * sizeof(wchar_t));
HGLOBAL mem = (HGLOBAL)GlobalAlloc(GMEM_MOVEABLE, ((SIZE_T)wsize + 1) * sizeof(wchar_t));
if (mem)
{
wchar_t* wstr = (wchar_t*)GlobalLock(mem);
Expand All @@ -585,7 +606,7 @@ nk_gdi_init(GdiFont *gdifont, HDC window_dc, unsigned int width, unsigned int he
font->height = (float)gdifont->height;
font->width = nk_gdifont_get_text_width;

gdi.bitmap = CreateCompatibleBitmap(window_dc, width, height);
gdi.bitmap = CreateCompatibleBitmap(window_dc, (int)width, (int)height);
gdi.window_dc = window_dc;
gdi.memory_dc = CreateCompatibleDC(window_dc);
gdi.width = width;
Expand Down Expand Up @@ -615,12 +636,12 @@ nk_gdi_handle_event(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
case WM_SIZE:
{
unsigned width = LOWORD(lparam);
unsigned height = HIWORD(lparam);
unsigned int width = LOWORD(lparam);
unsigned int height = HIWORD(lparam);
if (width != gdi.width || height != gdi.height)
{
DeleteObject(gdi.bitmap);
gdi.bitmap = CreateCompatibleBitmap(gdi.window_dc, width, height);
gdi.bitmap = CreateCompatibleBitmap(gdi.window_dc, (int)width, (int)height);
gdi.width = width;
gdi.height = height;
SelectObject(gdi.memory_dc, gdi.bitmap);
Expand Down
4 changes: 2 additions & 2 deletions nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ extern "C" {
#define NK_SIZE_TYPE unsigned __int32
#elif defined(__GNUC__) || defined(__clang__)
#if defined(__x86_64__) || defined(__ppc64__)
#define NK_SIZE_TYPE unsigned long
#define NK_SIZE_TYPE unsigned long long
#else
#define NK_SIZE_TYPE unsigned int
#endif
Expand All @@ -385,7 +385,7 @@ extern "C" {
#define NK_POINTER_TYPE unsigned __int32
#elif defined(__GNUC__) || defined(__clang__)
#if defined(__x86_64__) || defined(__ppc64__)
#define NK_POINTER_TYPE unsigned long
#define NK_POINTER_TYPE unsigned long long
#else
#define NK_POINTER_TYPE unsigned int
#endif
Expand Down