0

我的问题与 Allegro 5 C++ 有关。这是我的代码的某些部分,它们必须在屏幕上绘制文本。我已经完成了所有声明,错误肯定在这部分代码中。

所以起初我声明了一个全局变量ALLEGRO_FONT * font; ,我在 main 中调用了这个函数al_init_font_addon();

这是另一个绘制文本的函数。

    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;
        }
    }

问题是这个应用程序在 while 循环的 507 步崩溃

4

1 回答 1

1

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.

于 2013-06-11T17:36:49.293 回答