char in a for loop
NickyP

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Ā 

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

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

Ohh thank you, I forgot about the %s

Thank you guys :)

Chris Katko
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.

Thread #613676. Printed from Allegro.cc