Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.13 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.     if(!(hwnd = CreateWindowEx(
  70.                     WS_EX_COMPOSITED,
  71.                     TEXT("Window"),
  72.                     TEXT("Direct3D 10"),
  73.                     WS_OVERLAPPEDWINDOW,
  74.                     CW_USEDEFAULT,
  75.                     CW_USEDEFAULT,
  76.                     width,
  77.                     height,
  78.                     NULL,
  79.                     NULL,
  80.                     instance,
  81.                     NULL)))
  82.         FatalAppExit(0, TEXT("Unable to create window"));
  83.  
  84.     MARGINS m = {-1};
  85.     DwmExtendFrameIntoClientArea(hwnd, &m);
  86.  
  87.     ShowWindow(hwnd, SW_SHOWNORMAL);
  88.     UpdateWindow(hwnd);
  89. }
  90.  
  91. void InitDevice()
  92. {
  93.     HRESULT hr;
  94.  
  95.     DXGI_SWAP_CHAIN_DESC sd;
  96.     SecureZeroMemory(&sd, sizeof(sd));
  97.     sd.BufferCount = 1;
  98.     sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  99.     sd.Windowed = TRUE;
  100.     sd.OutputWindow = hwnd;
  101.     sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  102.     sd.SampleDesc.Count = 1;
  103.     sd.SampleDesc.Quality = 0;
  104.     sd.Flags = 0;
  105.     sd.BufferDesc.Width = width;
  106.     sd.BufferDesc.Height = height;
  107.     sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  108.     sd.BufferDesc.RefreshRate.Numerator = 60;
  109.     sd.BufferDesc.RefreshRate.Denominator = 1;
  110.  
  111.     hr = D3D10CreateDeviceAndSwapChain(
  112.             NULL,
  113.             D3D10_DRIVER_TYPE_HARDWARE,
  114.             NULL,
  115.             0,
  116.             D3D10_SDK_VERSION,
  117.             &sd,            
  118.             &swapChain,
  119.             &device);
  120.  
  121.     if(FAILED(hr))
  122.         FatalAppExit(0, TEXT("Unable to create device and/or swap chain"));
  123.  
  124.     ResizeBuffers();
  125. }
  126.  
  127. void ResizeBuffers()
  128. {
  129.     if(!device || !swapChain)
  130.         return;
  131.  
  132.     HRESULT hr;
  133.     device->OMSetRenderTargets(0, NULL, NULL);
  134.  
  135.     if(backBufferRTV)
  136.         backBufferRTV->Release();
  137.  
  138.     hr = swapChain->ResizeBuffers(1, width, height, DXGI_FORMAT_R8G8B8A8_UNORM, 0);
  139.     if(FAILED(hr))
  140.         FatalAppExit(0, TEXT("Unable to resize backbuffer"));
  141.  
  142.     ID3D10Texture2D * backBuffer = NULL;
  143.    
  144.     hr = swapChain->GetBuffer(0, IID_ID3D10Texture2D, (void**)&backBuffer);
  145.     if(FAILED(hr))
  146.         FatalAppExit(0, TEXT("Unable to obtain backbuffer"));
  147.  
  148.     hr = device->CreateRenderTargetView(backBuffer, NULL, &backBufferRTV);
  149.     if(FAILED(hr))
  150.         FatalAppExit(0, TEXT("Unable to [re]create render target view"));
  151.  
  152.     backBuffer->Release();
  153.  
  154.     D3D10_VIEWPORT vp;
  155.     vp.MinDepth = 0.0f;
  156.     vp.MaxDepth = 1.0f;
  157.     vp.TopLeftX = 0;
  158.     vp.TopLeftY = 0;
  159.     vp.Width = width;
  160.     vp.Height = height;
  161.  
  162.     device->RSSetViewports(1, &vp);
  163.  
  164.     D3D10_RECT sr;
  165.     sr.top = 0;
  166.     sr.left = 0;
  167.     sr.right = width;
  168.     sr.bottom = height;
  169.  
  170.     device->RSSetScissorRects(1, &sr);
  171. }
  172.  
  173. void Cleanup()
  174. {
  175.     if(backBufferRTV)
  176.         backBufferRTV->Release();
  177.  
  178.     if(swapChain)
  179.         swapChain->Release();
  180.  
  181.     if(device)
  182.     {
  183.         device->ClearState();
  184.         device->Release();
  185.     }
  186.  
  187. }
  188.  
  189. void Render()
  190. {
  191.     device->OMSetRenderTargets(1, &backBufferRTV, NULL);
  192.  
  193.     FLOAT clearColor[4] = { 0.0f, 0.5f, 1.0f, 0.5f };
  194.     device->ClearRenderTargetView(backBufferRTV, clearColor);
  195.  
  196.     swapChain->Present(0, 0);
  197. }
  198.  
  199. LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  200. {
  201.     switch(msg)
  202.     {
  203.         case WM_SIZE:
  204.             width = LOWORD(lParam);
  205.             height = HIWORD(lParam);
  206.             ResizeBuffers();
  207.             return 0;
  208.  
  209.         case WM_PAINT:
  210.             return 0;
  211.  
  212.         case WM_DESTROY:
  213.             PostQuitMessage(0);
  214.             return 0;
  215.  
  216.         default:
  217.             return DefWindowProc(hwnd, msg, wParam, lParam);
  218.     }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement