drawing integer values...

I want to draw my score to my display, but al_draw_text() only allows the drawing of constant pointer-to-char so it's impossible to draw something like an unsigned int score, even with itoa().

Is there a way to still do this? It's probably something real simple that I'm just overlooking xD


Arthur Kalliokoski

A couple minutes of poking around in the examples found this in ex_membmp.c.

1static void print(ALLEGRO_FONT *myfont, char *message, int x, int y) 2{ 3 al_draw_text(myfont, al_map_rgb(0, 0, 0), x+2, y+2, 0, message); 4 al_draw_text(myfont, al_map_rgb(255, 255, 255), x, y, 0, message); 5} 6 7 . 8 . 9 . 10 print(font, message, 0, 0); 11 sprintf(second_line, "%.1f FPS", fps); 12 print(font, second_line, 0, al_get_font_line_height(font)+5); 13 . 14 . 15 .


I never really learned C but went straight to C++, so I am a bit confused with the C parts of this code.
I see that print() writes a shadowed text to the screen, but it still really used al_draw_text() to do it and so it still requires a *char.

I am guessing sprintf() is used to print fps into second line and so converts it into a string? If so I'll read up on sprintf() to see how it works so I can use this myself.

Thanks for the help bro ;D


How about al_draw_textf? It'll take out the middle man, allowing you to pass the format string directly to the Allegro function. Look up a printf tutorial on the Internets to figure out how to use format strings (will help with sprintf too, if you end up needing it someday).

Also... you can use std::stringstream alongside al_draw_text for a more... C++... solution.


al_draw_textf() works perfectly!
I looked up some stuff about printf() and just using "%d" as the format works!
Thanks a lot everyone :)

Thread #609848. Printed from Allegro.cc