Skip to main content
gamedev.net
🔒 Locked

Tile Engine

Started by Bimble Bob Apr 8, 2006 at 8:17 AM 1 replies 650+ views
Original Post
Bimble Bob
Bimble Bob
I recently re-coded my Tile Engine so its faster as it draws thousands of polygons on the screen at once rather than two at a time. But I've run into problems. I updated my Layer class which contains the code to Add a Tile to the layers array:

int LAYER::AppendTile(float x, float y, float u, float v)
{
	//Define the Tiles Co-ordinates in model space
	Tile[0][0] = 0.0f;
	Tile[0][1] =  0.0f;
	Tile[0][2] = 0.0f;
	Tile[0][3] =  0.0f;
	Tile[0][4] = 0.0f;

	Tile[1][0] = 1.0f;
	Tile[1][1] =  0.0f;
	Tile[1][2] = 0.0f;
	Tile[1][3] =  u;
	Tile[1][4] = 0.0f;

	Tile[2][0] = 0.0f;
	Tile[2][1] =  -1.0f;
	Tile[2][2] = 0.0f;
	Tile[2][3] =  0.0f;
	Tile[2][4] = v;

	Tile[3][0] = 1.0f;
	Tile[3][1] =  0.0f;
	Tile[3][2] = 0.0f;
	Tile[3][3] =  u;
	Tile[3][4] = 0.0f;

	Tile[4][0] = 1.0f;
	Tile[4][1] =  -1.0f;
	Tile[4][2] = 0.0f;
	Tile[4][3] = u;
	Tile[4][4] = v;

	Tile[5][0] = 0.0f;
	Tile[5][1] =  -1.0f;
	Tile[5][2] = 0.0f;
	Tile[5][3] =  0.0f;
	Tile[5][4] = v;

	
	//Convert the model space Co-ordinates into world Co-ordinates by transforming the
	//world matrix using the X and Y values given by the function
	D3DXMatrixTranslation(&WorldMat, x, y, 0.0f);
	d3ddevice->SetTransform(D3DTS_WORLD, &WorldMat);
	//This is the actual Co-ordinate conversion
	D3DXVec3TransformCoord(&WTiles[numtiles][0], &D3DXVECTOR3(Tile[0][0], Tile[0][1], Tile[0][2]), &WorldMat);
	D3DXVec3TransformCoord(&WTiles[numtiles][1], &D3DXVECTOR3(Tile[1][0], Tile[1][1], Tile[1][2]), &WorldMat);
	D3DXVec3TransformCoord(&WTiles[numtiles][2], &D3DXVECTOR3(Tile[2][0], Tile[2][1], Tile[2][2]), &WorldMat);
	D3DXVec3TransformCoord(&WTiles[numtiles][3], &D3DXVECTOR3(Tile[3][0], Tile[3][1], Tile[3][2]), &WorldMat);
	D3DXVec3TransformCoord(&WTiles[numtiles][4], &D3DXVECTOR3(Tile[4][0], Tile[4][1], Tile[4][2]), &WorldMat);
	D3DXVec3TransformCoord(&WTiles[numtiles][5], &D3DXVECTOR3(Tile[5][0], Tile[5][1], Tile[5][2]), &WorldMat);

	TileVb->Lock(0, 0, &TileVerts, D3DLOCK_NOOVERWRITE);

	memcpy(&TileVerts, &Tile, sizeof(Tile));

	TileVb->Unlock();

	//Increase the Number of tiles in the Layer by one
	numtiles++;
	
	return 1;
}
And then to Render all the Tiles to the screen:

int LAYER::Render()
{
	d3ddevice->SetStreamSource(0, TileVb, 0, sizeof(VTile));
	d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, numtiles*2);
				
	return 1;
} 
But when I put this into practice nothing at all is rendered. I'm sure I coded everything OK but I was wondering if anyone could check if there are any mistakes or anything I've missed out.
It's not a bug... it's a feature!
matches81
matches81
One thing that seems odd to me is your call to SetTransform() in the AppendTile function. Why are you doing this, I don´t see a reason for that?
The next thing is
D3DXVec3TransformCoord(&WTiles[numtiles][0], &D3DXVECTOR3(Tile[0][0], Tile[0][1], Tile[0][2]), &WorldMat);

You are transforming the tile´s position vector by WorldMat and store the result in WTiles. After that you copy Tile into your VB. So you´re not copying the transformed tile into your VB but the untransformed one, which leads me to the question: Why do you do that?

Furthermore I´m pretty sure that your Lock / memcpy / Unlock operations will result in only one tile being stored correctly in the VB, since you´re copying 'Tile' (which is only one tile if I´m correct) into the VB at the same position every time.
Bimble Bob
Bimble Bob
Yeah, I was messing around with it and changed WTiles to Tile for experiments and forgot to change it back. I was transforming the World Matrix because I couldn't work out how to position each tile in a different place if I'm then creating the Tiles in Model space. And again I was copying Tile into it because of experimentation. If I copied WTiles into it it wouldn't have any UV co-ordinates you see.
It's not a bug... it's a feature!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.