dx9模板.cpp
1 //-----------------------------------【头文件包含部分】--------------------------------------- 2 #include<d3d9.h> 3 #include <d3dx9.h> 4 #include <tchar.h> 5 #include <time.h> 6 #include <dinput.h> 7 8 //-----------------------------------【库文件包含部分】--------------------------------------- 9 #pragma comment(lib,"winmm.lib") 10 #pragma comment(lib,"d3d9.lib") 11 #pragma comment(lib,"d3dx9.lib") 12 #pragma comment(lib, "dinput8.lib") 13 #pragma comment(lib,"dxguid.lib") 14 15 //-----------------------------------【宏定义部分】-------------------------------------------- 16 #define WINDOW_WIDTH 800 17 #define WINDOW_HEIGHT 600 18 #define WINDOW_TITLE L"安康学院数字媒体技术" 19 #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } } 20 #define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } } 21 22 //-----------------------------------【全局变量声明部分】------------------------------------- 23 LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; 24 LPD3DXFONT g_pTextFPS =NULL; 25 LPD3DXFONT g_pTextAdaperName = NULL; 26 LPD3DXFONT g_pTextHelper = NULL; 27 LPD3DXFONT g_pTextInfor = NULL; 28 float g_FPS=0.0f; 29 wchar_t g_strFPS[50]={0}; 30 wchar_t g_strAdapterName[60]={0}; 31 LPDIRECTINPUT8 g_pDirectInput = NULL; 32 LPDIRECTINPUTDEVICE8 g_pMouseDevice = NULL; 33 DIMOUSESTATE g_diMouseState = {0}; 34 LPDIRECTINPUTDEVICE8 g_pKeyboardDevice = NULL; 35 char g_pKeyStateBuffer[256] = {0}; 36 D3DXMATRIX g_matWorld; 37 LPD3DXMESH g_pMesh = NULL; 38 D3DMATERIAL9* g_pMaterials = NULL; 39 LPDIRECT3DTEXTURE9* g_pTextures = NULL; 40 DWORD g_dwNumMtrls = 0; 41 42 //-----------------------------------【全局函数声明部分】------------------------------------- 43 LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam ); 44 HRESULT Direct3D_Init(HWND hwnd); 45 HRESULT Objects_Init(HWND hwnd); 46 void Direct3D_Render(HWND hwnd); 47 void Direct3D_Update( HWND hwnd); 48 void Direct3D_CleanUp( ); 49 float Get_FPS(); 50 void Matrix_Set(); 51 BOOL Device_Read(IDirectInputDevice8 *pDIDevice, void* pBuffer, long lSize) ; 52 53 //-----------------------------------【WinMain( )函数】-------------------------------------- 54 // 描述:Windows应用程序的入口函数,我们的程序从这里开始 55 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nShowCmd) 56 { 57 //【1】开始设计一个完整的窗口类 58 WNDCLASSEX wndClass = { 0 }; 59 wndClass.cbSize = sizeof( WNDCLASSEX ) ; 60 wndClass.style = CS_HREDRAW | CS_VREDRAW; 61 wndClass.lpfnWndProc = WndProc; 62 wndClass.cbClsExtra = 0; 63 wndClass.cbWndExtra = 0; 64 wndClass.hInstance = hInstance; 65 wndClass.hIcon=(HICON)::LoadImage(NULL,L"icon.ico",IMAGE_ICON,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE); 66 wndClass.hCursor = LoadCursor( NULL, IDC_ARROW ); 67 wndClass.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH); 68 wndClass.lpszMenuName = NULL; 69 wndClass.lpszClassName = L"ForTheDreamOfGameDevelop"; 70 71 //【2】注册窗口类 72 if( !RegisterClassEx( &wndClass ) ) 73 return -1; 74 75 //【3】正式创建窗口 76 HWND hwnd = CreateWindow( L"ForTheDreamOfGameDevelop",WINDOW_TITLE, 77 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, 78 WINDOW_HEIGHT, NULL, NULL, hInstance, NULL ); 79 80 if (S_OK==Direct3D_Init (hwnd)) 81 { 82 MessageBox(hwnd, L"Direct3D初始化完成~!", L"消息窗口", 0); 83 } 84 else 85 { 86 MessageBox(hwnd, L"Direct3D初始化失败~!", L"消息窗口", 0); 87 } 88 89 //【4】窗口的移动、显示与更新 90 MoveWindow(hwnd,2250,80,WINDOW_WIDTH,WINDOW_HEIGHT,true); 91 ShowWindow( hwnd, nShowCmd ); 92 UpdateWindow(hwnd); 93 94 PlaySound(L"刻限.wav", NULL, SND_FILENAME | SND_ASYNC|SND_LOOP); 95 96 //【5】消息循环过程 97 MSG msg = { 0 }; 98 while( msg.message != WM_QUIT ) 99 { 100 if( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) ) //查看应用程序消息队列,有消息时将队列中的消息派发出去。 101 { 102 TranslateMessage( &msg ); //将虚拟键消息转换为字符消息 103 DispatchMessage( &msg ); //该函数分发一个消息给窗口程序。 104 } 105 else 106 { 107 Direct3D_Render(hwnd); //进行渲染 108 } 109 } 110 111 //【6】窗口类的注销 112 UnregisterClass(L"ForTheDreamOfGameDevelop", wndClass.hInstance); 113 return 0; 114 } 115 116 //-----------------------------------【WndProc( )函数】-------------------------------------- 117 LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam ) 118 { 119 switch( message ) 120 { 121 case WM_PAINT: 122 Direct3D_Render(hwnd); 123 ValidateRect(hwnd, NULL); 124 break; 125 126 case WM_KEYDOWN: 127 if (wParam == VK_ESCAPE) 128 DestroyWindow(hwnd); 129 break; 130 131 case WM_DESTROY: 132 Direct3D_CleanUp(); 133 PostQuitMessage( 0 ); 134 break; 135 136 default: 137 return DefWindowProc( hwnd, message, wParam, lParam ); 138 } 139 return 0; 140 } 141 142 //-----------------------------------【Direct3D_Init( )函数】-------------------------------------- 143 HRESULT Direct3D_Init(HWND hwnd) 144 { 145 //Direct3D初始化,第一步 146 LPDIRECT3D9 pD3D=NULL; 147 if(NULL==(pD3D=Direct3DCreate9(D3D_SDK_VERSION))) 148 return E_FAIL; 149 150 //Direct3D初始化,第二步 151 D3DCAPS9 caps; int vp = 0; 152 if( FAILED( pD3D->GetDeviceCaps( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps ) ) ) 153 { 154 return E_FAIL; 155 } 156 if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT ) 157 158 vp = D3DCREATE_HARDWARE_VERTEXPROCESSING; 159 else 160 vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING; 161 162 163 //Direct3D初始化,第三步,D3DPRESENT_PARAMETERS结构体 164 165 D3DPRESENT_PARAMETERS d3dpp; 166 ZeroMemory(&d3dpp, sizeof(d3dpp)); 167 d3dpp.BackBufferWidth = WINDOW_WIDTH; 168 d3dpp.BackBufferHeight = WINDOW_HEIGHT; 169 d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; 170 d3dpp.BackBufferCount = 1; 171 d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; 172 d3dpp.MultiSampleQuality = 0; 173 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 174 d3dpp.hDeviceWindow = hwnd; 175 d3dpp.Windowed = true; 176 d3dpp.EnableAutoDepthStencil = true; 177 d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; 178 d3dpp.Flags = 0; 179 d3dpp.FullScreen_RefreshRateInHz = 0; 180 d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; 181 182 //Direct3D初始化,第四步 183 184 if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, 185 hwnd, vp, &d3dpp, &g_pd3dDevice))) 186 return E_FAIL; 187 188 wchar_t TempName[60]=L"当前显卡型号:"; 189 D3DADAPTER_IDENTIFIER9 Adapter; 190 pD3D->GetAdapterIdentifier(0,0,&Adapter); 191 int len = MultiByteToWideChar(CP_ACP,0, Adapter.Description, -1, NULL, 0); 192 MultiByteToWideChar(CP_ACP, 0, Adapter.Description, -1, g_strAdapterName, len); 193 wcscat_s(TempName,g_strAdapterName); 194 wcscpy_s(g_strAdapterName,TempName); 195 196 if(!(S_OK==Objects_Init(hwnd))) return E_FAIL; 197 SAFE_RELEASE(pD3D); 198 return S_OK; 199 200 } 201 202 //-----------------------------------【Object_Init( )函数】-------------------------------------- 203 HRESULT Objects_Init(HWND hwnd) 204 { 205 D3DXCreateFont(g_pd3dDevice, 36, 0, 0, 1000, false, DEFAULT_CHARSET, 206 OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, _T("Calibri"), &g_pTextFPS); 207 208 D3DXCreateFont(g_pd3dDevice, 20, 0, 1000, 0, false, DEFAULT_CHARSET, 209 OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, L"华文中宋", &g_pTextAdaperName); 210 211 D3DXCreateFont(g_pd3dDevice, 23, 0, 1000, 0, false, DEFAULT_CHARSET, 212 OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, L"微软雅黑", &g_pTextHelper); 213 214 D3DXCreateFont(g_pd3dDevice, 26, 0, 1000, 0, false, DEFAULT_CHARSET, 215 OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, L"黑体", &g_pTextInfor); 216 217 218 // 从X文件中加载网格数据 219 LPD3DXBUFFER pAdjBuffer = NULL; 220 LPD3DXBUFFER pMtrlBuffer = NULL; 221 D3DXLoadMeshFromX(L"loli.x", D3DXMESH_MANAGED, g_pd3dDevice, 222 &pAdjBuffer, &pMtrlBuffer, NULL, &g_dwNumMtrls, &g_pMesh); 223 224 D3DXMATERIAL *pMtrls = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer(); 225 g_pMaterials = new D3DMATERIAL9[g_dwNumMtrls]; 226 g_pTextures = new LPDIRECT3DTEXTURE9[g_dwNumMtrls]; 227 228 for (DWORD i=0; i<g_dwNumMtrls; i++) 229 { 230 g_pMaterials[i] = pMtrls[i].MatD3D; 231 g_pMaterials[i].Ambient = g_pMaterials[i].Diffuse; 232 g_pTextures[i] = NULL; 233 D3DXCreateTextureFromFileA(g_pd3dDevice, pMtrls[i].pTextureFilename, &g_pTextures[i]); 234 } 235 pAdjBuffer->Release(); 236 pMtrlBuffer->Release(); 237 238 g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); 239 g_pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f)); 240 return S_OK; 241 } 242 243 //-----------------------------------【Direct3D_Render( )函数】-------------------------------------- 244 void Direct3D_Render(HWND hwnd) 245 { 246 g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); 247 248 RECT formatRect; 249 GetClientRect(hwnd, &formatRect); 250 251 g_pd3dDevice->BeginScene(); 252 253 int charCount = swprintf_s(g_strFPS, 20, _T("FPS:%0.3f"), Get_FPS() ); 254 g_pTextFPS->DrawText(NULL, g_strFPS, charCount , &formatRect, DT_TOP | DT_RIGHT, D3DCOLOR_RGBA(0,239,136,255)); 255 256 g_pTextAdaperName->DrawText(NULL,g_strAdapterName, -1, &formatRect, 257 DT_TOP | DT_LEFT, D3DXCOLOR(1.0f, 0.5f, 0.0f, 1.0f)); 258 formatRect.top = 30; 259 static wchar_t strInfo[256] = {0}; 260 swprintf_s(strInfo,-1, L"模型坐标: (%.2f, %.2f, %.2f)", g_matWorld._41, g_matWorld._42, g_matWorld._43); 261 g_pTextHelper->DrawText(NULL, strInfo, -1, &formatRect, DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(135,239,136,255)); 262 263 264 265 formatRect.left = 0,formatRect.top = 380; 266 g_pTextInfor->DrawText(NULL, L"控制说明:", -1, &formatRect, 267 DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(235,123,230,255)); 268 formatRect.top += 35; 269 g_pTextHelper->DrawText(NULL, L" 按住鼠标左键并拖动:平移模型", -1, &formatRect, 270 DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255)); 271 formatRect.top += 25; 272 g_pTextHelper->DrawText(NULL, L" 按住鼠标右键并拖动:旋转模型", -1, &formatRect, 273 DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255)); 274 formatRect.top += 25; 275 g_pTextHelper->DrawText(NULL, L" 滑动鼠标滚轮:拉伸模型", -1, &formatRect, 276 DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255)); 277 formatRect.top += 25; 278 g_pTextHelper->DrawText(NULL, L" W、S、A、D键:平移模型 ", -1, &formatRect, 279 DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255)); 280 formatRect.top += 25; 281 g_pTextHelper->DrawText(NULL, L" 上、下、左、右方向键:旋转模型 ", -1, &formatRect, 282 DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255)); 283 formatRect.top += 25; 284 g_pTextHelper->DrawText(NULL, L" ESC键 : 退出程序", -1, &formatRect, 285 DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255)); 286 //4 287 g_pd3dDevice->EndScene(); 288 //5 289 g_pd3dDevice->Present(NULL, NULL, NULL, NULL); 290 } 291 292 //-----------------------------------【Matrix_Set( )函数】-------------------------------------- 293 void Matrix_Set() 294 { 295 D3DXMATRIX matView; 296 D3DXVECTOR3 vEye(0.0f, 0.0f, -250.0f); 297 D3DXVECTOR3 vAt(0.0f, 0.0f, 0.0f); 298 D3DXVECTOR3 vUp(0.0f, 1.0f, 0.0f); 299 D3DXMatrixLookAtLH(&matView, &vEye, &vAt, &vUp); 300 g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView); 301 302 303 D3DXMATRIX matProj; 304 D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4.0f,(float)((double)WINDOW_WIDTH/WINDOW_HEIGHT),1.0f, 1000.0f); 305 g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj); 306 307 D3DVIEWPORT9 vp; 308 vp.X = 0; 309 vp.Y = 0; 310 vp.Width = WINDOW_WIDTH; 311 vp.Height = WINDOW_HEIGHT; 312 vp.MinZ = 0.0f; 313 vp.MaxZ = 1.0f; 314 g_pd3dDevice->SetViewport(&vp); 315 } 316 //-----------------------------------【Direct3D_Update( )函数】-------------------------------- 317 void Direct3D_Update( HWND hwnd) 318 { 319 320 321 322 } 323 324 325 //-----------------------------------【Get_FPS( )函数】------------------------------------------ 326 float Get_FPS() 327 { 328 static float fps = 0;//fps值 329 static int frameCount = 0;//帧数 330 static float currentTime =0.0f;//当前时间 331 static float lastTime = 0.0f;//持续时间 332 333 frameCount++; 334 currentTime=timeGetTime()*0.001f;//获取系统时间 335 336 if(currentTime-lastTime>1.0f) 337 { 338 fps = (float)frameCount /(currentTime - lastTime); 339 lastTime=currentTime; 340 frameCount=0; 341 342 } 343 return fps; 344 345 } 346 //-----------------------------------【Direct3D_CleanUp( )函数】-------------------------------- 347 void Direct3D_CleanUp() 348 { 349 //释放COM接口对象 350 //g_pMouseDevice->Unacquire(); 351 //g_pKeyboardDevice->Unacquire(); 352 for (DWORD i = 0; i<g_dwNumMtrls; i++) 353 SAFE_RELEASE(g_pTextures[i]); 354 SAFE_DELETE(g_pTextures) 355 SAFE_DELETE(g_pMaterials) 356 SAFE_RELEASE(g_pMesh) 357 SAFE_RELEASE(g_pTextFPS) 358 SAFE_RELEASE(g_pd3dDevice) 359 SAFE_RELEASE(g_pMouseDevice) 360 SAFE_RELEASE(g_pKeyboardDevice) 361 }