Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Error Drawing Fonts

This thread is locked; no one can reply to it. rss feed Print
Error Drawing Fonts
Shinn22
Member #14,667
October 2012

The program worked fine until I turned it into a function and added it in a menu to a larger program.
The user clicks on the wordsearch builder, the program closes and returns to a menu. The user clicks on the wordsearch builder a second time and the program crashes.
With error:
"Assertion failed: font, file allegro-5.0.x\addons\font\text.c line 198"

The problem is when I run my word-search builder for the second time it crashes when it tries to display the fonts.
My program is really long so here is the basic code snippits I'm trying to find the bug in.

#SelectExpand
1 2int builder();// prototype for the wordsearch builder 3 4int builder() 5{ 6 7 ALLEGRO_FONT *font24 = NULL;//initializes a font pointer 8 ALLEGRO_FONT *font36 = NULL;//initializes a font pointer 9 ALLEGRO_FONT *font12 = NULL;//initializes a font pointer 10 ALLEGRO_FONT *font16 = NULL;//initializes a font pointer 11 12 font24 = al_load_font("cour.ttf", 24, 0); 13 font36 = al_load_font("cour.ttf", 36, 0); 14 font12 = al_load_font("cour.ttf", 12, 0); 15 font16 = al_load_font("cour.ttf", 16, 0); 16 17 al_draw_textf(font12, al_map_rgb(0, 0, 0), screen_w/2, 380, ALLEGRO_ALIGN_CENTRE, 18 "%s" , instructions[0]);// CRASHES HERE 19 al_draw_textf(font12, al_map_rgb(0, 0, 0), 360, 440, ALLEGRO_ALIGN_LEFT, 20 "Word Number: %d || Word Position: %d", wordNumber, wordPosition); 21 al_destroy_font(font12); 22 al_destroy_font(font16); 23 al_destroy_font(font24); 24 al_destroy_font(font36); 25 return 0; 26}

jmasterx
Member #11,410
October 2009

This indicates that either your font pointer is null or your const char* format is null.

Kris Asick
Member #1,424
July 2001

My guess is that "cour.ttf" is not located in the folder that you're running your program from. You need to check the return results of ALL al_load commands to ensure you're loading your files properly.

However, it's much safer to specify full paths to the files you want to load rather than just using filenames. You can use ALLEGRO_PATH objects to accomplish this.

In your case:

1. Make a pointer to an ALLEGRO_PATH object.

2. Call al_get_standard_path() to get the path which leads to where your font is installed.

3. Use al_append_path_component() to append any additional folder names as needed. (IE: If your fonts are stored in a folder called "Fonts".)

4. Set the filename to load using al_set_path_filename().

5. When calling al_load_font(), in place of your filename, use al_path_cstr().

6. Call al_destroy_path() on your ALLEGRO_PATH object to clear it from memory when you're done with it, or if you intend to call al_get_standard_path() on it again.

This may seem like a ton of work, but it pretty much guarantees that the file you want to load will be loaded properly on virtually any user's system. You can read up the details of these functions and how to use them in the Allegro Manual. (Note that al_get_standard_path() is a "System Routine".)

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Shinn22
Member #14,667
October 2012

Thank you for your help it narrowed down the bug search. But I am still having a problem when the function loads for a *second* time during the program. The font pointer comes out null despite everything seeming OK.

I capture the null pointer with error code and the function exits returning -1. With this in console window: "Error loading font24."

I don't understand where the memory leak is coming from.

Here is the code snippets:

