Sam379
Sam379

Reputation: 271

C++ Allegro al_draw_textf ()

My question is related with Allegro 5 C++ . Here is some parts of my code which must draw text on the screen . I have done all the declarations and the error is definitely in this part of the code.

So at first I have declared a global variable ALLEGRO_FONT * font; I have called this function in main al_init_font_addon();

And here is another function which draws the text.

    void draw (){
        int score=0 ;
        while (!GetAsyncKeyState(VK_ESCAPE)){
          al_clear_to_color(al_map_rgb( 0 , 0 , 0));
          al_init_ttf_addon();
          font = al_load_font ("font.ttf" , 24 , NULL);
          al_draw_textf(font , al_map_rgb(255 , 0 , 255) , 200 , 200 , ALLEGRO_ALIGN_CENTRE   , "SCORE: %d" , score );
          al_flip_display();
          score +=10;
        }
    }

The problem is that this app crashes on the 507 step of the while loop

Upvotes: 0

Views: 1886

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409206

You're initializing a new font each loop, while not unallocating the resource when you're done with it.

Instead call al_init_ttf_addon and al_load_font only once, before the loop, and use it in the loop. Remember to free the font when you're done with it. I actually recommend you call al_init_ttf_addon when you initialize the program instead, in other words in the main function before you enter the event loop.

Upvotes: 1

Related Questions