|
本帖最后由 小生怕怕啊 于 2015-6-3 17:39 编辑
- #include <d3d9.h>
- #include <d3dx9.h>
- #include <stdio.h>
- #include <mmsystem.h>
- #pragma comment(lib,"d3d9.lib")
- #pragma comment(lib,"d3dx9.lib")
- #pragma comment(lib,"winmm.lib")
- #define WIN_CLASS "Simple"
- #define WIN_NAME "simple"
- #define WIN_WIDTH 800
- #define WIN_HEIGHT 600
- #define RELEASE(P) {if(P!=NULL){P->Release();P=NULL;}}
- bool EngineInit(HWND hwnd);
- void EngineEnd();
- bool GameInit(HWND hwnd);
- void GameRun(HWND hwnd);
- void GameEnd();
- void ShowMessage(LPCSTR lpText);
- void Matrix_Set();
- float GetFPS();
- LPDIRECT3D9 g_D3D=NULL;
- LPDIRECT3DDEVICE9 g_DEV=NULL;
- LPD3DXMESH g_Mesh=NULL; //网格
- D3DMATERIAL9* g_pMaterials=NULL; //材质
- LPDIRECT3DTEXTURE9* g_pTextures=NULL; //纹理
- DWORD g_dwMaterialsCount=0; //材质数量
- LPD3DXFONT g_Font=NULL; //字体
- LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
- {
- switch (msg)
- {
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- }
- return DefWindowProc(hwnd,msg,wparam,lparam);
- }
- int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
- {
- WNDCLASSEX wc={sizeof(WNDCLASSEX),CS_CLASSDC,WndProc,0,0,hInstance,NULL,NULL,NULL,NULL,WIN_CLASS,NULL};
- RegisterClassEx(&wc);
- HWND hwnd= CreateWindow(WIN_CLASS,WIN_NAME,WS_OVERLAPPEDWINDOW,0,0,WIN_WIDTH,WIN_HEIGHT,NULL,NULL,hInstance,NULL);
- if (hwnd)
- {
- if (GameInit(hwnd))
- {
- ShowWindow(hwnd,nShowCmd);
- UpdateWindow(hwnd);
- MSG msg;
- ZeroMemory(&msg,sizeof(msg));
- while (msg.message!=WM_QUIT)
- {
- if (PeekMessage(&msg,NULL,0U,0U,PM_REMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else
- {
- GameRun(hwnd);
- }
- }
- GameEnd();
- }
- }
- UnregisterClass(WIN_CLASS,wc.hInstance);
- return 0;
- }
- bool EngineInit( HWND hwnd )
- {
- if ((g_D3D=Direct3DCreate9(D3D_SDK_VERSION))==NULL)
- {
- return false;
- }
- D3DPRESENT_PARAMETERS d3dpp;
- ZeroMemory(&d3dpp,sizeof(d3dpp));
- d3dpp.BackBufferCount=2;
- d3dpp.BackBufferWidth=WIN_WIDTH;
- d3dpp.BackBufferHeight=WIN_HEIGHT;
- d3dpp.BackBufferFormat=D3DFMT_A8R8G8B8;
- d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
- d3dpp.EnableAutoDepthStencil=TRUE;
- d3dpp.AutoDepthStencilFormat=D3DFMT_D24S8;
- d3dpp.Windowed=TRUE;
- d3dpp.hDeviceWindow=hwnd;
- d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE;
- if (FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dpp,&g_DEV)))
- {
- ShowMessage("创建3D设备失败!");
- return false;
- }
- if (g_DEV==NULL)
- {
- return false;
- }
- //设置光照
- D3DLIGHT9 light;
- ZeroMemory(&light,sizeof(light));
- light.Type=D3DLIGHT_DIRECTIONAL;
- light.Ambient=D3DXCOLOR(0.5f,0.5f,0.5f,1.0f);
- light.Diffuse=D3DXCOLOR(1.0f,1.0f,1.0f,1.0f);
- light.Specular=D3DXCOLOR(0.0f,0.0f,0.0f,1.0f);
- light.Direction=D3DXVECTOR3(1.0f,0.0f,1.0f);
- g_DEV->SetLight(0,&light);
- g_DEV->LightEnable(0,TRUE);
- g_DEV->SetRenderState(D3DRS_NORMALIZENORMALS,true);
- g_DEV->SetRenderState(D3DRS_SPECULARENABLE,true);
- //开启背面消隐
- g_DEV->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);
- //创建字体
- if (FAILED(D3DXCreateFont(g_DEV,36,0,0,1,FALSE,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,DEFAULT_QUALITY,0,"微软雅黑",&g_Font)))
- {
- ShowMessage("创建字体失败!");
- return false;
- }
- return true;
- }
- void EngineEnd()
- {
- RELEASE(g_DEV);
- RELEASE(g_D3D);
- RELEASE(g_Mesh);
- }
- bool GameInit( HWND hwnd )
- {
- if (!(EngineInit(hwnd)))
- {
- return false;
- }
- LPD3DXBUFFER lpMaterials=NULL;
- if (FAILED(D3DXLoadMeshFromX("miki.X",D3DXMESH_MANAGED,g_DEV,NULL,&lpMaterials,NULL,&g_dwMaterialsCount,&g_Mesh)))
- {
- ShowMessage("载入模型失败!");
- return false;
- }
- D3DXMATERIAL* Material=(D3DXMATERIAL*)lpMaterials->GetBufferPointer();
- OutputDebugString(Material->pTextureFilename);
- g_pMaterials=new D3DMATERIAL9[g_dwMaterialsCount];
- g_pTextures=new LPDIRECT3DTEXTURE9[g_dwMaterialsCount];
- for (DWORD i=0;i<g_dwMaterialsCount;i++)
- {
- g_pMaterials[i]=Material[i].MatD3D;
- g_pMaterials[i].Ambient=g_pMaterials[i].Diffuse;
- //创建纹理
- g_pTextures[i]=NULL;
- if (FAILED(D3DXCreateTextureFromFile(g_DEV,Material[i].pTextureFilename,&g_pTextures[i])))
- {
- ShowMessage("创建纹理失败!");
- return false;
- }
- }
- return true;
- }
- void GameRun(HWND hwnd)
- {
- RECT formatRect;
- GetClientRect(hwnd,&formatRect);
- g_DEV->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
- g_DEV->BeginScene();
- Matrix_Set();
- for (DWORD i=0;i<g_dwMaterialsCount;i++)
- {
- g_DEV->SetMaterial(&g_pMaterials[i]);
- g_DEV->SetTexture(0,g_pTextures[i]);
- g_Mesh->DrawSubset(i);
- }
- float fps=GetFPS();
- char chFps[50]={0};
- UINT strCount= sprintf(chFps,"FPS:%0.3f",fps);
- g_Font->DrawTextA(NULL,chFps,strCount,&formatRect,DT_TOP|DT_RIGHT,D3DCOLOR_XRGB(255,0,0));
- g_DEV->EndScene();
- g_DEV->Present(NULL,NULL,NULL,NULL);
- }
- void GameEnd()
- {
- EngineEnd();
- }
- void ShowMessage( LPCSTR lpText )
- {
- MessageBox(NULL,lpText,"Info",MB_OK);
- }
- void Matrix_Set()
- {
- float fAngle=(float)(GetTickCount()*0.001);
- //世界矩阵
- D3DXMATRIX matrixWorld;
- D3DXMatrixRotationY(&matrixWorld,fAngle);
- g_DEV->SetTransform(D3DTS_WORLD,&matrixWorld);
- D3DXMATRIX matrixView;
- D3DXVECTOR3 vEye(0.0f,0.0f,-300.0f);
- D3DXVECTOR3 vAt(0.0f,0.0f,0.0f);
- D3DXVECTOR3 vUp(0.0f,1.0f,0.0f);
- D3DXMatrixLookAtLH(&matrixView,&vEye,&vAt,&vUp);
- g_DEV->SetTransform(D3DTS_VIEW,&matrixView); //取景变换
- D3DXMATRIX matrixProjection;
- if (FAILED(D3DXMatrixPerspectiveFovLH(&matrixProjection,D3DX_PI/4.0f,1,1.0f,1000.0f)))
- {
- ShowMessage("设置投影变换失败!");
- return ;
- }
- g_DEV->SetTransform(D3DTS_PROJECTION,&matrixProjection); //投影变换
- }
- float GetFPS()
- {
- static float fps=0;
- static int frameCount=0;
- static float currentTime=0.0f;
- static float lastTime=0.0f;
- frameCount++;
- currentTime=timeGetTime()*0.001f;
- if (currentTime-lastTime>1.0f)
- {
- fps=(float)frameCount/(currentTime-lastTime);
- lastTime=currentTime;
- frameCount=0;
- }
- return fps;
- }
复制代码 |
-
-
DxSimple.part01.rar
2 MB, 下载次数: 15, 下载积分: 精币 -2 枚
-
-
DxSimple.part02.rar
2 MB, 下载次数: 14, 下载积分: 精币 -2 枚
-
-
DxSimple.part03.rar
2 MB, 下载次数: 19, 下载积分: 精币 -2 枚
-
-
DxSimple.part04.rar
2 MB, 下载次数: 22, 下载积分: 精币 -2 枚
-
-
DxSimple.part05.rar
844.84 KB, 下载次数: 31, 下载积分: 精币 -2 枚
|