본문 바로가기
  • fishing...
  • eating...
MISCELLANEOUSNESS

Direct X SDK Tutorial 01: Create Device & Vertex

by 회색뿔 2008. 4. 3.

invalid-file

CreateDevice&Vertex



사용자 삽입 이미지


#include <d3d9.h>

#define D3DFVF_CUSTOMVERTEX ( D3DFVF_XYZRHW | D3DFVF_DIFFUSE )

struct CUSTOMVERTEX
{
 FLOAT x, y, z, rhw;
 DWORD color;
};

LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;

VOID Cleanup();
VOID Render();

HRESULT InitD3D( HWND hWnd );
HRESULT InitVB();
HRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );

INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
 WNDCLASSEX wc = { sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
  NULL, NULL, NULL, NULL, NULL, TEXT( "D3d Tutorial" ), NULL };

 RegisterClassEx( &wc );

 HWND hWnd = CreateWindow( TEXT( "D3D Tutorial" ), TEXT( "D3D Tutorial 01: CreateDevice" ),
  WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, GetDesktopWindow(), NULL, wc.hInstance, NULL );

 if( SUCCEEDED( InitD3D(hWnd) ) )
 {

  if( SUCCEEDED( InitVB() ) )
  {
   ShowWindow( hWnd, SW_SHOW );
   UpdateWindow( hWnd );

   MSG msg;

   ZeroMemory( &msg, sizeof( msg ) );

   while ( msg.message != WM_QUIT )
   {
    if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
    {
     TranslateMessage( &msg );
     DispatchMessage( &msg );
    }
    else
     Render();
   }
  }
 }

 UnregisterClass( TEXT( "D3D Tutorial" ), wc.hInstance );
 return 0;
}

VOID Cleanup()
{
 if( g_pd3dDevice != NULL )
  g_pd3dDevice->Release();

 if( g_pD3D != NULL )
  g_pD3D->Release();

 if( g_pVB != NULL )
  g_pVB->Release();
}

VOID Render()
{
 if( NULL == g_pd3dDevice )
  return;

 g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255 ), 1.0F, 0 );

 if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
 {
  g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
  g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
  g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );

  g_pd3dDevice->EndScene();
 }
 g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}


HRESULT InitD3D( HWND hWnd )
{
 if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
  return E_FAIL;

 D3DPRESENT_PARAMETERS d3dpp;

 ZeroMemory( &d3dpp, sizeof( d3dpp ) );

 d3dpp.Windowed = TRUE;
 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
 d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

 if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) )
 {
  return E_FAIL;
 }

 return S_OK;
}

HRESULT InitVB()
{
 CUSTOMVERTEX vertices[] =
 {
  { 150.0f,  50.0f, 0.5f, 1.0f, 0xffff0000, },
  { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },
  {  50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },
 };
 
 if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3 * sizeof( CUSTOMVERTEX ), 0,
  D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
 {
  return E_FAIL;
 }

 VOID *pVertices;

 if( FAILED( g_pVB->Lock( 0, sizeof( vertices ), (VOID **)&pVertices, 0 )))
 {
  return E_FAIL;
 }

 memcpy( pVertices, vertices, sizeof( vertices) );
 g_pVB->Unlock();
 
 return S_OK;
}


HRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
 switch( msg )
 {
 case WM_DESTROY:
   Cleanup();
   PostQuitMessage( 0 );
   return 0;
 }
 return DefWindowProc( hWnd, msg, wParam, lParam );
}


IT Expert S 3D 게임 프로그래밍 Chapter 1

'MISCELLANEOUSNESS' 카테고리의 다른 글

Direct X SDK Tutorial 02: Matrices  (2) 2008.04.06
nokia 미래형 휴대폰 단말기 morph  (2) 2008.03.23
데스노트 패러디...ㅋㅋ;;  (0) 2008.03.02