This post is a portion of Part 2 in my on going series about 2D Game Development using the Allegro 5 library. These posts are in course order. You can see all of the posts in this course by clicking the “2D Game Development” button at the top of this site.
- Part 2.0: 2D Graphic Basics
- Part 2.1: Starting the Code
- Part 2.2: Initializing Allegro Graphics
- Part 2.3: Using Fonts and Text
- Part 2.4: Graphical Primitives
This video walks you through creating ALLEGRO_FONT objects, initializing the font addons, and writing to the screen.
Fill source code can be downloaded here.
Notice that the source code is slightly different from the video? Here is why.
Well, it works but only for a brief moment. It should stay 5 seconds on the screen, yet it quits immediately.
Here’s the code:
#include
#define ALLEGRO_STATICLINK
#include “allegro5/allegro.h”
#include “allegro5/allegro_font.h”
#include “allegro5/allegro_ttf.h”
#include
int main(void){
ALLEGRO_DISPLAY *display = NULL;
if(!al_init())
{
al_show_native_message_box(NULL, NULL, NULL, “failed to initialize allegro!”, NULL, NULL);
return -1;
}
display = al_create_display(640, 480);
if(!display){
al_show_native_message_box(NULL, NULL, NULL, “failed to initialize display!”, NULL, NULL);
return -1;
}
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_FONT *font24 = al_load_font(“lucon.ttf”,24,0);
al_clear_to_color(al_map_rgb(0,0,0)); // clears the screen to the selected color
al_draw_text(font24, al_map_rgb(255,180,0), 50, 50, 0, “Hi 2D graphics world! “);
al_flip_display();
al_rest(5.0);
al_destroy_display(display);
return 0;
}
Can you check you exit value to see if it is -1? The program could be crashing, and since you never directly include the native dialog header file, you might not be seeing the dialog box popup to inform you.
The code works for me. My assumption is that your font is not in the appropriate location and the application is crashing.
The funny thing is that it returns 0, and from what I know that is a successful termination.
The text shows up, I can see it, but the window disappears very fast. I’ve put the font file in the project directory.
Also, what do you mean by “and since you never directly include the native dialog header file, you might not be seeing the dialog box popup to inform you”. Then what’s the point of writing those messages? 😀
I apologize then as I misunderstood. If you are seeing the text on the screen, no matter how briefly, then the font and program is working.
Also, what I meant was that in the code you posted, you weren’t using the statement #include so I assumed that may have caused an issue. There is an empty “#include” so maybe it was supposed to be there and it didn’t paste correctly. There must be an issue with the way your system interprets al_rest(); What OS are you using?
I am using windwos 7 and microsoft visual studio 2008 and when running this above source code i get debug error?What should i do.
The empty “include” could simply be this website taking the less-than (”) symbols and thinking they are some sort of HTML element.
How can I draw Unicode text on screen? If I try to draw “Košir”, only “Ko” is displayed.
I found this which may help: http://www.allegro.cc/forums/thread/608419. I have never worked with Unicode in Allegro so I am not entirely certain how to do it.
I solved the issue by using al_draw_ustr() function and converting the .cpp file to UTF-8 without BOM.
1) Declare your string. For example :
char * _kosir = “Košir”;
2) Save the file with the text (File->Advanced Save Options) as “Unicode (UTF-8 without signature) – Codepage 65001”
3) Load and use a font that has the corresponding characters you
need.
ALLEGRO_FONT * font = al_load_ttf_font(“UnYetgul.ttf”, 48, 0 );
al_draw_textf(font, makecol(5, 5, 5), 10, 30, 0, _kosir );
Thank you very much!
Finally found this simple anwser to unicode problem!!!
Can one write text onto a bitmap to save time with screens?
Absolutely. If you will be rendering the same text, saving it to a bitmap will speed up updating your screens. You may need a little code manipulation if you will change the text displayed (since the width of the bitmap will obviously change), and you’ll want to remember to realign it (if center- or right-justified).
When I compile and execute the program, I get a error message, saying “R6010 – abort() has been called”
Anyone have any idea of what this means?
I’ve followed all of Mike’s instructions, and I can’t find a solution to this anywhere…
Can we see your code? I really need some forums or something to help out with questions.
I had the same problem, It happens if you forget to put arial.ttf in the solutions folder.
I have put the text in the solutions folder. However I am still having that error. After debuggin I have found the program crashes when I am drawing the text(al_draw_text). Code is below
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_FONT* font24 = al_load_font(“arial”, 24, 0);
al_clear_to_color(al_map_rgb(0,0,0));
al_draw_text(font24, (al_map_rgb(255,0,0)), 50, 50, 0, “Hellow World!”);
al_flip_display();
al_rest(5.0);
al_destroy_font(font24);
al_destroy_display(display);
Never mind. Sorted the name has to have .ttf
Great thanx for your help I got it writing text to a bitmap. How do you create more than one screen? Like for example you have a front screen, a high score screen etc. ? Would you just use bitmaps again?
Just have multiple bitmaps
It works perfect. You are a very good teacher 🙂
I am trying to write text to my screen. This is explained in full detail on your page, however I have multiple classes. One of which is the class responsible for displaying and creating all graphics. The font is a private data field of the class. Is this allowable? I tried to initialize the font in the class constructor but the program crashed when I ran it. Does the initialization and drawing have to be in the same function? The program runs perfectly with no code for fonts included.
It returned the following error when I tried to include the fonts:
Assertion failed: font, file d:librariesbuildallegrosrcallegro-5.0.xaddonsfonttext.c , line 73.
Any ideas what I am doing wrong here?
Where do we get the font file that needs to go into the Solutions folder?
Nevermind. I was able to download a font. =)
How come all I get is a black screen when I run the program? Here’s the code:
#include
#include
#include
#include
int main(void)
{
ALLEGRO_DISPLAY *display = NULL;
if(!al_init())
{
al_show_native_message_box(NULL, NULL, NULL,
“failed to initialize allegro!”, NULL, NULL);
return -1;
}
display = al_create_display(640, 480);
if(!display)
{
al_show_native_message_box(NULL, NULL, NULL,
“failed to initialize display!”, NULL, NULL);
return -1;
}
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_FONT *font24 = al_load_font(“Aladdin.ttf”, 24, 0);
ALLEGRO_FONT *font36 = al_load_font(“Aladdin.ttf”, 36, 0);
ALLEGRO_FONT *font18 = al_load_font(“Aladdin.ttf”, 18, 0);
al_clear_to_color(al_map_rgb(0,0,0));
al_draw_text(font24, al_map_rgb(255, 0, 255), 50, 50, 0, “Hello World, this is 24 point”);
al_draw_text(font36, al_map_rgb(255, 127, 127), 640 / 2, 480 / 2, ALLEGRO_ALIGN_CENTRE, “This is Centered and 36 point”);
al_draw_text(font18, al_map_rgb(15, 240, 18), 620, 350, ALLEGRO_ALIGN_RIGHT, “This is right aligned and 18 point”);
int screen_w = al_get_display_width(display);
int screen_h = al_get_display_height(display);
al_draw_textf(font18, al_map_rgb(255, 255, 255), screen_w/2, 400, ALLEGRO_ALIGN_CENTRE,
“TEXT with variable output (textf): Screen width and height = %i / %i” , screen_w, screen_h);
al_flip_display();
al_rest(5.0);
al_destroy_font(font18);
al_destroy_font(font24);
al_destroy_font(font36);
al_destroy_display(display);
return 0;
}
Pasting code into my site mangles it quite a bit. Can you email it to me?
Also, does the window stay up for a full 5 seconds or does it close quickly? That can elude to a problem if it closes immediately.
I have such an error:
“Assertion failed: target, file allegro-5.0.xsrcdisplay.c, line 187” – in console
and Debug error
“R6010
– abort() has been called”
Using Allegro 5.0.5 on Windows 7
Can you email your code?
Everything is Fine, I’ve made a mistake while printig and compiler didn’t matched it so i was frustrated and spend a lot of time Googling )) Finally I’ve used source from your site and everithing has gone excellent!
Thanks for respond and for interesting and usefull tutorials!
Is there a way to remove the text after switching a boolean? Or a way to make it disapear.
Have written, as well as copy pasted the source code you have provided, still the same error pops up,
“Assertion failed: font, file allegro-5.0.xaddonsfonttext.c, line 73”
and
R6010, abort() has been called
Help will be appreciated sir.
I assume you are missing the font file in your project folder. I just copied the arial font there. You will have to move whatever font you want and load it appropriately
Stupidly, i did copy but in the solution folder, not in the one with the resources, thanks for a reminder sir. Cheers!
Had been looking for an Allegro tut since almost an year now! Finally got one! *v*
all worked untill the font, trying getting the info from the coments but nowhere secified far i found where the ttf file should be then!
Old tutorial, but thinking its on the net for reference and use, so hope gettting the answer! Looked just the tutorial i was looking for!
when im runnin the program im gettin “Unhandled exception at 0x5e0fb1b1 in AllegroProject.exe: 0xC0000005: Access violation reading location 0x00000008.” error message.
and “font24” get no value: “0x646e6120 {data=??? height=??? vtable=??? }”
what to do? :
using allegro 5.0.5 on windows 7 in visual c++ express 2010
Can you email me your code? Could be a lot of things.
yh of course. whats ur email?
tnx Mike.
I sent it to u.
Oops i put the .ttf file in wrong dic.
x:
and still.. its not working :
weird..
O-K !
Its working now 🙂
gday.
hey, quick question.
How can I use al_draw_textf with chars? it keeps printing @ when i type %c.
Yes, you should be able to textf with chars. Not 100% certain though as I have never tried it.
i compiled your code.what i got was screen poped-out and desappeared at the instant . there was no text or font.
i got error like-
/allegro/addons/font/text.c:198: al_draw_textf: Assertion `font’ failed.
Aborted
i got same error with another code that was related to fonts.i think al_load_font() is not working. i am using gcc compiler with allegro libraries installed..your all other code are working fine.
plz tell me how to fix it..
Sounds like your fonts aren’t in the correct folder.
same kind of error comes when i use allegro_native addon
Hey there Mike,
Thanks for all the tutorials, they really seem great. Ran into a small problem while trying to follow this one though. All I get while trying to run the code is a black screen with no text what so ever. Any ideas how I can fix this? The screen does stay for 5 seconds, it just doesn’t show anything. Thanks in advance!
You forgetting to flip the display?
same problem :/ screen showing nothing but waiting for 5s
so i got the same error Andrew got
“R6010
– abort() has been called”
again im using the same as him 5.0.5
and windows 7
can anyone shed some light on it, i even tried just copying the source still no resault
Your font is probably not in the correct folder.
If you have problems in drawing characters (like éàö) on screen :
Converting your .c/.cpp from something to UTF-8 Without BOM should be the solution. Be careful, UTF-8 With BOM is different and it may not works ;-).
How did you handle it i have a same problem
“R6010 – abort() has been called”
1. you may forget the line “al_init_font_addon();”
it’s before the initialization “al_init_ttf_addon();”
2. or you may forget to copy the “arial.ttf” to the folder where you main.cpp is
Hi mike thanks a lot for your videos.
I followed your instruction and I can’t see the lines. I dowloaded your code and I got the same problem. No matter what I do, I always see that black background. I don’t know I did wrong, but I know that earlier I tried changing the background and it worked, but now it just won’t change. Thanks in advance.
Are you flipping the display?
Thank you Mike.
I fix that problem by starting a new project. Everyting is cool now. But I really don’t know what I was doing wrong.
Anyway thanks again for your hard work, it’s really appreciated.
For some reason I get the
“Assertion failed: font, file allegro-5.0.xaddonsfonttext.c, line 73?
and
R6010, abort() has been called
whenever I type in the code.
But when I copy past your code from about line 26 down it works perfectly. Even though it is the exact same code.
This is the line that made VS give me the error when I was debugging my code.
al_draw_text(font24, al_map_rgb(255, 0, 255), 50, 50, 0, “Hello World, this is 24 point”);
Error:
/allegro/addons/font/text.c:198: al_draw_textf: Assertion `font’ failed.
Aborted
Solution:
make sure to put your font in the folder with your .cpp file. I was putting into the Debug folder with the .exe file. What works if you are running the program manually.. but not if you are running it from Visual Studio. I put the font into the folder my main.cpp file is and it runs fine from within Visual Studio. Odd to me.
All but the variable text output (textf) is working for me. I don’t get any errors, but instead of replacing either %I with screen_h/w %I comes up on the display as if it were just part of the text. Also, it seems to cut off after the / after the first %I.
Code (all one line):
al_draw_textf(font18, al_map_rgb(255, 255, 255), screen_w / 2, screen_h / 2, ALLEGRO_ALIGN_CENTRE, "TEXT with variable object (textf): screen width and height = %I / %I", screen_w, screen_h);
Woops – found it. Capital I.
I had same prob with the font not being in the right folder. Not familliar with VC++10 so learning the ropes with that as I follow your tutorial. Thanks for dumbing it down for us.
I have a quick questions relating to allegro fonts.
Is there a way to get the length and height of the font in pixels? I found the two functions “text_height(…)” and “text_length(…)” but it says they are undefined.
Nevermind, I think I figured it out. If I did, i’ll post my results.
Hey just a quick question. I am very new to using msvc10 and am just wondering how to get to the solutions folder to put the text.ttf in? It is much appreciated. Thanks 🙂
I am running a program which uses TTF fonts. It will load fonts initially, close the program down, then restart itself again. (BY GOING BACK TO THE SAME PROGRAM WITHIN A MENU) But when it loads again the fonts will not load and the font pointer ends up NULL.
I’ve been checking to make sure I destroyed all my fonts when the program closes down but I don’t think thats where the bug is coming from.
I resolved the issue:
*al_load_font* was substituted with *al_load_ttf_font*
Why does the latter work better?
where is the solutions folder?????????
Thank You so much for this great tutorial!!!!
I really want to practice them all.
Greeting from germany 😉
Great tutorial! But I have a question… Why do we need to supply an x value in our drawtext function when we have the ALLIGN_CENTRE flag?
So that the text can be centered about that point. Try it out for yourself and see what supplying different x values does.
sir i have a question, why do we need the functions — al_init_font_addon() and al_init_ttf_addon() ? how does it affect the whole program or part of it? i went to the manual here (http://alleg.sourceforge.net/a5docs/refman/font.html#al_init_font_addon) but i couldnt make any sense of it. plus al_draw_text() function takes 2 parameters and the first one is the the value of font, not the address of the pointer(http://alleg.sourceforge.net/a5docs/refman/font.html#al_load_font) bt in this example we are using just the font pointer name(without asterik) , shouldnt we use it with asterik like this –>*fontName?
tnx in advance sir 🙂
i got the pointer part 🙂 bt still dont understand the first part of the ques(addon initializing part and its affected part in the program)
Help me please, I can’t seem to run the program. When I run it it windows says that it has stopped working. I copy your code exactly, put arial.ttf in the folder with source and .dll files but it doesn’t run correctly. I have no compile errors, but I have warnings. 2 warnings that basically say the same cryptic thing: “warning: passing NILL to non-pointer argument 6 of ‘int al_show_native_message_bo(ALLEGRO_DISPLAY*, const char*, const char*, const char*, const char*, int)’ [-Wconversion-null]”
What the heck does that mean? I bet it is connected to my problem, but I don’t know how to solve it. I am using mingw cmd line compiler and notepad++ if that helps you help me troubleshoot. Thanks.
I’m doing game, Snake, and I have a problem – I have a bug loading font in my program.. When I copy and paste program from this part of the course everything is OK, but when I use my code there is kind of Debug Error:
“Debug Error!
Program
..10ProjectsAllegro_Snake_MultiDebugAllegro_Snake_Multi.exe
R6010
– abort() bas been called
(Press Retry to debug the application)”
I don’t know what to do 🙁
Font is probably in the wrong folder.
I didn’t write the code (only the important part – file Snake.h is header file with declarations of the classes made by me – including BOARD class):
”
#include
#include
#include
#include
#include “Snake.h”
int main(void)
{
//allegro variable
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_FONT *font24 = NULL;
int width = 1000;
int height = 700;
bool done = false;
bool redraw = true;
int FPS = 60;
bool keys[4] = {false, false, false, false};
DIRECTION direction = RIGHT;
//ALLEGRO_BITMAP *board_bitmap = NULL;
//program init
if(!al_init()) //initialize Allegro
return -1;
display = al_create_display(width, height); //create our display object
font24 = al_load_font(“arial.ttf”,24,0); //load my font
if(!display) //test display object
return -1;
//addon init
al_install_keyboard();
al_init_primitives_addon();
//board_bitmap = al_create_bitmap(40, 40);
BOARD board(display, 55, 100, width-45, height-20,10);
al_draw_text(font24,al_map_rgb(255,255,255),10, 10, ALLEGRO_ALIGN_CENTRE, “FOO”);
[…]
”
The problem occures on the line:
al_draw_text(font24,al_map_rgb(255,255,255),10, 10, ALLEGRO_ALIGN_CENTRE, “FOO”);
but when I comment this line everything is ok =< I also noticed that typing the line "if(font24 == NULL) return -1;) after loading the font causes, that my program ends with the the signal -1.
I didn't know why my font doesn't load, but it works perfectly with the tutorial code… =/
I have font file in ALL folders and subfolders in my solution folder and it still doesn’t work with my program but with the code from this lesson works, as I said..
Double check your font file. If it is in the correct places then I am not sure what your problem could be. I am 99.9% you are looking in the wrong place.
But I said it twice, font IS ALREADY in ALL folders and subfolders of my solution folder.. (After your advice I checked this once again..). The most strange thing is that when I cut my code (and paste it into Notepad for instance) and paste code from this lesson, compile and run it, the program works perfectly as in the video! 🙁
You are missing:
al_init_font_addon();
al_init_ttf_addon();
IT WORKS! 😀 I must be really tired today that I didn’t noticed that, I THANK YOU VERY MUCH for help and your patience! =) And also for your such rapid reply! 🙂 Now I could back coding, thanks a lot again!
Ok so just kind of want to know a trick to get around this.
So i want the text “window will close in 10 seconds” to erase every time it goes through the loop and replace with the next number counting down. But all i get now is overlapping.
I just want it to count down and display as it goes.
//FILE: Main.cpp
//PROGR: Hank Bates
//PURPOSE: To display text on screen for 10 seconds.
//EXTRA ADD ON FILES: Slendermanswriting.ttf
// PrometheusSiren.wav
#include
#include
#include
#include
#include
#include
int main(void)
{
//summon the fonts and stuff
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_FONT *font50;
ALLEGRO_FONT *font36;
ALLEGRO_FONT *font18;
ALLEGRO_SAMPLE *song;
int a = 100;
if (!al_init())
{
al_show_native_message_box(NULL, NULL, NULL,
“failed to initialize allegro!”, NULL, NULL);
return -1;
}
//set up some of the display settings
al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_RESIZABLE);
display = al_create_display(640, 480);
al_set_window_title(display, “A bad horror game”);
if (!display)
{
al_show_native_message_box(NULL, NULL, NULL,
“failed to initialize display!”, NULL, NULL);
return -1;
}
al_init_font_addon();
al_init_ttf_addon();
//Install Slender Man font here
font50 = al_load_font(“Slendermanswriting.ttf”, 50, 0);
font36 = al_load_font(“Slendermanswriting.ttf”, 36, 0);
font18 = al_load_font(“Slendermanswriting.ttf”, 18, 0);
//set up music here
al_install_audio();
al_init_acodec_addon();
al_reserve_samples(1);
song = al_load_sample(“PrometheusSiren.wav”);
//play song this will loop around and around like a record man!
al_play_sample(song, 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, NULL);
int screen_w = al_get_display_width(display);
int screen_h = al_get_display_height(display);
al_clear_to_color(al_map_rgb(0, 0, 0));
al_draw_text(font50, al_map_rgb(255, 0, 0), 12, 50, 0, “SLENDER MAN IS COMING”);
al_draw_text(font36, al_map_rgb(255, 5, 10), 200, 100, 0, “RUN AND HIDE”);
al_draw_text(font18, al_map_rgb(100, 15, 18), 150, 150, 0, “ENJOY THE PROMETHEUS SIREN MUSIC”);
int timer = 10;
while (timer != 0)
{
al_draw_textf(font18, al_map_rgb(255, 255, 255), screen_w / 3, 300, ALLEGRO_ALIGN_CENTRE,
“TURN UP YOUR VOLUME TO %i PRECENT!”, a);
al_draw_textf(font18, al_map_rgb(255, 255, 255), screen_w / 2, 400, ALLEGRO_ALIGN_CENTRE,
“WINDOW WILL CLOSE IN %i seconds!”, timer);
al_flip_display();
al_rest(1.0);
timer = timer – 1;
}
al_rest(10.0);
//destroy stuff
al_destroy_font(font18);
al_destroy_font(font50);
al_destroy_font(font36);
al_destroy_display(display);
al_destroy_sample(song);
//pew pew pew, bang…. all destoryed 🙂
return 0;
}
I am new to game programming and while I was learning to draw fonts on the screen I encountered a problem. The problem was that when I pass ALLEGRO_ALIGN_RIGHT as the argument in the al_draw_font() function then it aligns the font to the LEFT and when I pass the argument ALLEGRO_ALIGN_LEFT as the argument then it aligns the font to the RIGHT. See I don’t want to modify the enum in which these keywords are defined. So what do I do to get it right??
OS: Windows 7 Compiler: Visual C++ Allegro Version: 5.0.10
#include
#include
#include
#include
int main(void)
{
ALLEGRO_DISPLAY *display = NULL;
if(!al_init())
{
al_show_native_message_box(NULL, NULL, NULL,
“failed to initialize allegro!”, NULL, NULL);
return -1;
}
display = al_create_display(640, 480);
if(!display)
{
al_show_native_message_box(NULL, NULL, NULL,
“failed to initialize display!”, NULL, NULL);
return -1;
}
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_FONT *font24 = al_load_font(“arial.ttf”, 24, 0);
al_clear_to_color(al_map_rgb(255,255,255));
al_draw_text(font24, al_map_rgb(0,0,0), 50, 50, 0, “Hi”);
al_flip_display();
al_rest(5.0);
al_destroy_display(display);
return 0;
}
Mine only flashes on the screen for about half a second and while it’s up it says “Hello World!” which is not what i’m telling it to display
I don’t quite understand what’s going on here, the compiler seems to get along just fine with all header files, that is with exception to “ttf.h” and “font.h” *Let’s assume that I’m including the allegro5 directory within the header space.
The compiler spits out errors such as, “printf undeclared” and “__attribute unknown override specifier, and for the count, yes, I have already linked the monolith library
Sir, Im having a problem with my display. when i run the programs using visual studio. it displays only in the menu bar like a running application and there is no way i can make it show on the screen. i have to place the mouse pointer on it for me to see what is going on.
i have gone to properties -> linker -> system -> substem and tried all the options.
what could be the problem ?
thanks.
Hello Mike. I’m not sure if it’s always been this way but I noticed the “Notice that the source code is slightly different from the video? Here is why.” but there wasn’t any thing after that besides an Ad. I also no longer see any comments. I remember seeing your replies too numerous comments for a good while, but I haven’t seen them recently. Thank you for your lessons on game development.
I don’t know my console crash when i ran my code here is it:
#include
#include
#include
#include
main()
{
if(!al_init()){
al_show_native_message_box(NULL,NULL,NULL,”Fallo al inicializar Allergro”,NULL,NULL);
}
ALLEGRO_DISPLAY * pantalla=NULL;
pantalla= al_create_display(1000, 454);
al_init_font_addon;
al_init_ttf_addon;
al_clear_to_color(al_map_rgb(0,0,0));
ALLEGRO_FONT *gabriolatext = al_load_font(“gabriola.TTF”,25,0);
al_draw_text(gabriolatext,al_map_rgb(255,255,255),10,10,0,”How are u”);
al_flip_display();
al_rest(5.0);
al_destroy_font(gabriolatext);
al_destroy_display(pantalla);
return 0;
}
In Allegro 5 there is a built-in font :
ALLEGRO_FONT* font = al_create_builtin_font();
😀
P.S: Great videos , i’m starting to love allegro!