Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Engine.h"
- #include <DL_Debug.h>
- #include <d3dx11.h>
- #include <d3dx10.h>
- Engine* Engine::ourInstance = nullptr;
- Engine::Engine()
- {
- }
- Engine::~Engine()
- {
- myRenderTargetView->Release();
- myDepthBufferView->Release();
- myDepthBuffer->Release();
- mySwapChain->Release();
- myContext->Release();
- my3DDevice->Release();
- }
- bool Engine::Create(HWND& aHwnd, WNDPROC aWindowProc, const SetupInfo& aInfoArgument)
- {
- if (ourInstance == nullptr)
- {
- ourInstance = new Engine;
- if (ourInstance->Init(aHwnd, aWindowProc, aInfoArgument) == false)
- {
- DL_ASSERT("Failed to init engine");
- return false;
- }
- return true;
- }
- else
- {
- assert(false && "Engine already created");
- }
- }
- void Engine::ShutDown()
- {
- delete ourInstance;
- ourInstance = nullptr;
- }
- bool Engine::Init(HWND& aHwnd, WNDPROC aWindowProc, const SetupInfo& aInfoArgument)
- {
- myInfoArgument = aInfoArgument;
- if (WindowSetup(aWindowProc, aInfoArgument) == false)
- {
- return false;
- }
- else if (D3DSetup() == false)
- {
- return false;
- }
- aHwnd = myHWND;
- return true;
- }
- void Engine::SwitchBuffers()
- {
- // clear the back buffer to a deep blue
- myContext->ClearRenderTargetView(myRenderTargetView, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));
- // do 3D rendering on the back buffer here
- // switch the back buffer and the front buffer
- mySwapChain->Present(0, 0);
- }
- void Engine::OnResize(int aWidth, int aHeight)
- {
- mySwapChain->ResizeBuffers(1, aWidth, aHeight, DXGI_FORMAT_R8G8B8A8_UNORM, );
- }
- bool Engine::WindowSetup(WNDPROC aWindowProc, const SetupInfo& aInfoArgument)
- {
- HINSTANCE Hinstance = GetModuleHandle(NULL);
- std::wstring sessionName(aInfoArgument.mySessionName.begin(), aInfoArgument.mySessionName.end());
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = aWindowProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = Hinstance;
- wcex.hIcon = LoadIcon(Hinstance, (LPCTSTR)IDI_APPLICATION);
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = sessionName.c_str();
- wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_APPLICATION);
- if (!RegisterClassEx(&wcex))
- {
- return E_FAIL;
- }
- RECT rc = { 0, 0, aInfoArgument.myResolution.x, aInfoArgument.myResolution.y };
- AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
- myHWND = CreateWindow(sessionName.c_str(), sessionName.c_str(),
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, GetModuleHandle(NULL),
- NULL);
- ShowWindow(myHWND, 10);
- if (!myHWND)
- {
- return false;
- }
- return true;
- }
- bool Engine::D3DSetup()
- {
- if (D3DDeviceSetup() == false)
- {
- return false;
- }
- else if (D3DSwapChainSetup() == false)
- {
- return false;
- }
- else if (D3DViewPortSetup(myInfoArgument.myResolution.x, myInfoArgument.myResolution.y) == false)
- {
- return false;
- }
- else if (D3DStencilBufferSetup(myInfoArgument.myResolution.x, myInfoArgument.myResolution.y) == false)
- {
- return false;
- }
- return true;
- }
- bool Engine::D3DDeviceSetup()
- {
- // create a struct to hold information about the swap chain
- DXGI_SWAP_CHAIN_DESC scd;
- // clear out the struct for use
- ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
- // fill the swap chain description struct
- scd.BufferCount = 1; // one back buffer
- scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
- scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
- scd.OutputWindow = myHWND; // the window to be used
- scd.SampleDesc.Count = 4; // how many multisamples
- scd.Windowed = TRUE; // windowed/full-screen mode
- // create a device, device context and swap chain using the information in the scd struct
- D3D11CreateDeviceAndSwapChain(NULL,
- D3D_DRIVER_TYPE_HARDWARE,
- NULL,
- NULL,
- NULL,
- NULL,
- D3D11_SDK_VERSION,
- &scd,
- &mySwapChain,
- &my3DDevice,
- NULL,
- &myContext);
- return true;
- }
- bool Engine::D3DSwapChainSetup()
- {
- // get the address of the back buffer
- ID3D11Texture2D *pBackBuffer;
- mySwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
- // use the back buffer address to create the render target
- my3DDevice->CreateRenderTargetView(pBackBuffer, NULL, &myRenderTargetView);
- pBackBuffer->Release();
- // set the render target as the back buffer
- myContext->OMSetRenderTargets(1, &myRenderTargetView, NULL);
- return true;
- }
- bool Engine::D3DViewPortSetup(int aWidth, int aHeight)
- {
- D3D11_VIEWPORT vp;
- vp.Width = aWidth;
- vp.Height = aHeight;
- vp.MinDepth = 0.0f;
- vp.MaxDepth = 1.0f;
- vp.TopLeftX = 0;
- vp.TopLeftY = 0;
- myContext->RSSetViewports(1, &vp);
- return true;
- }
- bool Engine::D3DStencilBufferSetup(int aWidth, int aHeight)
- {
- // create the depth buffer texture
- D3D11_TEXTURE2D_DESC texd;
- ZeroMemory(&texd, sizeof(texd));
- texd.Width = aWidth;
- texd.Height = aHeight;
- texd.ArraySize = 1;
- texd.MipLevels = 1;
- texd.SampleDesc.Count = 4;
- texd.Format = DXGI_FORMAT_D32_FLOAT;
- texd.BindFlags = D3D11_BIND_DEPTH_STENCIL;
- my3DDevice->CreateTexture2D(&texd, NULL, &myDepthBuffer);
- // create the depth buffer
- D3D11_DEPTH_STENCIL_VIEW_DESC dsvd;
- ZeroMemory(&dsvd, sizeof(dsvd));
- dsvd.Format = DXGI_FORMAT_D32_FLOAT;
- dsvd.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
- my3DDevice->CreateDepthStencilView(myDepthBuffer, &dsvd, &myDepthBufferView);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement