progress on DX9 isometric engine using direct3D

Started by
0 comments, last by DarkSaint 20 years, 5 months ago
I just started on an isometric engine using DX9 at the beginning of this week, I just finished implementing lights and next I''m gonna finish the scrolling. Right now it''s 3D although it''s gonna be represented in a 2d fashion. What I want to know is if my rendering function is good right now or can it be optimized? here''s my maptile structure


struct BIGMAPTILE
{
    D3DXVECTOR3 position; // The 3D position for the vertex

    D3DXVECTOR3 normal;   // The surface normal for the vertex


#ifndef SHOW_HOW_TO_USE_TCI
	FLOAT tu,tv;
#endif
};
and my map structure


struct MAPSTRUCTURE{

	short TileID;
	LPDIRECT3DTEXTURE9 pTexture;
	short light[2]; //light[0] is used to determine whether or not a tile is lit while light[1] is the light type.


};

and other variables used

float const TileX = 128; //width of tiles in worldcoords

float const TileY = 64; //height of tiles in worldcoords


short ScreenTilesX = 8;
short ScreenTilesY = 20;
Here''s my rendering function


void RenderTiles (short CrntRndrLocX, short CrntRndrLocY)
{

	short ScrlPosX = 0;
	short ScrlPosY = 0;
	short CrntTileX = CrntRndrLocX;
	short CrntTileY = CrntRndrLocY;
	short CrntLayer = 0;
	short MapLayers = 2;
	BIGMAPTILE vertices[4];

	// Move to next column of tiles once the screen has scrolled a full tile width

	if(ScrlPosX <= -128)
	{
		CrntTileX += 1;
		ScrlPosX = 0;
	}

	for (int j = 0; j < 128; j++)
	{

		for (int i = 0; i < 128; i++)
	{
		map[2][i][j].pTexture = g_pTexture;

	}

	}

		// makes sure rendering never happens outside of map boundaries. solid numbers

		// need to be replaced with map width/hieght variables.

		// Start

		if (CrntTileX > 120)
		{
			CrntTileX = 120;
		}
		if (CrntTileX < 0)
		{
			CrntTileX = 0;
		}
		if (CrntTileY > 108)
		{
			CrntTileY = 108;
		}
		if (CrntTileY < 0)
		{
			CrntTileY = 0;
		}
		// End


	// Enter the tile Draw loop.

	for (int L = 0; L <= MapLayers; L++)
	{

		float Vert1PosX = (-512.0f + ScrlPosX);
		float Vert1PosY = (320.0f  + ScrlPosY);
		float Vert2PosX = (-448.0f + ScrlPosX);
		float Vert2PosY = (352.0f  + ScrlPosY);
		float Vert3PosX = (-448.0f + ScrlPosX);
		float Vert3PosY = (288.0f  + ScrlPosY);
		float Vert4PosX = (-384.0f + ScrlPosX);
		float Vert4PosY = (320.0f  + ScrlPosY);

	for (int Y = 0; Y < ScreenTilesY; Y++)
	{

	// Draw 1 row of tiles across the screen. Starts at outside leftside of the screen and ends

	// at the right, outside the screen.

	for (int X = 0; X < ScreenTilesX; X++)
	{

			// dapply a texture to the cuurent tile depending on what the tiles pTexture holds

			g_pd3dDevice->SetTexture( 0, map[CrntLayer][CrntTileX][CrntTileY].pTexture );
			g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
			g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
			g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
			g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );

			//Increase tile position along the x axis and declare image coords

			vertices[0].position = D3DXVECTOR3 (Vert1PosX += TileX, Vert1PosY, 0.5f);
			vertices[0].normal   = D3DXVECTOR3 (Vert1PosX ,Vert1PosY, -1.0f);
			vertices[0].tu       = 0;
			vertices[0].tv       = 1;

			vertices[1].position = D3DXVECTOR3 (Vert2PosX += TileX,Vert2PosY, 0.5f);
			vertices[1].normal   = D3DXVECTOR3 (Vert2PosX, Vert2PosY, 1.0f);
			vertices[1].tu       = 0;
		    vertices[1].tv       = 0;


			vertices[2].position = D3DXVECTOR3 (Vert3PosX += TileX,Vert3PosY, 0.5f);
			vertices[2].normal   = D3DXVECTOR3 (Vert3PosX, Vert3PosY , 1.0f);
			vertices[2].tu       = 1;
			vertices[2].tv       = 1;

			vertices[3].position = D3DXVECTOR3 (Vert4PosX += TileX,Vert4PosY, 0.5f);
			vertices[3].normal   = D3DXVECTOR3 (Vert4PosX, Vert4PosY, 1.0f);
			vertices[3].tu       = 1;
			vertices[3].tv       = 0;


			// Creates a light at a tile if the specified tile contains a light.

			// After the light is created light[0] is set to 0 to avoid creating multiple

			// lights in the same spot when the loop comes back around.

			if(map[CrntLayer][CrntTileX][CrntTileY].light[0] == 1)
			{

				CreateTileLt(Vert1PosX + 64, Vert1PosY, 10.0f, map[CrntLayer][CrntTileX][CrntTileY].light[1]);
				map[CrntLayer][CrntTileX][CrntTileY].light[0] = 0;

			}
    
    // Now we fill the vertex buffer. To do this, we need to Lock() the VB to

    // gain access to the vertices. This mechanism is required becuase vertex

    // buffers may be in device memory.


    VOID* pVertices;
    ( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) );
    memcpy( pVertices, vertices, sizeof(vertices) );
    g_pVB->Unlock();

        // Draw the triangles in the vertex buffer. This is broken into a few

        // steps. We are passing the vertices down a "stream", so first we need

        // to specify the source of that stream, which is our vertex buffer. Then

        // we need to let D3D know what vertex shader to use. Full, custom vertex

        // shaders are an advanced topic, but in most cases the vertex shader is

        // just the FVF, so that D3D knows what type of vertices we are dealing

        // with. Finally, we call DrawPrimitive() which does the actual rendering

        // of our geometry (in this case, just one triangle).

        g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(BIGMAPTILE) );
        g_pd3dDevice->SetFVF( D3DFVF_BIGMAPTILE );
        g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
		CrntTileX += 1;
	}

	// Shift the vertices back to outside of the left of the screen in order to draw the next row of tiles

	if ( 1 == CrntTileY % 2)
	{

		
		Vert1PosX -= TileX * ScreenTilesX -64.0f;
		Vert2PosX -= TileX * ScreenTilesX -64.0f;
		Vert3PosX -= TileX * ScreenTilesX -64.0f;
		Vert4PosX -= TileX * ScreenTilesX -64.0f;

	}
	else
	{
		Vert1PosX -= TileX * ScreenTilesX +64.0f;
		Vert2PosX -= TileX * ScreenTilesX +64.0f;
		Vert3PosX -= TileX * ScreenTilesX +64.0f;
		Vert4PosX -= TileX * ScreenTilesX +64.0f;
	}




	// Shift vertices down the screen half a tile width.

	Vert1PosY -= 32.0f;
	Vert2PosY -= 32.0f;
	Vert3PosY -= 32.0f;
	Vert4PosY -= 32.0f;

	//Move to the next row of tiles

	CrntTileY += 1;
	CrntTileX -= ScreenTilesX;
	}

	//Shift back up to the top row and move to the next layer

	CrntTileY -= ScreenTilesY;
	CrntLayer += 1;
	}

}

Advertisement
Well this is not exactly an optimization thing, but something you should definitly think about nonetheless.

You''ve got quite a lot of magic numbers there. I would not even want think about changing the tile size. You should simplify this to be scaleable. When you improve your engine you may want to show higher-res tiles and you''ll be in quite a bit of trouble there.

This topic is closed to new replies.

Advertisement