@@ -68,6 +68,7 @@ static struct nk_allegro5 {
68
68
NK_API NkAllegro5Font *
69
69
nk_allegro5_font_create_from_file (const char * file_name , int font_size , int flags )
70
70
{
71
+ NkAllegro5Font * font ;
71
72
if (!al_init_image_addon ()) {
72
73
fprintf (stdout , "Unable to initialize required allegro5 image addon\n" );
73
74
exit (1 );
@@ -80,7 +81,7 @@ nk_allegro5_font_create_from_file(const char *file_name, int font_size, int flag
80
81
fprintf (stdout , "Unable to initialize required allegro5 TTF font addon\n" );
81
82
exit (1 );
82
83
}
83
- NkAllegro5Font * font = (NkAllegro5Font * )calloc (1 , sizeof (NkAllegro5Font ));
84
+ font = (NkAllegro5Font * )calloc (1 , sizeof (NkAllegro5Font ));
84
85
85
86
font -> font = al_load_font (file_name , font_size , flags );
86
87
if (font -> font == NULL ) {
@@ -94,6 +95,8 @@ nk_allegro5_font_create_from_file(const char *file_name, int font_size, int flag
94
95
static float
95
96
nk_allegro5_font_get_text_width (nk_handle handle , float height , const char * text , int len )
96
97
{
98
+ float width ;
99
+ char * strcpy ;
97
100
NkAllegro5Font * font = (NkAllegro5Font * )handle .ptr ;
98
101
if (!font || !text ) {
99
102
return 0 ;
@@ -102,10 +105,12 @@ nk_allegro5_font_get_text_width(nk_handle handle, float height, const char *text
102
105
as nuklear uses variable size buffers and al_get_text_width doesn't
103
106
accept a length, it infers length from null-termination
104
107
(which is unsafe API design by allegro devs!) */
105
- char strcpy [ len + 1 ] ;
106
- strncpy (( char * ) & strcpy , text , len );
108
+ strcpy = malloc ( len + 1 ) ;
109
+ strncpy (strcpy , text , len );
107
110
strcpy [len ] = '\0' ;
108
- return al_get_text_width (font -> font , strcpy );
111
+ width = al_get_text_width (font -> font , strcpy );
112
+ free (strcpy );
113
+ return width ;
109
114
}
110
115
111
116
NK_API void
@@ -170,18 +175,18 @@ nk_allegro5_render()
170
175
(float )r -> rounding , color );
171
176
} break ;
172
177
case NK_COMMAND_CIRCLE : {
178
+ float xr , yr ;
173
179
const struct nk_command_circle * c = (const struct nk_command_circle * )cmd ;
174
180
color = nk_color_to_allegro_color (c -> color );
175
- float xr , yr ;
176
181
xr = (float )c -> w /2 ;
177
182
yr = (float )c -> h /2 ;
178
183
al_draw_ellipse (((float )(c -> x )) + xr , ((float )c -> y ) + yr ,
179
184
xr , yr , color , (float )c -> line_thickness );
180
185
} break ;
181
186
case NK_COMMAND_CIRCLE_FILLED : {
187
+ float xr , yr ;
182
188
const struct nk_command_circle_filled * c = (const struct nk_command_circle_filled * )cmd ;
183
189
color = nk_color_to_allegro_color (c -> color );
184
- float xr , yr ;
185
190
xr = (float )c -> w /2 ;
186
191
yr = (float )c -> h /2 ;
187
192
al_draw_filled_ellipse (((float )(c -> x )) + xr , ((float )c -> y ) + yr ,
@@ -200,54 +205,61 @@ nk_allegro5_render()
200
205
(float )t -> b .y , (float )t -> c .x , (float )t -> c .y , color );
201
206
} break ;
202
207
case NK_COMMAND_POLYGON : {
208
+ int i ;
209
+ float * vertices ;
203
210
const struct nk_command_polygon * p = (const struct nk_command_polygon * )cmd ;
211
+ vertices = calloc (p -> point_count * 2 , sizeof (float ));
204
212
color = nk_color_to_allegro_color (p -> color );
205
- int i ;
206
- float vertices [p -> point_count * 2 ];
207
213
for (i = 0 ; i < p -> point_count ; i ++ ) {
208
214
vertices [i * 2 ] = p -> points [i ].x ;
209
215
vertices [(i * 2 ) + 1 ] = p -> points [i ].y ;
210
216
}
211
217
al_draw_polyline ((const float * )& vertices , (2 * sizeof (float )),
212
218
(int )p -> point_count , ALLEGRO_LINE_JOIN_ROUND , ALLEGRO_LINE_CAP_CLOSED ,
213
219
color , (float )p -> line_thickness , 0.0 );
220
+ free (vertices );
214
221
} break ;
215
222
case NK_COMMAND_POLYGON_FILLED : {
223
+ int i ;
224
+ float * vertices ;
216
225
const struct nk_command_polygon_filled * p = (const struct nk_command_polygon_filled * )cmd ;
226
+ vertices = calloc (p -> point_count * 2 , sizeof (float ));
217
227
color = nk_color_to_allegro_color (p -> color );
218
- int i ;
219
- float vertices [p -> point_count * 2 ];
220
228
for (i = 0 ; i < p -> point_count ; i ++ ) {
221
229
vertices [i * 2 ] = p -> points [i ].x ;
222
230
vertices [(i * 2 ) + 1 ] = p -> points [i ].y ;
223
231
}
224
232
al_draw_filled_polygon ((const float * )& vertices , (int )p -> point_count , color );
233
+ free (vertices );
225
234
} break ;
226
235
case NK_COMMAND_POLYLINE : {
236
+ int i ;
237
+ float * vertices ;
227
238
const struct nk_command_polyline * p = (const struct nk_command_polyline * )cmd ;
239
+ vertices = calloc (p -> point_count * 2 , sizeof (float ));
228
240
color = nk_color_to_allegro_color (p -> color );
229
- int i ;
230
- float vertices [p -> point_count * 2 ];
231
241
for (i = 0 ; i < p -> point_count ; i ++ ) {
232
242
vertices [i * 2 ] = p -> points [i ].x ;
233
243
vertices [(i * 2 ) + 1 ] = p -> points [i ].y ;
234
244
}
235
245
al_draw_polyline ((const float * )& vertices , (2 * sizeof (float )),
236
246
(int )p -> point_count , ALLEGRO_LINE_JOIN_ROUND , ALLEGRO_LINE_CAP_ROUND ,
237
247
color , (float )p -> line_thickness , 0.0 );
248
+ free (vertices );
238
249
} break ;
239
250
case NK_COMMAND_TEXT : {
251
+ NkAllegro5Font * font ;
240
252
const struct nk_command_text * t = (const struct nk_command_text * )cmd ;
241
253
color = nk_color_to_allegro_color (t -> foreground );
242
- NkAllegro5Font * font = (NkAllegro5Font * )t -> font -> userdata .ptr ;
254
+ font = (NkAllegro5Font * )t -> font -> userdata .ptr ;
243
255
al_draw_text (font -> font ,
244
256
color , (float )t -> x , (float )t -> y , 0 ,
245
257
(const char * )t -> string );
246
258
} break ;
247
259
case NK_COMMAND_CURVE : {
260
+ float points [8 ];
248
261
const struct nk_command_curve * q = (const struct nk_command_curve * )cmd ;
249
262
color = nk_color_to_allegro_color (q -> color );
250
- float points [8 ];
251
263
points [0 ] = (float )q -> begin .x ;
252
264
points [1 ] = (float )q -> begin .y ;
253
265
points [2 ] = (float )q -> ctrl [0 ].x ;
@@ -425,12 +437,13 @@ NK_API struct nk_context*
425
437
nk_allegro5_init (NkAllegro5Font * allegro5font , ALLEGRO_DISPLAY * dsp ,
426
438
unsigned int width , unsigned int height )
427
439
{
440
+ struct nk_user_font * font ;
428
441
if (!al_init_primitives_addon ()) {
429
442
fprintf (stdout , "Unable to initialize required allegro5 primitives addon\n" );
430
443
exit (1 );
431
444
}
432
445
433
- struct nk_user_font * font = & allegro5font -> nk ;
446
+ font = & allegro5font -> nk ;
434
447
font -> userdata = nk_handle_ptr (allegro5font );
435
448
font -> height = (float )allegro5font -> height ;
436
449
font -> width = nk_allegro5_font_get_text_width ;
0 commit comments