void Game::PrintScore()
{
std::ostringstream oss;
oss << Score;
std::string score = oss.str();
oss.flush();
D3DXVECTOR3 numberPos;
numberPos.x=95.0f;
numberPos.y=340.0f;
numberPos.z=0.0f;
for(int i = score.length()-1; i >=0; i--)
{
switch(score)
{
case '0':
g_engine->GetSprite()->Draw(numbers, &number0, NULL, &numberPos, 0xFFFFFFFF);
break;
case '1':
g_engine->GetSprite()->Draw(numbers, &number1, NULL, &numberPos, 0xFFFFFFFF);
break;
case '2':
g_engine->GetSprite()->Draw(numbers, &number2, NULL, &numberPos, 0xFFFFFFFF);
break;
case '3':
g_engine->GetSprite()->Draw(numbers, &number3, NULL, &numberPos, 0xFFFFFFFF);
break;
case '4':
g_engine->GetSprite()->Draw(numbers, &number4, NULL, &numberPos, 0xFFFFFFFF);
break;
case '5':
g_engine->GetSprite()->Draw(numbers, &number5, NULL, &numberPos, 0xFFFFFFFF);
break;
case '6':
g_engine->GetSprite()->Draw(numbers, &number6, NULL, &numberPos, 0xFFFFFFFF);
break;
case '7':
g_engine->GetSprite()->Draw(numbers, &number7, NULL, &numberPos, 0xFFFFFFFF);
break;
case '8':
g_engine->GetSprite()->Draw(numbers, &number8, NULL, &numberPos, 0xFFFFFFFF);
break;
case '9':
g_engine->GetSprite()->Draw(numbers, &number9, NULL, &numberPos, 0xFFFFFFFF);
break;
}//End Switch
//Update numberPos
numberPos.x -= 15.0f;
}//End for loop
}//End function
I need some advice
I am working on a game, and I am to the point where I need to print the score onto the window. My plan was to have an integer Score and convert that to a string. Then cycle through and print the corresponding numbers. I am using the ID3DXSprite interface with DirectX, but I know that isn't the problem. What happens is it prints the 0 when the game starts, then when I score 10 points it still prints 0, but when I score a total of 20 points it prints 20. When I get to 100 it prints 0 again. I really don't understand what I'm doing wrong.
Here is my code
Thanks in advance
Adam
I would start by printing the value of 'Score' and the 'score' string to the console (using std::cout) to see if they're correct, i.e.:
std::cout << Score << std::endl;std::cout << score << std::endl;
That might help you narrow down the problem.
I don't remember exactly, but I think you should do it this way:
std::ostringstream oss;
oss << Score;
std::string score;
oss >> score;
Then as jyk recommends, try checking the content of 'score'.
std::ostringstream oss;
oss << Score;
std::string score;
oss >> score;
Then as jyk recommends, try checking the content of 'score'.
switch(score) { case '0': g_engine->GetSprite()->Draw(numbers, &number0, NULL, &numberPos, 0xFFFFFFFF); break; case '1': g_engine->GetSprite()->Draw(numbers, &number1, NULL, &numberPos, 0xFFFFFFFF); break; case '2': g_engine->GetSprite()->Draw(numbers, &number2, NULL, &numberPos, 0xFFFFFFFF); break; case '3': g_engine->GetSprite()->Draw(numbers, &number3, NULL, &numberPos, 0xFFFFFFFF); break; case '4': g_engine->GetSprite()->Draw(numbers, &number4, NULL, &numberPos, 0xFFFFFFFF); break; case '5': g_engine->GetSprite()->Draw(numbers, &number5, NULL, &numberPos, 0xFFFFFFFF); break; case '6': g_engine->GetSprite()->Draw(numbers, &number6, NULL, &numberPos, 0xFFFFFFFF); break; case '7': g_engine->GetSprite()->Draw(numbers, &number7, NULL, &numberPos, 0xFFFFFFFF); break; case '8': g_engine->GetSprite()->Draw(numbers, &number8, NULL, &numberPos, 0xFFFFFFFF); break; case '9': g_engine->GetSprite()->Draw(numbers, &number9, NULL, &numberPos, 0xFFFFFFFF); break;
I am tearing my eyes out here. This is what arrays are for. Note that '0' through '9' are consecutive numeric values.
Personally I would do this by doing the arithmetic myself, dividing by 10 in a loop:
void Game::PrintScore() { int tmp = Score; D3DXVECTOR3 numberPos; numberPos.x=95.0f; numberPos.y=340.0f; numberPos.z=0.0f; // BTW, I'm already worried about your engine design from seeing this part... Sprite s = g_engine->GetSprite(); // using do-while ensures that a digit is displayed for zero. do { s->Draw(numbers, &number[tmp % 10], NULL, &numberPos, 0xFFFFFFFF); numberPos.x -= 15.0f; tmp /= 10; } while (tmp);}
But that's because last time I had to do this it was in an environment without nice tools like std::stringstream ;)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement