Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include "Core/Window.h"
- #include <d3d11.h>
- #include <d3dx10.h>
- #include <d3dx11.h>
- #include <d3dCompiler.h>
- #pragma comment(lib,"d3d11.lib")
- #pragma comment(lib,"d3dx10.lib")
- #pragma comment(lib,"d3dx11.lib")
- #pragma comment(lib,"d3dCompiler.lib")
- //マクロ
- #define SAFE_RELEASE(x) if(x){x->Release(); x=NULL;}
- #define WINDOW_WIDTH 640 //ウィンドウ幅
- #define WINDOW_HEIGHT 480 //ウィンドウ高さ
- using namespace NeneLabyrinth;
- ID3D11Device* m_pDevice;
- ID3D11DeviceContext* m_pDeviceContext;
- IDXGISwapChain* m_pSwapChain;
- ID3D11Texture2D* m_pBuckBuffer_DSTex;
- ID3D11RenderTargetView* m_pBackBuffer_TexRTV;
- ID3D11DepthStencilView* m_pBuckBuffer_DSTexDSV;
- HRESULT InitializeDirectX(HWND _hWnd)
- {
- // デバイスとスワップチェーンの作成
- DXGI_SWAP_CHAIN_DESC sd;
- ZeroMemory(&sd, sizeof(sd));
- sd.BufferCount = 1;
- sd.BufferDesc.Width = WINDOW_WIDTH;
- sd.BufferDesc.Height = WINDOW_HEIGHT;
- sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- sd.BufferDesc.RefreshRate.Numerator = 60;
- sd.BufferDesc.RefreshRate.Denominator = 1;
- sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
- sd.OutputWindow = _hWnd;
- sd.SampleDesc.Count = 1;
- sd.SampleDesc.Quality = 0;
- sd.Windowed = TRUE;
- D3D_FEATURE_LEVEL pFeatureLevels = D3D_FEATURE_LEVEL_11_0;
- D3D_FEATURE_LEVEL* pFeatureLevel = NULL;
- if (FAILED(D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL,
- 0, &pFeatureLevels, 1, D3D11_SDK_VERSION, &sd, &m_pSwapChain, &m_pDevice,
- pFeatureLevel, &m_pDeviceContext)))
- {
- return FALSE;
- }
- //バックバッファーテクスチャーを取得(既にあるので作成ではない)
- ID3D11Texture2D *pBackBuffer_Tex;
- m_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer_Tex);
- //そのテクスチャーに対しレンダーターゲットビュー(RTV)を作成
- m_pDevice->CreateRenderTargetView(pBackBuffer_Tex, NULL, &m_pBackBuffer_TexRTV);
- SAFE_RELEASE(pBackBuffer_Tex);
- //デプスステンシルビュー用のテクスチャーを作成
- D3D11_TEXTURE2D_DESC descDepth;
- descDepth.Width = WINDOW_WIDTH;
- descDepth.Height = WINDOW_HEIGHT;
- descDepth.MipLevels = 1;
- descDepth.ArraySize = 1;
- descDepth.Format = DXGI_FORMAT_D32_FLOAT;
- descDepth.SampleDesc.Count = 1;
- descDepth.SampleDesc.Quality = 0;
- descDepth.Usage = D3D11_USAGE_DEFAULT;
- descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
- descDepth.CPUAccessFlags = 0;
- descDepth.MiscFlags = 0;
- m_pDevice->CreateTexture2D(&descDepth, NULL, &m_pBuckBuffer_DSTex);
- //そのテクスチャーに対しデプスステンシルビュー(DSV)を作成
- m_pDevice->CreateDepthStencilView(m_pBuckBuffer_DSTex, NULL, &m_pBuckBuffer_DSTexDSV);
- //レンダーターゲットビューとデプスステンシルビューをパイプラインにセット
- m_pDeviceContext->OMSetRenderTargets(1, &m_pBackBuffer_TexRTV, m_pBuckBuffer_DSTexDSV);
- //ビューポートの設定
- D3D11_VIEWPORT vp;
- vp.Width = WINDOW_WIDTH;
- vp.Height = WINDOW_HEIGHT;
- vp.MinDepth = 0.0f;
- vp.MaxDepth = 1.0f;
- vp.TopLeftX = 0;
- vp.TopLeftY = 0;
- m_pDeviceContext->RSSetViewports(1, &vp);
- //ラスタライズ設定
- D3D11_RASTERIZER_DESC rdc;
- ZeroMemory(&rdc, sizeof(rdc));
- rdc.CullMode = D3D11_CULL_NONE;
- rdc.FillMode = D3D11_FILL_SOLID;
- ID3D11RasterizerState* pIr = NULL;
- m_pDevice->CreateRasterizerState(&rdc, &pIr);
- m_pDeviceContext->RSSetState(pIr);
- return S_OK;
- }
- void ReleaseDirectXResource()
- {
- SAFE_RELEASE(m_pSwapChain);
- SAFE_RELEASE(m_pBackBuffer_TexRTV);
- SAFE_RELEASE(m_pDeviceContext);
- SAFE_RELEASE(m_pBuckBuffer_DSTexDSV);
- SAFE_RELEASE(m_pBuckBuffer_DSTex);
- SAFE_RELEASE(m_pDevice);
- }
- void Rendering()
- {
- //画面クリア
- float ClearColor[4] = { 0,0,0.5,1 };// クリア色 RGBAの順
- m_pDeviceContext->ClearRenderTargetView(m_pBackBuffer_TexRTV, ClearColor);//カラーバッファクリア
- m_pDeviceContext->ClearDepthStencilView(m_pBuckBuffer_DSTexDSV, D3D11_CLEAR_DEPTH, 1.0f, 0);//デプスステンシルバッファクリア
- m_pSwapChain->Present(0, 0);//画面更新(バックバッファをフロントバッファに)
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
- {
- Core::Window window(
- hInstance,
- "NeneLabyrinth",
- "NeneLabyrinth",
- WINDOW_WIDTH,
- 480);
- window.Create();
- InitializeDirectX(window);
- while (!window.IsReceiveQuitMessage())
- {
- Rendering();
- }
- ReleaseDirectXResource();
- return 0;
- }
Add Comment
Please, Sign In to add comment