home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / MasterClock.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  2.8 KB  |  120 lines

  1. /* 
  2.  *  MasterClock.cpp
  3.  *
  4.  *    Copyright (C) Alberto Vigata - July 2000 - ultraflask@yahoo.com
  5.  *
  6.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  7.  *    
  8.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  FlasKMPEG is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24. // This is the implementation of a common clock in our system
  25. // All methods have to be reentrant
  26.  
  27. #include "MasterClock.h"
  28. #include <windows.h>
  29. #include <winbase.h>
  30.  
  31. CMasterClock::CMasterClock(ClkType nMode)
  32. {
  33.   m_nMode = nMode;
  34. }
  35.  
  36. CMasterClock::~CMasterClock()
  37. {
  38. }
  39.  
  40. // Determine if the high performance counter is avaliable.
  41. bool CMasterClock::Initialize()
  42. {
  43.   bool nResult=true;
  44.  
  45.   switch( m_nMode )
  46.   {
  47.     case ClkSystem:
  48.       nResult = QueryPerformanceFrequency( (PLARGE_INTEGER)&m_tiFrequency ) == TRUE;
  49.       break;
  50.     default:
  51.       break;
  52.   }
  53.   return nResult;
  54. }
  55.  
  56.  
  57. void CMasterClock::Set(mmtick mmtkTime)
  58. {
  59.  
  60.   // Lock the critical section first
  61.   CFlAutoLock lock(&m_csGlobal);
  62.  
  63.   switch( m_nMode )
  64.   {
  65.     case ClkSystem:
  66.       QueryPerformanceCounter( (PLARGE_INTEGER)&m_tiStartTime );
  67.  
  68.       m_mmtkStartTime = FROMCOUNT_TO_TICK(m_tiStartTime, m_tiFrequency);
  69.  
  70.       // delay = clk  -  our_clk
  71.       m_mmtkStartDelay = m_mmtkStartTime - mmtkTime;
  72.       break;
  73.     case ClkSlave:
  74.       m_tkTime = mmtkTime;
  75.       break;
  76.   }
  77. }
  78.  
  79. mmtick CMasterClock::GetTickCount()
  80. {
  81.   // Lock the critical section first
  82.   CFlAutoLock lock(&m_csGlobal);
  83.   mmtick nTickCount;
  84.  
  85.   switch( m_nMode )
  86.   {
  87.   case ClkSystem:
  88.     tick tiCurrentTime;
  89.     QueryPerformanceCounter( (PLARGE_INTEGER)&tiCurrentTime );
  90.     nTickCount = FROMCOUNT_TO_TICK(tiCurrentTime, m_tiFrequency) - m_mmtkStartDelay;
  91.     break;
  92.   case ClkSlave:
  93.     nTickCount = m_tkTime;
  94.     break;
  95.   }
  96.   return nTickCount;
  97. }
  98.  
  99. void CMasterClock::Wait( mmtick tkTime, mmtick tkWindowPeriod )
  100. {
  101.   // Crop by the window
  102.   if( tkTime > tkWindowPeriod )
  103.     tkTime = tkWindowPeriod;
  104.  
  105.   // Sleep the thread for that number of ms
  106.   Sleep( DWORD(tkTime / 10) );
  107.  
  108.   return;
  109. }
  110.  
  111. void CMasterClock::WaitUntil( mmtick tkTime )
  112. {
  113.   while(1)
  114.   {
  115.     if( GetTickCount() >= tkTime )
  116.       return;
  117.     Sleep(10);
  118.   }
  119.   return;
  120. }