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

DirextX编程

开发平台:

Visual C++

  1. #include "zfxd3dUtility.h"
  2. #include <stdio.h>
  3. // D3D对象接口
  4. IDirect3D9* g_pd3d;
  5. // D3D设备接口
  6. IDirect3DDevice9* g_pd3dDevice;
  7. // 字体
  8. ID3DXFont* g_pFont = NULL;
  9. // 建立渲染环境
  10. bool Setup()
  11. {
  12. // 设定渲染状态
  13. // 设置投影矩阵
  14. return true;
  15. }
  16. bool DrawTextX()
  17. {
  18. //用于渲染侦FPS
  19. static int tmLastTime = GetTickCount();
  20. int tmNow = GetTickCount();
  21. //用于计算渲染FPS
  22. static int nFrameCount = 0;
  23. static int nFPS = 0;
  24. if(tmNow - tmLastTime > 1000)
  25. {
  26. //计算FPS
  27. tmLastTime = tmNow;
  28. nFPS = nFrameCount;
  29. nFrameCount = 0;
  30. }
  31. nFrameCount ++;
  32. char str[500];
  33. RECT rect;
  34. rect.left = rect.top = 10;
  35. rect.bottom = rect.top + 30;
  36. rect.right = rect.left + 260;
  37. sprintf_s(str, 500, "Current FPS:%d, ", nFPS);
  38. g_pFont->DrawTextA(NULL, str, (int)strlen(str), &rect, DT_LEFT|DT_NOCLIP|DT_WORDBREAK, 0xffffff00);
  39. return true;
  40. }
  41. bool Display()
  42. {
  43. // 获取两次进入函数入口的时间间隔
  44. static float lastTime = (float)timeGetTime();
  45. float curTime  = (float)timeGetTime();
  46. float timeDelta = (curTime - lastTime)*0.001f;
  47. lastTime = curTime;
  48. // 绘制场景
  49. g_pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
  50. g_pd3dDevice->BeginScene();
  51. // 在屏幕上写字
  52. if(g_pFont)
  53. DrawTextX(); 
  54. g_pd3dDevice->EndScene();
  55. g_pd3dDevice->Present(0, 0, 0, 0);
  56. return true;
  57. }
  58. bool CleanUp()
  59. {
  60. SAFE_RELEASE(g_pd3dDevice);
  61. SAFE_RELEASE(g_pd3d);
  62. return true;
  63. }
  64. int WINAPI WinMain(HINSTANCE hInstance,
  65.    HINSTANCE prevInstance, 
  66.    PSTR cmdLine,
  67.    int showCmd)
  68. {
  69. // 应用程序初始化
  70. if(!InitD3DApp(hInstance, false/*true代表窗口模式*/, 
  71. D3DDEVTYPE_HAL, &g_pd3d, &g_pd3dDevice, L"CreateMeshX"/*窗口标题*/, 1024, 768))
  72. {
  73. MessageBoxW(0, L"应用程序初始化失败", 0, 0);
  74. return 0;
  75. }
  76. // 创建字体
  77. if(!InitDXFont(g_pd3dDevice, &g_pFont, L"Times New Roman"))
  78. g_pFont = NULL;
  79. Setup();
  80. // 进入消息循环
  81. MSG msg;
  82. ZeroMemory(&msg, sizeof(msg));
  83. while(msg.message != WM_QUIT)
  84. {
  85. if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  86. {
  87. TranslateMessage(&msg);
  88. DispatchMessage(&msg);
  89. }
  90. else
  91. {
  92. Display();
  93. }
  94. }
  95. CleanUp();
  96. return 0;
  97. }
  98. // 消息循环函数
  99. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  100. {
  101. static bool bWireFrame = false;
  102. switch( msg )
  103. {
  104. case WM_DESTROY:
  105. ::PostQuitMessage(0);
  106. break;
  107. case WM_KEYDOWN:
  108. if( wParam == VK_ESCAPE )
  109. ::DestroyWindow(hwnd);
  110. break;
  111. case WM_CHAR:
  112. if(wParam == 'q')
  113. bWireFrame = !bWireFrame;
  114. if(bWireFrame)
  115. g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );
  116. else
  117. g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
  118. break;
  119. }
  120. return ::DefWindowProc(hwnd, msg, wParam, lParam);
  121. }