Advertisement
vovan333

fucking bullshit

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