Skip to content

Commit 85c88b6

Browse files
authored
Merge pull request #42 from ccawley2011/c89
Build all demos as C89
2 parents e129ca6 + 9af0103 commit 85c88b6

File tree

26 files changed

+187
-115
lines changed

26 files changed

+187
-115
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: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,20 @@ static struct nk_allegro5 {
7070

7171
NK_API struct nk_image* nk_allegro5_create_image(const char* file_name)
7272
{
73+
ALLEGRO_BITMAP *bitmap;
74+
struct nk_image *image;
7375
if (!al_init_image_addon()) {
7476
fprintf(stdout, "Unable to initialize required allegro5 image addon\n");
7577
exit(1);
7678
}
7779

78-
ALLEGRO_BITMAP* bitmap = al_load_bitmap(file_name);
80+
bitmap = al_load_bitmap(file_name);
7981
if (bitmap == NULL) {
8082
fprintf(stdout, "Unable to load image file: %s\n", file_name);
8183
return NULL;
8284
}
8385

84-
struct nk_image *image = (struct nk_image*)calloc(1, sizeof(struct nk_image));
86+
image = (struct nk_image*)calloc(1, sizeof(struct nk_image));
8587
image->handle.ptr = bitmap;
8688
image->w = al_get_bitmap_width(bitmap);
8789
image->h = al_get_bitmap_height(bitmap);
@@ -98,25 +100,30 @@ NK_API void nk_allegro5_del_image(struct nk_image* image)
98100
static float
99101
nk_allegro5_font_get_text_width(nk_handle handle, float height, const char *text, int len)
100102
{
101-
NK_UNUSED(height);
103+
float width;
104+
char *strcpy;
102105
NkAllegro5Font *font = (NkAllegro5Font*)handle.ptr;
106+
NK_UNUSED(height);
103107
if (!font || !text) {
104108
return 0;
105109
}
106110
/* We must copy into a new buffer with exact length null-terminated
107111
as nuklear uses variable size buffers and al_get_text_width doesn't
108112
accept a length, it infers length from null-termination
109113
(which is unsafe API design by allegro devs!) */
110-
char strcpy[len+1];
111-
strncpy((char*)&strcpy, text, len);
114+
strcpy = malloc(len + 1);
115+
strncpy(strcpy, text, len);
112116
strcpy[len] = '\0';
113-
return al_get_text_width(font->font, strcpy);
117+
width = al_get_text_width(font->font, strcpy);
118+
free(strcpy);
119+
return width;
114120
}
115121

116122
/* Flags are identical to al_load_font() flags argument */
117123
NK_API NkAllegro5Font*
118124
nk_allegro5_font_create_from_file(const char *file_name, int font_size, int flags)
119125
{
126+
NkAllegro5Font *font;
120127
if (!al_init_image_addon()) {
121128
fprintf(stdout, "Unable to initialize required allegro5 image addon\n");
122129
exit(1);
@@ -129,7 +136,7 @@ nk_allegro5_font_create_from_file(const char *file_name, int font_size, int flag
129136
fprintf(stdout, "Unable to initialize required allegro5 TTF font addon\n");
130137
exit(1);
131138
}
132-
NkAllegro5Font *font = (NkAllegro5Font*)calloc(1, sizeof(NkAllegro5Font));
139+
font = (NkAllegro5Font*)calloc(1, sizeof(NkAllegro5Font));
133140

134141
font->font = al_load_font(file_name, font_size, flags);
135142
if (font->font == NULL) {
@@ -201,18 +208,18 @@ nk_allegro5_render()
201208
(float)r->rounding, color);
202209
} break;
203210
case NK_COMMAND_CIRCLE: {
211+
float xr, yr;
204212
const struct nk_command_circle *c = (const struct nk_command_circle *)cmd;
205213
color = nk_color_to_allegro_color(c->color);
206-
float xr, yr;
207214
xr = (float)c->w/2;
208215
yr = (float)c->h/2;
209216
al_draw_ellipse(((float)(c->x)) + xr, ((float)c->y) + yr,
210217
xr, yr, color, (float)c->line_thickness);
211218
} break;
212219
case NK_COMMAND_CIRCLE_FILLED: {
220+
float xr, yr;
213221
const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
214222
color = nk_color_to_allegro_color(c->color);
215-
float xr, yr;
216223
xr = (float)c->w/2;
217224
yr = (float)c->h/2;
218225
al_draw_filled_ellipse(((float)(c->x)) + xr, ((float)c->y) + yr,
@@ -231,54 +238,61 @@ nk_allegro5_render()
231238
(float)t->b.y, (float)t->c.x, (float)t->c.y, color);
232239
} break;
233240
case NK_COMMAND_POLYGON: {
241+
int i;
242+
float *vertices;
234243
const struct nk_command_polygon *p = (const struct nk_command_polygon*)cmd;
244+
vertices = calloc(p->point_count * 2, sizeof(float));
235245
color = nk_color_to_allegro_color(p->color);
236-
int i;
237-
float vertices[p->point_count * 2];
238246
for (i = 0; i < p->point_count; i++) {
239247
vertices[i*2] = p->points[i].x;
240248
vertices[(i*2) + 1] = p->points[i].y;
241249
}
242250
al_draw_polyline((const float*)&vertices, (2 * sizeof(float)),
243251
(int)p->point_count, ALLEGRO_LINE_JOIN_ROUND, ALLEGRO_LINE_CAP_CLOSED,
244252
color, (float)p->line_thickness, 0.0);
253+
free(vertices);
245254
} break;
246255
case NK_COMMAND_POLYGON_FILLED: {
256+
int i;
257+
float *vertices;
247258
const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd;
259+
vertices = calloc(p->point_count * 2, sizeof(float));
248260
color = nk_color_to_allegro_color(p->color);
249-
int i;
250-
float vertices[p->point_count * 2];
251261
for (i = 0; i < p->point_count; i++) {
252262
vertices[i*2] = p->points[i].x;
253263
vertices[(i*2) + 1] = p->points[i].y;
254264
}
255265
al_draw_filled_polygon((const float*)&vertices, (int)p->point_count, color);
266+
free(vertices);
256267
} break;
257268
case NK_COMMAND_POLYLINE: {
269+
int i;
270+
float *vertices;
258271
const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd;
272+
vertices = calloc(p->point_count * 2, sizeof(float));
259273
color = nk_color_to_allegro_color(p->color);
260-
int i;
261-
float vertices[p->point_count * 2];
262274
for (i = 0; i < p->point_count; i++) {
263275
vertices[i*2] = p->points[i].x;
264276
vertices[(i*2) + 1] = p->points[i].y;
265277
}
266278
al_draw_polyline((const float*)&vertices, (2 * sizeof(float)),
267279
(int)p->point_count, ALLEGRO_LINE_JOIN_ROUND, ALLEGRO_LINE_CAP_ROUND,
268280
color, (float)p->line_thickness, 0.0);
281+
free(vertices);
269282
} break;
270283
case NK_COMMAND_TEXT: {
284+
NkAllegro5Font *font;
271285
const struct nk_command_text *t = (const struct nk_command_text*)cmd;
272286
color = nk_color_to_allegro_color(t->foreground);
273-
NkAllegro5Font *font = (NkAllegro5Font*)t->font->userdata.ptr;
287+
font = (NkAllegro5Font*)t->font->userdata.ptr;
274288
al_draw_text(font->font,
275289
color, (float)t->x, (float)t->y, 0,
276290
(const char*)t->string);
277291
} break;
278292
case NK_COMMAND_CURVE: {
293+
float points[8];
279294
const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
280295
color = nk_color_to_allegro_color(q->color);
281-
float points[8];
282296
points[0] = (float)q->begin.x;
283297
points[1] = (float)q->begin.y;
284298
points[2] = (float)q->ctrl[0].x;
@@ -466,12 +480,13 @@ NK_API struct nk_context*
466480
nk_allegro5_init(NkAllegro5Font *allegro5font, ALLEGRO_DISPLAY *dsp,
467481
unsigned int width, unsigned int height)
468482
{
483+
struct nk_user_font *font;
469484
if (!al_init_primitives_addon()) {
470485
fprintf(stdout, "Unable to initialize required allegro5 primitives addon\n");
471486
exit(1);
472487
}
473488

474-
struct nk_user_font *font = &allegro5font->nk;
489+
font = &allegro5font->nk;
475490

476491
allegro5.dsp = dsp;
477492
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)