Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.72 KB | None | 0 0
  1. #include "DX10Renderer.h"
  2. #include "Game.h"
  3. #include "types.h"
  4. #include <Windows.h>
  5. #include <D3D10.h>
  6. #include <D3DX10.h>
  7. #include <crtdbg.h>
  8.  
  9. HRESULT CDX10Renderer::SetupWindow( int Width, int Height, int nCmdShow, WNDPROC WndProc )
  10. {
  11.     // Register class
  12.     WNDCLASSEX wcex;
  13.     wcex.cbSize = sizeof( WNDCLASSEX );
  14.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  15.     wcex.lpfnWndProc = WndProc;
  16.     wcex.cbClsExtra = 0;
  17.     wcex.cbWndExtra = 0;
  18.     wcex.hInstance = m_InstanceHandle;
  19.     wcex.hIcon = NULL;
  20.     wcex.hCursor = NULL;
  21.     wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
  22.     wcex.lpszMenuName = NULL;
  23.     wcex.lpszClassName = L"IndriWnd";
  24.     wcex.hIconSm = NULL;
  25.  
  26.     if( !RegisterClassEx( &wcex ) )
  27.         return E_FAIL;
  28.  
  29.     // Create window
  30.     RECT rc = { 0, 0, Width, Height };
  31.     AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
  32.  
  33.     m_WindowHandle = CreateWindow( L"IndriWnd", L"IndriDX10", WS_OVERLAPPEDWINDOW,
  34.                                  CW_USEDEFAULT, CW_USEDEFAULT, Width, Height, NULL, NULL, m_InstanceHandle,
  35.                                  NULL );
  36.     if( !m_WindowHandle )
  37.         return E_FAIL;
  38.  
  39.     ShowWindow( m_WindowHandle, nCmdShow );
  40.  
  41.     // Save the viewport size
  42.     m_ViewportWidth = Width;
  43.     m_ViewportHeight = Height;
  44.  
  45.     return S_OK;
  46. }
  47.  
  48. void CDX10Renderer::Render()
  49. {
  50.     // Clear the back buffer
  51.     float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f }; // red,green,blue,alpha
  52.     m_Device->ClearRenderTargetView( m_RenderTargetView, ClearColor );
  53.  
  54.     // Render a triangle
  55.     D3D10_TECHNIQUE_DESC techDesc;
  56.     m_Technique->GetDesc( &techDesc );
  57.     for( UINT p = 0; p < techDesc.Passes; ++p )
  58.     {
  59.         m_Technique->GetPassByIndex( p )->Apply( 0 );
  60.         m_Device->Draw( 3, 0 );
  61.     }
  62.  
  63.     // Swap buffers
  64.     m_SwapChain->Present( 0, 0 );
  65. }
  66.  
  67. HRESULT CDX10Renderer::SetupDevice()
  68. {
  69.     HRESULT hr = S_OK;
  70.  
  71.     UINT createDeviceFlags = 0;
  72. #ifdef _DEBUG
  73.     createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
  74. #endif
  75.  
  76.     DXGI_SWAP_CHAIN_DESC sd;
  77.     ZeroMemory( &sd, sizeof( sd ) );
  78.  
  79.     sd.BufferCount = 1;
  80.     sd.BufferDesc.Width = m_ViewportWidth;
  81.     sd.BufferDesc.Height = m_ViewportHeight;
  82.     sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  83.     sd.BufferDesc.RefreshRate.Numerator = 60;
  84.     sd.BufferDesc.RefreshRate.Denominator = 1;
  85.     sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  86.     sd.OutputWindow = m_WindowHandle;
  87.     sd.SampleDesc.Count = 1;
  88.     sd.SampleDesc.Quality = 0;
  89.     sd.Windowed = TRUE;
  90.  
  91.     hr = D3D10CreateDeviceAndSwapChain( NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags,
  92.                                         D3D10_SDK_VERSION, &sd, &m_SwapChain, &m_Device );
  93.  
  94.     if( FAILED( hr ) )
  95.         return hr;
  96.  
  97.     // Create a render target view
  98.     ID3D10Texture2D* pBuffer;
  99.     hr = m_SwapChain->GetBuffer( 0, __uuidof( ID3D10Texture2D ), ( LPVOID* )&pBuffer );
  100.     if( FAILED( hr ) )
  101.         return hr;
  102.  
  103.     hr = m_Device->CreateRenderTargetView( pBuffer, NULL, &m_RenderTargetView );
  104.     pBuffer->Release();
  105.     if( FAILED( hr ) )
  106.         return hr;
  107.  
  108.     m_Device->OMSetRenderTargets( 1, &m_RenderTargetView, NULL );
  109.  
  110.     // Setup the viewport
  111.     D3D10_VIEWPORT vp;
  112.     vp.Width = m_ViewportWidth;
  113.     vp.Height = m_ViewportHeight;
  114.     vp.MinDepth = 0.0f;
  115.     vp.MaxDepth = 1.0f;
  116.     vp.TopLeftX = 0;
  117.     vp.TopLeftY = 0;
  118.     m_Device->RSSetViewports( 1, &vp );
  119.  
  120.     // Create the effect
  121.     DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
  122. #if defined( DEBUG ) || defined( _DEBUG )
  123.     // Set the D3D10_SHADER_DEBUG flag to embed debug information in the shaders.
  124.     // Setting this flag improves the shader debugging experience, but still allows
  125.     // the shaders to be optimized and to run exactly the way they will run in
  126.     // the release configuration of this program.
  127.     dwShaderFlags |= D3D10_SHADER_DEBUG;
  128.     #endif
  129.     hr = D3DX10CreateEffectFromFile( L"default.fx", NULL, NULL, "fx_4_0", dwShaderFlags, 0,
  130.                                          m_Device, NULL, NULL, &m_Effect, NULL, NULL );
  131.     if( FAILED( hr ) )
  132.     {
  133.         MessageBox( NULL,
  134.                     L"The FX file cannot be located.  Please run this executable from the directory that contains the FX file.", L"Error", MB_OK );
  135.         return hr;
  136.     }
  137.  
  138.     // Obtain the technique
  139.     m_Technique = m_Effect->GetTechniqueByName( "Render" );
  140.  
  141.     // Define the input layout
  142.     D3D10_INPUT_ELEMENT_DESC layout[] =
  143.     {
  144.         { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
  145.     };
  146.     UINT numElements = sizeof( layout ) / sizeof( layout[0] );
  147.  
  148.     // Create the input layout
  149.     D3D10_PASS_DESC PassDesc;
  150.     m_Technique->GetPassByIndex( 0 )->GetDesc( &PassDesc );
  151.     hr = m_Device->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature,
  152.                                           PassDesc.IAInputSignatureSize, &m_VertexLayout );
  153.     if( FAILED( hr ) )
  154.         return hr;
  155.  
  156.     // Set the input layout
  157.     m_Device->IASetInputLayout( m_VertexLayout );
  158.  
  159.     // Create vertex buffer
  160.     Vertex vertices[] =
  161.     {
  162.         Vec4_t( 0.0f, 0.5f, 0.5f ),
  163.         Vec4_t( 0.5f, -0.5f, 0.5f ),
  164.         Vec4_t( -0.5f, -0.5f, 0.5f ),
  165.     };
  166.  
  167.     D3D10_BUFFER_DESC bd;
  168.     bd.Usage = D3D10_USAGE_DEFAULT;
  169.     bd.ByteWidth = sizeof( Vertex ) * 3;
  170.     bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
  171.     bd.CPUAccessFlags = 0;
  172.     bd.MiscFlags = 0;
  173.  
  174.     D3D10_SUBRESOURCE_DATA InitData;
  175.     InitData.pSysMem = vertices;
  176.  
  177.     hr = m_Device->CreateBuffer( &bd, &InitData, &m_VertexBuffer );
  178.    
  179.     if( FAILED( hr ) )
  180.         return hr;
  181.  
  182.     // Set vertex buffer
  183.     UINT stride = sizeof( Vertex );
  184.     UINT offset = 0;
  185.     m_Device->IASetVertexBuffers( 0, 1, &m_VertexBuffer, &stride, &offset );
  186.  
  187.     // Set primitive topology
  188.     m_Device->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
  189.  
  190.     return S_OK;
  191. }
  192.  
  193. LRESULT CALLBACK CDX10Renderer::StaticWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  194. {
  195.     // Call the window procedure in the game object
  196.     return CGame::GetInstance()->GameWndProc( hWnd, uMsg, wParam, lParam );
  197. }
  198.  
  199. CDX10Renderer::CDX10Renderer(void)
  200. {
  201.     // Get the current instance handle
  202.     m_InstanceHandle = GetModuleHandle( NULL );
  203.     _ASSERTE( m_InstanceHandle != NULL );
  204.  
  205.     // Call SetupWindow with the default arguments
  206.     _ASSERTE( SetupWindow( 1600, 1000, SW_SHOW, CDX10Renderer::StaticWndProc ) != E_FAIL );
  207.  
  208.     // Create the D3D10 device
  209.     _ASSERTE( SetupDevice() != E_FAIL );
  210. }
  211.  
  212. CDX10Renderer::~CDX10Renderer(void)
  213. {
  214.     // Cleanup
  215.     if( m_Device ) m_Device->Release();
  216.     if( m_SwapChain ) m_SwapChain->Release();
  217.     if( m_RenderTargetView ) m_RenderTargetView->Release();
  218.     if( m_VertexLayout ) m_VertexLayout->Release();
  219.     if( m_Effect ) m_Effect->Release();
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement