Skip to content
Merged
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
2 changes: 1 addition & 1 deletion demo/allegro5/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
BIN = demo

# Flags
CFLAGS += -std=c99 -pedantic -O2
CFLAGS += -std=c89 -pedantic -O2

SRC = main.c
OBJ = $(SRC:.c=.o)
Expand Down
7 changes: 4 additions & 3 deletions demo/allegro5/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ int main(void)
/* Platform */
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
NkAllegro5Font *font;
struct nk_context *ctx;

if (!al_init()) {
fprintf(stdout, "failed to initialize allegro5!\n");
Expand Down Expand Up @@ -85,9 +87,7 @@ int main(void)
al_register_event_source(event_queue, al_get_mouse_event_source());
al_register_event_source(event_queue, al_get_keyboard_event_source());

NkAllegro5Font *font;
font = nk_allegro5_font_create_from_file("../../../extra_font/Roboto-Regular.ttf", 12, 0);
struct nk_context *ctx;

ctx = nk_allegro5_init(font, display, WINDOW_WIDTH, WINDOW_HEIGHT);

Expand All @@ -99,11 +99,12 @@ int main(void)

while(1)
{
bool get_event;
ALLEGRO_EVENT ev;
ALLEGRO_TIMEOUT timeout;
al_init_timeout(&timeout, 0.06);

bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout);
get_event = al_wait_for_event_until(event_queue, &ev, &timeout);

if (get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
break;
Expand Down
51 changes: 33 additions & 18 deletions demo/allegro5/nuklear_allegro5.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,20 @@ static struct nk_allegro5 {

NK_API struct nk_image* nk_allegro5_create_image(const char* file_name)
{
ALLEGRO_BITMAP *bitmap;
struct nk_image *image;
if (!al_init_image_addon()) {
fprintf(stdout, "Unable to initialize required allegro5 image addon\n");
exit(1);
}

ALLEGRO_BITMAP* bitmap = al_load_bitmap(file_name);
bitmap = al_load_bitmap(file_name);
if (bitmap == NULL) {
fprintf(stdout, "Unable to load image file: %s\n", file_name);
return NULL;
}

struct nk_image *image = (struct nk_image*)calloc(1, sizeof(struct nk_image));
image = (struct nk_image*)calloc(1, sizeof(struct nk_image));
image->handle.ptr = bitmap;
image->w = al_get_bitmap_width(bitmap);
image->h = al_get_bitmap_height(bitmap);
Expand All @@ -98,25 +100,30 @@ NK_API void nk_allegro5_del_image(struct nk_image* image)
static float
nk_allegro5_font_get_text_width(nk_handle handle, float height, const char *text, int len)
{
NK_UNUSED(height);
float width;
char *strcpy;
NkAllegro5Font *font = (NkAllegro5Font*)handle.ptr;
NK_UNUSED(height);
if (!font || !text) {
return 0;
}
/* We must copy into a new buffer with exact length null-terminated
as nuklear uses variable size buffers and al_get_text_width doesn't
accept a length, it infers length from null-termination
(which is unsafe API design by allegro devs!) */
char strcpy[len+1];
strncpy((char*)&strcpy, text, len);
strcpy = malloc(len + 1);
strncpy(strcpy, text, len);
strcpy[len] = '\0';
return al_get_text_width(font->font, strcpy);
width = al_get_text_width(font->font, strcpy);
free(strcpy);
return width;
}

/* Flags are identical to al_load_font() flags argument */
NK_API NkAllegro5Font*
nk_allegro5_font_create_from_file(const char *file_name, int font_size, int flags)
{
NkAllegro5Font *font;
if (!al_init_image_addon()) {
fprintf(stdout, "Unable to initialize required allegro5 image addon\n");
exit(1);
Expand All @@ -129,7 +136,7 @@ nk_allegro5_font_create_from_file(const char *file_name, int font_size, int flag
fprintf(stdout, "Unable to initialize required allegro5 TTF font addon\n");
exit(1);
}
NkAllegro5Font *font = (NkAllegro5Font*)calloc(1, sizeof(NkAllegro5Font));
font = (NkAllegro5Font*)calloc(1, sizeof(NkAllegro5Font));

font->font = al_load_font(file_name, font_size, flags);
if (font->font == NULL) {
Expand Down Expand Up @@ -201,18 +208,18 @@ nk_allegro5_render()
(float)r->rounding, color);
} break;
case NK_COMMAND_CIRCLE: {
float xr, yr;
const struct nk_command_circle *c = (const struct nk_command_circle *)cmd;
color = nk_color_to_allegro_color(c->color);
float xr, yr;
xr = (float)c->w/2;
yr = (float)c->h/2;
al_draw_ellipse(((float)(c->x)) + xr, ((float)c->y) + yr,
xr, yr, color, (float)c->line_thickness);
} break;
case NK_COMMAND_CIRCLE_FILLED: {
float xr, yr;
const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
color = nk_color_to_allegro_color(c->color);
float xr, yr;
xr = (float)c->w/2;
yr = (float)c->h/2;
al_draw_filled_ellipse(((float)(c->x)) + xr, ((float)c->y) + yr,
Expand All @@ -231,54 +238,61 @@ nk_allegro5_render()
(float)t->b.y, (float)t->c.x, (float)t->c.y, color);
} break;
case NK_COMMAND_POLYGON: {
int i;
float *vertices;
const struct nk_command_polygon *p = (const struct nk_command_polygon*)cmd;
vertices = calloc(p->point_count * 2, sizeof(float));
color = nk_color_to_allegro_color(p->color);
int i;
float vertices[p->point_count * 2];
for (i = 0; i < p->point_count; i++) {
vertices[i*2] = p->points[i].x;
vertices[(i*2) + 1] = p->points[i].y;
}
al_draw_polyline((const float*)&vertices, (2 * sizeof(float)),
(int)p->point_count, ALLEGRO_LINE_JOIN_ROUND, ALLEGRO_LINE_CAP_CLOSED,
color, (float)p->line_thickness, 0.0);
free(vertices);
} break;
case NK_COMMAND_POLYGON_FILLED: {
int i;
float *vertices;
const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd;
vertices = calloc(p->point_count * 2, sizeof(float));
color = nk_color_to_allegro_color(p->color);
int i;
float vertices[p->point_count * 2];
for (i = 0; i < p->point_count; i++) {
vertices[i*2] = p->points[i].x;
vertices[(i*2) + 1] = p->points[i].y;
}
al_draw_filled_polygon((const float*)&vertices, (int)p->point_count, color);
free(vertices);
} break;
case NK_COMMAND_POLYLINE: {
int i;
float *vertices;
const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd;
vertices = calloc(p->point_count * 2, sizeof(float));
color = nk_color_to_allegro_color(p->color);
int i;
float vertices[p->point_count * 2];
for (i = 0; i < p->point_count; i++) {
vertices[i*2] = p->points[i].x;
vertices[(i*2) + 1] = p->points[i].y;
}
al_draw_polyline((const float*)&vertices, (2 * sizeof(float)),
(int)p->point_count, ALLEGRO_LINE_JOIN_ROUND, ALLEGRO_LINE_CAP_ROUND,
color, (float)p->line_thickness, 0.0);
free(vertices);
} break;
case NK_COMMAND_TEXT: {
NkAllegro5Font *font;
const struct nk_command_text *t = (const struct nk_command_text*)cmd;
color = nk_color_to_allegro_color(t->foreground);
NkAllegro5Font *font = (NkAllegro5Font*)t->font->userdata.ptr;
font = (NkAllegro5Font*)t->font->userdata.ptr;
al_draw_text(font->font,
color, (float)t->x, (float)t->y, 0,
(const char*)t->string);
} break;
case NK_COMMAND_CURVE: {
float points[8];
const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
color = nk_color_to_allegro_color(q->color);
float points[8];
points[0] = (float)q->begin.x;
points[1] = (float)q->begin.y;
points[2] = (float)q->ctrl[0].x;
Expand Down Expand Up @@ -466,12 +480,13 @@ NK_API struct nk_context*
nk_allegro5_init(NkAllegro5Font *allegro5font, ALLEGRO_DISPLAY *dsp,
unsigned int width, unsigned int height)
{
struct nk_user_font *font;
if (!al_init_primitives_addon()) {
fprintf(stdout, "Unable to initialize required allegro5 primitives addon\n");
exit(1);
}

struct nk_user_font *font = &allegro5font->nk;
font = &allegro5font->nk;

allegro5.dsp = dsp;
allegro5.width = width;
Expand Down
12 changes: 8 additions & 4 deletions demo/d3d11/nuklear_d3d11.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,15 @@ nk_d3d11_get_projection_matrix(int width, int height, float *result)
const float B = (float)height;
float matrix[4][4] =
{
{ 2.0f / (R - L), 0.0f, 0.0f, 0.0f },
{ 0.0f, 2.0f / (T - B), 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.5f, 0.0f },
{ (R + L) / (L - R), (T + B) / (B - T), 0.5f, 1.0f },
{ 0.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.5f, 0.0f },
{ 0.0f, 0.0f, 0.5f, 1.0f },
};
matrix[0][0] = 2.0f / (R - L);
matrix[1][1] = 2.0f / (T - B);
matrix[3][0] = (R + L) / (L - R);
matrix[3][1] = (T + B) / (B - T);
memcpy(result, matrix, sizeof(matrix));
}

Expand Down
3 changes: 2 additions & 1 deletion demo/d3d9/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ WindowProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
if (width != 0 && height != 0 &&
(width != present.BackBufferWidth || height != present.BackBufferHeight))
{
HRESULT hr;
nk_d3d9_release();
present.BackBufferWidth = width;
present.BackBufferHeight = height;
HRESULT hr = IDirect3DDevice9_Reset(device, &present);
hr = IDirect3DDevice9_Reset(device, &present);
NK_ASSERT(SUCCEEDED(hr));
nk_d3d9_resize(width, height);
}
Expand Down
29 changes: 20 additions & 9 deletions demo/d3d9/nuklear_d3d9.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,15 @@ nk_d3d9_get_projection_matrix(int width, int height, float *result)
const float T = 0.5f;
const float B = (float)height + 0.5f;
float matrix[4][4] = {
{ 2.0f / (R - L), 0.0f, 0.0f, 0.0f },
{ 0.0f, 2.0f / (T - B), 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 0.0f },
{ (R + L) / (L - R), (T + B) / (B - T), 0.0f, 1.0f },
{ 0.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 1.0f },
};
matrix[0][0] = 2.0f / (R - L);
matrix[1][1] = 2.0f / (T - B);
matrix[3][0] = (R + L) / (L - R);
matrix[3][1] = (T + B) / (B - T);
memcpy(result, matrix, sizeof(matrix));
}

Expand Down Expand Up @@ -426,30 +430,35 @@ nk_d3d9_handle_event(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
static void
nk_d3d9_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
{
HGLOBAL mem;
SIZE_T size;
LPCWSTR wstr;
int utf8size;

(void)usr;
if (!IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(NULL)) {
return;
}

HGLOBAL mem = GetClipboardData(CF_UNICODETEXT);
mem = GetClipboardData(CF_UNICODETEXT);
if (!mem) {
CloseClipboard();
return;
}

SIZE_T size = GlobalSize(mem) - 1;
size = GlobalSize(mem) - 1;
if (!size) {
CloseClipboard();
return;
}

LPCWSTR wstr = (LPCWSTR)GlobalLock(mem);
wstr = (LPCWSTR)GlobalLock(mem);
if (!wstr) {
CloseClipboard();
return;
}

int utf8size = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)size / sizeof(wchar_t), NULL, 0, NULL, NULL);
utf8size = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)size / sizeof(wchar_t), NULL, 0, NULL, NULL);
if (utf8size) {
char *utf8 = (char *)malloc(utf8size);
if (utf8) {
Expand All @@ -466,12 +475,14 @@ nk_d3d9_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
static void
nk_d3d9_clipboard_copy(nk_handle usr, const char *text, int len)
{
int wsize;

(void)usr;
if (!OpenClipboard(NULL)) {
return;
}

int wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
if (wsize) {
HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, (wsize + 1) * sizeof(wchar_t));
if (mem) {
Expand Down
Loading