Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

gameprogrammerwiz

displaying numbers

This topic is 6930 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i''m using directx to display text in my game. now, i can display text (char*) just fine, but when i try to display numbers (int) it wont work. anyone know how i could do this? here is my current text display code: void drawtext(int msgx, int msgy, char* msgstr); void drawtext(int msgx, int msgy, char* msgstr) { HDC hdc; lpDDSPrimary-> GetDC(&hdc); SetBkMode (hdc, TRANSPARENT); SetTextColor (hdc, RGB(0, 255, 0)); TextOut (hdc, msgx, msgy, msgstr, strlen(msgstr)); lpDDSPrimary->ReleaseDC (hdc); } to use this prototype: drawtext(640, 355, "HP:"); but i want to display the numbers inside of a int instead of displaying characters inside of a string. something like: int player_hp; drawnumber(640, 355, player_hp); thanks

Share this post


Link to post
Share on other sites
Advertisement
You will need to convert the integer to a string. I think you can use the itoa() function. (I''m having trouble with MSDN Locking up my computer). If I remember correctly itoa takes 3 parameters the first is a char array. The next is the integer to copy into it, then finally the number of characters to copy. You would use it like this:

char hitpoints[10];
// Copies the integer player_hp into the char hitpoints
itoa(hitpoints,player_hp,10);

I''m am fairly sure this is correct, but you may want to check the itoa function in the MSDN library (if you have it).

Share this post


Link to post
Share on other sites
I''ve just installed MSDN on my brother''s PC and I found that I had the integer and the char the wrong way round. It should have been:

int player_hp; // This is your value to display
char hitpoints[10]; // Temporary char array
itoa(player_hp,hitpoints,10);

You can then use the hitpoints variable in your draw text function. You will need to include stdlib.h to use the itoa() function.

Share this post


Link to post
Share on other sites
ok, i got you''re second post. this time, the program compiled without error, however the program did have an error as fallows:
"The value of ESP was not properly saved across a function call"

i did remember to add the include file, btw.

Share this post


Link to post
Share on other sites
whoops, forgot to put the int player_hp; line within my function brackets. ;] now i have yet ANOTHER problem. it displays the value of player_hp at 10201, when i set it to just 100 in my code.

Share this post


Link to post
Share on other sites
I only put the "int player_hp;" in my code to show that there was such a variable. You should leave your drawtext function as it was in your first post. If you have a number that you want displaying e.g. player_hp you would call drawtext like this:

char playerhit[10];
itoa(player_hp,playerhit,10)
drawtext(640,355,playerhit);

Share this post


Link to post
Share on other sites
If you you put this code in to your main loop what does the program do:

int number = 1000;
char buffer[4];
itoa(number,buffer,4);
drawtext(300,300,buffer);

If this doesn''t display 1000 on your screen you can e-mail me your code and I would be happy to look through it.
jonathanhobson@redhotant.com

Share this post


Link to post
Share on other sites
I looked through you code and there wasn''t anything wrong. So I looked at itoa in MSDN again and the last number isn''t the number of characters to copy its the radix! So 10201 is 100 in base 3 (Trinary?:-)). So if you change your code to itoa(temp_hp,buffer,10); You should get the number printed in base 10. Sorry for the mixup.

Hope this helped!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!