Skip to content

Commit 5687670

Browse files
committed
Build all demos as C89
1 parent fb25dee commit 5687670

File tree

26 files changed

+188
-114
lines changed

26 files changed

+188
-114
lines changed

demo/allegro5/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
BIN = demo
33

44
# Flags
5-
CFLAGS += -std=c99 -pedantic -O2
5+
CFLAGS += -std=c89 -pedantic -O2
66

77
SRC = main.c
88
OBJ = $(SRC:.c=.o)

demo/allegro5/main.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ int main(void)
5555
/* Platform */
5656
ALLEGRO_DISPLAY *display = NULL;
5757
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
58+
NkAllegro5Font *font;
59+
struct nk_context *ctx;
5860

5961
if (!al_init()) {
6062
fprintf(stdout, "failed to initialize allegro5!\n");
@@ -85,9 +87,7 @@ int main(void)
8587
al_register_event_source(event_queue, al_get_mouse_event_source());
8688
al_register_event_source(event_queue, al_get_keyboard_event_source());
8789

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

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

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

100100
while(1)
101101
{
102+
bool get_event;
102103
ALLEGRO_EVENT ev;
103104
ALLEGRO_TIMEOUT timeout;
104105
al_init_timeout(&timeout, 0.06);
105106

106-
bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout);
107+
get_event = al_wait_for_event_until(event_queue, &ev, &timeout);
107108

108109
if (get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
109110
break;

demo/allegro5/nuklear_allegro5.h

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,22 @@ static struct nk_allegro5 {
7070

7171
NK_API struct nk_image* nk_allegro5_create_image(const char* file_name)
7272
{
73+
NkAllegro5Font *font;
74+
ALLEGRO_BITMAP *bitmap;
75+
struct nk_image *image;
7376
if (!al_init_image_addon()) {
7477
fprintf(stdout, "Unable to initialize required allegro5 image addon\n");
7578
exit(1);
7679
}
80+
font = (NkAllegro5Font*)calloc(1, sizeof(NkAllegro5Font));
7781

78-
ALLEGRO_BITMAP* bitmap = al_load_bitmap(file_name);
82+
bitmap = al_load_bitmap(file_name);
7983
if (bitmap == NULL) {
8084
fprintf(stdout, "Unable to load image file: %s\n", file_name);
8185
return NULL;
8286
}
8387

84-
struct nk_image *image = (struct nk_image*)calloc(1, sizeof(struct nk_image));
88+
image = (struct nk_image*)calloc(1, sizeof(struct nk_image));
8589
image->handle.ptr = bitmap;
8690
image->w = al_get_bitmap_width(bitmap);
8791
image->h = al_get_bitmap_height(bitmap);
@@ -98,6 +102,8 @@ NK_API void nk_allegro5_del_image(struct nk_image* image)
98102
static float
99103
nk_allegro5_font_get_text_width(nk_handle handle, float height, const char *text, int len)
100104
{
105+
float width;
106+
char *strcpy;
101107
NkAllegro5Font *font = (NkAllegro5Font*)handle.ptr;
102108
if (!font || !text) {
103109
return 0;
@@ -106,16 +112,19 @@ nk_allegro5_font_get_text_width(nk_handle handle, float height, const char *text
106112
as nuklear uses variable size buffers and al_get_text_width doesn't
107113
accept a length, it infers length from null-termination
108114
(which is unsafe API design by allegro devs!) */
109-
char strcpy[len+1];
110-
strncpy((char*)&strcpy, text, len);
115+
strcpy = malloc(len + 1);
116+
strncpy(strcpy, text, len);
111117
strcpy[len] = '\0';
112-
return al_get_text_width(font->font, strcpy);
118+
width = al_get_text_width(font->font, strcpy);
119+
free(strcpy);
120+
return width;
113121
}
114122

115123
/* Flags are identical to al_load_font() flags argument */
116124
NK_API NkAllegro5Font*
117125
nk_allegro5_font_create_from_file(const char *file_name, int font_size, int flags)
118126
{
127+
NkAllegro5Font *font;
119128
if (!al_init_image_addon()) {
120129
fprintf(stdout, "Unable to initialize required allegro5 image addon\n");
121130
exit(1);
@@ -128,7 +137,7 @@ nk_allegro5_font_create_from_file(const char *file_name, int font_size, int flag
128137
fprintf(stdout, "Unable to initialize required allegro5 TTF font addon\n");
129138
exit(1);
130139
}
131-
NkAllegro5Font *font = (NkAllegro5Font*)calloc(1, sizeof(NkAllegro5Font));
140+
font = (NkAllegro5Font*)calloc(1, sizeof(NkAllegro5Font));
132141

133142
font->font = al_load_font(file_name, font_size, flags);
134143
if (font->font == NULL) {
@@ -200,18 +209,18 @@ nk_allegro5_render()
200209
(float)r->rounding, color);
201210
} break;
202211
case NK_COMMAND_CIRCLE: {
212+
float xr, yr;
203213
const struct nk_command_circle *c = (const struct nk_command_circle *)cmd;
204214
color = nk_color_to_allegro_color(c->color);
205-
float xr, yr;
206215
xr = (float)c->w/2;
207216
yr = (float)c->h/2;
208217
al_draw_ellipse(((float)(c->x)) + xr, ((float)c->y) + yr,
209218
xr, yr, color, (float)c->line_thickness);
210219
} break;
211220
case NK_COMMAND_CIRCLE_FILLED: {
221+
float xr, yr;
212222
const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
213223
color = nk_color_to_allegro_color(c->color);
214-
float xr, yr;
215224
xr = (float)c->w/2;
216225
yr = (float)c->h/2;
217226
al_draw_filled_ellipse(((float)(c->x)) + xr, ((float)c->y) + yr,
@@ -230,54 +239,61 @@ nk_allegro5_render()
230239
(float)t->b.y, (float)t->c.x, (float)t->c.y, color);
231240
} break;
232241
case NK_COMMAND_POLYGON: {
242+
int i;
243+
float *vertices;
233244
const struct nk_command_polygon *p = (const struct nk_command_polygon*)cmd;
245+
vertices = calloc(p->point_count * 2, sizeof(float));
234246
color = nk_color_to_allegro_color(p->color);
235-
int i;
236-
float vertices[p->point_count * 2];
237247
for (i = 0; i < p->point_count; i++) {
238248
vertices[i*2] = p->points[i].x;
239249
vertices[(i*2) + 1] = p->points[i].y;
240250
}
241251
al_draw_polyline((const float*)&vertices, (2 * sizeof(float)),
242252
(int)p->point_count, ALLEGRO_LINE_JOIN_ROUND, ALLEGRO_LINE_CAP_CLOSED,
243253
color, (float)p->line_thickness, 0.0);
254+
free(vertices);
244255
} break;
245256
case NK_COMMAND_POLYGON_FILLED: {
257+
int i;
258+
float *vertices;
246259
const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd;
260+
vertices = calloc(p->point_count * 2, sizeof(float));
247261
color = nk_color_to_allegro_color(p->color);
248-
int i;
249-
float vertices[p->point_count * 2];
250262
for (i = 0; i < p->point_count; i++) {
251263
vertices[i*2] = p->points[i].x;
252264
vertices[(i*2) + 1] = p->points[i].y;
253265
}
254266
al_draw_filled_polygon((const float*)&vertices, (int)p->point_count, color);
267+
free(vertices);
255268
} break;
256269
case NK_COMMAND_POLYLINE: {
270+
int i;
271+
float *vertices;
257272
const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd;
273+
vertices = calloc(p->point_count * 2, sizeof(float));
258274
color = nk_color_to_allegro_color(p->color);
259-
int i;
260-
float vertices[p->point_count * 2];
261275
for (i = 0; i < p->point_count; i++) {
262276
vertices[i*2] = p->points[i].x;
263277
vertices[(i*2) + 1] = p->points[i].y;
264278
}
265279
al_draw_polyline((const float*)&vertices, (2 * sizeof(float)),
266280
(int)p->point_count, ALLEGRO_LINE_JOIN_ROUND, ALLEGRO_LINE_CAP_ROUND,
267281
color, (float)p->line_thickness, 0.0);
282+
free(vertices);
268283
} break;
269284
case NK_COMMAND_TEXT: {
285+
NkAllegro5Font *font;
270286
const struct nk_command_text *t = (const struct nk_command_text*)cmd;
271287
color = nk_color_to_allegro_color(t->foreground);
272-
NkAllegro5Font *font = (NkAllegro5Font*)t->font->userdata.ptr;
288+
font = (NkAllegro5Font*)t->font->userdata.ptr;
273289
al_draw_text(font->font,
274290
color, (float)t->x, (float)t->y, 0,
275291
(const char*)t->string);
276292
} break;
277293
case NK_COMMAND_CURVE: {
294+
float points[8];
278295
const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
279296
color = nk_color_to_allegro_color(q->color);
280-
float points[8];
281297
points[0] = (float)q->begin.x;
282298
points[1] = (float)q->begin.y;
283299
points[2] = (float)q->ctrl[0].x;
@@ -465,12 +481,13 @@ NK_API struct nk_context*
465481
nk_allegro5_init(NkAllegro5Font *allegro5font, ALLEGRO_DISPLAY *dsp,
466482
unsigned int width, unsigned int height)
467483
{
484+
struct nk_user_font *font;
468485
if (!al_init_primitives_addon()) {
469486
fprintf(stdout, "Unable to initialize required allegro5 primitives addon\n");
470487
exit(1);
471488
}
472489

473-
struct nk_user_font *font = &allegro5font->nk;
490+
font = &allegro5font->nk;
474491

475492
allegro5.dsp = dsp;
476493
allegro5.width = width;

demo/d3d11/nuklear_d3d11.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,15 @@ nk_d3d11_get_projection_matrix(int width, int height, float *result)
179179
const float B = (float)height;
180180
float matrix[4][4] =
181181
{
182-
{ 2.0f / (R - L), 0.0f, 0.0f, 0.0f },
183-
{ 0.0f, 2.0f / (T - B), 0.0f, 0.0f },
184-
{ 0.0f, 0.0f, 0.5f, 0.0f },
185-
{ (R + L) / (L - R), (T + B) / (B - T), 0.5f, 1.0f },
182+
{ 0.0f, 0.0f, 0.0f, 0.0f },
183+
{ 0.0f, 0.0f, 0.0f, 0.0f },
184+
{ 0.0f, 0.0f, 0.5f, 0.0f },
185+
{ 0.0f, 0.0f, 0.5f, 1.0f },
186186
};
187+
matrix[0][0] = 2.0f / (R - L);
188+
matrix[1][1] = 2.0f / (T - B);
189+
matrix[3][0] = (R + L) / (L - R);
190+
matrix[3][1] = (T + B) / (B - T);
187191
memcpy(result, matrix, sizeof(matrix));
188192
}
189193

demo/d3d9/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ WindowProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
8282
if (width != 0 && height != 0 &&
8383
(width != present.BackBufferWidth || height != present.BackBufferHeight))
8484
{
85+
HRESULT hr;
8586
nk_d3d9_release();
8687
present.BackBufferWidth = width;
8788
present.BackBufferHeight = height;
88-
HRESULT hr = IDirect3DDevice9_Reset(device, &present);
89+
hr = IDirect3DDevice9_Reset(device, &present);
8990
NK_ASSERT(SUCCEEDED(hr));
9091
nk_d3d9_resize(width, height);
9192
}

demo/d3d9/nuklear_d3d9.h

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,15 @@ nk_d3d9_get_projection_matrix(int width, int height, float *result)
204204
const float T = 0.5f;
205205
const float B = (float)height + 0.5f;
206206
float matrix[4][4] = {
207-
{ 2.0f / (R - L), 0.0f, 0.0f, 0.0f },
208-
{ 0.0f, 2.0f / (T - B), 0.0f, 0.0f },
209-
{ 0.0f, 0.0f, 0.0f, 0.0f },
210-
{ (R + L) / (L - R), (T + B) / (B - T), 0.0f, 1.0f },
207+
{ 0.0f, 0.0f, 0.0f, 0.0f },
208+
{ 0.0f, 0.0f, 0.0f, 0.0f },
209+
{ 0.0f, 0.0f, 0.0f, 0.0f },
210+
{ 0.0f, 0.0f, 0.0f, 1.0f },
211211
};
212+
matrix[0][0] = 2.0f / (R - L);
213+
matrix[1][1] = 2.0f / (T - B);
214+
matrix[3][0] = (R + L) / (L - R);
215+
matrix[3][1] = (T + B) / (B - T);
212216
memcpy(result, matrix, sizeof(matrix));
213217
}
214218

@@ -426,30 +430,35 @@ nk_d3d9_handle_event(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
426430
static void
427431
nk_d3d9_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
428432
{
433+
HGLOBAL mem;
434+
SIZE_T size;
435+
LPCWSTR wstr;
436+
int utf8size;
437+
429438
(void)usr;
430439
if (!IsClipboardFormatAvailable(CF_UNICODETEXT) && OpenClipboard(NULL)) {
431440
return;
432441
}
433442

434-
HGLOBAL mem = GetClipboardData(CF_UNICODETEXT);
443+
mem = GetClipboardData(CF_UNICODETEXT);
435444
if (!mem) {
436445
CloseClipboard();
437446
return;
438447
}
439448

440-
SIZE_T size = GlobalSize(mem) - 1;
449+
size = GlobalSize(mem) - 1;
441450
if (!size) {
442451
CloseClipboard();
443452
return;
444453
}
445454

446-
LPCWSTR wstr = (LPCWSTR)GlobalLock(mem);
455+
wstr = (LPCWSTR)GlobalLock(mem);
447456
if (!wstr) {
448457
CloseClipboard();
449458
return;
450459
}
451460

452-
int utf8size = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)size / sizeof(wchar_t), NULL, 0, NULL, NULL);
461+
utf8size = WideCharToMultiByte(CP_UTF8, 0, wstr, (int)size / sizeof(wchar_t), NULL, 0, NULL, NULL);
453462
if (utf8size) {
454463
char *utf8 = (char *)malloc(utf8size);
455464
if (utf8) {
@@ -466,12 +475,14 @@ nk_d3d9_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
466475
static void
467476
nk_d3d9_clipboard_copy(nk_handle usr, const char *text, int len)
468477
{
478+
int wsize;
479+
469480
(void)usr;
470481
if (!OpenClipboard(NULL)) {
471482
return;
472483
}
473484

474-
int wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
485+
wsize = MultiByteToWideChar(CP_UTF8, 0, text, len, NULL, 0);
475486
if (wsize) {
476487
HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, (wsize + 1) * sizeof(wchar_t));
477488
if (mem) {

0 commit comments

Comments
 (0)