|
|
# include <windows.h>
//# include <iostream.h>
# include <d3d8.h>
LPDIRECT3D8 pD3D;
LPDIRECT3DDEVICE8 pd3dDevice;
HINSTANCE hInst;
HWND wndHandle;
bool initDirect3D(void);
void render(void);
void cleanUp (void);
bool initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPTSTR lpCmdLine, int nCmdShow )
//int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine, int nCmdShow)
{
if(! initWindow(hInstance))
return false;
if(! initDirect3D())
return false;
MSG msg;
ZeroMemory(&msg,sizeof(msg));
//if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
// {
// TranslateMessage ( &msg );
// DispatchMessage ( &msg );
// }
// else
// {
// render( );
// }
BOOL fMessage;
//PeekMessage(&msg,NULL,0U,0U,PM_NOREMOVE);
while(msg.message != WM_QUIT)
{
fMessage = PeekMessage(&msg,NULL,0U,0U,PM_REMOVE);//对列里没有消息返回真,有返回假
if (fMessage)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
render();
}
}
}
bool initWindow(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbClsExtra=0;
wcex.cbSize=sizeof(WNDCLASSEX);
wcex.cbWndExtra=0;
wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
wcex.hIcon=0;
wcex.hIconSm=0;
wcex.hInstance=hInstance;
wcex.lpfnWndProc=(WNDPROC)WndProc;
wcex.lpszClassName="DirectxExample";
wcex.lpszMenuName=NULL;
wcex.style=CS_HREDRAW|CS_VREDRAW;
RegisterClassEx(&wcex);
wndHandle=CreateWindow("DirectxExample","DirectxExample",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,640,480,NULL,NULL,hInstance,NULL);
if(!wndHandle)
return false;
ShowWindow(wndHandle,SW_SHOW);
//UpdataWindow(wndHandle);
UpdateWindow(wndHandle);
return true;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
bool initDirect3D(void)
{
pD3D = NULL;
pd3dDevice = NULL;
// Create the DirectX object
if( NULL == ( pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
{
return false;
}
// Fill the presentation parameters structure
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,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pd3dDevice ) ) )
{
return false;
}
return true;
}
void render(void)
{
// Check to make sure you have a valid Direct3D device
if( NULL == pd3dDevice )
return;// Clear the back buffer to a blue color
pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,
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( );
}
|
|