Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DX10Renderer.h"
- #include "Game.h"
- #include "types.h"
- #include <Windows.h>
- #include <D3D10.h>
- #include <D3DX10.h>
- #include <crtdbg.h>
- HRESULT CDX10Renderer::SetupWindow( int Width, int Height, int nCmdShow, WNDPROC WndProc )
- {
- // Register class
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof( WNDCLASSEX );
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = m_InstanceHandle;
- wcex.hIcon = NULL;
- wcex.hCursor = NULL;
- wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = L"IndriWnd";
- wcex.hIconSm = NULL;
- if( !RegisterClassEx( &wcex ) )
- return E_FAIL;
- // Create window
- RECT rc = { 0, 0, Width, Height };
- AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
- m_WindowHandle = CreateWindow( L"IndriWnd", L"IndriDX10", WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT, Width, Height, NULL, NULL, m_InstanceHandle,
- NULL );
- if( !m_WindowHandle )
- return E_FAIL;
- ShowWindow( m_WindowHandle, nCmdShow );
- // Save the viewport size
- m_ViewportWidth = Width;
- m_ViewportHeight = Height;
- return S_OK;
- }
- void CDX10Renderer::Render()
- {
- // Clear the back buffer
- float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f }; // red,green,blue,alpha
- m_Device->ClearRenderTargetView( m_RenderTargetView, ClearColor );
- // Render a triangle
- D3D10_TECHNIQUE_DESC techDesc;
- m_Technique->GetDesc( &techDesc );
- for( UINT p = 0; p < techDesc.Passes; ++p )
- {
- m_Technique->GetPassByIndex( p )->Apply( 0 );
- m_Device->Draw( 3, 0 );
- }
- // Swap buffers
- m_SwapChain->Present( 0, 0 );
- }
- HRESULT CDX10Renderer::SetupDevice()
- {
- HRESULT hr = S_OK;
- UINT createDeviceFlags = 0;
- #ifdef _DEBUG
- createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
- #endif
- DXGI_SWAP_CHAIN_DESC sd;
- ZeroMemory( &sd, sizeof( sd ) );
- sd.BufferCount = 1;
- sd.BufferDesc.Width = m_ViewportWidth;
- sd.BufferDesc.Height = m_ViewportHeight;
- 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 = m_WindowHandle;
- sd.SampleDesc.Count = 1;
- sd.SampleDesc.Quality = 0;
- sd.Windowed = TRUE;
- hr = D3D10CreateDeviceAndSwapChain( NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags,
- D3D10_SDK_VERSION, &sd, &m_SwapChain, &m_Device );
- if( FAILED( hr ) )
- return hr;
- // Create a render target view
- ID3D10Texture2D* pBuffer;
- hr = m_SwapChain->GetBuffer( 0, __uuidof( ID3D10Texture2D ), ( LPVOID* )&pBuffer );
- if( FAILED( hr ) )
- return hr;
- hr = m_Device->CreateRenderTargetView( pBuffer, NULL, &m_RenderTargetView );
- pBuffer->Release();
- if( FAILED( hr ) )
- return hr;
- m_Device->OMSetRenderTargets( 1, &m_RenderTargetView, NULL );
- // Setup the viewport
- D3D10_VIEWPORT vp;
- vp.Width = m_ViewportWidth;
- vp.Height = m_ViewportHeight;
- vp.MinDepth = 0.0f;
- vp.MaxDepth = 1.0f;
- vp.TopLeftX = 0;
- vp.TopLeftY = 0;
- m_Device->RSSetViewports( 1, &vp );
- // Create the effect
- DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
- #if defined( DEBUG ) || defined( _DEBUG )
- // Set the D3D10_SHADER_DEBUG flag to embed debug information in the shaders.
- // Setting this flag improves the shader debugging experience, but still allows
- // the shaders to be optimized and to run exactly the way they will run in
- // the release configuration of this program.
- dwShaderFlags |= D3D10_SHADER_DEBUG;
- #endif
- hr = D3DX10CreateEffectFromFile( L"default.fx", NULL, NULL, "fx_4_0", dwShaderFlags, 0,
- m_Device, NULL, NULL, &m_Effect, NULL, NULL );
- if( FAILED( hr ) )
- {
- MessageBox( NULL,
- L"The FX file cannot be located. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK );
- return hr;
- }
- // Obtain the technique
- m_Technique = m_Effect->GetTechniqueByName( "Render" );
- // Define the input layout
- D3D10_INPUT_ELEMENT_DESC layout[] =
- {
- { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
- };
- UINT numElements = sizeof( layout ) / sizeof( layout[0] );
- // Create the input layout
- D3D10_PASS_DESC PassDesc;
- m_Technique->GetPassByIndex( 0 )->GetDesc( &PassDesc );
- hr = m_Device->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature,
- PassDesc.IAInputSignatureSize, &m_VertexLayout );
- if( FAILED( hr ) )
- return hr;
- // Set the input layout
- m_Device->IASetInputLayout( m_VertexLayout );
- // Create vertex buffer
- Vertex vertices[] =
- {
- Vec4_t( 0.0f, 0.5f, 0.5f ),
- Vec4_t( 0.5f, -0.5f, 0.5f ),
- Vec4_t( -0.5f, -0.5f, 0.5f ),
- };
- D3D10_BUFFER_DESC bd;
- bd.Usage = D3D10_USAGE_DEFAULT;
- bd.ByteWidth = sizeof( Vertex ) * 3;
- bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
- bd.CPUAccessFlags = 0;
- bd.MiscFlags = 0;
- D3D10_SUBRESOURCE_DATA InitData;
- InitData.pSysMem = vertices;
- hr = m_Device->CreateBuffer( &bd, &InitData, &m_VertexBuffer );
- if( FAILED( hr ) )
- return hr;
- // Set vertex buffer
- UINT stride = sizeof( Vertex );
- UINT offset = 0;
- m_Device->IASetVertexBuffers( 0, 1, &m_VertexBuffer, &stride, &offset );
- // Set primitive topology
- m_Device->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
- return S_OK;
- }
- LRESULT CALLBACK CDX10Renderer::StaticWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- // Call the window procedure in the game object
- return CGame::GetInstance()->GameWndProc( hWnd, uMsg, wParam, lParam );
- }
- CDX10Renderer::CDX10Renderer(void)
- {
- // Get the current instance handle
- m_InstanceHandle = GetModuleHandle( NULL );
- _ASSERTE( m_InstanceHandle != NULL );
- // Call SetupWindow with the default arguments
- _ASSERTE( SetupWindow( 1600, 1000, SW_SHOW, CDX10Renderer::StaticWndProc ) != E_FAIL );
- // Create the D3D10 device
- _ASSERTE( SetupDevice() != E_FAIL );
- }
- CDX10Renderer::~CDX10Renderer(void)
- {
- // Cleanup
- if( m_Device ) m_Device->Release();
- if( m_SwapChain ) m_SwapChain->Release();
- if( m_RenderTargetView ) m_RenderTargetView->Release();
- if( m_VertexLayout ) m_VertexLayout->Release();
- if( m_Effect ) m_Effect->Release();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement