Sign in to follow this  
phelaphant

DirectX tile texture translation issue!

Recommended Posts

Hi all, Im trying to draw to screen an array of tiles, however the translation semms to be off! Its my understanding that when drawing to screen the coordinates (0,0) should represent the top left of the screen, however, when I draw the tiles it starts from the center of the screen even though ive set the first tile to 0,0! Something like this - Image Hosted by ImageShack.us<br/> anyway here is some code:
// Set up the grid positions
	for (int x = 0; x < 12; ++x)
	{
		for (int y = 0; y < 12; ++y)
		{
			mBackCenter[x][y] = D3DXVECTOR3((float)(x + 1) * 64.0f, (float)(y+1) * 64.0f, 256.0f);
		}
	}

void TwoDGraphics::onResetDevice()
{
	HR(mFont->OnResetDevice());
	mGfxStats->onResetDevice();
	HR(mSprite->OnResetDevice());

	// Setup thew camera
	D3DXMATRIX V;
	D3DXVECTOR3 pos(0.0f, 0.0f, -1000.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
	D3DXMatrixLookAtLH(&V, &pos, &target, &up);
	HR(gd3dDevice->SetTransform(D3DTS_VIEW, &V));

	// define what the camera see's
	D3DXMATRIX P;
	RECT R;
	GetClientRect(mhMainWnd, &R);
	float width = (float)R.right;
	float height = (float)R.bottom;
	D3DXMatrixPerspectiveFovLH(&P, D3DX_PI*0.25f, 500/500, 1.0f, 5000.0f);
	HR(gd3dDevice->SetTransform(D3DTS_PROJECTION, &P));
}

void TwoDGraphics::drawScene()
{
	HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));

	HR(gd3dDevice->BeginScene());

	HR(mSprite->Begin(D3DXSPRITE_OBJECTSPACE|D3DXSPRITE_DONOTMODIFY_RENDERSTATE));

	drawBackground();

	mGfxStats->display();
	HR(mSprite->End())
	HR(gd3dDevice->EndScene());
	HR(gd3dDevice->Present(0, 0, 0, 0));
}

void TwoDGraphics::drawBackground()
{
	// position and size the backgroiund
	D3DXMATRIX T, S;
	D3DXMatrixTranslation(&T, 0.0f, 0.0f, 0.0f);
	D3DXMatrixScaling(&S, 1.0f, 1.0f, 0.0f);
	HR(mSprite->SetTransform(&(S*T)));

	// Draw the background
	for (int x = 0; x < 12; ++x)
	{
		for (int y = 0; y < 12; ++y)
		{

			RECT rec;
			rec.left = x * 64;
			rec.right = rec.left + 64;
			rec.top = y * 64;
			rec.bottom = rec.top + 64;
			
			char intX[512];
			sprintf(intX,"%d",x);

			char intY[512];
			sprintf(intY,"%d",y);

			mFont->DrawText(0, _T(intY), -1, 
				&rec, DT_CENTER | DT_VCENTER, 
				D3DCOLOR_XRGB(256, 0, 256));

			HR(mSprite->Draw(mBackTex, NULL, NULL, &mBackCenter[x][y], D3DCOLOR_XRGB(255, 255, 255)));
		}
	}
	HR(mSprite->Flush());
}
I think its a matrix translation issue but I just cant seem to get my head round it! Any help is much appreciated!

Share this post


Link to post
Share on other sites

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