Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Drawing string from a variable to the screen?

This thread is locked; no one can reply to it. rss feed Print
Drawing string from a variable to the screen?
j0rdant13
Member #14,974
March 2013

Pretty much what the title says ;D, i need to print the text of my string variable to a position on the screen, i know how to do it with int's etc. Any help would be appreciated (Allegro newbie/gettin there)

:D

al_draw_textf(myFont, al_map_rgb(255,255,255), 0, 260, 0, "Player walking: %std::string", walkString);

Arvidsson
Member #4,603
May 2004

Assuming walkString is a std::string, my guess would be:

al_draw_textf(myFont, al_map_rgb(255,255,255), 0, 260, 0, "Player walking: %s", walkString.c_str());

j0rdant13
Member #14,974
March 2013

wow its that simple haha thankyou! Btw what does the c_str() mean?

pkrcel
Member #14,001
February 2012

That's correct, maybe for future reference keep a tab on the various printf function format specifiers (another link).

EDIT:
c_str() is a std::string method that returns a C-style NULL-terminated string from the container class.

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

j0rdant13
Member #14,974
March 2013

thanks, bookmarked that link, last question, is it bad using this?

LennyLen
Member #5,313
December 2004
avatar

j0rdant13 said:

thanks, bookmarked that link, last question, is it bad using this?

No. It's the best way to use C++ strings with C libraries.

Neil Walker
Member #210
April 2000
avatar

c_str converts a string into a char*, i.e. to something you can use in C.

A typical reason for doing this is to format strings using a stringstream, e.g.

ostringstream os;
os << "Player walking: " << walkString << " their score is " << 25;
string s1=os.str();
al_draw_textf(myFont, al_map_rgb(255,255,255), 0, 260, 0, s1.c_str());

Or words to that effect, I'm typing not compiling :)

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

j0rdant13
Member #14,974
March 2013

Wow thanks for the replies, problem solved and understood :D thanks!

Go to: