@@ -68,6 +68,7 @@ static struct nk_allegro5 {
6868NK_API NkAllegro5Font *
6969nk_allegro5_font_create_from_file (const char * file_name , int font_size , int flags )
7070{
71+ NkAllegro5Font * font ;
7172 if (!al_init_image_addon ()) {
7273 fprintf (stdout , "Unable to initialize required allegro5 image addon\n" );
7374 exit (1 );
@@ -80,7 +81,7 @@ nk_allegro5_font_create_from_file(const char *file_name, int font_size, int flag
8081 fprintf (stdout , "Unable to initialize required allegro5 TTF font addon\n" );
8182 exit (1 );
8283 }
83- NkAllegro5Font * font = (NkAllegro5Font * )calloc (1 , sizeof (NkAllegro5Font ));
84+ font = (NkAllegro5Font * )calloc (1 , sizeof (NkAllegro5Font ));
8485
8586 font -> font = al_load_font (file_name , font_size , flags );
8687 if (font -> font == NULL ) {
@@ -94,6 +95,8 @@ nk_allegro5_font_create_from_file(const char *file_name, int font_size, int flag
9495static float
9596nk_allegro5_font_get_text_width (nk_handle handle , float height , const char * text , int len )
9697{
98+ float width ;
99+ char * strcpy ;
97100 NkAllegro5Font * font = (NkAllegro5Font * )handle .ptr ;
98101 if (!font || !text ) {
99102 return 0 ;
@@ -102,10 +105,12 @@ nk_allegro5_font_get_text_width(nk_handle handle, float height, const char *text
102105 as nuklear uses variable size buffers and al_get_text_width doesn't
103106 accept a length, it infers length from null-termination
104107 (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 );
107110 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 ;
109114}
110115
111116NK_API void
@@ -170,18 +175,18 @@ nk_allegro5_render()
170175 (float )r -> rounding , color );
171176 } break ;
172177 case NK_COMMAND_CIRCLE : {
178+ float xr , yr ;
173179 const struct nk_command_circle * c = (const struct nk_command_circle * )cmd ;
174180 color = nk_color_to_allegro_color (c -> color );
175- float xr , yr ;
176181 xr = (float )c -> w /2 ;
177182 yr = (float )c -> h /2 ;
178183 al_draw_ellipse (((float )(c -> x )) + xr , ((float )c -> y ) + yr ,
179184 xr , yr , color , (float )c -> line_thickness );
180185 } break ;
181186 case NK_COMMAND_CIRCLE_FILLED : {
187+ float xr , yr ;
182188 const struct nk_command_circle_filled * c = (const struct nk_command_circle_filled * )cmd ;
183189 color = nk_color_to_allegro_color (c -> color );
184- float xr , yr ;
185190 xr = (float )c -> w /2 ;
186191 yr = (float )c -> h /2 ;
187192 al_draw_filled_ellipse (((float )(c -> x )) + xr , ((float )c -> y ) + yr ,
@@ -200,54 +205,61 @@ nk_allegro5_render()
200205 (float )t -> b .y , (float )t -> c .x , (float )t -> c .y , color );
201206 } break ;
202207 case NK_COMMAND_POLYGON : {
208+ int i ;
209+ float * vertices ;
203210 const struct nk_command_polygon * p = (const struct nk_command_polygon * )cmd ;
211+ vertices = calloc (p -> point_count * 2 , sizeof (float ));
204212 color = nk_color_to_allegro_color (p -> color );
205- int i ;
206- float vertices [p -> point_count * 2 ];
207213 for (i = 0 ; i < p -> point_count ; i ++ ) {
208214 vertices [i * 2 ] = p -> points [i ].x ;
209215 vertices [(i * 2 ) + 1 ] = p -> points [i ].y ;
210216 }
211217 al_draw_polyline ((const float * )& vertices , (2 * sizeof (float )),
212218 (int )p -> point_count , ALLEGRO_LINE_JOIN_ROUND , ALLEGRO_LINE_CAP_CLOSED ,
213219 color , (float )p -> line_thickness , 0.0 );
220+ free (vertices );
214221 } break ;
215222 case NK_COMMAND_POLYGON_FILLED : {
223+ int i ;
224+ float * vertices ;
216225 const struct nk_command_polygon_filled * p = (const struct nk_command_polygon_filled * )cmd ;
226+ vertices = calloc (p -> point_count * 2 , sizeof (float ));
217227 color = nk_color_to_allegro_color (p -> color );
218- int i ;
219- float vertices [p -> point_count * 2 ];
220228 for (i = 0 ; i < p -> point_count ; i ++ ) {
221229 vertices [i * 2 ] = p -> points [i ].x ;
222230 vertices [(i * 2 ) + 1 ] = p -> points [i ].y ;
223231 }
224232 al_draw_filled_polygon ((const float * )& vertices , (int )p -> point_count , color );
233+ free (vertices );
225234 } break ;
226235 case NK_COMMAND_POLYLINE : {
236+ int i ;
237+ float * vertices ;
227238 const struct nk_command_polyline * p = (const struct nk_command_polyline * )cmd ;
239+ vertices = calloc (p -> point_count * 2 , sizeof (float ));
228240 color = nk_color_to_allegro_color (p -> color );
229- int i ;
230- float vertices [p -> point_count * 2 ];
231241 for (i = 0 ; i < p -> point_count ; i ++ ) {
232242 vertices [i * 2 ] = p -> points [i ].x ;
233243 vertices [(i * 2 ) + 1 ] = p -> points [i ].y ;
234244 }
235245 al_draw_polyline ((const float * )& vertices , (2 * sizeof (float )),
236246 (int )p -> point_count , ALLEGRO_LINE_JOIN_ROUND , ALLEGRO_LINE_CAP_ROUND ,
237247 color , (float )p -> line_thickness , 0.0 );
248+ free (vertices );
238249 } break ;
239250 case NK_COMMAND_TEXT : {
251+ NkAllegro5Font * font ;
240252 const struct nk_command_text * t = (const struct nk_command_text * )cmd ;
241253 color = nk_color_to_allegro_color (t -> foreground );
242- NkAllegro5Font * font = (NkAllegro5Font * )t -> font -> userdata .ptr ;
254+ font = (NkAllegro5Font * )t -> font -> userdata .ptr ;
243255 al_draw_text (font -> font ,
244256 color , (float )t -> x , (float )t -> y , 0 ,
245257 (const char * )t -> string );
246258 } break ;
247259 case NK_COMMAND_CURVE : {
260+ float points [8 ];
248261 const struct nk_command_curve * q = (const struct nk_command_curve * )cmd ;
249262 color = nk_color_to_allegro_color (q -> color );
250- float points [8 ];
251263 points [0 ] = (float )q -> begin .x ;
252264 points [1 ] = (float )q -> begin .y ;
253265 points [2 ] = (float )q -> ctrl [0 ].x ;
@@ -425,12 +437,13 @@ NK_API struct nk_context*
425437nk_allegro5_init (NkAllegro5Font * allegro5font , ALLEGRO_DISPLAY * dsp ,
426438 unsigned int width , unsigned int height )
427439{
440+ struct nk_user_font * font ;
428441 if (!al_init_primitives_addon ()) {
429442 fprintf (stdout , "Unable to initialize required allegro5 primitives addon\n" );
430443 exit (1 );
431444 }
432445
433- struct nk_user_font * font = & allegro5font -> nk ;
446+ font = & allegro5font -> nk ;
434447 font -> userdata = nk_handle_ptr (allegro5font );
435448 font -> height = (float )allegro5font -> height ;
436449 font -> width = nk_allegro5_font_get_text_width ;
0 commit comments