Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » drawing integer values...

Credits go to Arthur Kalliokoski and SiegeLord for helping out!
This thread is locked; no one can reply to it. rss feed Print
drawing integer values...
xirb22
Member #13,917
January 2012

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

TIA ;D

Arthur Kalliokoski
Second in Command
February 2005
avatar

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

#SelectExpand
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 .

They all watch too much MSNBC... they get ideas.

xirb22
Member #13,917
January 2012

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

SiegeLord
Member #7,827
October 2006
avatar

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.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

xirb22
Member #13,917
January 2012

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

Go to: