Advertisement
SomeGenericUsername

Untitled

Aug 10th, 2019
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.19 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <d3d9.h>
  3. #include <d3dx9.h>
  4. #include <wrl.h>
  5. #include <vector>
  6.  
  7. using namespace Microsoft::WRL;
  8.  
  9. const unsigned long VertexStructFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;
  10.  
  11. ComPtr<IDirect3D9> _d3d{ };
  12. ComPtr<IDirect3DDevice9> _device{ };
  13. ComPtr<IDirect3DVertexBuffer9> _vertexBuffer{ };
  14. ComPtr<IDirect3DIndexBuffer9> _indexBuffer{ };
  15.  
  16. bool _running{ false };
  17.  
  18. template <typename T>
  19. unsigned int GetByteSize(const std::vector<T>& vec) {
  20.     return sizeof(vec[0]) * vec.size();
  21. }
  22.  
  23. struct VStruct {
  24.     float x, y, z;
  25.     D3DCOLOR color;
  26. };
  27.  
  28.  
  29. bool SetupTransform() {
  30.     D3DXMATRIX view{ };
  31.     D3DXMatrixLookAtLH(&view, &D3DXVECTOR3{ 0.0f, 0.0f, -20.0f }, &D3DXVECTOR3{ 0.0f, 0.0f, 0.0f }, &D3DXVECTOR3{ 0.0f, 1.0f, 0.0f });
  32.    
  33.     HRESULT result{ };
  34.     result = _device->SetTransform(D3DTS_VIEW, &view);
  35.     if (FAILED(result))
  36.         return false;
  37.  
  38.     D3DXMATRIX projection{ };
  39.     D3DXMatrixPerspectiveFovLH(&projection, D3DXToRadian(60.0f), 1024.0f / 768.0f, 0.0f, 100.0f);  
  40.  
  41.     result = _device->SetTransform(D3DTS_PROJECTION, &projection);
  42.     if (FAILED(result))
  43.         return false;
  44.  
  45.     result = _device->SetRenderState(D3DRS_LIGHTING, false);
  46.  
  47.     return true;
  48. }
  49.  
  50. IDirect3DVertexBuffer9* CreateBuffer(const std::vector<VStruct>& vertices) {
  51.     IDirect3DVertexBuffer9* buffer{ };
  52.     HRESULT result{ };
  53.    
  54.     result = _device->CreateVertexBuffer(GetByteSize(vertices), 0, VertexStructFVF, D3DPOOL_DEFAULT, &buffer, nullptr);
  55.     if (FAILED(result))
  56.         return nullptr;
  57.  
  58.     void* data{ };
  59.     result = buffer->Lock(0, GetByteSize(vertices), &data, 0);    
  60.     if (FAILED(result))
  61.         return nullptr;
  62.  
  63.     memcpy(data, vertices.data(), GetByteSize(vertices));
  64.     buffer->Unlock();
  65.  
  66.     _device->SetFVF(VertexStructFVF);
  67.    
  68.     return buffer;
  69. }
  70.  
  71. IDirect3DIndexBuffer9* CreateIndexBuffer(std::vector<unsigned int>& indices) {
  72.     IDirect3DIndexBuffer9* buffer{ };
  73.     HRESULT result{ };
  74.    
  75.     result = _device->CreateIndexBuffer(GetByteSize(indices), 0, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &buffer, nullptr);
  76.     if (FAILED(result))
  77.         return nullptr;
  78.  
  79.     void* data{ };
  80.     result = buffer->Lock(0, GetByteSize(indices), &data, 0);
  81.     if (FAILED(result))
  82.         return nullptr;
  83.  
  84.     memcpy(data, indices.data(), GetByteSize(indices));
  85.     buffer->Unlock();
  86.  
  87.     return buffer;
  88. }
  89.  
  90. bool InitD3D(HWND hWnd) {
  91.     RECT clientRect{ };
  92.     GetClientRect(hWnd, &clientRect);
  93.  
  94.     _d3d = Direct3DCreate9(D3D_SDK_VERSION);
  95.  
  96.     D3DDISPLAYMODE displayMode{ };
  97.     _d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode);
  98.  
  99.     D3DPRESENT_PARAMETERS pp{ };
  100.     pp.BackBufferWidth = clientRect.right;
  101.     pp.BackBufferHeight = clientRect.bottom;
  102.     pp.BackBufferFormat = displayMode.Format;
  103.     pp.BackBufferCount = 1;                
  104.     pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  105.     pp.hDeviceWindow = hWnd;              
  106.     pp.Windowed = true;                    
  107.     pp.Flags = 0;                    
  108.  
  109.     HRESULT result{ };
  110.     result = _d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &pp, &_device);
  111.     if (FAILED(result))
  112.         return false;
  113.  
  114.     D3DVIEWPORT9 viewport{ };
  115.     viewport.X = 0;      
  116.     viewport.Y = 0;  
  117.     viewport.Width = clientRect.right;
  118.     viewport.Height = clientRect.bottom;
  119.     viewport.MinZ = 0.0f;
  120.     viewport.MaxZ = 100.0f;
  121.  
  122.     result = _device->SetViewport(&viewport);
  123.     if (FAILED(result))
  124.         return false;
  125.  
  126.     return true;
  127. }
  128.  
  129. LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  130.     switch (msg) {
  131.         case WM_DESTROY:
  132.             _running = false;
  133.             PostQuitMessage(0);
  134.             break;
  135.     }
  136.     return DefWindowProc(hWnd, msg, wParam, lParam);
  137. }
  138.  
  139. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  140.     WNDCLASSEX wndEx{ };
  141.     wndEx.cbSize = sizeof(WNDCLASSEX);
  142.     wndEx.style = CS_VREDRAW | CS_HREDRAW;
  143.     wndEx.lpfnWndProc = WndProc;              
  144.     wndEx.cbClsExtra = 0;                      
  145.     wndEx.cbWndExtra = 0;              
  146.     wndEx.hInstance = hInstance;          
  147.     wndEx.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
  148.     wndEx.hCursor = LoadCursor(nullptr, IDC_ARROW);  
  149.     wndEx.hbrBackground = (HBRUSH)COLOR_WINDOW;    
  150.     wndEx.lpszMenuName = nullptr;                
  151.     wndEx.lpszClassName = "DirectXClass";        
  152.     wndEx.hIconSm = nullptr;            
  153.  
  154.     RegisterClassEx(&wndEx);
  155.  
  156.     HWND hWnd{ nullptr };
  157.     hWnd = CreateWindow("DirectXClass", "directx window", WS_OVERLAPPEDWINDOW, 50, 50, 1024, 768, nullptr, nullptr, hInstance, nullptr);  
  158.  
  159.     if (!hWnd)
  160.         return -1;
  161.  
  162.     ShowWindow(hWnd, nCmdShow);
  163.     UpdateWindow(hWnd);
  164.  
  165.     _running = true;
  166.  
  167.     if (!InitD3D(hWnd))
  168.         return -1;
  169.  
  170.     std::vector<VStruct> vertices{
  171.         VStruct{ -1.0f, -1.0f, 1.0f, D3DXCOLOR{ 1.0f, 0.0f, 0.0f, 1.0f } },
  172.         VStruct{ -1.0f,  1.0f, 1.0f, D3DXCOLOR{ 0.0f, 1.0f, 0.0f, 1.0f } },
  173.         VStruct{  1.0f,  1.0f, 1.0f, D3DXCOLOR{ 0.0f, 0.0f, 1.0f, 1.0f } },
  174.         VStruct{  1.0f, -1.0f, 1.0f, D3DXCOLOR{ 1.0f, 1.0f, 0.0f, 1.0f } }
  175.     };
  176.    
  177.     std::vector<unsigned int> indices{
  178.         0, 1, 2,
  179.         0, 2, 3
  180.     };
  181.  
  182.     if (!(_vertexBuffer = CreateBuffer(vertices)))
  183.         return -1;
  184.  
  185.     if (!(_indexBuffer = CreateIndexBuffer(indices)))
  186.         return -1;
  187.  
  188.     if (!SetupTransform())
  189.         return -1;
  190.  
  191.     HRESULT result = _device->SetStreamSource(0, _vertexBuffer.Get(), 0, sizeof(VStruct));
  192.     if (FAILED(result))
  193.         return -1;
  194.  
  195.     result = _device->SetIndices(_indexBuffer.Get());
  196.     if (FAILED(result))
  197.         return -1;
  198.  
  199.     D3DXMATRIX world{ };
  200.     D3DXMatrixIdentity(&world);
  201.  
  202.     D3DXMATRIX scaling{ };
  203.     D3DXMatrixScaling(&scaling, 10, 10, 1);
  204.  
  205.     D3DXMATRIX rotation{ };
  206.     D3DXMatrixIdentity(&rotation);
  207.    
  208.     D3DXMatrixMultiply(&world, &scaling, &rotation);
  209.     _device->SetTransform(D3DTS_WORLD, &world);
  210.  
  211.     _device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
  212.    
  213.     D3DXMatrixRotationY(&rotation, D3DXToRadian(0.5f));
  214.  
  215.     MSG msg{ };
  216.     while (_running) {
  217.         while (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)) {
  218.             TranslateMessage(&msg);
  219.             DispatchMessage(&msg);
  220.         }
  221.         _device->Clear(0, nullptr, D3DCLEAR_TARGET, D3DXCOLOR{ 0.0f, 0.0f, 0.0f, 1.0f }, 0.0f, 0);  
  222.  
  223.         _device->BeginScene();
  224.         _device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, indices.size(), 0, 2);
  225.         _device->EndScene();
  226.  
  227.         _device->Present(nullptr, nullptr, nullptr, nullptr);
  228.  
  229.         D3DXMatrixMultiply(&world, &world, &rotation);
  230.         _device->SetTransform(D3DTS_WORLD, &world);
  231.     }
  232.  
  233.     return 0;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement