home *** CD-ROM | disk | FTP | other *** search
/ 220 Jogos / 220 jogos.iso / tetris / tetron / SOURCE / Timer.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-13  |  2.9 KB  |  79 lines

  1. //----------------------------------------------------------------------------------------
  2. //----------------------------------------------------------------------------------------
  3. //
  4. //        Filename        :    Timer.cpp
  5. //        Description        :    Member Definitions Timer class
  6. //        Author            :   Marnich van Rensburg (2002)
  7. //
  8. //----------------------------------------------------------------------------------------
  9. //----------------------------------------------------------------------------------------
  10.  
  11. #include "timer.h"
  12.  
  13. Timer :: Timer()                                            // Initialize Our Timer (Get It Ready)
  14. {
  15.     memset(&TimerData, 0, sizeof(TimerData));                        // Clear Our Timer Structure
  16.  
  17.     // Check To See If A Performance Counter Is Available
  18.     // If One Is Available The Timer Frequency Will Be Updated
  19.     if (!QueryPerformanceFrequency((LARGE_INTEGER *) &TimerData.frequency))
  20.     {
  21.         // No Performace Counter Available
  22.         TimerData.performance_timer    = FALSE;                    // Set Performance Timer To FALSE
  23.         TimerData.mm_timer_start    = timeGetTime();            // Use timeGetTime() To Get Current Time
  24.         TimerData.resolution        = 1.0f/1000.0f;                // Set Our Timer Resolution To .001f
  25.         TimerData.frequency            = 1000;                        // Set Our Timer Frequency To 1000
  26.         TimerData.mm_timer_elapsed    = TimerData.mm_timer_start;        // Set The Elapsed Time To The Current Time
  27.     }
  28.     else
  29.     {
  30.         // Performance Counter Is Available, Use It Instead Of The Multimedia Timer
  31.         // Get The Current Time And Store It In performance_timer_start
  32.         QueryPerformanceCounter((LARGE_INTEGER *) &TimerData.performance_timer_start);
  33.         TimerData.performance_timer            = TRUE;                // Set Performance Timer To TRUE
  34.         // Calculate The Timer Resolution Using The Timer Frequency
  35.         TimerData.resolution                = (float) (((double)1.0f)/((double)TimerData.frequency));
  36.         // Set The Elapsed Time To The Current Time
  37.         TimerData.performance_timer_elapsed    = TimerData.performance_timer_start;
  38.     }//if
  39.  
  40.     Start = GetTime();
  41. } // Timer
  42.  
  43.  
  44.  
  45.  
  46. float Timer :: GetTime()                                        // Get Time In Milliseconds
  47. {
  48.     __int64 time;                                            // time Will Hold A 64 Bit Integer
  49.  
  50.     if (TimerData.performance_timer)                            // Are We Using The Performance Timer?
  51.     {
  52.         QueryPerformanceCounter((LARGE_INTEGER *) &time);    // Grab The Current Performance Time
  53.         // Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS)
  54.         return ( (float) ( time - TimerData.performance_timer_start) * TimerData.resolution)*1000.0f;
  55.     }
  56.     else
  57.     {
  58.         // Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS)
  59.         return( (float) ( timeGetTime() - TimerData.mm_timer_start) * TimerData.resolution)*1000.0f;
  60.     }//if
  61.  
  62. }// GetTime
  63.  
  64.  
  65. bool Timer :: CheckFreq(float v_Freq)
  66. {
  67.     if(v_Freq == 0.0f)
  68.         Start = GetTime();
  69.  
  70.     if ((GetTime() - Start) >= v_Freq)
  71.     {
  72.         Start = GetTime();        
  73.         return true;
  74.     }else{
  75.         return false;
  76.     }//if
  77.  
  78. }// CheckFreq
  79.