Advertisement
SomeGenericUsername

Untitled

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