Text Box

Is there any way of doing a \n for a new line in al_draw_textf or something along those lines?

I want to use this for all of the dialog or event explanation in the game I am working on. Is there any better way of doing what i want?

1 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_ttf.h> 4#include <allegro5/allegro_font.h> 5#include <allegro5/allegro_image.h> 6 7void TextBox() 8{ 9 ALLEGRO_DISPLAY *TextBoxDisplay; 10 ALLEGRO_BITMAP *TextBox = NULL; 11 ALLEGRO_FONT *font = al_load_ttf_font("tahoma.ttf", 16, 0 ); 12 13 TextBoxDisplay = al_create_display(800, 896); 14 15 TextBox = al_load_bitmap("TextBox.png"); 16 17 al_draw_bitmap(TextBox, 320, 544, 0); 18 19 al_draw_textf(font, al_map_rgb(153, 76, 0), 336, 560, 0, "Hi there, my name is Joey I will be your guide through the"); 20 al_draw_textf(font, al_map_rgb(153, 76, 0), 336, 576, 0, "tutorial."); 21 22 al_draw_textf(font, al_map_rgb(153, 76, 0), 336, 576 + 32, 0, "Press Any Key To Continue..."); 23 24 //User input for any key to continue or given options according to content 25 26 al_flip_display(); 27} 28 29int main() 30{ 31 al_init(); 32 al_init_image_addon(); 33 al_init_font_addon(); 34 al_init_ttf_addon(); 35 36 TextBox(); 37 38 al_rest(5.0); 39 40 return 0; 41}

Arthur Kalliokoski

There are plenty of functions in the string library to do this, or you could copy the characters to a scratch buffer one at a time yourself until you found the newline, then store a 0, draw the text in the buffer using a variable for the y component, then reset the destination pointer in the buffer, lather, rinse repeat.


What if i did something sort of along the lines of a tilemap?

could i make a character array that fits the text box that i have and then just write each line that way and then have the program draw from a bitmap each letter?

Arthur Kalliokoski

If you're going to do that, why not just put the text on an image with a paint program and just blit the image?


I have a class that does a lot of this for you:

It breaks on newlines, but also word wraps if a certain width is reached.

Edgar Reynaldo

I thought Beoran was gonna add this to A5.1...


Yes, I did add this, it's aready in git. Now we "just" have to make the next 5.1.x release...

Edgar Reynaldo

Is it in the docs yet? What are they named? ? ?


Yes, they are documented here:


The function al_draw_multiline_textf may be useful here.

Thread #614841. Printed from Allegro.cc