//-----------------------------------------------------------------
//
// DirectX 3D
//
//-----------------------------------------------------------------
#define sqrtf sqrt
#define sinf sin
#define cosf cos
#define tanf tan
#include <stdio.h>
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment( lib , "d3d9.dll" );
#pragma comment( lib , "d3dx9.dll" );
//-----------------------------------------------------------------
// Grobal Variables.
//-----------------------------------------------------------------
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPD3DXSPRITE g_pSprite = NULL;
LPDIRECT3DTEXTURE9 g_pTexture = NULL;
//-----------------------------------------------------------------
// Prototypes.
//-----------------------------------------------------------------
HWND InitApp( HINSTANCE , int );
BOOL InitDirect3D( HWND );
BOOL CleanupDirect3D();
BOOL RenderDirect3D();
LRESULT CALLBACK WndProc( HWND , UINT , WPARAM , LPARAM );
//-----------------------------------------------------------------
// Main.
//-----------------------------------------------------------------
int WINAPI WinMain( HINSTANCE hInst , HINSTANCE hPrevinst , LPSTR nCmdLine , int nCmdShow )
{
MSG msg;
HWND hWnd;
hWnd = InitApp( hInst , nCmdShow );
if ( !hWnd ) return FALSE;
if ( !InitDirect3D( hWnd ) ) return FALSE;
while( msg.message != WM_QUIT ){
if ( PeekMessage( &msg , NULL , 0 , 0 , PM_REMOVE ) ){
TranslateMessage( &msg );
DispatchMessage( &msg );
}else{
RenderDirect3D();
}
Sleep( 1 );
}
return msg.wParam;
}
//-----------------------------------------------------------------
// Initialize Application.
//-----------------------------------------------------------------
HWND InitApp( HINSTANCE hInst , int nCmdShow )
{
WNDCLASS wc;
HWND hWnd;
char szClassName[] = "Direct3D Test";
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.hInstance = hInst;
wc.hCursor = LoadCursor( NULL , IDC_ARROW );
wc.hIcon = LoadIcon( NULL , IDI_APPLICATION );
wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
wc.lpszClassName = szClassName;
wc.lpszMenuName = NULL;
wc.lpfnWndProc = WndProc;
wc.cbWndExtra = 0;
wc.cbClsExtra = 0;
if ( !RegisterClass( &wc ) ) return FALSE;
hWnd = CreateWindow( szClassName , "Direct3D Test" , WS_OVERLAPPEDWINDOW ,
CW_USEDEFAULT , CW_USEDEFAULT , 640 , 480,
NULL , NULL , hInst , NULL );
if ( !hWnd ) return FALSE;
ShowWindow( hWnd , nCmdShow );
UpdateWindow( hWnd );
return hWnd;
}
//-----------------------------------------------------------------
// Initialize Direct3D.
//-----------------------------------------------------------------
BOOL InitDirect3D( HWND hWnd )
{
D3DPRESENT_PARAMETERS d3dpp;
HRESULT hr;
if ( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ){
MessageBox( hWnd , "Can't create Direct3D." , "Error" , MB_OK );
return FALSE;
}
ZeroMemory( &d3dpp , sizeof( d3dpp ) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
hr = g_pD3D->CreateDevice( D3DADAPTER_DEFAULT , D3DDEVTYPE_HAL , hWnd ,
D3DCREATE_SOFTWARE_VERTEXPROCESSING ,
&d3dpp , &g_pd3dDevice );
if ( FAILED( hr ) ){
MessageBox( hWnd , "Can't create device." , "Error" , MB_OK );
return FALSE;
}
D3DXCreateSprite( g_pd3dDevice , &g_pSprite );
D3DXCreateTextureFromFile( g_pd3dDevice , "sample0004.bmp" , &g_pTexture );
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE , TRUE );
return TRUE;
}
//-----------------------------------------------------------------
// Cleanup Direct3D.
//-----------------------------------------------------------------
BOOL CleanupDirect3D()
{
if ( g_pTexture != NULL )
g_pTexture->Release();
if ( g_pSprite != NULL )
g_pSprite->Release();
if ( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
if ( g_pD3D != NULL )
g_pD3D->Release();
return TRUE;
}
//-----------------------------------------------------------------
// Window Proc.
//-----------------------------------------------------------------
LRESULT CALLBACK WndProc( HWND hWnd , UINT msg , WPARAM wp , LPARAM lp )
{
switch( msg ){
case WM_DESTROY:
CleanupDirect3D();
PostQuitMessage( 0 );
break;
default:
return DefWindowProc( hWnd , msg , wp , lp );
}
return 0L;
}
//-----------------------------------------------------------------
// Render Direct3D.
//-----------------------------------------------------------------
BOOL RenderDirect3D()
{
RECT rc;
D3DXVECTOR3 center , position;
g_pd3dDevice->Clear( 0 , NULL , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER ,
0xFFFFFFFF , 1.0f , 0 );
g_pd3dDevice->BeginScene();
g_pSprite->Begin( NULL );
// Yellow.
rc.left = 0;
rc.top = 128;
rc.right = 128;
rc.bottom = 256;
center.x = 0.0f;
center.y = 0.0f;
center.z = 0.0f;
position.x = 100.0f;
position.y = 20.0f;
position.z = 0.0f;
g_pSprite->Draw( g_pTexture , &rc , ¢er , &position , 0xFFFFFFFF );
// Blue.
rc.left = 128;
rc.top = 0;
rc.right = 256;
rc.bottom = 128;
center.x = 0.0f;
center.y = 0.0f;
center.z = 0.0f;
position.x = 180.0f;
position.y = 60.0f;
position.z = 0.5f;
g_pSprite->Draw( g_pTexture , &rc , ¢er , &position , 0xFFFFFFFF );
// Pink.
rc.left = 0;
rc.top = 0;
rc.right = 128;
rc.bottom = 128;
center.x = 0.0f;
center.y = 0.0f;
center.z = 0.0f;
position.x = 140.0f;
position.y = 100.0f;
position.z = 1.0f;
g_pSprite->Draw( g_pTexture , &rc , ¢er , &position , 0xFFFFFFFF );
g_pSprite->End();
g_pd3dDevice->EndScene();
g_pd3dDevice->Present( NULL , NULL , NULL , NULL );
return TRUE;
}
|