home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_02 / Timer / Timer.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-17  |  3.0 KB  |  98 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : timer.cpp                                                             //
  10. //  Description: Compare timer accuracy, Chapter 2                                   //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include <stdio.h>
  19. #include <tchar.h>
  20. #include <string.h>
  21. #include <mmsystem.h>
  22.  
  23. #include "resource.h"
  24.  
  25. int MyMessageBox(HWND hWnd, const TCHAR * text, const TCHAR * caption, DWORD style)
  26. {
  27.     MSGBOXPARAMS param;
  28.  
  29.     memset(& param, 0, sizeof(param));
  30.     param.cbSize      = sizeof(param);
  31.     param.hwndOwner   = hWnd;
  32.     param.hInstance   = GetModuleHandle(NULL);
  33.     param.lpszText    = text;
  34.     param.lpszCaption = caption;
  35.     param.dwStyle     = style | MB_USERICON;
  36.     param.lpszIcon    = MAKEINTRESOURCE(IDI_GRAPH);
  37.  
  38.     return MessageBoxIndirect(¶m);
  39. }
  40.  
  41. #pragma warning(disable : 4035 )
  42.  
  43. __int64 MyQueryCounter(void)
  44. {
  45.     _asm    _emit 0x0F
  46.     _asm    _emit 0x31
  47. }
  48.  
  49. __int64 MyQueryFrequency(void)
  50. {
  51.     __int64 start = MyQueryCounter();
  52.     Sleep(1000);
  53.  
  54.     __int64 stop  = MyQueryCounter();
  55.  
  56.     return stop - start;
  57. }
  58.  
  59. int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  60. {
  61.     const int SAMPLES = 32;
  62.  
  63.     DWORD   t1[SAMPLES], t2[SAMPLES];
  64.     __int64 t3[SAMPLES], t4[SAMPLES], freq3, freq4;
  65.  
  66.     TIMECAPS tc;
  67.     timeGetDevCaps(&tc, sizeof(tc));
  68.     timeBeginPeriod(1);
  69.     
  70.     QueryPerformanceFrequency((LARGE_INTEGER *) & freq3); freq3 /= 1000;
  71.     freq4 = MyQueryFrequency();                              freq4 /= 1000;
  72.  
  73.     for (int i=0; i<SAMPLES; i++)
  74.     {
  75.         for (int j=0; j<200; j++)    // roughly 0.2 ms to 0.5ms delay
  76.             DeleteObject(CreateSolidBrush(0));
  77.         
  78.         t1[i] = GetTickCount();
  79.         t2[i] = timeGetTime();
  80.         QueryPerformanceCounter((LARGE_INTEGER *) & t3[i]);
  81.         t4[i] = MyQueryCounter();
  82.     }
  83.     timeEndPeriod(1);
  84.  
  85.     TCHAR buffer[1024];
  86.  
  87.     sprintf(buffer, _T("tick   mm %d Khz %5.1f Mhz %5.1f Mhz\n\n"), tc.wPeriodMin, freq3/1000.0, freq4/1000.0);
  88.     
  89.     for (i=0; i<SAMPLES; i++)
  90.         wsprintf(buffer+ _tcslen(buffer), "%8d %8d %8d %8d\n", 
  91.             (t1[i]-t1[0])*1000000, 
  92.             (t2[i]-t2[0])*1000000, 
  93.             (int)((t3[i]-t3[0])*1000000/freq3), (int)((t4[i]-t4[0])*1000000/freq4));
  94.  
  95.     MyMessageBox(NULL, buffer, "Timer Accuracy", MB_OK);
  96.  
  97.     return 0;
  98. }