[英]C++ Allegro al_draw_textf ()
My question is related with Allegro 5 C++ . 我的问题与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;
因此,首先我声明了一个全局变量ALLEGRO_FONT * font;
I have called this function in main al_init_font_addon();
我在主要的al_init_font_addon();
调用了此函数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 问题是此应用在while循环的507步骤崩溃
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. 而是在循环之前仅调用al_init_ttf_addon
和al_load_font
,然后在循环中使用它。 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. 我实际上建议您在初始化程序时调用al_init_ttf_addon
,换句话说,在进入事件循环之前在main
函数中调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.