www.pudn.com > bookcode.rar > main.cpp
//Example2_2 //main.cpp //Ernest Pazera //07OCT2001 //TGO-02-A //Libs: d3d8.lib #include//include windows stuff #include //standard input/output #include "D3D8.h" //include direct3d8 stuff #include "d3dfmtutils.h" //include format utility functions //constants //window class name const char* WINDOWCLASS = "3D42DGP" ; //window title const char* WINDOWTITLE = "Example 2.2 (TGO-02-A): Creating a Device (Full Screen)" ; //globals //instance handle HINSTANCE g_hInstance = NULL ; //window handle HWND g_hWnd = NULL ; //IDirect3D8 pointer IDirect3D8* g_pd3d = NULL ; //device type in use D3DDEVTYPE g_devtype ; //device pointer IDirect3DDevice8* g_pd3ddev = NULL ; //function prototypes //winmain int WINAPI WinMain ( HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nShowCmd ) ; //window procedure LRESULT CALLBACK TheWindowProc ( HWND hWnd , UINT uMsg , WPARAM wParam , LPARAM lParam ) ; //initialization void Prog_Init ( ) ; //clean up void Prog_Done ( ) ; //window procedure LRESULT CALLBACK TheWindowProc ( HWND hWnd , UINT uMsg , WPARAM wParam , LPARAM lParam ) { //which message did we get? switch ( uMsg ) { case WM_DESTROY : //window being destroyed { //quit PostQuitMessage ( 0 ) ; //message handled, return 0 return ( 0 ) ; } break ; default: //all other messages, send to default handler return ( DefWindowProc ( hWnd , uMsg , wParam , lParam ) ) ; } } //winmain int WINAPI WinMain ( HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nShowCmd ) { //grab instance handle g_hInstance = hInstance ; //redirect stderr and stdout output freopen ( "stdout.txt" , "w" , stdout ) ; //fill in window class WNDCLASSEX wc ; wc.cbClsExtra = 0 ; //no extra class information wc.cbSize = sizeof ( WNDCLASSEX ) ; //size of structure wc.cbWndExtra = 0 ; //no extra window information wc.hbrBackground = ( HBRUSH ) GetStockObject ( BLACK_BRUSH ) ; //black brush wc.hCursor = NULL ; //no cursor wc.hIcon = NULL ; //no icon wc.hIconSm = NULL ; //no small icon wc.hInstance = g_hInstance ; //instance handle wc.lpfnWndProc = TheWindowProc ; //window procedure wc.lpszClassName = WINDOWCLASS ; //name of class wc.lpszMenuName = NULL ; //no menu wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_OWNDC ; //class styles //register window class RegisterClassEx ( &wc ) ; //create window g_hWnd = CreateWindowEx ( 0 , WINDOWCLASS , WINDOWTITLE , WS_OVERLAPPEDWINDOW , 0 , 0 , 640 , 480 , NULL , NULL , g_hInstance , NULL ) ; //show the window ShowWindow ( g_hWnd , nShowCmd ) ; //initialization Prog_Init ( ) ; MSG msg ; //message pump for ( ; ; ) { //check for a message if ( PeekMessage( &msg , NULL , 0 , 0 , PM_REMOVE ) ) { //message exists //check for quit message if ( msg.message == WM_QUIT ) break ; //translate the message TranslateMessage ( &msg ) ; //dispatch the message DispatchMessage ( &msg ) ; } } //clean up Prog_Done ( ) ; //exit return ( msg.wParam ) ; } //initialization void Prog_Init ( ) { //create the IDirect3D8 object g_pd3d = Direct3DCreate8 ( D3D_SDK_VERSION ) ; //error check if ( g_pd3d ) { //success fprintf ( stdout , "IDirect3D8 object created successfully.\n" ) ; } else { //failure fprintf ( stdout , "IDirect3D8 object creation failed.\n" ) ; //cannot proceed, so return return ; } //take the first display mode listed for the default adapter D3DDISPLAYMODE mode ; g_pd3d->EnumAdapterModes ( D3DADAPTER_DEFAULT , 0 , &mode ) ; //set up presentation parameters D3DPRESENT_PARAMETERS parms; //back buffer information parms.BackBufferWidth = mode.Width ; //use mode width parms.BackBufferHeight = mode.Height ; //use mode height parms.BackBufferFormat = mode.Format ; //use format of mode parms.BackBufferCount = 1 ; //make one back buffer //multisampling parms.MultiSampleType = D3DMULTISAMPLE_NONE ; //swap effect parms.SwapEffect = D3DSWAPEFFECT_COPY ; //we want to copy from back buffer to screen //destination window parms.hDeviceWindow = g_hWnd ; parms.Windowed = FALSE ; //depth buffer information parms.EnableAutoDepthStencil = FALSE ; parms.AutoDepthStencilFormat = D3DFMT_UNKNOWN ; //flags parms.Flags = 0 ; //refresh rate and presentation interval parms.FullScreen_RefreshRateInHz = mode.RefreshRate ; //use mode's refresh rate parms.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT ; //attempt to create a HAL device HRESULT hr ; //store return values in this variable hr = g_pd3d->CreateDevice ( D3DADAPTER_DEFAULT , D3DDEVTYPE_HAL , g_hWnd , D3DCREATE_SOFTWARE_VERTEXPROCESSING , &parms , &g_pd3ddev ) ; //error check if ( FAILED ( hr ) ) { //could not create HAL device fprintf ( stdout , "Could not create HAL device!\n" ) ; //attempt to make REF device hr = g_pd3d->CreateDevice ( D3DADAPTER_DEFAULT , D3DDEVTYPE_REF , g_hWnd , D3DCREATE_SOFTWARE_VERTEXPROCESSING , &parms , &g_pd3ddev ) ; if ( FAILED ( hr ) ) { //could not create REF device fprintf ( stdout , "Could not create REF device!\n" ) ; //post a quit message PostQuitMessage ( 0 ) ; } else { //successfully made REF device, store this value in a global g_devtype = D3DDEVTYPE_REF ; //report fprintf ( stdout , "Successfully created REF device.\n" ) ; } } else { //successfully made a HAL device, store this value in a global g_devtype = D3DDEVTYPE_HAL ; //report fprintf ( stdout , "Successfully created HAL device.\n" ) ; } } //clean up void Prog_Done ( ) { //safe release of device if ( g_pd3ddev ) { //release g_pd3ddev->Release ( ) ; //set to null g_pd3ddev = NULL ; //report fprintf ( stdout , "IDirect3DDevice8 object released.\n" ) ; } //safe release of IDirect3D8 object if ( g_pd3d ) { //release g_pd3d->Release ( ) ; //set to null g_pd3d = NULL ; //report action fprintf ( stdout , "IDirect3D8 object released.\n" ) ; } }