Sign in to follow this  

[C++/DirectX] Problems rendering D3DXSPRITES...

This topic is 3490 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 trying to render two sprites... One of them, the player's cursor, is controlled by the mouse. That sprite follows the mouse, no problem. I also have a corner sprite that doesn't move. With this code, it also follows the mouse around the screen, even though it never changes position. The render function:
void Render()
{
        // Start rendering on the backbuffer.
	if (d3ddev->BeginScene())
	{
		// Black background
		d3ddev->ColorFill(backBuffer, NULL, D3DCOLOR_XRGB(0, 0, 0));

		sprite->Begin(D3DXSPRITE_ALPHABLEND);

		D3DXVECTOR2 pos;
		pos.x = redHand.pos.x;
		pos.y = redHand.pos.y;

		D3DXVECTOR2 scaling(1.0f, 1.0f);
		D3DXMATRIX spriteMatrix;
		D3DXMatrixTransformation2D(&spriteMatrix, 0, 0, &scaling, 0, 0, &pos);
		sprite->SetTransform(&spriteMatrix);

		RECT srcRect;

		srcRect.left = 0;
		srcRect.right = 32;
		srcRect.top = 0;
		srcRect.bottom = 32;
		sprite->Draw(playerTexture, &srcRect, NULL, NULL, D3DCOLOR_XRGB(255, 255, 255));
	
		srcRect.left = 0;
		srcRect.right = 200;
		srcRect.top = 0;
		srcRect.bottom = 200;
		sprite->Draw(cornerTexture, &srcRect, NULL, NULL, D3DCOLOR_XRGB(255, 255, 255));

		sprite->End();

		d3ddev->EndScene();
	}

	// Flip back buffer to the screen
	d3ddev->Present(NULL, NULL, NULL, NULL);
}

The update function:
void Update(float delta, HWND hWnd)
{
	PollMouse();

	// Move the hand sprite.
	redHand.pos.x += MouseX();

	if (redHand.pos.x > WINDOW_WIDTH - redHand.width)
	{
		redHand.pos.x = WINDOW_WIDTH - redHand.width;
	}
	else if (redHand.pos.x < 0)
	{
		redHand.pos.x = 0;
	}

	redHand.pos.y += MouseY();

	if (redHand.pos.y > WINDOW_HEIGHT - redHand.height)
	{
		redHand.pos.y = WINDOW_HEIGHT - redHand.height;
	}
	else if (redHand.pos.y < 0)
	{
		redHand.pos.y = 0;
	}

	// Check for escape key to exit.
	if (KEY_DOWN(VK_ESCAPE))
	{
		gameIsRunning = false;
	}
}

As you can see, the Update function never changes the position of the corner sprite. Any ideas why it would be following my mouse around along with the cursor sprite?

Share this post


Link to post
Share on other sites
I think I figured it out. (Or, at least, the program is working as expected now.)

[source code="c"]
D3DXMatrixTransformation2D(&spriteMatrix, 0, 0, &scaling, 0, 0, &pos);




I assume I needed to reset the D3DXMatrixTransformation2D() since pos changed. Is this right?

I feel like I'm doing things the wrong way... Once I'm rendering a bunch of sprites, I'll be resetting the matrix a lot, right? What am I doing wrong?

Edit: It seems like I remember seeing D3DXSPRITE code that didn't even use the 2D transformation but I tried removing it from my code and it stops working. Should I be using it or no?

Share this post


Link to post
Share on other sites
I have applications where the transforms are changed innumerable times.

That's how you tell the device where to render the object.

If the corner sprite never moves, setup a matrix for just that position and reuse it, or use a different sprite.

Share this post


Link to post
Share on other sites
SetTransform sets the transformation matrix for all subsequent Draw() calls for that ID3DXSprite instance. Call it again with an identity matrix before drawing the corner sprite (IE to clear the left over translation that you applied to the cursor).

Insert this after you draw the cursor and before your draw the corner:


D3DXMATRIX idty;
D3DXMatrixIdentity(&idty);
sprite->SetTransform(&idty);

Share this post


Link to post
Share on other sites

This topic is 3490 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this