user11962394
user11962394

Reputation: 33

How do I copy DirectX texture to a surface?

I'm trying to capture whole desktop screen (front buffer) and add a logo and caption to each frame.

I load the logo (.png or .jpeg file) as IDirect3DTexture9 and I try to add it to a IDirect3DSurface9 image frame (screenshot).

As I'm new to DirecX9, I have no idea how to copy the logo (texture) to the screenshot (surface/buffer). Any help would be appreciated.

(If there's any other way to add logo to each frame without involving texture, please do tell me.)

EDIT: I have used the code suggested in an answer below. The hr result returned is an error.

IDirect3DSurface9 *pSurface = NULL;
pDevice->GetFrontBufferData(0, pSurface); //my screenshot

LPDIRECT3DTEXTURE9 tex = NULL;  //my logo
//[code to load logo from file here]

IDirect3DSurface9 *pSurf = NULL;
tex->GetSurfaceLevel(0, &pSurf);

hr = pDevice->StretchRect(pSurf, NULL, pSurface, NULL, D3DTEXF_NONE);  //HR GIVES AN ERROR

Upvotes: 2

Views: 908

Answers (1)

VuVirt
VuVirt

Reputation: 1917

You can use StretchRect. The code will look something like this (pseudo):

ID3DSurface9 *pScreenSurface = ... // your screenshot surface should be created in default pool and the same format as your texture(see bellow), like this:
CreateOffscreenPlainSurface(width, height, D3DFMT_X8B8G8R8, D3DPOOL_DEFAULT, &pScreenSurface, NULL);

// Now get a screenshot by using either GetFrontBufferData, or GetBackBuffer/GetRenderTargetData by supplying pScreenSurface as a parameter (not shown here).

ID3DTexture9 *pTexture = ... // your texture should be loaded in default pool and the same format as your screenshot surface, like this:
D3DXCreateTextureFromFileEx(*ppDevice, L"icon.bmp", 40, 40, D3DX_DEFAULT, 0, 
  D3DFMT_X8B8G8R8, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, 
  NULL, &pTexture);

ID3DSurface9 *pSurf;
pTexture->GetSurfaceLevel(0, &pSurf); // don't forget to Release pSurf after StretchRect

RECT rc = { ... initialize the destination rectangle here };
pDevice->StretchRect(pSurf, NULL, pScreenSurface, &rc);

You need to specify a destination rectangle inside the destination surface, where you want your texture to be copied.

Upvotes: 1

Related Questions