|
|
#include<windows.h>
#include<d3d9.h>
LRESULT CALLBACK MsgProc(HWND,UINT,WPARAM,LPARAM);
INT WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,INT)
{
//第1步 建立窗口对象和窗体 Register the window class
WNDCLASSEX wc;
wc.cbSize=sizeof(WNDCLASSEX);
wc.style =CS_CLASSDC;
wc.lpfnWndProc =MsgProc;
wc.cbClsExtra =0;
wc.cbWndExtra =0;
wc.hInstance=GetModuleHandle(NULL);
wc.lpszClassName=TEXT("WindowClass");
wc.hbrBackground =NULL;
wc.hCursor =NULL;
wc.hIcon =NULL;
wc.hIconSm =NULL;
wc.lpszMenuName =NULL;
RegisterClassEx(&wc);
//Create the application's window
HWND hWnd =CreateWindow(TEXT("WindowClass"),TEXT("Test"),WS_OVERLAPPEDWINDOW,
100,100,512,512,GetDesktopWindow(),NULL,wc.hInstance ,NULL);
//第2步 建立D3D对象
IDirect3D9* g_pD3D=NULL;
if(NULL==(g_pD3D=Direct3DCreate9(D3D_SDK_VERSION)))
return E_FAIL;
//第3步 建立D3D设备对象
//Pointer to a Direct3D device
IDirect3DDevice9 *g_pd3dDevice=NULL;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.Windowed=true;
d3dpp.SwapEffect= D3DSWAPEFFECT_DISCARD ;
d3dpp.BackBufferFormat=D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil=TRUE;
d3dpp.AutoDepthStencilFormat=D3DFMT_D16;
//Create the D3DDevice
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT ,
D3DDEVTYPE_HAL,hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,&g_pd3dDevice)))
{
return E_FAIL;
}
//第4步 进入消息循环
/* Windows默认的消息循环
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
*/
//游戏使用的消息循环
MSG mssg;
PeekMessage(&mssg,NULL,0,0,PM_NOREMOVE);
//run till completed
while(mssg.message!=WM_QUIT)
{
//is there a message to process?
if(PeekMessage(&mssg,NULL,0,0,PM_REMOVE))
{
//dispatch the message
TranslateMessage(&mssg);
DispatchMessage(&mssg);
}
// else
//No message to process?
//Then do your game stuff here
// Render();
}
//第5步 渲染与绘制场景
//Clear the back buffer and z buffer
g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0,0,255),1.0f,0);
//Begin the scene
if(SUCCEEDED(g_pd3dDevice->BeginScene()))
{
//Draw stuff here
//End the scene
g_pd3dDevice->EndScene();
//Present the back buffer contents to the display
g_pd3dDevice-> resent(NULL,NULL,NULL,NULL);
}
//第6步 关闭D3D
g_pD3D->Release();
g_pd3dDevice->Release();
}
LRESULT CALLBACK MsgProc(HWND hWnd,UINT mssg,WPARAM wParam,LPARAM lParam)
{
return DefWindowProc(hWnd,mssg,wParam,lParam);
} |
|