资源描述:
《游戏设计3d例程.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、#pragmacomment(lib,"d3d8.lib")//强制使用d3d8.lib的库文件,防止使用dx9sdk的不兼容问题includeLPDIRECT3D8 g_pD3D =NULL;//用来创建D3D设备LPDIRECT3DDEVICE8 g_pd3dDevice=NULL;//D3D设备//初始化D3DHRESULTInitD3D(HWNDhWnd){//创建D3D对象if(NULL==(g_pD3D=Direct3DCreate8(D3D_SDK_VERSIO
2、N)))returnE_FAIL;//获取当前的显示模式D3DDISPLAYMODEd3ddm;if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm)))returnE_FAIL;D3DPRESENT_PARAMETERSd3dpp;ZeroMemory(&d3dpp,sizeof(d3dpp));d3dpp.Windowed=TRUE; //窗口模式d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;//设置交换模式d3d
3、pp.BackBufferFormat=d3ddm.Format; //设置背景缓冲区格式为当前左面格式//创建D3D设备//第一个参数:使用默认的显卡适配器//第二个参数:请求使用硬件抽象层(HAL)//第三个参数:窗口句柄//第四个参数:使用软件处理顶点//第五个参数:创建的参数//第六个参数:创建的D3D设备指针if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d
4、3dpp,&g_pd3dDevice))){returnE_FAIL;}returnS_OK;}//释放D3DVOIDCleanup(){if(g_pd3dDevice!=NULL)g_pd3dDevice->Release();if(g_pD3D!=NULL)g_pD3D->Release();}//渲染VOIDRender(){if(NULL==g_pd3dDevice)return;//清除背景为兰色g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255
5、),1.0f,0);//开始绘制场景g_pd3dDevice->BeginScene();//这里绘制你需要的东西//结束场景绘制g_pd3dDevice->EndScene();//显示到屏幕上g_pd3dDevice->Present(NULL,NULL,NULL,NULL);}//消息处理LRESULTWINAPIMsgProc(HWNDhWnd,UINTmsg,WPARAMwParam,LPARAMlParam){switch(msg){caseWM_DESTROY://退出消息PostQuitMessage(0);retu
6、rn0;caseWM_PAINT: //绘制Render();ValidateRect(hWnd,NULL);return0;}returnDefWindowProc(hWnd,msg,wParam,lParam);}//WinMain程序入口INTWINAPIWinMain(HINSTANCEhInst,HINSTANCE,LPSTR,INT){//注册窗口类WNDCLASSEXwc={sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,GetModuleHandle(NULL),NULL,N
7、ULL,NULL,NULL,"D3DTutorial",NULL};RegisterClassEx(&wc);//创建窗口HWNDhWnd=CreateWindow("D3DTutorial","D3DTutorial01:CreateDevice",WS_OVERLAPPEDWINDOW,100,100,300,300,GetDesktopWindow(),NULL,wc.hInstance,NULL);//初始化D3Dif(SUCCEEDED(InitD3D(hWnd))){//显示窗口ShowWindow(hWnd,SW_SH
8、OWDEFAULT);UpdateWindow(hWnd);//消息循环MSGmsg;while(GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg);DispatchMessage(&m