Rendering Texture as "Background Image" in 2D game

Started by
5 comments, last by giantmung 18 years, 10 months ago
I've searched google for examples of this, it's not as easy as a device->SetTexture after loading from file of course (successfully) device->DrawPrimitive it seems so how would you go about making a texture a background image?
Advertisement
You have to load a texture, and create a vertex buffer.

Then:

device->SetTexture(...);
device->SetStreamSource(...); // set vertex buffer
device->DrawPrimitive(...);
well I got one to load, but how do you get it to stretch the whole client area? Is that with the vertex buffer?
Quote:Original post by giantmung
well I got one to load, but how do you get it to stretch the whole client area? Is that with the vertex buffer?

Yes. The final output will be mapped to wherever you placed the vertices that make up your background area. Simply move them to the four corners of your screen [smile]

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

well, I'm still having a problem with it, it may be my vertex buffer, but it's almost 4 am and I'm blasted - anybody see anything as to why I can only render a square in the middle of the screen?

	Vertex quadVertices[] =	{        {-1.0f, 1.0f, 0.0f,  0.0f, 0.0f },        { 1.0f, 1.0f, 0.0f,  1.0f, 0.0f },        {-1.0f,-1.0f, 0.0f,  0.0f, 1.0f },        { 1.0f,-1.0f, 0.0f,  1.0f, 1.0f }	};    if( FAILED( pd3dDevice->CreateVertexBuffer( 4 * sizeof(Vertex), 0,                                      D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT,                                      &pVertexBuffer, NULL ) ) )	{			return E_FAIL;	}	if( pd3dDevice != NULL )	{		pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,							D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );		pd3dDevice->BeginScene();   pd3dDevice->SetStreamSource( 0, pVertexBuffer, 0, sizeof(Vertex) );    pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );		pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );		pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );		pd3dDevice->SetTexture( 0, pTexture );		pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );#define D3DFVF_CUSTOMVERTEX ( D3DFVF_XYZ | D3DFVF_TEX1 )struct Vertex{	float x;   //xzy cooords	float y;	float z;	float tu;	float tv;};
You never actually copy quadVertices into the pVertexBuffer VB [smile]. Hopefully it isn't something that silly [wink]

You're using transformed geometry; D3DFVF_XYZ; meaning that it'll be affected by whatever transforms you have enabled.

You'll have better luck with some TL geometry:

//Definition codestruct TLVertex    {        D3DXVECTOR4 p;        DWORD c;        D3DXVECTOR2 t;    };#define FVF_TLVERTEX ( D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1 )//Creation codeif( FAILED( pd3dDevice->CreateVertexBuffer( 4 * sizeof(TLVertex), 0, FVF_TLVERTEX, D3DPOOL_DEFAULT, &pVertexBuffer, NULL ) ) )    {        return E_FAIL;    }TLVertex *pVerts;if( FAILED( pVertexBuffer->Lock( 0, reinterpret_cast< void** >( &pVerts ), 0, 0 ) ) )    {        return E_FAIL;    }pVerts[ 0 ].p = D3DXVECTOR4( 0.0f, 0.0f, 1.0f, 1.0f );pVerts[ 1 ].p = D3DXVECTOR4( fWidth, 0.0f, 1.0f, 1.0f );pVerts[ 2 ].p = D3DXVECTOR4( 0.0f, fHeight, 1.0f, 1.0f );pVerts[ 3 ].p = D3DXVECTOR4( fWidth, fHeight, 1.0f, 1.0f );pVerts[ 0 ].c = pVerts[ 1 ].c = pVerts[ 2 ].c = pVerts[ 3 ].c = D3DCOLOR_XRGB( 255, 255, 255 );pVerts[ 0 ].t = D3DXVECTOR2( 0.0f, 0.0f );pVerts[ 1 ].t = D3DXVECTOR2( 1.0f, 0.0f );pVerts[ 2 ].t = D3DXVECTOR2( 0.0f, 1.0f );pVerts[ 3 ].t = D3DXVECTOR2( 1.0f, 1.0f );if( FAILED( pVertexBuffer->Unlock( ) ) )    {        return E_FAIL;    }//Rendering codepd3dDevice->SetStreamSource( 0, pVertexBuffer, 0, sizeof( TLVertex ) );pd3dDevice->SetFVF( FVF_TLVERTEX );//etc...pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );


That's all off the top of my head, so it'll have a few syntax errors [smile]

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

well, I did copy them in, I just didn't post that portion because I thought it was trivial really... thanks for your help :)

This topic is closed to new replies.

Advertisement