@@ -70,18 +70,22 @@ static struct nk_allegro5 {
7070
7171NK_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)
98102static float
99103nk_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 */
116124NK_API NkAllegro5Font *
117125nk_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*
465481nk_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 ;
0 commit comments