Jump to content
  • Advertisement
Sign in to follow this  
adam23

I need some advice

This topic is 4486 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 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
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

Thanks in advance Adam

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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'.

Share this post


Link to post
Share on other sites

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 ;)

Share this post


Link to post
Share on other sites
Zahlman

Thank you for the tips, I never thought about doing it that way. If you don't mind expanding, what would you do different with my engine design. What my goal was is to create an engine that has only one point of contact g_engine.

Thanks everyone for the tips.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!