//Sample.h
#pragma once
#pragma warning( disable: 4005 )  
#pragma comment	(lib, "d3d11.lib")
#include <d3d11.h>
#include "TWindow.h"


class Sample : public TWindow
{
private:
	//--------------------------------------------------------------------------------------
	// Global Variables
	//--------------------------------------------------------------------------------------
	ID3D11Device*           m_pd3dDevice;		// 디바이스 객체	
	ID3D11DeviceContext*    m_pImmediateContext;// 디바이스 컨텍스트 객체
	IDXGISwapChain*         m_pSwapChain;		// 스왑체인 객체		
	ID3D11RenderTargetView* m_pRenderTargetView;// 메인 랜더타켓 뷰 객체

	D3D_DRIVER_TYPE			m_DriverType;		// 디바이스 타입 속성값
	D3D_FEATURE_LEVEL       m_FeatureLevel;		// Direct3D 특성레벨 속성값
	DXGI_SWAP_CHAIN_DESC	m_SwapChainDesc;	// 스왑체인 속성값 
	D3D11_VIEWPORT			m_ViewPort;			// 뷰 포트 속성값
public:
	bool		Init(); 
	bool		Render();
	bool		Release();
public:
	Sample(void);
	~Sample(void);
};


//Sample.cpp
#include "Sample.h"

// 다바이스 생성-1-
bool Sample::Init()
{
	HRESULT hr;

    UINT createDeviceFlags = 0;
    
    D3D_DRIVER_TYPE driverTypes[] =
    {
        D3D_DRIVER_TYPE_HARDWARE,
        D3D_DRIVER_TYPE_WARP,
        D3D_DRIVER_TYPE_REFERENCE,
    };
    
    UINT numDriverTypes = sizeof( driverTypes ) / sizeof( driverTypes[0] );

	D3D_FEATURE_LEVEL featureLevels[] =
    {
        D3D_FEATURE_LEVEL_11_0,
        D3D_FEATURE_LEVEL_10_1,
        D3D_FEATURE_LEVEL_10_0,
    };
    
	UINT numFeatureLevels = ARRAYSIZE( featureLevels );

    DXGI_SWAP_CHAIN_DESC sd;
    ZeroMemory( &sd, sizeof( sd ) );
    sd.BufferCount				= 1;
    sd.BufferDesc.Width			= m_iWindowWidth;
    sd.BufferDesc.Height		= m_iWindowHeight;
    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_hWnd;
    sd.SampleDesc.Count			= 1;
    sd.Windowed					= TRUE;

    for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ )
    {
        m_DriverType = driverTypes[driverTypeIndex];
        if( SUCCEEDED( hr =D3D11CreateDeviceAndSwapChain( 
			NULL, m_DriverType, NULL, createDeviceFlags,
			featureLevels, numFeatureLevels,
            D3D11_SDK_VERSION, &sd, 
			&m_pSwapChain, &m_pd3dDevice, &m_FeatureLevel, 
			&m_pImmediateContext )))
		{			
			if( FAILED( hr ) || m_FeatureLevel < D3D_FEATURE_LEVEL_11_0)
			{
				if( m_pImmediateContext ) m_pImmediateContext->Release();
				if( m_pd3dDevice ) m_pd3dDevice->Release();
				continue;
			}
			break;
		}		
    }
	if(FAILED(hr))	{
		return false;
	}
    
	//////////////////////////////////
	 // Create a render target view
    ID3D11Texture2D* pBackBuffer;
    hr = m_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer );
    if( FAILED( hr ) )         return false;

    hr = m_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &m_pRenderTargetView );
    pBackBuffer->Release();
    if( FAILED( hr ) )        return false;    
	
    m_pImmediateContext->OMSetRenderTargets( 1, &m_pRenderTargetView, NULL );


	///////////////////////////////////
	DXGI_SWAP_CHAIN_DESC Desc;
	m_pSwapChain->GetDesc( &Desc );
	// Setup the viewport    
    m_ViewPort.Width	= (FLOAT)Desc.BufferDesc.Width;
    m_ViewPort.Height	= (FLOAT)Desc.BufferDesc.Height;
    m_ViewPort.MinDepth = 0.0f;
    m_ViewPort.MaxDepth = 1.0f;
    m_ViewPort.TopLeftX = 0;
    m_ViewPort.TopLeftY = 0;	
	m_pImmediateContext->RSSetViewports( 1, &m_ViewPort );
	return true;
}

bool Sample::Render()
{	
	// 백퍼버를 지운다.
    float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f }; //red,green,blue,alpha
	m_pImmediateContext->ClearRenderTargetView( m_pRenderTargetView, ClearColor );    
	m_pSwapChain->Present( 0, 0 );
	return true;
}

bool Sample::Release()
{	
	if( m_pImmediateContext ) m_pImmediateContext->ClearState();
    if( m_pRenderTargetView ) m_pRenderTargetView->Release();
    if( m_pSwapChain ) m_pSwapChain->Release();
	if( m_pImmediateContext ) m_pImmediateContext->Release();
    if( m_pd3dDevice ) m_pd3dDevice->Release();

	m_pd3dDevice		= NULL;
	m_pSwapChain		= NULL;
	m_pRenderTargetView = NULL;
	m_pImmediateContext = NULL;
	return true;
}



Sample::Sample(void)
{
	m_pd3dDevice		= NULL;
	m_pSwapChain		= NULL;
	m_pRenderTargetView = NULL;
	m_pImmediateContext = NULL;
}

Sample::~Sample(void)
{
}

int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
{
	Sample gSample;
	if( !gSample.InitWindow( hInstance, nCmdShow,  L"CreateDevice_0" ) )
	{
		MessageBox( 0, _T("Sample  초기화 실패"), _T("Fatal error"), MB_OK );
		return 0;
	}	
	ShowCursor ( TRUE );
	gSample.Run();	
	return 1;
}

 


실행화면

'DirectX > Project' 카테고리의 다른 글

0. CreateDevice 2  (0) 2019.05.13
0. Windows 만들기  (0) 2019.05.13
0. DirectX 사용하기  (0) 2019.05.13

+ Recent posts