Archived

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

supagu

screen shot code

Recommended Posts

supagu    148
im trying to take screen shots in my engine, it works fine fullscreen, but in window mode it doesnt work: [cpp] if( SUCCEEDED( GetDevice()->GetDevice()->GetBackBuffer(0,0, D3DBACKBUFFER_TYPE_MONO, &src) ) ) { retval = D3DXSaveSurfaceToFile(buffer, D3DXIFF_BMP, src, NULL,NULL); //retval = D3DXSaveSurfaceToFile(buffer, D3DXIFF_JPG, src, NULL,NULL); } else { ENGINE.GetConsole()->Error( __FILE__, __LINE__, "Failed to take screen shot"); } [/cpp] also you can see the line commented out, i would like to to save the image as a jpg if possible, but when i open it the dimensions are wrong, as the bottom part of the image is missing. any ideas?

Share this post


Link to post
Share on other sites
Ataru    133
Here is my code, hope this helps:


HRESULT MYCLASS::saveScreenShot(char *fileName, D3DXIMAGE_FILEFORMAT format)
{

HRESULT hr;
LPDIRECT3DSURFACE9 buffer = NULL; //The front buffer used to get the screenshot


// Create a surface for the screenshot to be rendered to

if( FAILED( hr = g_pDevice->CreateOffscreenPlainSurface( screenSizeX, screenSizeY,
D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &buffer, NULL ) ) )
return hr;



// Write the front buffer to the surface

if (!fullScreen)
{
if (FAILED (hr = g_pDevice->GetRenderTarget( 0, &buffer )))
{
gLOG("ERROR FRONT BUFFER AQUASITION FAILED\n");
//do error handling etc...

buffer->Release();
return hr;
}
}
else
{
if( FAILED( hr = g_pDevice->GetFrontBufferData( 0, buffer ) ) )
{
gLOG("ERROR FRONT BUFFER AQUASITION FAILED\n");
//do error handling etc...

buffer->Release();
return hr;
}
}


D3DXSaveSurfaceToFile( fileName, format, buffer, NULL, NULL );

// Cleanup

buffer->Release();


return D3D_OK;
}


[edited by - ataru on January 6, 2004 11:17:15 AM]

Share this post


Link to post
Share on other sites
FenrirWolf    288
Supagu, JPG saving is definately broken in the DX9 SDK. I''m not sure if they fixed that in the winter/summer update that just happened not all that long ago.

I''d save it as a TGA or BMP, and then use __________ to convert it to JPEG. Can''t trust DX to do a good job of compressing the files, anyway. Doing it by hand I can generally get a %20-%30 space savings. (And if that blank equals Photoshop, you can batch this in order to process large runs of screenshots.)

---
[[ Gaping Wolf Software ]] [[ GameGenesis Forums ]]

Share this post


Link to post
Share on other sites
supagu    148
okay, bmp is fine.

i tried your code ataru, but the code still failes on D3DXSaveSurfaceToFile, i tried changing D3DFMT_A8R8G8B8 to D3DFMT_R8G8B8 which would be more suited to bitmaps, but still crashes :-/

would be nice to be able to output the stencil buffer to a file also for debugging

[edited by - supagu on January 6, 2004 10:49:28 PM]

Share this post


Link to post
Share on other sites
__Daedalus__    480
quote:
would be nice to be able to output the stencil buffer to a file also for debugging


You are able to access the stencil buffer with this method:

m_D3DDevice->GetDepthStencilSurface()

Share this post


Link to post
Share on other sites
31337    100
For some reason though I tried to output the stencil buffer to a png file using the same surface to file method as before into png format and it fails when exporting the file. Perhaps this has something to do with your same problem supagu.

Share this post


Link to post
Share on other sites
Ataru    133
Sorry, have not gotten back to this thread for a while. I''ve tested this on windowed and non-windowed modes for .bmp I assumed other formats would work if you change the settings.

Did you the debug output?

Share this post


Link to post
Share on other sites
Cybertron    122
You cant read from the back buffer. There is an IDirect3D function to get the front buffer though. I makes a copy in system memory

Don''t forget to Release() the pointer to the surface when you are done

PNG files are probably the best bet since they have lossless compression and are well accepted

Share this post


Link to post
Share on other sites
supagu    148
HRESULT: 0x8876086c (2289436780)
Name: D3DERR_INVALIDCALL
Description: Invalid call
Severity code: Failed
Facility Code: Unknown (2166)
Error Code: 0x086c (2156)

:-/
hr = D3DXSaveSurfaceToFile( buffer, D3DXIFF_PNG, src, NULL,NULL)

definately that line ^

Share this post


Link to post
Share on other sites
Ataru    133
Use the Debug dll''s and run dbMon maybe that will telll you what the invalid parameters are. That code I posted has one bug, for some reason the release does not really work, and after 100 or so screenshots it fails. I solved it by moving the buffer variable to be a class member, and create it only once. Maybe that will help.

But it looks like you can''t even save 1, so that''s strange. Are you saving as .bmp? That code should work for .bmp.

Make sure you are setting fullscreen to false.

Share this post


Link to post
Share on other sites