Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include "iostream"
  4. #include "windows.h"
  5. #include "D3D9.h"
  6. #include <Wincodec.h>
  7. #include <chrono>
  8.  
  9. #pragma comment(lib, "D3d9.lib")
  10.  
  11. using namespace std;
  12.  
  13. #define EXIT(hr) { if (FAILED(hr)) { printf("Incorrect hr! Exit..."); goto cleanup; } }
  14. #define RELEASE(p) { if (p != nullptr) p->Release(); }
  15.  
  16.  
  17. void Direct3D9TakeScreenshots()
  18. {
  19.     HRESULT hr;
  20.     IDirect3D9 *d3d;
  21.     IDirect3DDevice9 *device;
  22.     IDirect3DSurface9 *surface;
  23.     D3DPRESENT_PARAMETERS parameters = { 0 };
  24.     D3DDISPLAYMODE mode;
  25.     D3DLOCKED_RECT rc;
  26.     LPBYTE *shots;
  27.  
  28.     // Init D3D and get screen size.
  29.     d3d = Direct3DCreate9(D3D_SDK_VERSION);
  30.     hr = d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &mode);
  31.     EXIT(hr);
  32.  
  33.     parameters.Windowed = TRUE;
  34.     parameters.BackBufferCount = 1;
  35.     parameters.BackBufferHeight = mode.Height;
  36.     parameters.BackBufferWidth = mode.Width;
  37.     parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
  38.     parameters.hDeviceWindow = NULL;
  39.  
  40.     // Create device.
  41.     hr = d3d->CreateDevice(D3DADAPTER_DEFAULT,
  42.         D3DDEVTYPE_HAL, NULL,
  43.         D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  44.         &parameters, &device);
  45.  
  46.     EXIT(hr);
  47.  
  48.     // Capture surface.
  49.     hr = device->CreateOffscreenPlainSurface(mode.Width,
  50.         mode.Height,
  51.         D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM,
  52.         &surface, nullptr);
  53.  
  54.     EXIT(hr);
  55.  
  56.     // Compute the required buffer size.
  57.  
  58.     hr = surface->LockRect(&rc, NULL, 0);
  59.     EXIT(hr);
  60.  
  61.     UINT pitch = rc.Pitch;
  62.  
  63.     hr = surface->UnlockRect();
  64.     EXIT(hr);
  65.  
  66.     for (int i = 0; i < 20; i++)
  67.     {
  68.         auto start = std::chrono::high_resolution_clock::now();
  69.  
  70.         // Allocate screenshot buffer.
  71.         LPBYTE shot = new BYTE[pitch * mode.Height];
  72.  
  73.         // Get the data.
  74.         hr = device->GetFrontBufferData(0, surface);
  75.         EXIT(hr);
  76.  
  77.         // Copy data into the buffers.
  78.  
  79.         hr = surface->LockRect(&rc, NULL, 0);
  80.         EXIT(hr);
  81.  
  82.         CopyMemory(shot, rc.pBits, rc.Pitch * mode.Height);
  83.  
  84.         hr = surface->UnlockRect();
  85.         EXIT(hr);
  86.  
  87.         auto finish = std::chrono::high_resolution_clock::now();
  88.         std::cout << "Time: " << std::chrono::duration_cast<std::chrono::microseconds>(finish - start).count() << std::endl;
  89.     }
  90.  
  91. cleanup:
  92.  
  93.     RELEASE(surface);
  94.     RELEASE(device);
  95.     RELEASE(d3d);
  96. }
  97.  
  98. void main()
  99. {
  100.     Direct3D9TakeScreenshots();
  101.     system("pause");
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement