motorollin
Member
- Joined
- Jul 31, 2007
- Messages
- 163
I have a sprite called "font" which contains 10 items: the numbers 0-9. If I want to draw a number on the screen I use fontClips[n], where n is the number I want from 0-9. Before anybody asks why I don't just render the text using a TTF font, the font I am using is commercial so I can't include it with my game. I have therefore converted only the characters I need to an image to use as a sprite.
My game's score obviously needs to be stored as an integer so I can add to it. When I want to draw the score on the screen, I have to break the integer up in to individual numbers. Then I need to convert that character back to an integer to use it in the apply_surface command to place the correct digit.
For some reason the code below doesn't work. I know the integer is successfully converted to scorestr. And I know the apply_surface line works as I tried changing it to fontClips[0] instead of fontClips and it drew zeroes in place of each number of the score. So the problem seems to be with either getting the character from scorestr, or converting the character back to an integer. Can anyone help?
CODE
//Set a test score
int score=123;
//Convert the score to a string "scorestr"
stringstream ss;
string scorestr;
ss << score;
ss >> scorestr;
//Find out where to start placing the numbers
int nums = scorestr.length();
int scorewidth = nums*12;
int scorestartpos = (SCREEN_WIDTH/2)-(scorewidth/2);
//For each character in the score...
for ( int i = 0; i < nums; i++ )
{
//Convert this char back to an integer
int s=scorestr+0;
//Show the correct sprite
apply_surface( scorestartpos+(i*12), 10, font, screen, &fontClips[ s ] );
}
My game's score obviously needs to be stored as an integer so I can add to it. When I want to draw the score on the screen, I have to break the integer up in to individual numbers. Then I need to convert that character back to an integer to use it in the apply_surface command to place the correct digit.
For some reason the code below doesn't work. I know the integer is successfully converted to scorestr. And I know the apply_surface line works as I tried changing it to fontClips[0] instead of fontClips
CODE
//Set a test score
int score=123;
//Convert the score to a string "scorestr"
stringstream ss;
string scorestr;
ss << score;
ss >> scorestr;
//Find out where to start placing the numbers
int nums = scorestr.length();
int scorewidth = nums*12;
int scorestartpos = (SCREEN_WIDTH/2)-(scorewidth/2);
//For each character in the score...
for ( int i = 0; i < nums; i++ )
{
//Convert this char back to an integer
int s=scorestr+0;
//Show the correct sprite
apply_surface( scorestartpos+(i*12), 10, font, screen, &fontClips[ s ] );
}