3

I'm playing around with Allegro 5 in C++ and Visual Studio 2012, but for some reason I can't get a font to load using the sample code from the Allegro wiki:

    ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf",72,0 );

   if (!font){
      fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
      return false;
   }

   al_clear_to_color(al_map_rgb(50,10,70));
   al_draw_text(font, al_map_rgb(255,255,255), 640/2, (480/4),ALLEGRO_ALIGN_CENTRE, "It worked!");

I've tried placing the font file in about every feasible directory in my Visual Studio project, as well as in the directory the .exe is in (as is suggested by several other threads). I also tried just dropping a copy of it on the C: drive and calling it with the fully qualified path: I tried C:\\pirulen.ttf and C:/pirulen.ttf neither of which worked. I've also tried adding it to the "Resource Files" folder in my project, but that did not work either.

Any advice on what could be going on?

Thanks.

PseudoPsyche
  • 4,332
  • 5
  • 37
  • 58

2 Answers2

3

Everything that needs to be said is here:

Matthew
  • 47,584
  • 11
  • 86
  • 98
  • Thanks, that did the trick. It didn't say anything regarding the init functions on the tutorial page for fonts: http://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial/Addons/Fonts – PseudoPsyche Dec 24 '12 at 18:01
1

I know this is a yonks old question, but I've just found it and read the docs as suggested by Matthew (which everyone ought to do), so thought I'd add this here for any others looking for an easy answer.

You can handle creating a path for each resource if you wish but it is easier to use al_change_directory to set up your resources directory, then you can find you assets there.

al_init_font_addon(); // initialize the font addon
al_init_ttf_addon();// initialize the ttf (True Type Font) addon

ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
al_append_path_component(path, "resources");
al_change_directory(al_path_cstr(path, '/'));
al_destroy_path(path);

Then you can reference paths relative to the resource root.

ALLEGRO_FONT* font = al_load_ttf_font("LibreCaslonText-Bold.ttf", 72, 0);

// Check font is loaded
if (!font) {
    fprintf(stderr, "failed to load font!\n");
    al_destroy_font(font);
    return -1;
}

To clarify LibreCaslonText-Bold.ttf exists in project_dir/resources/LibreCaslonText-Bold.ttf

Side note

In my case, I also needed to copy the resources over to my build directory. So I added this to my CMakeLists.txt.

if (NOT ${PROJECT_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY "${PROJECT_SOURCE_DIR}/resources" DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
endif()
OrderAndChaos
  • 3,547
  • 2
  • 30
  • 57