Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Text Box

This thread is locked; no one can reply to it. rss feed Print
Text Box
Antone333
Member #15,545
March 2014

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?

#SelectExpand
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
Second in Command
February 2005
avatar

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.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Antone333
Member #15,545
March 2014

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
Second in Command
February 2005
avatar

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?

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

jmasterx
Member #11,410
October 2009

I have a class that does a lot of this for you:
https://github.com/jmasterx/Agui/blob/master/src/Agui/ResizableText.cpp

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

Edgar Reynaldo
Member #8,592
May 2007
avatar

beoran
Member #12,636
March 2011

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

Edgar Reynaldo
Member #8,592
May 2007
avatar

beoran
Member #12,636
March 2011

Yes, they are documented here:

http://alleg.sourceforge.net/a5docs/refman/font.html#multiline-text-drawing

The function al_draw_multiline_textf may be useful here.

Go to: