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

Started by
2 comments, last by cpr007 15 years, 11 months ago
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?
Advertisement
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?
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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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

This topic is closed to new replies.

Advertisement