Drawing text from variables?
curious_newbie

I understand the basics of allegro 5's draw text routines, but was wondering if it's possible to use text variables when using the routines?

example, I'm hoping to do something similar to this:

string answer = "blahblah";

al_draw_textf(font18, al_map_rgb(255, 255, 255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "The answer is: %?", answer);

Any help is greatly appreciated

Yodhe23

In 'C' I use "strcpy", "strcat", and "itoa" on a char[] (in 'C' a string variable is an array of char) variable, which I pass to the text display function.
It might be a place to start, though there are other seemingly funkier ways to do it with other languages e.g. C++.

james_lohr

I'm not sure whether or not allegro supports it, but if not then you could wrap al_draw_textf in a function that does by using sprintf (for string formatting), and va_list (for a variable number of arguments to your new function).

AMCerasoli

Just use answer.c_str() Generates a null-terminated sequence of characters (c-string) with the same content as the string object and returns it as a pointer to an array of characters.

curious_newbie

The answer.c_str() did exactly what I was looking for, thank you all for the replies

weapon_S
Yodhe23 said:

n 'C' I use "strcpy", "strcat", and "itoa" on a char[]

itoa is non-standard. For standard, use sprintf (on an extra buffer).

Yodhe23

Thanks for the heads-up about "itoa" and "sprintf", have duly changed my code and coding habits.

Thread #608396. Printed from Allegro.cc