font returns NULL
Havacore

I'm trying to add in ttf for my first game, but I don't think that fonts installed properly. I'm just trying to compile the code that the wiki has but I added in a print statement to standard error to let me know whats up

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3// dont forget these two headers and their respective libraries if your not using the monolith 4#include <allegro5/allegro_font.h> 5#include <allegro5/allegro_ttf.h> 6 7int main() 8{ 9 if(!al_init()) 10 { 11 fprintf(stderr, "Failed to initialize Allegro.\n"); 12 return -1; 13 } 14 15 al_init_font_addon(); // initialize the font addon 16 al_init_ttf_addon();// initialize the ttf (True Type Font) addon 17 ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf",72,0 ); // load the font | Font from Larabie Free Fonts 18 ALLEGRO_DISPLAY *display = NULL; 19 display = al_create_display(640,480); 20 21 if(!font) { 22 fprintf(stderr, "failed to initialize font.\n"); 23 return -1; 24 } 25 26 if (!display) 27 { 28 fprintf(stderr, "Failed to create display.\n"); 29 return -1; 30 } 31 32 al_clear_to_color(al_map_rgb(50,10,70)); 33 al_draw_text(font, al_map_rgb(255,255,255), 640/2, (480/4),ALLEGRO_ALIGN_CENTRE, "Your Text Here!");// draw the text 34 35 al_flip_display(); 36 37 al_rest(10.0); 38 39 al_destroy_display(display); 40 41 return 0; 42}

After running it I get my printf statement, meaning that font is NULL. I've gone through the linux installation guide on the wiki again to make sure every addon is up to date. ???

Elias

Try using the complete path to the font, something like "/usr/local/mygame/data/pirulen.ttf".

Arthur Kalliokoski

Are you running it from a console in the current directory, or clicking on it from Konqueror or something? Maybe you want to add this right after al_init().

[EDIT]

No, wait, the current directory might not be your game directory. :P
Maybe allegro needs a function for that. In the meantime, sometimes argv[0] has the complete path, other times it doesn't. For Linux, you can do something like on this page

http://www.linuxforums.org/forum/programming-scripting/1360-get-application-path.html

[EDIT2]

I used to have that somewhere in my programs, but it's hopelessly lost among the gigabytes. This seems to work.

#SelectExpand
1#include <stdio.h> 2#include <sys/types.h> 3#include <unistd.h> 4#include <string.h> 5#include <libgen.h> 6 7char buff[256]; 8char buff2[256]; 9 10int main(void) 11{ 12 pid_t mypid; 13 mypid = getpid(); 14 sprintf(buff,"/proc/%d/exe",mypid); 15 readlink(buff,buff2,sizeof(buff2)); 16 strcpy(buff,(char *)dirname(buff2)); 17 printf("%s\n",buff); 18 return 0; 19}

No matter where you run it from it'll print the directory it's in, and if I click it from a file manager it'll print it to the originating console for X.

[EDIT3]
Changed example to use dirname() to strip off the name of the executable itself.

Matthew Leverton

Maybe allegro needs a function for that.

You mean al_get_standard_path()?

Step one is to use an absolute path to verify that's the problem.

If it works, step two is to make sure the game data is stored in a location relative to the executable, and then use the ALLEGRO_RESOURCES standard path to build the path.

Arthur Kalliokoski

I was looking in docs.liballeg.org :'(

Anyway, for a completed game I'd do this anyway, and it should overcome any cruft introduced by debugging project settings etc.

[EDIT]

It's on docs.liballeg.org too, but I haven't read the a5 docs enough to remember what's there and what isn't.

Havacore

Well I've tried adding in the *al_get_current_directory(void) into that code to get the current directory, but I can't figure out how to properly implement it. I've tried using

char directory = *al_get_current_directory(void);
printf("current directory: %s \n", directory );

// AND

printf("current directory: %s \n", *al_get_current_directory(void) );

//AND

char directory = *al_get_current_directory(); //take out the void?
printf("current directory: %s \n", directory );

Now I'm not sure why this is necessary, I know the directory that the executable and source code is in.

Elias said:

Try using the complete path to the font, something like "/usr/local/mygame/data/pirulen.ttf".

Sorry I'm not sure what you mean by that :-/ , I've only studied C/C++ for about 1 1/2 years and this is my first time dealing with a non-standard library.

Step one is to use an absolute path to verify that's the problem.

If it works, step two is to make sure the game data is stored in a location relative to the executable, and then use the ALLEGRO_RESOURCES standard path to build the path.

Not sure what you mean by absolute path ???

Also, the executable is in the same place as the source code. I'm reading the al5pdf about ALLEGRO_RESOURCES_PATH, so from what I understand this will direct the my program to use the default path where Allegro's resources are kept?

Are you running it from a console in the current directory, or clicking on it from Konqueror or something?

I'm running it from the terminal in the current directory, and I don't even know what Konqueror is.

This is how I'm compiling and running it:

jason@Wally:~/Documents/game dev/allegro wiki lessons/lesson 6$ gcc lesson6fonts.cc -o lesson $(pkg-config --libs allegro-5.0 allegro_font-5.0 allegro_ttf-5.0)

jason@Wally:~/Documents/game dev/allegro wiki lessons/lesson 6$ ./lesson

Matthew Leverton

An absolute path means the full path, like Elias wrote. However, if you're running it like that you don't need to worry about it (for now). The problem is when you run your program from an IDE, the working directory is not always where you expect it to be. Thus, the file is not found and you get NULL as a response.

Does that font file exist on your file system? In order for it to load, you need that file sitting in the same folder as your exe.

If so, then I would try a different font file and/or a smaller size.

Arthur Kalliokoski

When it says

it means that function returns a char pointer and takes no parameters.
So your line that says

char directory = *al_get_current_directory(void);

should be

char *directory = al_get_current_directory();

An absolute path means the whole directory path is written out, without the parent or current directory dots or tilde (home shortcut)

e.g.
"/usr/local/mygame/data/pirulen.ttf"
not something like
./mygame/data.pirulen.ttf
or ~/mmygame/data.pirulen.ttf

The '~' is a shortcut to your home directory, not an absolute path

Try saying "pwd <enter>" at your command prompt and you'll see the absolute path to the directory you're in.

Havacore

Does that font file exist on your file system? In order for it to load, you need that file sitting in the same folder as your exe.

If it comes with Allegro, I should have it since I installed everything that the wiki's installation tutorial said to install. When I run a search for an allegro font file in my file system, a lot of stuff comes up. What kind of file am I looking for (or where should I find it?)

Thomas Fjellstrom

Allegro's source happens to come with a ttf font file, but it never gets installed. You need to find the correct location to a valid font file.

Thread #606597. Printed from Allegro.cc