zfxd3dUtility.cpp
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:5k
源码类别:

DirextX编程

开发平台:

Visual C++

  1. #include "zfxd3dUtility.h"
  2. bool InitD3DApp(HINSTANCE hInstance,  // 程序实例句柄
  3. bool windowed,             // 布尔变量,是否为窗口模式
  4. D3DDEVTYPE deviceType,     // HAL or REF
  5. IDirect3D9** pd3d9,      // D3D对象接口
  6. IDirect3DDevice9** pd3dDevice,// D3D设备接口
  7. PCTSTR szTitle,           // 窗口标题名
  8. int width, int height)
  9. {
  10. // 注册窗口类
  11. WNDCLASS wc;
  12. wc.style         = CS_HREDRAW | CS_VREDRAW;
  13. wc.lpfnWndProc   = (WNDPROC)WndProc; 
  14. wc.cbClsExtra    = 0;
  15. wc.cbWndExtra    = 0;
  16. wc.hInstance     = hInstance;
  17. wc.hIcon         = LoadIcon(0, IDI_APPLICATION);
  18. wc.hCursor       = LoadCursor(0, IDC_ARROW);
  19. wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
  20. wc.lpszMenuName  = 0;
  21. wc.lpszClassName = L"ZFXD3DApp";
  22. if( !RegisterClass(&wc) ) 
  23. {
  24. ::MessageBox(0, L"FAILED to register WND class", 0, 0);
  25. return false;
  26. }
  27. // 创建窗口
  28. HWND hWnd = 0;
  29. hWnd = ::CreateWindow(L"ZFXD3DApp", szTitle, 
  30. WS_OVERLAPPEDWINDOW ,
  31. 50, 50, width, height,
  32. 0 /*父窗口*/, 0 /* 菜单名 */, hInstance, 0); 
  33. if( !hWnd )
  34. {
  35. ::MessageBox(0, L"FAILED to create a window", 0, 0);
  36. return false;
  37. }
  38. ::ShowWindow(hWnd, SW_SHOW);
  39. ::UpdateWindow(hWnd);
  40. // 准备创建设备
  41. if(!((*pd3d9) = Direct3DCreate9(D3D_SDK_VERSION)))
  42. {
  43. ::MessageBox(0, L"Create D3D Object FAILED", 0, 0);
  44. return false;
  45. }
  46. // 检查硬件是否支持顶点处理
  47. D3DCAPS9 caps;
  48. (*pd3d9)->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps);
  49. DWORD vp = 0;
  50. if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
  51. vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  52. else
  53. vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  54. // 填充D3DPRESENT_PARAMETERS结构体.
  55.  
  56. D3DPRESENT_PARAMETERS d3dpp;
  57. d3dpp.BackBufferWidth            = width;
  58. d3dpp.BackBufferHeight           = height;
  59. d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  60. d3dpp.BackBufferCount            = 1;
  61. d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  62. d3dpp.MultiSampleQuality         = 0;
  63. d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
  64. d3dpp.hDeviceWindow              = hWnd;
  65. d3dpp.Windowed                   = windowed;
  66. d3dpp.EnableAutoDepthStencil     = true; 
  67. d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  68. d3dpp.Flags                      = 0;
  69. d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  70. // 限制帧刷新率,最高为60,由显示属性设置决定
  71. d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_DEFAULT/*IMMEDIATE*/;
  72. // 创建设备
  73. HRESULT hr;
  74. hr = (*pd3d9)->CreateDevice(
  75. D3DADAPTER_DEFAULT,
  76. deviceType,
  77. hWnd,
  78. vp,
  79.     &d3dpp,
  80.     pd3dDevice);
  81. if( FAILED(hr) )
  82. {
  83. (*pd3d9)->Release(); // 释放D3D对象资源
  84. ::MessageBox(0, L"Create Device FAILED", 0, 0);
  85. return false;
  86. }
  87. return true;
  88. }
  89. bool InitDXFont(IDirect3DDevice9* pd3dDevice, ID3DXFont** pFont, PCTSTR szFontType, 
  90. bool isItalic, int height, int width, int weight)
  91. {
  92. //创建字体
  93. D3DXFONT_DESC fontDesc;
  94. ZeroMemory( &fontDesc, sizeof(D3DXFONT_DESC) );
  95. fontDesc.Height = height;
  96. fontDesc.Width = width;
  97. fontDesc.Weight = weight;
  98. fontDesc.Italic = isItalic;
  99. wcscpy( fontDesc.FaceName, szFontType);
  100. if( SUCCEEDED( D3DXCreateFontIndirect( pd3dDevice, &fontDesc, pFont )))
  101. return true;
  102. else
  103. return false;
  104. }
  105. D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction, D3DXCOLOR* color)
  106. {
  107. D3DLIGHT9 light;
  108. ::ZeroMemory(&light, sizeof(light));
  109. light.Type      = D3DLIGHT_DIRECTIONAL;
  110. light.Ambient   = *color * 0.6f;
  111. light.Diffuse   = *color;
  112. light.Specular  = *color * 0.6f;
  113. light.Direction = *direction;
  114. return light;
  115. }
  116. D3DLIGHT9 InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color)
  117. {
  118. D3DLIGHT9 light;
  119. ::ZeroMemory(&light, sizeof(light));
  120. light.Type      = D3DLIGHT_POINT;
  121. light.Ambient   = *color * 0.6f;
  122. light.Diffuse   = *color;
  123. light.Specular  = *color * 0.6f;
  124. light.Position  = *position;
  125. light.Range        = 1000.0f;
  126. light.Falloff      = 1.0f;
  127. light.Attenuation0 = 1.0f;
  128. light.Attenuation1 = 0.0f;
  129. light.Attenuation2 = 0.0f;
  130. return light;
  131. }
  132. D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color)
  133. {
  134. D3DLIGHT9 light;
  135. ::ZeroMemory(&light, sizeof(light));
  136. light.Type      = D3DLIGHT_SPOT;
  137. light.Ambient   = *color * 0.0f;
  138. light.Diffuse   = *color;
  139. light.Specular  = *color * 0.6f;
  140. light.Position  = *position;
  141. light.Direction = *direction;
  142. light.Range        = 1000.0f;
  143. light.Falloff      = 1.0f;
  144. light.Attenuation0 = 1.0f;
  145. light.Attenuation1 = 0.0f;
  146. light.Attenuation2 = 0.0f;
  147. light.Theta        = 0.4f;
  148. light.Phi          = 0.9f;
  149. return light;
  150. }
  151. D3DMATERIAL9 InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p)
  152. {
  153. D3DMATERIAL9 mtrl;
  154. mtrl.Ambient  = a;
  155. mtrl.Diffuse  = d;
  156. mtrl.Specular = s;
  157. mtrl.Emissive = e;
  158. mtrl.Power    = p;
  159. return mtrl;
  160. }