Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Question for al_draw_text inside a function

This thread is locked; no one can reply to it. rss feed Print
Question for al_draw_text inside a function
HighTechINC
Member #14,314
May 2012

Hey guys! I need a little help with my project for my Computer Programming class again. This time I am trying to draw Allegro fonts with "al_draw_text" INSIDE of a function, and I can't figure out how to load a font and be able to use it without loading it every single time in my game loop. I look at CTRL+ALT+Delete and watch the memory rise dramatically. This does the same thing when I load the text box.

I know it is probably a very simple fix, but I looked around for quite some time and couldn't find an answer.

Sample.cpp#SelectExpand
1void interact(Character &player) 2 3{ 4 ALLEGRO_FONT *font = al_load_font ("8Bit.ttf", 20, 0); 5 6 ALLEGRO_BITMAP *textBox = NULL; 7 textBox = al_load_bitmap ("C:\\RPG\\Text Box.bmp"); 8 9 //Text test 10 if (chat) 11 { 12al_draw_bitmap (textbox, 50, 50, 0); 13 if (player.xGrid == 2 && player.yGrid == 2) 14 { 15 al_draw_text (font, al_map_rgb (0, 0, 0), 50, 50, 0, "TEST"); 16 } 17 } 18}

Please speak in terms that I would know easily, for I am just a new programmer.

Thanks for taking the time to read and look over this!

Thomas Fjellstrom
Member #476
June 2000
avatar

Don't keep re-loading the font each time. Load it once some where and re-use it. Or at the very least, destroy the font at the end of the function.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

HighTechINC
Member #14,314
May 2012

Well my question is: How can I load the font somewhere else? I tried where I loaded my other bitmaps (Right at the start of main() ), and then I got an error saying it was not declared in the scope.

Arthur Kalliokoski
Second in Command
February 2005
avatar

Oversimplified

#SelectExpand
1 2ALLEGRO_FONT *font; 3 4void interact(Character &player) 5{ 6 ALLEGRO_BITMAP *textBox = NULL; 7 textBox = al_load_bitmap ("C:\\RPG\\Text Box.bmp"); 8 9 //Text test 10 if (chat) 11 { 12 al_draw_bitmap (textbox, 50, 50, 0); 13 if (player.xGrid == 2 && player.yGrid == 2) 14 { 15 al_draw_text (font, al_map_rgb (0, 0, 0), 50, 50, 0, "TEST"); 16 } 17 } 18} 19 20int main(int argc, char **argv) 21{ 22 23 if (!al_init()) 24 { 25 fprintf(stderr,"Could not init Allegro.\n"); 26 return 1; 27 } 28 29 al_install_keyboard(); 30 al_install_mouse(); 31 al_init_font_addon(); 32 al_init_ttf_addon(); 33 font = al_load_font ("8Bit.ttf", 20, 0); 34 if(!font) 35 { 36 error message blah blah 37 exit(1); 38 } 39 40 blah blah etc.

[EDIT]

Removed the second load of font :-/

They all watch too much MSNBC... they get ideas.

Thomas Fjellstrom
Member #476
June 2000
avatar

Two options, pass the font into the function as an argument, or make the font variable a global (cue bammcaig ranting about how globals are evil...).

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

HighTechINC
Member #14,314
May 2012

Arthur Kalliokoski's method worked, thanks a ton!

I have sooo much stuff before main now. The more you write, the harder it is to read in my opinion :-/.

How do I do this with bitmaps now?

Arthur Kalliokoski
Second in Command
February 2005
avatar

The same way?

They all watch too much MSNBC... they get ideas.

Thomas Fjellstrom
Member #476
June 2000
avatar

I'd start having your game objects store the data they need to use. You won't need to use quite so many globals that way, and should keep things cleaner, and more compartmentalized.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

HighTechINC
Member #14,314
May 2012

I figured out the bitmap thing, no biggy.

On the other hand: I am just drawing a text box/chat when the main character is in a certain area and presses a certain key. I am not sure on how to reply on what you said, sorry.

Arthur Kalliokoski
Second in Command
February 2005
avatar

I'd start having your game objects store the data they need to use. You won't need to use quite so many globals that way, and should keep things cleaner, and more compartmentalized.

I am not sure on how to reply on what you said, sorry.

I thought he'd show the C++ way, but I'm only using C. You could keep it local like this:

#SelectExpand
1void interact(Character &player) 2{ 3 static ALLEGRO_BITMAP *textBox = NULL; 4 static ALLEGRO_FONT *font = 0; 5 6 if(textBox == 0) 7 { 8 textBox = al_load_bitmap ("C:/RPG/Text Box.bmp"); 9 if(textBox == 0) 10 { 11 fprintf(stderr,"Can't load \"Text Box.bmp\"\n"); 12 exit(1); 13 } 14 } 15 16 if(font == 0) 17 { 18 font = al_load_font ("8Bit.ttf", 20, 0); 19 if(font == 0) 20 { 21 fprintf(stderr,"Can't load font \"8Bit.ttf\"\n"); 22 exit(1); 23 } 24 } 25 26 //Text test 27 if (chat) 28 { 29 al_draw_bitmap (textbox, 50, 50, 0); 30 if (player.xGrid == 2 && player.yGrid == 2) 31 { 32 al_draw_text (font, al_map_rgb (0, 0, 0), 50, 50, 0, "TEST"); 33 } 34 } 35} 36 37int main(int argc, char **argv) 38{ 39 40 if (!al_init()) 41 { 42 fprintf(stderr,"Could not init Allegro.\n"); 43 return 1; 44 } 45 46 al_install_keyboard(); 47 al_install_mouse(); 48 al_init_font_addon(); 49 al_init_ttf_addon(); 50 if(!font) 51 { 52 error message blah blah 53 exit(1); 54 } 55 56 blah blah etc.

They all watch too much MSNBC... they get ideas.

Peter Wang
Member #23
April 2000

Don't do that, you have a memory leak. Also you're unable to create a new bitmap and font for a new display.

To begin with, just use a single global structure with all the bitmaps/fonts you require.

Go to: