Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.63 KB | None | 0 0
  1. #include "Engine.h"
  2. #include <DL_Debug.h>
  3.  
  4. #include <d3dx11.h>
  5. #include <d3dx10.h>
  6.  
  7. Engine* Engine::ourInstance = nullptr;
  8.  
  9. Engine::Engine()
  10. {
  11. }
  12.  
  13. Engine::~Engine()
  14. {
  15.     myRenderTargetView->Release();
  16.     myDepthBufferView->Release();
  17.     myDepthBuffer->Release();
  18.     mySwapChain->Release();
  19.     myContext->Release();
  20.     my3DDevice->Release();
  21. }
  22.  
  23. bool Engine::Create(HWND& aHwnd, WNDPROC aWindowProc, const SetupInfo& aInfoArgument)
  24. {
  25.     if (ourInstance == nullptr)
  26.     {
  27.         ourInstance = new Engine;
  28.  
  29.         if (ourInstance->Init(aHwnd, aWindowProc, aInfoArgument) == false)
  30.         {
  31.             DL_ASSERT("Failed to init engine");
  32.             return false;
  33.         }
  34.  
  35.         return true;
  36.     }
  37.     else
  38.     {
  39.         assert(false && "Engine already created");
  40.     }
  41. }
  42.  
  43. void Engine::ShutDown()
  44. {
  45.     delete ourInstance;
  46.     ourInstance = nullptr;
  47. }
  48.  
  49. bool Engine::Init(HWND& aHwnd, WNDPROC aWindowProc, const SetupInfo& aInfoArgument)
  50. {
  51.     myInfoArgument = aInfoArgument;
  52.  
  53.     if (WindowSetup(aWindowProc, aInfoArgument) == false)
  54.     {
  55.         return false;
  56.     }
  57.     else if (D3DSetup() == false)
  58.     {
  59.         return false;
  60.     }
  61.  
  62.     aHwnd = myHWND;
  63.  
  64.     return true;
  65. }
  66.  
  67. void Engine::SwitchBuffers()
  68. {
  69.     // clear the back buffer to a deep blue
  70.     myContext->ClearRenderTargetView(myRenderTargetView, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));
  71.  
  72.     // do 3D rendering on the back buffer here
  73.  
  74.     // switch the back buffer and the front buffer
  75.     mySwapChain->Present(0, 0);
  76. }
  77.  
  78. void Engine::OnResize(int aWidth, int aHeight)
  79. {
  80.     mySwapChain->ResizeBuffers(1, aWidth, aHeight, DXGI_FORMAT_R8G8B8A8_UNORM, );
  81. }
  82.  
  83. bool Engine::WindowSetup(WNDPROC aWindowProc, const SetupInfo& aInfoArgument)
  84. {
  85.     HINSTANCE Hinstance = GetModuleHandle(NULL);
  86.  
  87.     std::wstring sessionName(aInfoArgument.mySessionName.begin(), aInfoArgument.mySessionName.end());
  88.  
  89.     WNDCLASSEX wcex;
  90.     wcex.cbSize = sizeof(WNDCLASSEX);
  91.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  92.     wcex.lpfnWndProc = aWindowProc;
  93.     wcex.cbClsExtra = 0;
  94.     wcex.cbWndExtra = 0;
  95.     wcex.hInstance = Hinstance;
  96.     wcex.hIcon = LoadIcon(Hinstance, (LPCTSTR)IDI_APPLICATION);
  97.     wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  98.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  99.     wcex.lpszMenuName = NULL;
  100.     wcex.lpszClassName = sessionName.c_str();
  101.     wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_APPLICATION);
  102.     if (!RegisterClassEx(&wcex))
  103.     {
  104.         return E_FAIL;
  105.     }
  106.  
  107.     RECT rc = { 0, 0, aInfoArgument.myResolution.x, aInfoArgument.myResolution.y };
  108.     AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
  109.     myHWND = CreateWindow(sessionName.c_str(), sessionName.c_str(),
  110.         WS_OVERLAPPEDWINDOW,
  111.         CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, GetModuleHandle(NULL),
  112.         NULL);
  113.  
  114.     ShowWindow(myHWND, 10);
  115.  
  116.     if (!myHWND)
  117.     {
  118.         return false;
  119.     }
  120.  
  121.     return true;
  122. }
  123.  
  124. bool Engine::D3DSetup()
  125. {
  126.     if (D3DDeviceSetup() == false)
  127.     {
  128.         return false;
  129.     }
  130.     else if (D3DSwapChainSetup() == false)
  131.     {
  132.         return false;
  133.     }
  134.     else if (D3DViewPortSetup(myInfoArgument.myResolution.x, myInfoArgument.myResolution.y) == false)
  135.     {
  136.         return false;
  137.     }
  138.     else if (D3DStencilBufferSetup(myInfoArgument.myResolution.x, myInfoArgument.myResolution.y) == false)
  139.     {
  140.         return false;
  141.     }
  142.  
  143.     return true;
  144. }
  145.  
  146. bool Engine::D3DDeviceSetup()
  147. {
  148.     // create a struct to hold information about the swap chain
  149.     DXGI_SWAP_CHAIN_DESC scd;
  150.  
  151.     // clear out the struct for use
  152.     ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
  153.  
  154.     // fill the swap chain description struct
  155.     scd.BufferCount = 1;                                    // one back buffer
  156.     scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;     // use 32-bit color
  157.     scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;      // how swap chain is to be used
  158.     scd.OutputWindow = myHWND;                                // the window to be used
  159.     scd.SampleDesc.Count = 4;                               // how many multisamples
  160.     scd.Windowed = TRUE;                                    // windowed/full-screen mode
  161.  
  162.     // create a device, device context and swap chain using the information in the scd struct
  163.     D3D11CreateDeviceAndSwapChain(NULL,
  164.         D3D_DRIVER_TYPE_HARDWARE,
  165.         NULL,
  166.         NULL,
  167.         NULL,
  168.         NULL,
  169.         D3D11_SDK_VERSION,
  170.         &scd,
  171.         &mySwapChain,
  172.         &my3DDevice,
  173.         NULL,
  174.         &myContext);
  175.  
  176.     return true;
  177. }
  178.  
  179. bool Engine::D3DSwapChainSetup()
  180. {
  181.     // get the address of the back buffer
  182.     ID3D11Texture2D *pBackBuffer;
  183.     mySwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
  184.  
  185.     // use the back buffer address to create the render target
  186.     my3DDevice->CreateRenderTargetView(pBackBuffer, NULL, &myRenderTargetView);
  187.     pBackBuffer->Release();
  188.  
  189.     // set the render target as the back buffer
  190.     myContext->OMSetRenderTargets(1, &myRenderTargetView, NULL);
  191.  
  192.     return true;
  193. }
  194.  
  195. bool Engine::D3DViewPortSetup(int aWidth, int aHeight)
  196. {
  197.     D3D11_VIEWPORT vp;
  198.     vp.Width = aWidth;
  199.     vp.Height = aHeight;
  200.     vp.MinDepth = 0.0f;
  201.     vp.MaxDepth = 1.0f;
  202.     vp.TopLeftX = 0;
  203.     vp.TopLeftY = 0;
  204.     myContext->RSSetViewports(1, &vp);
  205.  
  206.     return true;
  207. }
  208.  
  209. bool Engine::D3DStencilBufferSetup(int aWidth, int aHeight)
  210. {
  211.     // create the depth buffer texture
  212.     D3D11_TEXTURE2D_DESC texd;
  213.     ZeroMemory(&texd, sizeof(texd));
  214.  
  215.     texd.Width = aWidth;
  216.     texd.Height = aHeight;
  217.     texd.ArraySize = 1;
  218.     texd.MipLevels = 1;
  219.     texd.SampleDesc.Count = 4;
  220.     texd.Format = DXGI_FORMAT_D32_FLOAT;
  221.     texd.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  222.  
  223.     my3DDevice->CreateTexture2D(&texd, NULL, &myDepthBuffer);
  224.  
  225.     // create the depth buffer
  226.     D3D11_DEPTH_STENCIL_VIEW_DESC dsvd;
  227.     ZeroMemory(&dsvd, sizeof(dsvd));
  228.  
  229.     dsvd.Format = DXGI_FORMAT_D32_FLOAT;
  230.     dsvd.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
  231.  
  232.     my3DDevice->CreateDepthStencilView(myDepthBuffer, &dsvd, &myDepthBufferView);
  233.  
  234.     return true;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement