Ember

Create

Feb 3rd, 2014
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.97 KB | None | 0 0
  1.     bool Window::Create(Description::Window windowDesc)
  2.     {
  3.         HRESULT result;
  4.  
  5.         WNDCLASSEX wc;
  6.         DEVMODE dmScreenSettings;
  7.         Int posX, posY;
  8.  
  9.         IDXGIFactory* factory;
  10.         IDXGIAdapter* adapter;
  11.         IDXGIOutput* adapterOutput;
  12.         UInt numModes, i, numerator, denominator;
  13. #ifdef _WIN64
  14.         ULong stringLength;
  15. #else
  16.         UInt stringLength;
  17. #endif
  18.         DXGI_MODE_DESC* displayModeList;
  19.         DXGI_ADAPTER_DESC adapterDesc;
  20.         Int error;
  21.         DXGI_SWAP_CHAIN_DESC swapChainDesc;
  22.         D3D_FEATURE_LEVEL featureLevel;
  23.         ID3D11Texture2D* backBufferPtr;
  24.         D3D11_TEXTURE2D_DESC depthBufferDesc;
  25.         D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
  26.         D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
  27.         D3D11_RASTERIZER_DESC rasterDesc;
  28.         D3D11_VIEWPORT viewport;
  29.         D3D11_BUFFER_DESC matrixBufferDesc;
  30.         Float fieldOfView, screenAspect;
  31.  
  32.         // Initialize the Windows API (and create a window)
  33.  
  34.         // Get the instance of this application
  35.         mHINSTANCE = GetModuleHandle(NULL);
  36.  
  37.         // Give the application a name
  38.         //mApplicationName = L"Engine";
  39.         //std::mbstowcs(mApplicationName, );
  40.         //WString wstr = windowDesc.caption.;
  41.  
  42.         // Setup the windows class with default settings
  43.         wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  44.         wc.lpfnWndProc = WndProc;
  45.         wc.cbClsExtra = 0;
  46.         wc.cbWndExtra = 0;
  47.         wc.hInstance = mHINSTANCE;
  48.         wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
  49.         wc.hIconSm = wc.hIcon;
  50.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  51.         wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  52.         wc.lpszMenuName = NULL;
  53.         wc.lpszClassName = windowDesc.wcaption;
  54.         wc.cbSize = sizeof(WNDCLASSEX);
  55.  
  56.         // Register the window class
  57.         RegisterClassEx(&wc);
  58.  
  59.         // Setup the screen settings depending on whether it is running in full screen or in windowed mode
  60.         if(windowDesc.fullscreen)
  61.         {
  62.             // If full screen set the screen to maximum size of the users desktop and 32bit
  63.             windowDesc.width = GetSystemMetrics(SM_CXSCREEN);
  64.             windowDesc.height = GetSystemMetrics(SM_CYSCREEN);
  65.  
  66.             // Initialize the screen settings
  67.             ZeroMemory(&dmScreenSettings, sizeof(dmScreenSettings));
  68.             dmScreenSettings.dmSize = sizeof(dmScreenSettings);
  69.             dmScreenSettings.dmPelsWidth = (ULong)windowDesc.width;
  70.             dmScreenSettings.dmPelsHeight = (ULong)windowDesc.height;
  71.             dmScreenSettings.dmBitsPerPel = 32;
  72.             dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  73.  
  74.             // Change the display settings to full screen
  75.             ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
  76.  
  77.             // Set the position of the window to the top left corner
  78.             posX = posY = 0;
  79.         }
  80.         else
  81.         {
  82.             // Place the window in the middle of the screen
  83.             posX = (GetSystemMetrics(SM_CXSCREEN) - windowDesc.width) / 2;
  84.             posY = (GetSystemMetrics(SM_CYSCREEN) - windowDesc.height) / 2;
  85.         }
  86.  
  87.         // Create the window with the screen settings and get the handle to it
  88.         mHWND = CreateWindowExW(WS_EX_APPWINDOW, windowDesc.wcaption, windowDesc.wcaption, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, posX, posY, windowDesc.width, windowDesc.height, NULL, NULL, mHINSTANCE, NULL);
  89.  
  90.         // Initialize and create the Direct3D rendering device and context
  91.  
  92.         // Create a DirectX graphics interface factory
  93.         result = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&factory);
  94.         if(FAILED(result)) { return false; }
  95.  
  96.         // Use the factory to create an adapter for the primary graphics interface (video card)
  97.         result = factory->EnumAdapters(0, &adapter);
  98.         if(FAILED(result)) { return false; }
  99.  
  100.         // Enumerate the primary adapter output (monitor)
  101.         result = adapter->EnumOutputs(0, &adapterOutput);
  102.         if(FAILED(result)) { return false; }
  103.  
  104.         // Set to null to retreive all of the modes
  105.         numModes = NULL;
  106.  
  107.         // Get the number of modes that fit the DXGI_FORMAT_R8G8B8A8_UNORM display format for the adapter output (monitor)
  108.         result = adapterOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &numModes, NULL);
  109.         if(FAILED(result)) { return false; }
  110.  
  111.         // Create a list to hold all the possible display modes for this monitor/video card combination
  112.         displayModeList = new DXGI_MODE_DESC[numModes];
  113.         if(!displayModeList) { return false; }
  114.  
  115.         // Now fill the display mode list structures
  116.         result = adapterOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &numModes, displayModeList);
  117.         if(FAILED(result)) { return false; }
  118.  
  119.         // Now go through all the display modes and find the one that matches the screen width and height
  120.         // When a match is found store the numerator and denominator of the refresh rate for that monitor
  121.         for(i = 0; i<numModes; i++)
  122.         {
  123.             if(displayModeList[i].Width == windowDesc.width)
  124.             {
  125.                 if(displayModeList[i].Height == windowDesc.height)
  126.                 {
  127.                     numerator = displayModeList[i].RefreshRate.Numerator;
  128.                     denominator = displayModeList[i].RefreshRate.Denominator;
  129.                 }
  130.             }
  131.         }
  132.  
  133.         // Get the adapter (video card) description
  134.         result = adapter->GetDesc(&adapterDesc);
  135.         if(FAILED(result)) { return false; }
  136.  
  137.         // Store the dedicated video card memory in megabytes
  138.         mVideoCardMemory = (UInt)(adapterDesc.DedicatedVideoMemory / 1024 / 1024);
  139.  
  140.         // Convert the name of the video card to a character array and store it
  141.         error = wcstombs_s(&stringLength, mVideoCardDescription, 128, adapterDesc.Description, 128);
  142.         if(error != 0) { return false; }
  143.  
  144.         // Release the display mode list
  145.         delete[] displayModeList;
  146.         displayModeList = 0;
  147.  
  148.         // Release the adapter output
  149.         adapterOutput->Release();
  150.         adapterOutput = 0;
  151.  
  152.         // Release the adapter
  153.         adapter->Release();
  154.         adapter = 0;
  155.  
  156.         // Release the factory
  157.         factory->Release();
  158.         factory = 0;
  159.  
  160.         // Initialize the swap chain description
  161.         ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
  162.  
  163.         // Set to a single back buffer
  164.         swapChainDesc.BufferCount = 1;
  165.  
  166.         // Set the width and height of the back buffer
  167.         swapChainDesc.BufferDesc.Width = windowDesc.width;
  168.         swapChainDesc.BufferDesc.Height = windowDesc.height;
  169.  
  170.         // Set regular 32-bit surface for the back buffer
  171.         swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  172.  
  173.         // Set the refresh rate of the back buffer
  174.         if(mVsyncEnabled)
  175.         {
  176.             swapChainDesc.BufferDesc.RefreshRate.Numerator = numerator;
  177.             swapChainDesc.BufferDesc.RefreshRate.Denominator = denominator;
  178.         }
  179.         else
  180.         {
  181.             swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
  182.             swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
  183.         }
  184.  
  185.         // Set the usage of the back buffer
  186.         swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  187.  
  188.         // Set the handle for the window to render to
  189.         swapChainDesc.OutputWindow = mHWND;
  190.  
  191.         // Turn multisampling off
  192.         swapChainDesc.SampleDesc.Count = 1;
  193.         swapChainDesc.SampleDesc.Quality = 0;
  194.  
  195.         // Set to full screen or windowed mode
  196.         if(windowDesc.fullscreen) { swapChainDesc.Windowed = false; }
  197.         else { swapChainDesc.Windowed = true; }
  198.  
  199.         // Set the scan line ordering and scaling to unspecified
  200.         swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  201.         swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  202.  
  203.         // Discard the back buffer contents after presenting
  204.         swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  205.  
  206.         // Don't set the advanced flags
  207.         swapChainDesc.Flags = 0;
  208.  
  209.         // Set the feature level to DirectX 11
  210.         featureLevel = D3D_FEATURE_LEVEL_11_0;
  211.  
  212.         // Create the swap chain, Direct3D device, and Direct3D device context
  213.         result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &featureLevel, 1, D3D11_SDK_VERSION, &swapChainDesc, &mSwapChain, &mDevice, NULL, &mDeviceContext);
  214.         if(FAILED(result)) { return false; }
  215.  
  216.         // Get the pointer to the back buffer
  217.         result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferPtr);
  218.         if(FAILED(result)) { return false; }
  219.  
  220.         // Create the render target view with the back buffer pointer
  221.         result = mDevice->CreateRenderTargetView(backBufferPtr, NULL, &mRenderTargetView);
  222.         if(FAILED(result)) { return false; }
  223.  
  224.         // Release pointer to the back buffer as we no longer need it
  225.         backBufferPtr->Release();
  226.         backBufferPtr = 0;
  227.  
  228.         // Initialize the description of the depth buffer
  229.         ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc));
  230.  
  231.         // Set up the description of the depth buffer
  232.         depthBufferDesc.Width = windowDesc.width;
  233.         depthBufferDesc.Height = windowDesc.height;
  234.         depthBufferDesc.MipLevels = 1;
  235.         depthBufferDesc.ArraySize = 1;
  236.         depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  237.         depthBufferDesc.SampleDesc.Count = 1;
  238.         depthBufferDesc.SampleDesc.Quality = 0;
  239.         depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  240.         depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  241.         depthBufferDesc.CPUAccessFlags = 0;
  242.         depthBufferDesc.MiscFlags = 0;
  243.  
  244.         // Create the texture for the depth buffer using the filled out description
  245.         result = mDevice->CreateTexture2D(&depthBufferDesc, NULL, &mDepthStencilBuffer);
  246.         if(FAILED(result)) { return false; }
  247.  
  248.         // Initialize the description of the stencil state
  249.         ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc));
  250.  
  251.         // Set up the description of the stencil state
  252.         depthStencilDesc.DepthEnable = true;
  253.         depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
  254.         depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
  255.  
  256.         depthStencilDesc.StencilEnable = true;
  257.         depthStencilDesc.StencilReadMask = 0xFF;
  258.         depthStencilDesc.StencilWriteMask = 0xFF;
  259.  
  260.         // Stencil operations if pixel is front-facing
  261.         depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  262.         depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
  263.         depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  264.         depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  265.  
  266.         // Stencil operations if pixel is back-facing
  267.         depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  268.         depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
  269.         depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  270.         depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  271.  
  272.         // Create the depth stencil state
  273.         result = mDevice->CreateDepthStencilState(&depthStencilDesc, &mDepthStencilState);
  274.         if(FAILED(result)) { return false; }
  275.  
  276.         // Set the depth stencil state
  277.         mDeviceContext->OMSetDepthStencilState(mDepthStencilState, 1);
  278.  
  279.         // Initialize the depth stencil view
  280.         ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));
  281.  
  282.         // Set up the depth stencil view description
  283.         depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  284.         depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
  285.         depthStencilViewDesc.Texture2D.MipSlice = 0;
  286.  
  287.         // Create the depth stencil view
  288.         result = mDevice->CreateDepthStencilView(mDepthStencilBuffer, &depthStencilViewDesc, &mDepthStencilView);
  289.         if(FAILED(result)) { return false; }
  290.  
  291.         // Bind the render target view and depth stencil buffer to the output render pipeline
  292.         mDeviceContext->OMSetRenderTargets(1, &mRenderTargetView, mDepthStencilView);
  293.  
  294.         // Setup the raster description which will determine how and what polygons will be drawn
  295.         ZeroMemory(&rasterDesc, sizeof(rasterDesc));
  296.         rasterDesc.AntialiasedLineEnable = false;
  297.         rasterDesc.CullMode = D3D11_CULL_BACK;
  298.         rasterDesc.DepthBias = 0;
  299.         rasterDesc.DepthBiasClamp = 0.0f;
  300.         rasterDesc.DepthClipEnable = true;
  301.         rasterDesc.FillMode = D3D11_FILL_SOLID;
  302.         rasterDesc.FrontCounterClockwise = false;
  303.         rasterDesc.MultisampleEnable = false;
  304.         rasterDesc.ScissorEnable = false;
  305.         rasterDesc.SlopeScaledDepthBias = 0.0f;
  306.  
  307.         // Create the rasterizer state from the description we just filled out
  308.         result = mDevice->CreateRasterizerState(&rasterDesc, &mRasterState);
  309.         if(FAILED(result)) { return false; }
  310.  
  311.         // Now set the rasterizer state
  312.         mDeviceContext->RSSetState(mRasterState);
  313.  
  314.         // Setup the viewport for rendering
  315.         ZeroMemory(&viewport, sizeof(viewport));
  316.         viewport.Width = (Float)windowDesc.width;
  317.         viewport.Height = (Float)windowDesc.height;
  318.         viewport.MinDepth = 0.0f;
  319.         viewport.MaxDepth = 1.0f;
  320.         viewport.TopLeftX = 0.0f;
  321.         viewport.TopLeftY = 0.0f;
  322.  
  323.         // Create the viewport
  324.         mDeviceContext->RSSetViewports(1, &viewport);
  325.  
  326.         // Initialize the world matrices
  327.         //mWorldMatrix = DirectX::XMMatrixIdentity();
  328.  
  329.         // Initialize the projection matrix
  330.         mProjectionMatrix = DirectX::XMMatrixPerspectiveFovLH(Pi / 4.0f, (FLOAT)windowDesc.width / (FLOAT)windowDesc.height, windowDesc.near, windowDesc.far);
  331.  
  332.         // Setup the constant buffer that'll upload default data for shaders
  333.         //ZeroMemory(&matrixBufferDesc, sizeof(matrixBufferDesc));
  334.         //matrixBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  335.         //matrixBufferDesc.ByteWidth = sizeof(MatrixBuffer);
  336.         //matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  337.         //matrixBufferDesc.CPUAccessFlags = 0;
  338.  
  339.         //// Create the shader constant buffer
  340.         //result = mDevice->CreateBuffer(&matrixBufferDesc, nullptr, &mBuffer);
  341.  
  342.         // Bring the window up on the screen and set it as main focus
  343.         ShowWindow(mHWND, SW_SHOW);
  344.         SetForegroundWindow(mHWND);
  345.         SetFocus(mHWND);
  346.  
  347.         // Show/Hide the mouse cursor
  348.         ShowCursor(true);
  349.  
  350.         // Store display settings
  351.         mWindowWidth = windowDesc.width;
  352.         mWindowHeight = windowDesc.height;
  353.         mFullscreen = windowDesc.fullscreen;
  354.         mVsyncEnabled = windowDesc.vsync;
  355.  
  356.         // SUCCESS! :v
  357.         return true;
  358.     }
Add Comment
Please, Sign In to add comment