Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

393 lines
13 KiB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <windows.h>
  4. #include <commdlg.h>
  5. #include "prfl.h"
  6. #include "skeltest.h"
  7. #include "hugetest.h"
  8. #include "primtest.h"
  9. #include "resource.h"
  10. #include "macros.h"
  11. #include "square.h"
  12. #include "large1.h"
  13. #include "small1.h"
  14. #include "teapot.h"
  15. #include "large2.h"
  16. #include "small2.h"
  17. #include "tptlght.h"
  18. #include "tpttxtr.h"
  19. static LPCTSTR lpszAppName = "Profiler";
  20. typedef PrimativeTest NewTestType;
  21. HINSTANCE hInstance;
  22. HWND hWndMain;
  23. #define MAXNUMBEROFTESTS 40
  24. SkeletonTest *tests[MAXNUMBEROFTESTS];
  25. static int iNumberOfTests = 0;
  26. #define DisableWindow(x) EnableWindow(x,FALSE)
  27. LRESULT CALLBACK mainWndProc(HWND, UINT, WPARAM, LPARAM);
  28. BOOL CALLBACK renameDlgProc(HWND, UINT, WPARAM, LPARAM);
  29. // Entry point of all Windows programs
  30. int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance,
  31. LPSTR lpCmdLine, int iCmdShow)
  32. {
  33. MSG msg;
  34. WNDCLASSEX wc;
  35. HACCEL hAccel;
  36. hInstance = hInst;
  37. // Register Window style
  38. wc.cbSize = sizeof(wc);
  39. wc.style = CS_HREDRAW | CS_VREDRAW;
  40. wc.lpfnWndProc = (WNDPROC) mainWndProc;
  41. wc.cbClsExtra = 0;
  42. wc.cbWndExtra = DLGWINDOWEXTRA;
  43. wc.hInstance = hInstance;
  44. wc.hIcon = NULL;
  45. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  46. wc.hbrBackground = (HBRUSH) COLOR_BTNSHADOW;
  47. wc.lpszMenuName = NULL;
  48. wc.lpszClassName = lpszAppName;
  49. wc.hIconSm = NULL;
  50. // Register the window class
  51. if(RegisterClassEx(&wc) == 0)
  52. exit(-1);
  53. if (prfl_RegisterClass(hInstance) == FALSE)
  54. exit(-1);
  55. bzero(tests,sizeof(tests));
  56. tests[0] = new SkeletonTest;
  57. tests[1] = new HugeTest;
  58. tests[2] = new PrimativeTest;
  59. tests[3] = new TeapotTest;
  60. tests[4] = new LargeTriangle;
  61. tests[5] = new SmallTriangle;
  62. tests[6] = new SquareTest;
  63. tests[7] = new TeapotTextureTest;
  64. tests[8] = new TeapotLightTest;
  65. tests[9] = new LargeTriangle2;
  66. tests[10] = new SmallTriangle2;
  67. iNumberOfTests = 11;
  68. hWndMain = CreateDialog(hInstance, lpszAppName, 0, NULL);
  69. // I'm not sure why, but my window isn't getting a WM_INITDIALOG
  70. // message, so we'll just force it to get one.
  71. // Perhaps because I'm using CreateDialog instead of DialogBox?
  72. SendMessage(hWndMain, WM_INITDIALOG, 0, 0);
  73. hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR1));
  74. while(GetMessage(&msg, NULL, 0, 0)) {
  75. if (!TranslateAccelerator(hWndMain, hAccel, &msg)) {
  76. TranslateMessage(&msg);
  77. DispatchMessage(&msg);
  78. }
  79. }
  80. return msg.wParam;
  81. }
  82. void EnableRunmode(HWND hDlg, BOOL f) {
  83. EnableWindow(GetDlgItem(hDlg,M_IDC_WHICHTEST), !f); // Test list
  84. EnableWindow(GetDlgItem(hDlg,M_IDC_CONFIG), !f); // Config
  85. EnableWindow(GetDlgItem(hDlg,M_IDC_NEW), !f); // New
  86. EnableWindow(GetDlgItem(hDlg,M_IDC_DELETE), !f); // Delete
  87. EnableWindow(GetDlgItem(hDlg,M_IDC_RENAME), !f); // Rename
  88. EnableWindow(GetDlgItem(hDlg,M_IDC_RUN), !f); // Run
  89. EnableWindow(GetDlgItem(hDlg,M_IDC_ABORT), f); // Abort
  90. EnableWindow(GetDlgItem(hDlg,M_IDC_QUIT), !f); // Quit
  91. if (iNumberOfTests == MAXNUMBEROFTESTS) {
  92. EnableWindow(GetDlgItem(hDlg, M_IDC_NEW), FALSE);
  93. EnableWindow(GetDlgItem(hDlg, M_IDC_LOAD), FALSE);
  94. }
  95. if (iNumberOfTests == 1)
  96. EnableWindow(GetDlgItem(hDlg, M_IDC_DELETE), FALSE);
  97. }
  98. int UpdateTestList(HWND hDlg, int iRadio)
  99. {
  100. int i;
  101. if (iRadio >= iNumberOfTests)
  102. iRadio = 0;
  103. CB_DlgResetContent(hDlg, M_IDC_WHICHTEST);
  104. CB_DlgSetRedraw(hDlg, M_IDC_WHICHTEST, FALSE);
  105. for (i = 0 ; i < iNumberOfTests ; i++) {
  106. CB_DlgAddString(hDlg, M_IDC_WHICHTEST, tests[i]->QueryName());
  107. }
  108. CB_DlgSetRedraw(hDlg, M_IDC_WHICHTEST, TRUE);
  109. CB_DlgSetSelect(hDlg, M_IDC_WHICHTEST, iRadio);
  110. SetFocus(GetDlgItem(hDlg, M_IDC_RUN));
  111. return iRadio;
  112. };
  113. void main_test_end_callback()
  114. {
  115. PostMessage(hWndMain, WM_USER, 0, 0);
  116. }
  117. // Window procedure, handles all messages for this window
  118. LRESULT CALLBACK mainWndProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  119. {
  120. static char szFileName[_MAX_PATH];
  121. static char szTitleName[_MAX_FNAME + _MAX_EXT];
  122. static OPENFILENAME ofn;
  123. static int iRadio = 0;
  124. static char szFilter[] = "Test Files (*.TST)\0*.tst\0" \
  125. "All Files (*.*)\0*.*\0\0" ;
  126. static char acBuffer[120];
  127. void *pTmp;
  128. char *pc;
  129. HANDLE hFile;
  130. int iControl;
  131. int i;
  132. switch (msg)
  133. {
  134. case WM_CREATE:
  135. ofn.lStructSize = sizeof (OPENFILENAME);
  136. ofn.hwndOwner = hDlg;
  137. ofn.hInstance = NULL;
  138. ofn.lpstrFilter = szFilter;
  139. ofn.lpstrCustomFilter = NULL;
  140. ofn.nMaxCustFilter = 0;
  141. ofn.nFilterIndex = 0;
  142. ofn.lpstrFile = szFileName;
  143. ofn.nMaxFile = _MAX_PATH;
  144. ofn.lpstrFileTitle = szTitleName;
  145. ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT;
  146. ofn.lpstrInitialDir = NULL;
  147. ofn.lpstrTitle = NULL;
  148. ofn.Flags = 0; // Set in Open and Close functions
  149. ofn.nFileOffset = 0;
  150. ofn.nFileExtension = 0;
  151. ofn.lpstrDefExt = "tst";
  152. ofn.lCustData = 0L;
  153. ofn.lpfnHook = NULL;
  154. ofn.lpTemplateName = NULL;
  155. break;
  156. case WM_INITDIALOG:
  157. iRadio = UpdateTestList(hDlg,iRadio);
  158. return 0; // WM_INITDIALOG
  159. case WM_COMMAND:
  160. {
  161. iControl = LOWORD(wParam);
  162. switch (iControl)
  163. {
  164. case M_IDC_WHICHTEST:
  165. iRadio = CB_DlgGetSelect(hDlg, M_IDC_WHICHTEST);
  166. break;
  167. case M_IDC_CONFIG:
  168. // if (tests[iRadio])
  169. if (-1 == tests[iRadio]->cnfgfunct(hDlg)) {
  170. MessageBeep(MB_OK);
  171. fprintf(stderr,"error calling cnfgfunct()\n");
  172. MessageBox(hDlg,"An error occured.",NULL,
  173. MB_OK|MB_ICONERROR);
  174. }
  175. break;
  176. case M_IDC_NEW: // New
  177. if (iNumberOfTests == MAXNUMBEROFTESTS) {
  178. MessageBox(hDlg,
  179. "Sorry, you've already got too many tests.",
  180. NULL, MB_OK|MB_ICONERROR);
  181. break;
  182. }
  183. tests[iNumberOfTests] = new NewTestType;
  184. iNumberOfTests++;
  185. if (iNumberOfTests == MAXNUMBEROFTESTS) {
  186. EnableWindow(GetDlgItem(hDlg, M_IDC_NEW), FALSE);
  187. EnableWindow(GetDlgItem(hDlg, M_IDC_LOAD), FALSE);
  188. }
  189. iRadio = UpdateTestList(hDlg,iRadio);
  190. break;
  191. case M_IDC_DELETE: // Delete
  192. if (iNumberOfTests == 1)
  193. break;
  194. delete(tests[iRadio]);
  195. iNumberOfTests--;
  196. tests[iRadio] = tests[iNumberOfTests];
  197. if (iNumberOfTests == 1)
  198. EnableWindow(GetDlgItem(hDlg, M_IDC_DELETE), FALSE);
  199. iRadio = UpdateTestList(hDlg,iRadio);
  200. break;
  201. case M_IDC_RENAME: // Rename
  202. pc = (char*)DialogBoxParam(hInstance,
  203. MAKEINTRESOURCE(IDD_RENAME),hDlg,
  204. (DLGPROC)renameDlgProc,
  205. (LPARAM)tests[iRadio]->QueryName());
  206. if (pc) {
  207. tests[iRadio]->Rename(pc);
  208. }
  209. iRadio = UpdateTestList(hDlg,iRadio);
  210. break;
  211. case M_IDC_LOAD: // Load
  212. if (iNumberOfTests == MAXNUMBEROFTESTS) {
  213. MessageBox(hDlg,
  214. "Sorry, you've already got too many tests.",
  215. NULL, MB_OK|MB_ICONERROR);
  216. break;
  217. }
  218. ofn.hwndOwner = hDlg;
  219. ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
  220. if (GetOpenFileName (&ofn)) {
  221. fprintf(stderr, "file to open: %s\n", szFileName);
  222. } else {
  223. fprintf(stderr, "load canceled\n");
  224. break;
  225. }
  226. pTmp = prfl_AutoLoad(szFileName, &i);
  227. if (i) {
  228. MessageBox(hDlg, (char*)pTmp, NULL, MB_OK | MB_ICONERROR);
  229. break;
  230. }
  231. tests[iNumberOfTests] = (SkeletonTest*) pTmp;
  232. iRadio = iNumberOfTests;
  233. iNumberOfTests++;
  234. if (iNumberOfTests == MAXNUMBEROFTESTS) {
  235. EnableWindow(GetDlgItem(hDlg, M_IDC_NEW), FALSE);
  236. EnableWindow(GetDlgItem(hDlg, M_IDC_LOAD), FALSE);
  237. }
  238. iRadio = UpdateTestList(hDlg,iRadio);
  239. break;
  240. case M_IDC_SAVE: // Save
  241. ofn.hwndOwner = hDlg;
  242. ofn.Flags = OFN_HIDEREADONLY;
  243. if (GetSaveFileName (&ofn)) {
  244. fprintf(stderr,"file to save: %s\n",szFileName);
  245. } else {
  246. fprintf(stderr,"save canceled\n");
  247. break;
  248. }
  249. hFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL,
  250. CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  251. if (hFile == INVALID_HANDLE_VALUE) {
  252. fprintf(stderr,"Error opening file: %d\n",GetLastError());
  253. break;
  254. }
  255. i = tests[iRadio]->Save(hFile);
  256. if (i < 0) {
  257. sprintf(acBuffer,"Error number %d while reading file",i);
  258. MessageBox(hDlg,acBuffer,NULL,MB_OK|MB_ICONERROR);
  259. }
  260. if (!CloseHandle(hFile))
  261. fprintf(stderr,"Error closing file: %d\n",GetLastError());
  262. break;
  263. case M_IDC_RUN: // Run
  264. EnableRunmode(hDlg,TRUE);
  265. if (!prfl_StartTest(tests[iRadio],
  266. tests[iRadio]->QueryName(),
  267. main_test_end_callback)) {
  268. fprintf(stderr,"%cError: StartTest == FALSE\n",7);
  269. }
  270. break;
  271. case M_IDC_ABORT:
  272. prfl_StopTest();
  273. KillTimer(hDlg,101);
  274. EnableRunmode(hDlg,FALSE);
  275. break;
  276. case M_IDC_QUIT: // Quit
  277. DestroyWindow(hDlg);
  278. break;
  279. default: // This is an error!
  280. MessageBeep(0);
  281. return DefWindowProc(hDlg,msg,wParam,lParam);
  282. }
  283. }
  284. return 0; // WM_COMMAND
  285. case WM_TIMER:
  286. KillTimer(hDlg,101);
  287. case WM_USER:
  288. if (prfl_TestRunning()) {
  289. SetTimer(hDlg, 101, 1, NULL);
  290. } else {
  291. char acStr[200];
  292. double dNumCalls, dDuration, dResult;
  293. dNumCalls = prfl_GetCount();
  294. dDuration = prfl_GetDuration();
  295. dResult = prfl_GetResult();
  296. sprintf(acStr,
  297. "Test:\t%s\r"
  298. "duration (ms):\t%.10g\r"
  299. "frames drawn: \t%.10g\r"
  300. "FPS: \t%.10g\r"
  301. "%-12s: \t%.10g\r"
  302. "%cPS: \t%.10g",
  303. tests[iRadio]->QueryName(),
  304. dDuration,
  305. dNumCalls,
  306. dNumCalls*1000/dDuration,
  307. tests[iRadio]->td.acTestStatName, dResult,
  308. *tests[iRadio]->td.acTestStatName,dResult*1000/dDuration);
  309. MessageBox(hDlg,acStr,"Test Results",MB_ICONINFORMATION|MB_OK);
  310. EnableRunmode(hDlg,FALSE);
  311. }
  312. return 0; // WM_TIMER
  313. case WM_DESTROY:
  314. PostQuitMessage(0);
  315. return 0; // WM_DESTROY
  316. }
  317. return DefWindowProc(hDlg,msg,wParam,lParam);
  318. }
  319. BOOL CALLBACK renameDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  320. {
  321. static char acBuffer[120];
  322. switch (msg)
  323. {
  324. case WM_INITDIALOG:
  325. SetDlgItemText(hDlg, M_IDC_NEWNAME, (char*) lParam);
  326. return TRUE;
  327. case WM_COMMAND:
  328. {
  329. int i = LOWORD(wParam);
  330. switch (i)
  331. {
  332. case IDOK:
  333. GetDlgItemText(hDlg,M_IDC_NEWNAME,acBuffer,sizeof(acBuffer));
  334. EndDialog(hDlg, (int) acBuffer);
  335. break;
  336. case IDCANCEL:
  337. EndDialog(hDlg, 0);
  338. break;
  339. }
  340. }
  341. return 0; // WM_COMMAND
  342. }
  343. return 0;
  344. }