Advertisement
Guest User

D3D10 && DWM

a guest
Feb 21st, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.34 KB | None | 0 0
  1. //
  2. // cl.exe MyProgram.cpp kernel32.lib user32.lib dwmapi.lib d3d10.lib
  3. //
  4. #define UNICODE
  5. #define _UNICODE
  6. #define INITGUID
  7. #include <windows.h>
  8. #include <dwmapi.h>
  9. #include <dxgi.h>
  10. #include <d3d10.h>
  11.  
  12. void InitWindow();
  13. void InitDevice();
  14. void ResizeBuffers();
  15. void Render();
  16. void Cleanup();
  17. LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  18.  
  19. HWND hwnd = NULL;
  20. HINSTANCE instance = NULL;
  21. ID3D10Device * device = NULL;
  22. IDXGISwapChain * swapChain = NULL;
  23. ID3D10RenderTargetView * backBufferRTV = NULL;
  24. UINT width = 640;
  25. UINT height = 480;
  26.  
  27. int WinMain(HINSTANCE _instance, HINSTANCE prevInstance, LPSTR cmdLine, int cmdShow)
  28. {
  29.     instance = _instance;
  30.  
  31.     InitWindow();
  32.     InitDevice();
  33.  
  34.     MSG msg = {0};
  35.     while(WM_QUIT != msg.message)
  36.     {
  37.         if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  38.         {
  39.             TranslateMessage(&msg);
  40.             DispatchMessage(&msg);
  41.         }
  42.         else
  43.         {
  44.             Render();
  45.         }
  46.     }
  47.  
  48.     Cleanup();
  49.  
  50.     return 0;
  51. }
  52.  
  53. void InitWindow()
  54. {
  55.     WNDCLASSEX wc;
  56.     SecureZeroMemory(&wc, sizeof(wc));
  57.     wc.cbSize = sizeof(wc);
  58.     wc.hbrBackground = (HBRUSH)NULL;
  59.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  60.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  61.     wc.hInstance = instance;
  62.     wc.lpfnWndProc = MyWindowProc;
  63.     wc.lpszClassName = TEXT("Window");
  64.     wc.style = CS_VREDRAW | CS_HREDRAW;
  65.  
  66.     if(!(RegisterClassEx(&wc)))
  67.         FatalAppExit(0, TEXT("Unable to register class"));
  68.  
  69.     RECT r;
  70.     GetWindowRect(GetDesktopWindow(), &r);
  71.  
  72.     if(!(hwnd = CreateWindowEx(
  73.                     WS_EX_COMPOSITED,
  74.                     TEXT("Window"),
  75.                     TEXT("Direct3D 10"),
  76.                     WS_POPUP,
  77.                     (r.right - r.left) / 2 - width / 2,
  78.                     (r.bottom - r.top) / 2 - height / 2,
  79.                     width,
  80.                     height,
  81.                     NULL,
  82.                     NULL,
  83.                     instance,
  84.                     NULL)))
  85.         FatalAppExit(0, TEXT("Unable to create window"));
  86.  
  87.     MARGINS m = {-1};
  88.     DwmExtendFrameIntoClientArea(hwnd, &m);
  89.  
  90.     ShowWindow(hwnd, SW_SHOWNORMAL);
  91.     UpdateWindow(hwnd);
  92. }
  93.  
  94. void InitDevice()
  95. {
  96.     HRESULT hr;
  97.  
  98.     DXGI_SWAP_CHAIN_DESC sd;
  99.     SecureZeroMemory(&sd, sizeof(sd));
  100.     sd.BufferCount = 1;
  101.     sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  102.     sd.Windowed = TRUE;
  103.     sd.OutputWindow = hwnd;
  104.     sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  105.     sd.SampleDesc.Count = 1;
  106.     sd.SampleDesc.Quality = 0;
  107.     sd.Flags = 0;
  108.     sd.BufferDesc.Width = width;
  109.     sd.BufferDesc.Height = height;
  110.     sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  111.     sd.BufferDesc.RefreshRate.Numerator = 60;
  112.     sd.BufferDesc.RefreshRate.Denominator = 1;
  113.  
  114.     hr = D3D10CreateDeviceAndSwapChain(
  115.             NULL,
  116.             D3D10_DRIVER_TYPE_HARDWARE,
  117.             NULL,
  118.             0,
  119.             D3D10_SDK_VERSION,
  120.             &sd,            
  121.             &swapChain,
  122.             &device);
  123.  
  124.     if(FAILED(hr))
  125.         FatalAppExit(0, TEXT("Unable to create device and/or swap chain"));
  126.  
  127.     ResizeBuffers();
  128. }
  129.  
  130. void ResizeBuffers()
  131. {
  132.     if(!device || !swapChain)
  133.         return;
  134.  
  135.     HRESULT hr;
  136.     device->OMSetRenderTargets(0, NULL, NULL);
  137.  
  138.     if(backBufferRTV)
  139.         backBufferRTV->Release();
  140.  
  141.     hr = swapChain->ResizeBuffers(1, width, height, DXGI_FORMAT_R8G8B8A8_UNORM, 0);
  142.     if(FAILED(hr))
  143.         FatalAppExit(0, TEXT("Unable to resize backbuffer"));
  144.  
  145.     ID3D10Texture2D * backBuffer = NULL;
  146.    
  147.     hr = swapChain->GetBuffer(0, IID_ID3D10Texture2D, (void**)&backBuffer);
  148.     if(FAILED(hr))
  149.         FatalAppExit(0, TEXT("Unable to obtain backbuffer"));
  150.  
  151.     hr = device->CreateRenderTargetView(backBuffer, NULL, &backBufferRTV);
  152.     if(FAILED(hr))
  153.         FatalAppExit(0, TEXT("Unable to [re]create render target view"));
  154.  
  155.     backBuffer->Release();
  156.  
  157.     D3D10_VIEWPORT vp;
  158.     vp.MinDepth = 0.0f;
  159.     vp.MaxDepth = 1.0f;
  160.     vp.TopLeftX = 0;
  161.     vp.TopLeftY = 0;
  162.     vp.Width = width;
  163.     vp.Height = height;
  164.  
  165.     device->RSSetViewports(1, &vp);
  166.  
  167.     D3D10_RECT sr;
  168.     sr.top = 0;
  169.     sr.left = 0;
  170.     sr.right = width;
  171.     sr.bottom = height;
  172.  
  173.     device->RSSetScissorRects(1, &sr);
  174. }
  175.  
  176. void Cleanup()
  177. {
  178.     if(backBufferRTV)
  179.         backBufferRTV->Release();
  180.  
  181.     if(swapChain)
  182.         swapChain->Release();
  183.  
  184.     if(device)
  185.     {
  186.         device->ClearState();
  187.         device->Release();
  188.     }
  189.  
  190. }
  191.  
  192. void Render()
  193. {
  194.     device->OMSetRenderTargets(1, &backBufferRTV, NULL);
  195.  
  196.     FLOAT clearColor[4] = { 0.0f, 0.2f, 1.0f, 0.4f };
  197.     device->ClearRenderTargetView(backBufferRTV, clearColor);
  198.  
  199.     swapChain->Present(0, 0);
  200. }
  201.  
  202. LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  203. {
  204.     switch(msg)
  205.     {
  206.         case WM_KEYDOWN:
  207.             if(VK_ESCAPE == wParam)
  208.                 PostQuitMessage(0);
  209.             return 0;
  210.  
  211.         case WM_SIZE:
  212.             width = LOWORD(lParam);
  213.             height = HIWORD(lParam);
  214.             ResizeBuffers();
  215.             return 0;
  216.  
  217.         case WM_PAINT:
  218.             return 0;
  219.  
  220.         case WM_DESTROY:
  221.             PostQuitMessage(0);
  222.             return 0;
  223.  
  224.         default:
  225.             return DefWindowProc(hwnd, msg, wParam, lParam);
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement