Facebook
From Emerald Ibis, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 236
  1. #include <windows.h>
  2. #include <d3d11.h>
  3. #include <dxgi1_3.h>
  4. #include <tchar.h>
  5.  
  6. #include "BaseWindow.h"
  7. #include "Logger.h"
  8. #include "VertexDescriptor.h"
  9. #include "PixelShader.h"
  10. #include "VertexShader.h"
  11. #include "InputLayout.h"
  12. #include "Geometry.h"
  13. #include "GeometryLoader.h"
  14.  
  15. #pragma comment( lib, "d3d11.lib" )
  16. #pragma comment( lib, "dxgi.lib" )
  17. #pragma comment( lib, "D3DCompiler.lib" )
  18. #pragma comment( lib, "dxguid.lib" )
  19.  
  20. class Engine : public BaseWindow
  21. {
  22. private:
  23.         ID3D11Device* device;
  24.         ID3D11DeviceContext* context;
  25.         int width;
  26.         int height;
  27.         IDXGIFactory2* dxgiFactory;
  28.         IDXGISwapChain1* swapChain;
  29.         ID3D11Texture2D* backBuffer;
  30.         ID3D11RenderTargetView* renderTargetView;
  31.  
  32. private:
  33.         void getBackBufferPointer()
  34.         {
  35.                 swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&backBuffer));
  36.         }
  37.  
  38.         void createDeviceAndDeviceContext()
  39.         {
  40.                 DWORD deviceFlags = D3D11_CREATE_DEVICE_SINGLETHREADED;
  41.                 #if defined(_DEBUG)
  42.                                 deviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
  43.                 #endif
  44.  
  45.                 D3D_FEATURE_LEVEL chosenFeatureLevel;
  46.                 HRESULT deviceResult = D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, deviceFlags,
  47.                         nullptr, 0, D3D11_SDK_VERSION, &device, &chosenFeatureLevel, &context);
  48.  
  49.                 if (FAILED(deviceResult))
  50.                 {
  51.                         std::cout << "ERROR device creation failed!" << std::endl;
  52.                 }
  53.  
  54.         }
  55.         void createSwapChain()
  56.         {
  57.                 HRESULT factoryResult = CreateDXGIFactory2(0, __uuidof(IDXGIFactory2),
  58.                         (void**)(&dxgiFactory));
  59.                 if (FAILED(factoryResult))
  60.                 {
  61.                         std::cout << "ERROR: DXGI Factory creation failed!" << std::endl;
  62.                 }
  63.  
  64.                 DXGI_SWAP_CHAIN_DESC1 swapChainDescription;
  65.                 ZeroMemory(&swapChainDescription, sizeof(DXGI_SWAP_CHAIN_DESC1));
  66.                 swapChainDescription.Width = this->width;
  67.                 swapChainDescription.Height = this->height;
  68.                 swapChainDescription.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  69.                 swapChainDescription.Stereo = FALSE;
  70.                 swapChainDescription.SampleDesc.Count = 1;
  71.                 swapChainDescription.SampleDesc.Quality = 0;
  72.                 swapChainDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  73.                 swapChainDescription.BufferCount = 1;
  74.                 swapChainDescription.Scaling = DXGI_SCALING_STRETCH;
  75.                 swapChainDescription.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  76.                 swapChainDescription.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED;
  77.                 swapChainDescription.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
  78.  
  79.                 DXGI_SWAP_CHAIN_FULLSCREEN_DESC fullscreenDescription;
  80.                 ZeroMemory(&fullscreenDescription, sizeof(DXGI_SWAP_CHAIN_FULLSCREEN_DESC));
  81.                 fullscreenDescription.RefreshRate.Numerator = 60;
  82.                 fullscreenDescription.RefreshRate.Denominator = 1;
  83.                 fullscreenDescription.Windowed = true;
  84.  
  85.                 HRESULT swapChainResult = dxgiFactory->CreateSwapChainForHwnd(device,
  86.                         hwnd, &swapChainDescription, &fullscreenDescription, nullptr, &swapChain);
  87.  
  88.                 if (FAILED(swapChainResult))
  89.                 {
  90.                         std::cout << "ERROR! Swap Chain creation failed!" << std::endl;
  91.                 }
  92.  
  93.  
  94.         }
  95.         void createRenderTarget()
  96.         {
  97.  
  98.                 getBackBufferPointer();
  99.                 HRESULT result = device->CreateRenderTargetView(backBuffer, NULL, &renderTargetView);
  100.                 if (FAILED(result))
  101.                 {
  102.                         std::cout << "ERROR! Render Target View creation failed!" << std::endl;
  103.                 }
  104.  
  105.  
  106.         }
  107.         void createViewport()
  108.         {
  109.         }
  110.         void createTriangleBuffer(ID3D11Device* device)
  111.         {
  112.         }
  113.         void createShadersAndInputLayout(ID3D11Device* device)
  114.         {
  115.         }
  116.         void bindEngineResources()
  117.         {
  118.         }
  119.         void clearRenderTarget(int r, int g, int b)
  120.         {
  121.  
  122.                 float color[4] = { r / 255.0f, g / 255.0f, b / 255.0f, 0.0f };
  123.                 context->ClearRenderTargetView(renderTargetView, color);
  124.  
  125.         }
  126.  
  127.         void present()
  128.         {
  129.                 swapChain->Present(0, 0);
  130.         }
  131. public:
  132.         Engine() : BaseWindow("GK_001", 800, 600), width(800), height(600)
  133.         {
  134.         }
  135.         void initialize() override
  136.         {
  137.                 createDeviceAndDeviceContext();
  138.                 createSwapChain();
  139.                 createRenderTarget();
  140.                 createViewport();
  141.                 createTriangleBuffer(device);
  142.                 createShadersAndInputLayout(device);
  143.                 bindEngineResources();
  144.         }
  145.         void update() override
  146.         {      
  147.         }
  148.         void render() override
  149.         {
  150.                 clearRenderTarget(120, 30, 30);
  151.                 present();
  152.         }
  153. };
  154.  
  155. int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmd, int show)
  156. {
  157.         Engine* engine = new Engine;
  158.         engine->start();
  159.  
  160.         return 0;
  161. }