@@ -70,18 +70,20 @@ static struct nk_allegro5 {
7070
7171NK_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)
98100static float
99101nk_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 */
117123NK_API NkAllegro5Font *
118124nk_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*
466480nk_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 ;
0 commit comments