#SelectExpand
1int builder();// prototype for the wordsearch builder 2int builder() 3{ 4puts ("Builder Started"); 5 ALLEGRO_DISPLAY *display = NULL;//initializes a pointer to a display 6 ALLEGRO_EVENT_QUEUE *event_queue = NULL;// initializes a pointer to an event queue 7 ALLEGRO_BITMAP *image = NULL;//initializes a bitmap pointer 8 ALLEGRO_BITMAP *image2 = NULL;//initializes a bitmap pointer 9 ALLEGRO_BITMAP *image3 = NULL;//initializes a bitmap pointer 10 ALLEGRO_BITMAP *image4 = NULL;//initializes a bitmap pointer 11 ALLEGRO_BITMAP *image5 = NULL;//initializes a bitmap pointer 12 ALLEGRO_FONT *font24 = NULL;//initializes a font pointer 13 ALLEGRO_FONT *font36 = NULL;//initializes a font pointer 14 ALLEGRO_FONT *font12 = NULL;//initializes a font pointer 15 ALLEGRO_FONT *font16 = NULL;//initializes a font pointer 16 puts ("Pointers Initialized"); 17//allegro initializations 18 al_init_primitives_addon();//primitives 19 al_init_image_addon();//a library to load images 20 al_init_font_addon();// installs font library from allegro 21 al_init_ttf_addon();// installs true text fonts from allegro library 22 al_install_keyboard();//installs the keyboard from allegro 23 24//SET FILEPATH 25 ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);// initializes a filepath pointer 26 al_set_path_filename(path,"cour.ttf"); 27 printf ("%s", al_path_cstr(path, '/')); 28 //initializes fonts 29 font24 = al_load_font(al_path_cstr(path, '/'), 24, 0); 30 if (!font24) 31 { 32 puts("Error loading font24."); 33 return -1; 34 } 35 font36 = al_load_font("cour.ttf", 36, 0); 36 if (!font36) 37 { 38 puts("Error loading font36."); 39 return -1; 40 } 41 font12 = al_load_font("cour.ttf", 12, 0); 42 if (!font12) 43 { 44 puts("Error loading font12."); 45 return -1; 46 } 47 font16 = al_load_font("cour.ttf", 16, 0); 48 if (!font16) 49 { 50 puts("Error loading font16."); 51 return -1; 52 } 53 puts ("Initialization finished"); 54 //memory clean up section 55 al_destroy_bitmap(image); 56 al_destroy_bitmap(image2); 57 al_destroy_bitmap(image3); 58 al_destroy_bitmap(image4); 59 al_destroy_bitmap(image5); 60 al_destroy_font(font12); 61 al_destroy_font(font16); 62 al_destroy_font(font24); 63 al_destroy_font(font36); 64 al_destroy_path(path);// destroys path object 65 al_destroy_event_queue(event_queue); 66 al_shutdown_image_addon(); //removes image loader 67 al_uninstall_keyboard();//clears keyboard 68 al_shutdown_ttf_addon;// clears memory 69 al_shutdown_font_addon;//removes fonts 70 al_destroy_display(display); 71 debug (MEMORYCLEARED); 72 return 0; 73}

Kris Asick
Member #1,424
July 2001

Use ALLEGRO_NATIVE_PATH_SEP in your call to al_path_cstr() instead of "/". It's safer and more portable and may be the source of your problems.

Beyond that, the only other thing I can suggest is to make sure "cour.ttf" is located in the same place as your executable file and to make sure you actually run your program from the EXE itself, not from any menu options in the C program you're using. If you are using the menu options, you need to be absolutely sure that it's running your executable from the same place the EXE is actually stored in.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Shinn22
Member #14,667
October 2012

Thank you for the tips Kris.
My program has been improved with error tracking code and the file pathing recommendations.

I resolved the issue, it was simple and probably independent of your suggestions. But I don't know exactly why it worked.

*al_load_font* was substituted with *al_load_ttf_font*
Now the program can initialize from the header file without complaints.
That bug was frustrating. For some reason al_load_ttf_font works better??

Here is the final code snippit:

#SelectExpand
1 2 //SET FILEPATH 3 ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);// initializes a filepath pointer 4 al_set_path_filename(path,"cour.ttf"); 5 printf ("%s", al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP)); 6 //initializes fonts 7 font24 = al_load_ttf_font(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP), 24, 0); 8 if (!font24) 9 { 10 puts("Error loading font24."); 11 return -1; 12 } 13 font36 = al_load_ttf_font(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP), 36, 0); 14 if (!font36) 15 { 16 puts("Error loading font36."); 17 return -1; 18 } 19 font12 = al_load_ttf_font(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP), 12, 0); 20 if (!font12) 21 { 22 puts("Error loading font12."); 23 return -1; 24 } 25 font16 = al_load_ttf_font(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP), 16, 0); 26 if (!font16) 27 { 28 puts("Error loading font16."); 29 return -1; 30 } 31 puts ("Initialization finished");

Kris Asick
Member #1,424
July 2001

In all honesty, I make my own bitmapped fonts for my games due to the licensing issues with distributing True-Type fonts and because it helps to give my software its own identity. So as far as TTF loading goes, I have next to no experience. I don't know what bugs or nuances there are as a result of trying to load them in A5.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Go to: