Archived

This topic is now archived and is closed to further replies.

DarkSaint

map drawing problem

Recommended Posts

I''m working on creating a tile rendering function at the moment with directx 9. The problem I''m having now is figuring out how to render tiles based on what location they are at on the map. Right now my tile function just shifts vertices and renders 6 640 pixel wide tiles across the screen. My tile draw starts at the top left and then draws six tiles across then moves to the next line and repeats six times. Variable Defs used short ScreenTilesX = 6; short ScreenTilesY = 6; Here''s my map structure struct MAPSTRUCTURE{ short TileID; LPDIRECT3DTEXTURE9 pTexture; }; Here''s my tile drawing function.

void RenderTiles (void)
{

	short MapX = 0;
	short MapY = 0;
	float Vert1PosX = -640.0f;
	float Vert1PosY = 0.0f;
	float Vert2PosX = -320.0f;
	float Vert2PosY = -160.0f;
	float Vert3PosX = -320.0f;
	float Vert3PosY = 160.0f;
	float Vert4PosX = 0.0f;
	float Vert4PosY = 0.0f;

	// Enter the tile Draw loop.

	while (MapY < ScreenTilesY)
	{
		MapX = 0;

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

	// at the right, outside the screen.

	while (MapX < ScreenTilesX)
	{

		// determine what texture to apply to the current tile.

			g_pd3dDevice->SetTexture( 0, map[?][?][?].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

	BIGMAPTILE vertices[] =
		{
			{ Vert1PosX += BigTileX, Vert1PosY, 0.5f, 1.0f, 0xf0f0f0f0, 0, 1, }, // x, y, z, rhw, color

			{ Vert2PosX += BigTileX, Vert2PosY, 0.5f, 1.0f, 0xf0f0f0f0, 0, 0, },
			{ Vert3PosX += BigTileX, Vert3PosY, 0.5f, 1.0f, 0xf0f0f0f0, 1, 1, },
			{ Vert4PosX += BigTileX, Vert4PosY, 0.5f, 1.0f, 0xf0f0f0f0, 1,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 );
		MapX++;
	}

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

	Vert1PosX -= BigTileX * ScreenTilesX +320.0f;
	Vert2PosX -= BigTileX * ScreenTilesX +320.0f;
	Vert3PosX -= BigTileX * ScreenTilesX +320.0f;
	Vert4PosX -= BigTileX * ScreenTilesX +320.0f;


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

	Vert1PosY += 160.0f;
	Vert2PosY += 160.0f;
	Vert3PosY += 160.0f;
	Vert4PosY += 160.0f;


	MapY++;
	}
}

In order for the renderer to render different tiles with their according textures I set the texture with map[][][].pTexture but what variable would I need to place in the element spaces to get the map to render the tiles correctly? Is there anyone that can help me modify this loop/structure so that it renders tiles depeing on where the screen is on the map array? Thanks

Share this post


Link to post
Share on other sites
Think I figured it out. Here's my modified rendering function



void RenderTiles (void)
{

float Vert1PosX = -640.0f;
float Vert1PosY = 0.0f;
float Vert2PosX = -320.0f;
float Vert2PosY = -160.0f;
float Vert3PosX = -320.0f;
float Vert3PosY = 160.0f;
float Vert4PosX = 0.0f;
float Vert4PosY = 0.0f;
short CrntTileX = 0;
short CrntTileY = 0;

// Enter the tile Draw loop.

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++)
{

// determine what texture to apply to the current tile.

g_pd3dDevice->SetTexture( 0, map[0][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

BIGMAPTILE vertices[] =
{
{ Vert1PosX += BigTileX, Vert1PosY, 0.5f, 1.0f, 0xf0f0f0f0, 0, 1, }, // x, y, z, rhw, color

{ Vert2PosX += BigTileX, Vert2PosY, 0.5f, 1.0f, 0xf0f0f0f0, 0, 0, },
{ Vert3PosX += BigTileX, Vert3PosY, 0.5f, 1.0f, 0xf0f0f0f0, 1, 1, },
{ Vert4PosX += BigTileX, Vert4PosY, 0.5f, 1.0f, 0xf0f0f0f0, 1,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

Vert1PosX -= BigTileX * ScreenTilesX +320.0f;
Vert2PosX -= BigTileX * ScreenTilesX +320.0f;
Vert3PosX -= BigTileX * ScreenTilesX +320.0f;
Vert4PosX -= BigTileX * ScreenTilesX +320.0f;


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

Vert1PosY += 160.0f;
Vert2PosY += 160.0f;
Vert3PosY += 160.0f;
Vert4PosY += 160.0f;


CrntTileY += 1;
CrntTileX -= 6;
}

}



now I would just have to create a function to get the current location on the map and assign those to CrntTileX and CrntTileY instead of just having them at 0 which here is for testing perposes. This code renders all tiles depending on what pTexture value is at the current location in the array. Right now it also only draws one layer. I gonna work on making it draw multiple layers depending on how many layers a map has, but before I do that I'm gonna play some video games to make up for all the work I just did.

[edited by - DarkSaint on October 21, 2003 11:49:41 PM]

Share this post


Link to post
Share on other sites