Skip to content

Instantly share code, notes, and snippets.

@Themaister
Created June 10, 2012 14:26
Show Gist options
  • Save Themaister/2905909 to your computer and use it in GitHub Desktop.
Save Themaister/2905909 to your computer and use it in GitHub Desktop.
void D3DVideo::viewport_size(unsigned &width, unsigned &height)
{
width = final_viewport.Width;
height = final_viewport.Height;
}
bool D3DVideo::read_viewport(uint8_t *buffer)
{
bool ret = true;
IDirect3DSurface9 *target = nullptr;
IDirect3DSurface9 *dest = nullptr;
if (FAILED(dev->GetRenderTarget(0, &target)))
{
ret = false;
goto end;
}
if (FAILED(dev->CreateOffscreenPlainSurface(screen_width, screen_height,
D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM,
&dest, nullptr)))
{
ret = false;
goto end;
}
if (FAILED(dev->GetRenderTargetData(target, dest)))
{
ret = false;
goto end;
}
D3DLOCKED_RECT rect;
if (SUCCEEDED(dest->LockRect(&rect, nullptr, D3DLOCK_READONLY)))
{
unsigned pitchpix = rect.Pitch / 4;
const uint32_t *pixels = (const uint32_t*)rect.pBits;
pixels += final_viewport.X;
pixels += (final_viewport.Height - 1) * pitchpix;
pixels -= final_viewport.Y * pitchpix;
for (unsigned y = 0; y < final_viewport.Height; y++, pixels -= pitchpix)
{
for (unsigned x = 0; x < final_viewport.Width; x++)
{
*buffer++ = (pixels[x] >> 0) & 0xff;
*buffer++ = (pixels[x] >> 8) & 0xff;
*buffer++ = (pixels[x] >> 16) & 0xff;
}
}
dest->UnlockRect();
}
else
{
ret = false;
goto end;
}
end:
if (target)
target->Release();
if (dest)
dest->Release();
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment