Jump to content
  • Advertisement
Sign in to follow this  
giantmung

Rendering Texture as "Background Image" in 2D game

This topic is 4822 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You have to load a texture, and create a vertex buffer.

Then:

device->SetTexture(...);
device->SetStreamSource(...); // set vertex buffer
device->DrawPrimitive(...);

Share this post


Link to post
Share on other sites
well I got one to load, but how do you get it to stretch the whole client area? Is that with the vertex buffer?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
};


Share this post


Link to post
Share on other sites
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 code
struct TLVertex
{
D3DXVECTOR4 p;
DWORD c;
D3DXVECTOR2 t;
};

#define FVF_TLVERTEX ( D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1 )





//Creation code
if( 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 code
pd3dDevice->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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!