|
|
本人对书本“beginning directX 9”中第二章中的代码遇到麻烦
这段程序我从书上复制下来,可运行时窗口弹出却又马上关闭。没等我看清楚程序就结束了!
请问各位怎样解决?谢谢!(上传了附件)
#include <windows.h>
#include <d3d9.h>
LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pd3dDevice;
HWND wndHandle;
void render(void);
void cleanUp (void);
bool initDirect3D(void);
bool initWindow( HINSTANCE hInstance);
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow )
{
MSG msg;
if ( !initWindow( hInstance) )
return false;
if(!initDirect3D())
return false;
ZeroMemory( &msg, sizeof( msg ) );
while(msg.message!=WM_QUIT )
{
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
render();
}
return msg.wParam;
cleanUp();
}
bool initWindow( HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX); // the size of the structure
wcex.style = CS_HREDRAW | CS_VREDRAW; // the class style
wcex.lpfnWndProc = (WNDPROC)WndProc; // the window procedure callback
wcex.cbClsExtra = 0; // extra bytes to allocate for this class
wcex.cbWndExtra = 0; // extra bytes to allocate for this instance
wcex.hInstance = hInstance; // handle to the application instance
wcex.hIcon = 0; // icon to associate with the application
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);// the default cursor
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // the background color
wcex.lpszMenuName = NULL; // the resource name for the menu
wcex.lpszClassName = "DirectXExample";
wcex.hIconSm = 0; // the handle to the small icon
RegisterClassEx(&wcex);
wndHandle = CreateWindow(
"DirectXExample",
"DirectXExample",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, // the starting x coordinate
CW_USEDEFAULT, // the starting y coordinate
800, // the pixel width of the window
600, // the pixel height of the window
NULL, // the parent window; NULL for desktop
NULL, // the menu for the application; NULL for none
hInstance, // the handle to the application instance
NULL); // no values passed to the window
// Make sure that the window handle that is created is valid
if (!wndHandle)
return false;
else
{
ShowWindow(wndHandle, SW_SHOWNORMAL);
UpdateWindow(wndHandle);
return true;
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
bool initDirect3D(void)
{
pD3D = NULL;
pd3dDevice = NULL;
if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
{
return false;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferHeight = 480;
d3dpp.BackBufferWidth = 640;
d3dpp.hDeviceWindow = wndHandle;
// Create a default DirectX device
if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF,
wndHandle,
D3DADAPTER_DEFAULT,
&d3dpp,
&pd3dDevice ) ) )
{
return false;
}
return true;
}
void render(void)
{
// Check to make sure you have a valid Direct3D device
if( NULL != pd3dDevice )
{
pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, // Clear the back buffer to a blue color
D3DCOLOR_XRGB( 0,0,255 ), 1.0f, 0 ); // Present the back buffer contents to the display
pd3dDevice-> resent( NULL, NULL, NULL, NULL );
}
}
void cleanUp (void)
{
// Release the device and the Direct3D object
if( pd3dDevice != NULL )
pd3dDevice->Release( );
if( pD3D != NULL )
pD3D->Release( );
} |
|