Screen capture using DirectX-photos in 3D game scenarios

Source: Internet
Author: User

 

Each DirectX program contains a memory area called a buffer, which stores the video content related to the program, which is called a back buffer in the program ), some programs have more than one background buffer. There is also a buffer. By default, each program can access the front-end buffer. The front-end buffer saves the desktop-related video content, which is essentially a screen image.

Our program can capture the content of the current screen by accessing the front-end buffer. The underlying optimization mechanism of DirectX ensures that our screen capture efficiency is very high, at least higher than that of GDI.

It is very easy to access the front-end buffer in the DirectX program,IDirect3DDevice9The interface providesGetFrontBuffer()Method, it receivesIDirect3DSurface9The object pointer is used as a parameter, and the content of the front-end buffer is copied to thisSurfaceIDirect3DSurfce9Objects can be usedIDirect3DDevice9::CreateImageSurface(). Once the screen content is saved to this surface, we can useD3DXSaveSurfaceToFile()Method to directly Save the content to the diskbmpFile. The sample code is as follows:

Extern idirect3ddevice9 * g_pd3ddevice;

Void capturescreen ()

{

Idirect3dsurface9 * psurface;

G_pd3ddevice à createimagesurface (screenwidth, screenheight,

D3dfmt_a8r8g8b8, & psurface );

G_pd3ddevice-> getfrontbuffer (psurface );

D3dxsavesurfacetofile ("shorttop.bmp", d3dxiff_bmp, psurface,

Null, null );

Psurface-> release ();

}

Above,g_pd3dDeviceIs an initializedIDirect3DDeviceObject. In this example, the captured image is saved to a file. However, we can useIDirect3DSurface9::LockRect()It gives us an executionsurfaceMemory pointer, that is, the captured image data. We can copy the data to the memory defined by the program to operate on it. See the following code:

Extern void * pbits;

Extern idirect3ddevice9 * g_pd3ddevice;

Idirect3dsurface9 * psurface;

G_pd3ddevice à createimagesurface (screenwidth, screenheight,

D3dfmt_a8r8g8b8, & psurface );

G_pd3ddevice-> getfrontbuffer (psurface );

D3dlocked_rect lockedrect;

Psurface à lockrect (& lockedrect, null,

D3dlock_no_dirty_update | d3dlock_nosyslock |

D3dlock_readonly )));

For (INT I = 0; I <screenheight; I ++)

{

Memcpy (byte *) pbits + I * screenwidth * bitsperpixel/8,

(Byte *) lockedrect. pbits + I * lockedrect. pitch,

Screenwidth * bitsperpixel/8 );

}

G_psurface-> unlockrect ();

Psurface-> release ();

 

The above pbits is a void *. please ensure that the Group has enough memory space for it first. Bitsperpixel generally uses a 32-bit color. It also depends on the current configuration of your monitor. Note that the surface width is different from the captured screen width. Because of memory alignment (memory alignment by word is usually at a high access validity rate), the surface may have excess bits at the end of each line to align it to the word boundary.lockedRect.PitchIt provides the number of bytes between the beginning of two consecutive rows. That is to say, we need to move the pointer backward when reading a row.PitchByte insteadWidthBytes. You can use the following code to reverse copysurface

For (INT I = 0; I <screenheight; I ++)

{

Memcpy (byte *) pbits + (screenheight-I-1 )*

Screenwidth * bitsperpixel/8,

(Byte *) lockedrect. pbits + I * lockedrect. pitch,

Screenwidth * bitsperpixel/8 );

}

This is useful for top-down bitmap to bottom-up bitmap.

 

We can also useIDirect3DSurface9OfGetDC()Method acquisitionDirectX surfaceOfGDICompatibleDCAnd then copy its content to our compatibilityDC. If you are usingDirectX9Try it.

 

Finally, note that:FrontBufferIs a relatively slow operation, the design is so, so it should be avoided in the efficiency-critical program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.