Base.cpp
上传用户:whgydz
上传日期:2007-01-12
资源大小:2259k
文件大小:5k
源码类别:

其他书籍

开发平台:

HTML/CSS

  1. // Base.cpp: implementation of the CBase class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "Base.h"
  5. //////////////////////////////////////////////////////////////////////
  6. // Construction/Destruction
  7. //////////////////////////////////////////////////////////////////////
  8. CBase::CBase()
  9. {
  10. }
  11. CBase::~CBase()
  12. {
  13. }
  14. D3DXVECTOR3 CBase::GetTriangeNormal(D3DXVECTOR3* vVertex1, D3DXVECTOR3* vVertex2, D3DXVECTOR3* vVertex3)
  15. {
  16. D3DXVECTOR3 vNormal;
  17. D3DXVECTOR3 v1;
  18. D3DXVECTOR3 v2;
  19. D3DXVec3Subtract(&v1, vVertex2, vVertex1);
  20. D3DXVec3Subtract(&v2, vVertex3, vVertex1);
  21. D3DXVec3Cross(&vNormal, &v1, &v2);
  22. D3DXVec3Normalize(&vNormal, &vNormal);
  23. return vNormal;
  24. }
  25. bool CBase::m_fEnableLogging = false;
  26. void CBase::LogError(char *lpszText, ...)
  27. {
  28. if(m_fEnableLogging)
  29. {
  30. va_list argList;
  31. FILE *pFile = NULL;
  32. //Initialize variable argument list
  33. va_start(argList, lpszText);
  34. //Open the log file for appending
  35. pFile = fopen("Log.htm", "a+");
  36. if(pFile != NULL)
  37. {
  38. //Write the error to the log file
  39. fprintf(pFile, "<font face="Arial" size="2" color="#FF0000"><b>");
  40. vfprintf(pFile, lpszText, argList);
  41. fprintf(pFile, "</b></font><br>n");
  42. //Close the file
  43. fclose(pFile);
  44. }
  45. va_end(argList);
  46. }
  47. }
  48. void CBase::LogInfo(char *lpszText, ...)
  49. {
  50. if(m_fEnableLogging)
  51. {
  52. va_list argList;
  53. FILE *pFile = NULL;
  54. //Initialize variable argument list
  55. va_start(argList, lpszText);
  56. //Open the log file for appending
  57. pFile = fopen("Log.htm", "a+");
  58. if(pFile != NULL)
  59. {
  60. //Write the error to the log file
  61. fprintf(pFile, "<font face="Arial" size="2" color="#000000">");
  62. vfprintf(pFile, lpszText, argList);
  63. fprintf(pFile, "</font><br>n");
  64. //Close the file
  65. fclose(pFile);
  66. }
  67. va_end(argList);
  68. }
  69. }
  70. void CBase::LogWarning(char *lpszText, ...)
  71. {
  72. if(m_fEnableLogging)
  73. {
  74. va_list argList;
  75. FILE *pFile = NULL;
  76. //Initialize variable argument list
  77. va_start(argList, lpszText);
  78. //Open the log file for appending
  79. pFile = fopen("Log.htm", "a+");
  80. if(pFile != NULL)
  81. {
  82. //Write the error to the log file
  83. fprintf(pFile, "<font face="Arial" size="2" color="#E7651A"><b>");
  84. vfprintf(pFile, lpszText, argList);
  85. fprintf(pFile, "</b></font><br>n");
  86. //Close the file
  87. fclose(pFile);
  88. }
  89. va_end(argList);
  90. }
  91. }
  92. void CBase::StartLogging()
  93. {
  94. FILE* pFile = NULL;
  95. //OPen the file and clear the contents
  96. pFile = fopen("Log.htm", "wb");
  97. if(pFile != NULL)
  98. {
  99. //Write start html to log
  100. fprintf(pFile, "<html><head><title>Log File</title></head><body>n");
  101. fprintf(pFile, "<font face="Arial" size="4" color="#000000"><b><u>Log File</u></b></font><br>n");
  102. //Close the file
  103. fclose(pFile);
  104. m_fEnableLogging = true;
  105. }
  106. }
  107. void CBase::StopLogging()
  108. {
  109. if(m_fEnableLogging)
  110. {
  111. FILE *pFile = NULL;
  112. //Open the log file for appending
  113. pFile = fopen("Log.htm", "a+");
  114. if(pFile != NULL)
  115. {
  116. //Write end html to log
  117. fprintf(pFile, "</body></html>");
  118. //Close the file
  119. fclose(pFile);
  120. }
  121. m_fEnableLogging = false;
  122. }
  123. }
  124. DWORD CBase::GetMemoryUsage()
  125. {
  126. //This is slow. Don't use it inside the game loop!
  127. DWORD dwProcessID = GetCurrentProcessId();
  128. HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPHEAPLIST | TH32CS_SNAPMODULE, dwProcessID);
  129. DWORD dwSize = 0; // Will contain the total memory usage when we're done!
  130. MODULEENTRY32 module;
  131. module.dwSize = sizeof(module);
  132. //Get memory used by modules (dlls etc)
  133. if (Module32First(hSnapshot, &module))
  134. {
  135.    do
  136.    {
  137.     dwSize += module.modBaseSize;
  138. //LogInfo("<li>%s", module.szModule); //Log the module name that we are using
  139.    }
  140. while (Module32Next(hSnapshot, &module));
  141. }
  142. HEAPLIST32 heap;
  143. heap.dwSize = sizeof(heap); 
  144. //Get all memory used by the heap
  145. if (Heap32ListFirst(hSnapshot, &heap))
  146. {
  147. do
  148.    {
  149.     HEAPENTRY32 heapentry;
  150. heapentry.dwSize = sizeof(heapentry);
  151.    
  152. if (Heap32First(&heapentry,heap.th32ProcessID,heap.th32HeapID))
  153.     {
  154.      do
  155.      {
  156.       if (heapentry.dwFlags != LF32_FREE) // If the block is currently used
  157. {
  158.        dwSize += heapentry.dwBlockSize;
  159. }
  160.      }
  161. while (Heap32Next(&heapentry));
  162.     }
  163.    }
  164. while (Heap32ListNext(hSnapshot,&heap));
  165. }
  166. CloseHandle(hSnapshot);
  167. return dwSize;
  168. }
  169. void CBase::LogMemoryUsage()
  170. {
  171. DWORD dwMemoryUsage = GetMemoryUsage();
  172. float rMemoryUsage = 0.0f;
  173. if(dwMemoryUsage < 1024)
  174. {
  175. LogInfo("<li>Memory Usage: %d Bytes", dwMemoryUsage);
  176. }
  177. else if(dwMemoryUsage < 1048576)
  178. {
  179. rMemoryUsage = ((float)dwMemoryUsage / 1024.0f);
  180. LogInfo("<li>Memory Usage: %f KB", rMemoryUsage);
  181. }
  182. else if(dwMemoryUsage < 1073741824)
  183. {
  184. rMemoryUsage = ((float)dwMemoryUsage / 1048576.0f);
  185. LogInfo("<li>Memory Usage: %f MB", rMemoryUsage);
  186. }
  187. else
  188. {
  189. rMemoryUsage = ((float)dwMemoryUsage / 1073741824.0f);
  190. LogInfo("<li>Memory Usage: %f GB", rMemoryUsage);
  191. }
  192. }