Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » char in a for loop

This thread is locked; no one can reply to it. rss feed Print
char in a for loop
NickyP
Member #15,390
November 2013

Hello guys.

Well I was figuring out how can I print multiple chars (array) in an al_draw_textf.

So I got this:

#SelectExpand
1char text[] = "Hello World!"; 2int size = strlen(text);

My char and a size var to hold the size of the array, now the for loop:

#SelectExpand
1for(int i = 0; i < size; i++) 2{ 3 al_draw_textf(font, al_map_rgb(255,255,255), 10 + (i * 10), 20, 0, "%c", text[i], size); 4}

Everything seems fine but I get a disorder of my letters, and I get the following on my screen:

{"name":"0k9y.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/f\/af1e63049791db4f60d261d41cc3e935.png","w":650,"h":514,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/f\/af1e63049791db4f60d261d41cc3e935"}0k9y.png

So my question is: Is there a way to draw text to the screen from a char a better way?? Or how can I improve this one?

Thanks

taron 
Member #10,584
January 2009
avatar

Any specific reason you don't simple use:
al_draw_textf(font, al_map_rgb(255, 255, 255), 10, 20, 0, "%s", text[i]);

Or al_draw_text without format?

LennyLen
Member #5,313
December 2004
avatar

Is there any particular reason why you're not just using:

al_draw_textf(font, al_map_rgb(255,255,255), 10 + (i * 10), 20, 0, "%s", text);

If you do want to draw individual characters after each other, you need to use al_get_text_width to calculate the width of each character so you know where to draw the next one.

NickyP
Member #15,390
November 2013

Ohh thank you, I forgot about the %s

Thank you guys :)

Chris Katko
Member #1,881
January 2002
avatar

NickyP said:

Everything seems fine but I get a disorder of my letters, and I get the following on my screen:

The actual disorder you're getting is that you're monospacing a font that's not supposed to be monospaced. So you would have to use a higher spacing than 10 pixels in that case so "wide" letters like 'e' won't cross into the other letters, while the "thin" letters will seem like there's too much space between them.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Go to: