Sign in to follow this  
Sync Views

Rendering to a surface, rather than screen

Recommended Posts

Using Direct3D9, how can I render to some kind of surface/texture, and then draw that. I looked at useing D3DDevice->CreateTexture and useing the render target useage so I can set it as a render target, but then it has to by a dynamic texture, which must be recreated for a lost device :( I want to create quite a large number of textures (eg, merging some things into a sprite sheet, like a ship plus whatever turret images it uses), so reduce the number of texture changes. So What I want to do it: Create a surface/texture for use as a render target Render to it Convert it to a texture with D3DPOOL_MANAGED, so I don't ned to recreate it after a lost device.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sync Views
I want to create quite a large number of textures (eg, merging some things into a sprite sheet, like a ship plus whatever turret images it uses)


If you really want to "make a sprite sheet", then do that ahead of time. E.g. with an image editor.

If you want to combine a few images before blitting to screen to save time, well, chances are you won't really save time that way. After all, you still have to blit to the surface...

Share this post


Link to post
Share on other sites
Making them in advance insn't really an option, one other problem is that when ramdomly tiling something, I have to draw every single 32*32 square every frame...So I want to make these into a texture on level load (which is when the random tileing is determined, and placed ina n array anyways), so that I only have a single texture to render...

I'll take a look at lock rect, Is there no built in function from copying from one type to another though?

Share this post


Link to post
Share on other sites
seem to have hit a wall :( "Textures created with D3DPOOL_DEFAULT are not lockable", "The new render-target surface must have at least D3DUSAGE_RENDERTARGET specified.", "D3DUSAGE_RENDERTARGET can only be used with D3DPOOL_DEFAULT." So copying the bits from the render target, to a managed texture seems to not be possible, but I really don't want to render like 48*48 tiles every frame for the background, or have to recreate it on a lost device...

{
IDirect3DSurface9 *RenderTarget=0, *TileTmpSurface=0;
IDirect3DTexture9 *TilesTmp=0;

core->GetD3dDevice()->CreateTexture(
(unsigned)(core->GetWidth ()+1)*32,
(unsigned)(core->GetHeight()+1)*32,
NULL, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT,
&TilesTmp, NULL);
//set target
TilesTmp->GetSurfaceLevel(0, &TileTmpSurface);
core->GetD3dDevice()->GetRenderTarget(0, &RenderTarget);
core->GetD3dDevice()->SetRenderTarget(0, TileTmpSurface);
TileTmpSurface->Release();

//draw background
for(int y=0;y<(int)core->GetHeight()+1;++y)
for(int x=0;x<(int)core->GetWidth()+1;++x)
core->DrawQuad(rand()%6+2, Tile, (float)x*32,(float)y*32, 32,32, 0);
//restore render target
core->GetD3dDevice()->SetRenderTarget(0, RenderTarget);

//create background texture...
core->GetD3dDevice()->CreateTexture(
(unsigned)(core->GetWidth ()+1)*32,
(unsigned)(core->GetHeight()+1)*32,
NULL, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,
&BackGround, NULL);
//copy from tmp to background
//...ok..what now...

//release memory
TilesTmp->Release();
}

Share this post


Link to post
Share on other sites
ok I think i figured it out...So this should make me a managed texture with random tiling...

IDirect3DSurface9 *RenderTarget=0, *TilesTmpSurface=0;
IDirect3DTexture9 *TilesTmp=0;

core->GetD3dDevice()->CreateTexture(
(unsigned)core->GetWidth(),(unsigned)core->GetHeight(),
NULL, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT,
&TilesTmp, NULL);
//set render target
TilesTmp->GetSurfaceLevel(0, &TilesTmpSurface);
core->GetD3dDevice()->GetRenderTarget(0, &RenderTarget);
core->GetD3dDevice()->SetRenderTarget(0, TilesTmpSurface);

//draw background to tmp
core->GetD3dDevice()->BeginScene();
for(int y=0;y<(int)core->GetHeight()/32+1;++y)
for(int x=0;x<(int)core->GetWidth()/32+1;++x)
//DrawQuad( colID, texture, x,y, width,height, rotationZ)
//colIds are used because colours currently have to be defined in the vertex buffer... white,gray,red,blue,green,yellow,oranage,cyan. the position arguments are used to set the world transform matrix
core->DrawQuad(rand()%6+2, Tile, (float)x*32,(float)y*32, 32,32, 0);

//restore render target
core->GetD3dDevice()->EndScene();
core->GetD3dDevice()->SetRenderTarget(0, RenderTarget);
RenderTarget->Release();

//create a blank background texture...
core->GetD3dDevice()->CreateTexture(
(unsigned)core->GetWidth (),(unsigned)core->GetHeight(),
NULL, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,
&BackGround, NULL);

//copy from tmp to background
BackGround->GetSurfaceLevel(0,&BackGroundSurface);
BackGroundSurface=0;
D3DXLoadSurfaceFromSurface(
BackGroundSurface,NULL,NULL,
TilesTmpSurface, NULL, NULL,
D3DX_DEFAULT, 0);
BackGroundSurface->Release();

//cleanup, release memory
TilesTmpSurface->Release();
TilesTmp->Release();

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this