vovan333

D3D11

Jan 5th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma warning (disable:4996)
  2. #pragma comment (lib, "d3d11.lib")
  3.  
  4. #include <Windows.h>
  5. #include <D3D11.h>
  6. #include <D3DX10.h>
  7. #include <gdiplus.h>
  8. #include <string>
  9.  
  10. using namespace Gdiplus;
  11. using namespace std;
  12.  
  13. namespace D3DProject1
  14. {
  15.     UINT windowStyle = WS_OVERLAPPEDWINDOW;
  16.     HWND hMainWindow;
  17.     HINSTANCE hCurrentInstance;
  18.     IDXGISwapChain* swapchain;
  19.     ID3D11RenderTargetView* backbuffer;
  20.     ID3D11Device* dev;
  21.     ID3D11DeviceContext* devcon;
  22.  
  23.     wchar_t* W(wstring str)
  24.     {
  25.         wchar_t* result = new wchar_t[str.size()];
  26.         wcscpy(result, str.c_str());
  27.         return result;
  28.     }
  29.  
  30.     void CreateWC(wstring name, WNDPROC OnMessage)
  31.     {
  32.         WNDCLASSEX wc;
  33.         ZeroMemory(&wc, sizeof(wc));
  34.  
  35.         wc.cbSize = sizeof(WNDCLASSEX);
  36.         wc.style = CS_HREDRAW | CS_VREDRAW;
  37.         wc.lpfnWndProc = OnMessage;
  38.         wc.hInstance = hCurrentInstance;
  39.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  40.         wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  41.         wc.lpszClassName = W(name);
  42.  
  43.         RegisterClassEx(&wc);
  44.     }
  45.  
  46.     Size AdjustWndSzToClientAreaSz(int clientAreaWidth, int clientAreaHeight, UINT wndStyle = windowStyle)
  47.     {
  48.         RECT wr = { 0, 0, clientAreaWidth, clientAreaHeight };
  49.         AdjustWindowRect(&wr, wndStyle, FALSE);
  50.         return Size(wr.right - wr.left, wr.bottom - wr.top);
  51.     }
  52.  
  53.     DXGI_SWAP_CHAIN_DESC CreateSCD(HWND hWnd)
  54.     {
  55.         DXGI_SWAP_CHAIN_DESC scd;
  56.         ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
  57.  
  58.         scd.BufferCount = 1;
  59.         scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  60.         scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  61.         scd.OutputWindow = hWnd;
  62.         scd.SampleDesc.Count = 4;
  63.         scd.Windowed = TRUE;
  64.  
  65.         return scd;
  66.     }
  67.  
  68.     void InitD3DDeviceAndSwapChain(DXGI_SWAP_CHAIN_DESC& scd, IDXGISwapChain*& sc, ID3D11Device*& dev, ID3D11DeviceContext*& devCon)
  69.     {
  70.         D3D11CreateDeviceAndSwapChain
  71.         (
  72.             NULL,
  73.             D3D_DRIVER_TYPE_HARDWARE,
  74.             NULL,
  75.             NULL,
  76.             NULL,
  77.             NULL,
  78.             D3D11_SDK_VERSION,
  79.             &scd,
  80.             &swapchain,
  81.             &dev,
  82.             NULL,
  83.             &devcon
  84.         );
  85.     }
  86.  
  87.     D3D11_VIEWPORT CreateViewport(int width, int height)
  88.     {
  89.         D3D11_VIEWPORT viewport;
  90.         ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
  91.  
  92.         viewport.TopLeftX = 0;
  93.         viewport.TopLeftY = 0;
  94.         viewport.Width = width;
  95.         viewport.Height = height;
  96.        
  97.         return viewport;
  98.     }
  99.  
  100.     void SetupRenderTarget(ID3D11RenderTargetView*& pRenderTarget)
  101.     {
  102.         ID3D11Texture2D* pBackBuffer;
  103.         swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&pBackBuffer);
  104.         dev->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTarget);
  105.         devcon->OMSetRenderTargets(1, &pRenderTarget, NULL);
  106.         pBackBuffer->Release();
  107.     }
  108.  
  109.     void ClearRenderTarget(ID3D11RenderTargetView*& renderTarget)
  110.     {
  111.         devcon->ClearRenderTargetView(backbuffer, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));
  112.     }
  113.  
  114.     void SwapSCBuffers(IDXGISwapChain*& swapChain)
  115.     {
  116.         swapChain->Present(0, 0);
  117.     }
  118.  
  119.     void InitD3D(HWND hWnd = hMainWindow)
  120.     {
  121.         auto scd = CreateSCD(hWnd);
  122.         InitD3DDeviceAndSwapChain(scd, swapchain, dev, devcon);
  123.         SetupRenderTarget(backbuffer);
  124.         auto viewport = CreateViewport(800, 600);
  125.         devcon->RSSetViewports(1, &viewport);
  126.     }
  127.  
  128.     void RenderFrame()
  129.     {
  130.  
  131.     }
  132.  
  133.     void ReleaseD3D()
  134.     {
  135.         swapchain->Release();
  136.         backbuffer->Release();
  137.         dev->Release();
  138.         devcon->Release();
  139.     }
  140.  
  141.     LRESULT CALLBACK OnMessage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  142.     {
  143.         switch (message)
  144.         {
  145.             case WM_DESTROY:
  146.                 ReleaseD3D();
  147.                 ExitProcess(0);
  148.                 break;
  149.         }
  150.         return DefWindowProc(hWnd, message, wParam, lParam);
  151.     }
  152.  
  153.     HWND CreateWnd(int width, int height, wstring title, UINT wndStyle = windowStyle)
  154.     {
  155.         CreateWC(title, OnMessage);
  156.         HWND hWnd = CreateWindow
  157.         (
  158.             W(title),
  159.             W(title),
  160.             wndStyle,
  161.             300,
  162.             300,
  163.             width,
  164.             height,
  165.             NULL,
  166.             NULL,
  167.             hCurrentInstance,
  168.             NULL
  169.         );
  170.         ShowWindow(hWnd, SW_RESTORE);
  171.         return hWnd;
  172.     }
  173.  
  174.     void InitMessageLoop()
  175.     {
  176.         MSG msg;
  177.         while (true)
  178.         {
  179.             if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  180.             {
  181.                 TranslateMessage(&msg);
  182.                 DispatchMessage(&msg);
  183.             }
  184.             else
  185.             {
  186.                 ClearRenderTarget(backbuffer);
  187.                 RenderFrame();
  188.                 SwapSCBuffers(swapchain);
  189.             }
  190.         }
  191.     }
  192.  
  193.     int WinMain(HINSTANCE hInstance, string cmdLine, int showCmd)
  194.     {
  195.         hCurrentInstance = hInstance;
  196.         Size sz = AdjustWndSzToClientAreaSz(800, 600);
  197.         hMainWindow = CreateWnd(sz.Width, sz.Height, L"D3DProject1 Main Window");
  198.         InitD3D(hMainWindow);
  199.         InitMessageLoop();
  200.         return 0;
  201.     }
  202. }
  203.  
  204. int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int showCmd)
  205. {
  206.     return D3DProject1::WinMain(hInstance, cmdLine, showCmd);
  207. }
Advertisement
Add Comment
Please, Sign In to add comment