Guest User

Untitled

a guest
Mar 17th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. #include <windows.h>
  2. #include "Core/Window.h"
  3.  
  4. #include <d3d11.h>
  5. #include <d3dx10.h>
  6. #include <d3dx11.h>
  7. #include <d3dCompiler.h>
  8.  
  9. #pragma comment(lib,"d3d11.lib")
  10. #pragma comment(lib,"d3dx10.lib")
  11. #pragma comment(lib,"d3dx11.lib")
  12. #pragma comment(lib,"d3dCompiler.lib")
  13.  
  14. //マクロ
  15. #define SAFE_RELEASE(x) if(x){x->Release(); x=NULL;}
  16. #define WINDOW_WIDTH 640 //ウィンドウ幅
  17. #define WINDOW_HEIGHT 480 //ウィンドウ高さ
  18.  
  19. using namespace NeneLabyrinth;
  20.  
  21. ID3D11Device* m_pDevice;
  22. ID3D11DeviceContext* m_pDeviceContext;
  23. IDXGISwapChain* m_pSwapChain;
  24. ID3D11Texture2D* m_pBuckBuffer_DSTex;
  25. ID3D11RenderTargetView* m_pBackBuffer_TexRTV;
  26. ID3D11DepthStencilView* m_pBuckBuffer_DSTexDSV;
  27.  
  28. HRESULT InitializeDirectX(HWND _hWnd)
  29. {
  30. // デバイスとスワップチェーンの作成
  31. DXGI_SWAP_CHAIN_DESC sd;
  32. ZeroMemory(&sd, sizeof(sd));
  33. sd.BufferCount = 1;
  34. sd.BufferDesc.Width = WINDOW_WIDTH;
  35. sd.BufferDesc.Height = WINDOW_HEIGHT;
  36. sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  37. sd.BufferDesc.RefreshRate.Numerator = 60;
  38. sd.BufferDesc.RefreshRate.Denominator = 1;
  39. sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  40. sd.OutputWindow = _hWnd;
  41. sd.SampleDesc.Count = 1;
  42. sd.SampleDesc.Quality = 0;
  43. sd.Windowed = TRUE;
  44.  
  45. D3D_FEATURE_LEVEL pFeatureLevels = D3D_FEATURE_LEVEL_11_0;
  46. D3D_FEATURE_LEVEL* pFeatureLevel = NULL;
  47.  
  48. if (FAILED(D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL,
  49. 0, &pFeatureLevels, 1, D3D11_SDK_VERSION, &sd, &m_pSwapChain, &m_pDevice,
  50. pFeatureLevel, &m_pDeviceContext)))
  51. {
  52. return FALSE;
  53. }
  54.  
  55. //バックバッファーテクスチャーを取得(既にあるので作成ではない)
  56. ID3D11Texture2D *pBackBuffer_Tex;
  57. m_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer_Tex);
  58. //そのテクスチャーに対しレンダーターゲットビュー(RTV)を作成
  59. m_pDevice->CreateRenderTargetView(pBackBuffer_Tex, NULL, &m_pBackBuffer_TexRTV);
  60. SAFE_RELEASE(pBackBuffer_Tex);
  61.  
  62. //デプスステンシルビュー用のテクスチャーを作成
  63. D3D11_TEXTURE2D_DESC descDepth;
  64. descDepth.Width = WINDOW_WIDTH;
  65. descDepth.Height = WINDOW_HEIGHT;
  66. descDepth.MipLevels = 1;
  67. descDepth.ArraySize = 1;
  68. descDepth.Format = DXGI_FORMAT_D32_FLOAT;
  69. descDepth.SampleDesc.Count = 1;
  70. descDepth.SampleDesc.Quality = 0;
  71. descDepth.Usage = D3D11_USAGE_DEFAULT;
  72. descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  73. descDepth.CPUAccessFlags = 0;
  74. descDepth.MiscFlags = 0;
  75. m_pDevice->CreateTexture2D(&descDepth, NULL, &m_pBuckBuffer_DSTex);
  76. //そのテクスチャーに対しデプスステンシルビュー(DSV)を作成
  77. m_pDevice->CreateDepthStencilView(m_pBuckBuffer_DSTex, NULL, &m_pBuckBuffer_DSTexDSV);
  78.  
  79. //レンダーターゲットビューとデプスステンシルビューをパイプラインにセット
  80. m_pDeviceContext->OMSetRenderTargets(1, &m_pBackBuffer_TexRTV, m_pBuckBuffer_DSTexDSV);
  81.  
  82. //ビューポートの設定
  83. D3D11_VIEWPORT vp;
  84. vp.Width = WINDOW_WIDTH;
  85. vp.Height = WINDOW_HEIGHT;
  86. vp.MinDepth = 0.0f;
  87. vp.MaxDepth = 1.0f;
  88. vp.TopLeftX = 0;
  89. vp.TopLeftY = 0;
  90. m_pDeviceContext->RSSetViewports(1, &vp);
  91.  
  92. //ラスタライズ設定
  93. D3D11_RASTERIZER_DESC rdc;
  94. ZeroMemory(&rdc, sizeof(rdc));
  95. rdc.CullMode = D3D11_CULL_NONE;
  96. rdc.FillMode = D3D11_FILL_SOLID;
  97. ID3D11RasterizerState* pIr = NULL;
  98. m_pDevice->CreateRasterizerState(&rdc, &pIr);
  99. m_pDeviceContext->RSSetState(pIr);
  100.  
  101. return S_OK;
  102. }
  103. void ReleaseDirectXResource()
  104. {
  105. SAFE_RELEASE(m_pSwapChain);
  106. SAFE_RELEASE(m_pBackBuffer_TexRTV);
  107. SAFE_RELEASE(m_pDeviceContext);
  108. SAFE_RELEASE(m_pBuckBuffer_DSTexDSV);
  109. SAFE_RELEASE(m_pBuckBuffer_DSTex);
  110. SAFE_RELEASE(m_pDevice);
  111. }
  112.  
  113. void Rendering()
  114. {
  115. //画面クリア
  116. float ClearColor[4] = { 0,0,0.5,1 };// クリア色 RGBAの順
  117. m_pDeviceContext->ClearRenderTargetView(m_pBackBuffer_TexRTV, ClearColor);//カラーバッファクリア
  118. m_pDeviceContext->ClearDepthStencilView(m_pBuckBuffer_DSTexDSV, D3D11_CLEAR_DEPTH, 1.0f, 0);//デプスステンシルバッファクリア
  119.  
  120. m_pSwapChain->Present(0, 0);//画面更新(バックバッファをフロントバッファに)
  121. }
  122.  
  123. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
  124. {
  125. Core::Window window(
  126. hInstance,
  127. "NeneLabyrinth",
  128. "NeneLabyrinth",
  129. WINDOW_WIDTH,
  130. 480);
  131. window.Create();
  132.  
  133. InitializeDirectX(window);
  134.  
  135. while (!window.IsReceiveQuitMessage())
  136. {
  137. Rendering();
  138. }
  139.  
  140. ReleaseDirectXResource();
  141. return 0;
  142. }
Add Comment
Please, Sign In to add comment