我正在尝试挂钩 DirectX 游戏。我成功加载了我的钩子,并且可以使用以下方法将图像/后备缓冲区保存到磁盘:
HRESULT Capture(IDirect3DDevice9* Device, const char* FilePath)
{
IDirect3DSurface9* RenderTarget = nullptr;
HRESULT result = Device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &RenderTarget);
//for some reason result is never S_OK but this still works.
result = D3DXSaveSurfaceToFile(FilePath, D3DXIFF_PNG, RenderTarget, nullptr, nullptr);
SafeRelease(RenderTarget);
return result;
}
它成功保存,我很高兴。但是,我想保存到像素阵列而不是磁盘。我尝试使用:
#include <memory>
std::unique_ptr<std::uint8_t[]> mem(new std::uint8_t[100 * 100 * 4]);
HRESULT Direct3DDevice9Proxy::EndScene()
{
IDirect3DSurface9* RenderTarget = nullptr;
HRESULT result = ptr_Direct3DDevice9->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &RenderTarget);
D3DLOCKED_RECT LR;
RenderTarget->LockRect(&LR, nullptr, D3DLOCK_NOSYSLOCK | D3DLOCK_READONLY);
memcpy(mem.get(), LR.pBits, LR.Pitch - 1);
RenderTarget->UnlockRect();
SafeRelease(RenderTarget);
return ptr_Direct3DDevice9->EndScene();
}
但是,它永远不会锁定,如果我尝试访问 LR.pBits,它会引发访问冲突。我不知道为什么它不会锁定。有没有另一种方法可以将后缓冲区中的像素抓取到字节数组中?游戏的最大视口为 1366x768。
编辑:我试过:
void dump_buffer(LPDIRECT3DDEVICE9 Device, std::unique_ptr<std::uint8_t[]> &bits)
{
IDirect3DSurface9* RenderTarget = nullptr;
IDirect3DSurface9* DestTarget = nullptr;
D3DSURFACE_DESC rtDesc = {};
Device->GetRenderTarget(0, &RenderTarget);
RenderTarget->GetDesc(&rtDesc);
Device->CreateOffscreenPlainSurface(rtDesc.Width, rtDesc.Height, rtDesc.Format, D3DPOOL_DEFAULT, &DestTarget, nullptr);
Device->GetRenderTargetData(RenderTarget, DestTarget);
if(DestTarget != nullptr)
{
D3DLOCKED_RECT rect;
DestTarget->LockRect(&rect, 0, D3DLOCK_READONLY);
memcpy(bits.get(), rect.pBits, rtDesc.Width * rtDesc.Height * 4);
std::uint8_t* ptr = &bits[0];
CG::Image(ptr, rtDesc.Width, rtDesc.Height).Save("Foo.bmp");
DestTarget->UnlockRect();
DestTarget->Release();
}
RenderTarget->Release();
}
HRESULT Direct3DDevice9Proxy::EndScene()
{
dump_buffer(ptr_Direct3DDevice9, mem);
return ptr_Direct3DDevice9->EndScene();
}
但我的图像是黑色的。有任何想法吗?