OSDN Git Service

#xxxxx DTXViewerのプロジェクトを追加。
[dtxmania/dtxmania.git] / DTXViewerプロジェクト / @FDK10プロジェクト / CTimer.cpp
diff --git a/DTXViewerプロジェクト/@FDK10プロジェクト/CTimer.cpp b/DTXViewerプロジェクト/@FDK10プロジェクト/CTimer.cpp
new file mode 100644 (file)
index 0000000..8df693e
--- /dev/null
@@ -0,0 +1,144 @@
+#include "stdafx.h"
+#include "CTimer.h"
+
+namespace FDK {
+       namespace General {
+
+// static \83\81\83\93\83o
+int                                    CTimer::nRefCount = 0;
+int                                    CTimer::timerType = TIMERTYPE_GETTICKCOUNT;
+TIMECAPS                       CTimer::dwTimeCaps;
+LARGE_INTEGER          CTimer::liFrequency;
+//double                               CTimerEx::dbTimeGetTime = 0;
+
+void   CTimer::Init( int timerType )
+{
+       // \8dÅ\8f\89\82Ì\83C\83\93\83X\83^\83\93\83X\82È\82ç\81A\91S\83C\83\93\83X\83^\83\93\83X\8b¤\92Ê\82Ì\8f\89\8aú\89»\82ð\8ds\82¤\81B
+       if( this->nRefCount == 0 )
+       {
+               // \8eg\97p\82·\82é\83^\83C\83}\82ð\8c\88\92è\82·\82é
+               switch( timerType )
+               {
+               // \82`\81D\8d\82\90¸\93x\83^\83C\83}
+               // \82c\81D\96¢\92è\8b`
+               case TIMERTYPE_PERFORMANCECOUNTER:
+               case TIMERTYPE_UNKNOWN:
+                       if( CheckAndSetA_HighPerformanceTimer() ) break;
+                       if( CheckAndSetB_MultimediaTimer() ) break;
+                       SetC_OldTimer();
+                       break;
+
+               // \82a\81D\83}\83\8b\83`\83\81\83f\83B\83A\83^\83C\83}
+               case TIMERTYPE_TIMEGTTIME:
+                       if( CheckAndSetB_MultimediaTimer() ) break;
+                       if( CheckAndSetA_HighPerformanceTimer() ) break;
+                       SetC_OldTimer();
+                       break;
+
+               // \82»\82Ì\91¼\81D\8b\8c\8e®\83^\83C\83}
+               default:
+                       SetC_OldTimer();
+                       break;
+               }
+       }
+
+       Reset();                // \83^\83C\83}\83\8a\83Z\83b\83g
+       this->nRefCount++;      // \8eQ\8fÆ\83J\83E\83\93\83g\91\9d\89Á
+}
+
+bool   CTimer::CheckAndSetA_HighPerformanceTimer()
+{
+       if( ::QueryPerformanceFrequency( &liFrequency ) )
+       {
+               CTimer::timerType = TIMERTYPE_PERFORMANCECOUNTER;
+               return true;
+       }
+       return false;
+}
+
+bool   CTimer::CheckAndSetB_MultimediaTimer()
+{
+       if( ::timeGetDevCaps( &dwTimeCaps, sizeof( TIMECAPS ) ) == TIMERR_NOERROR && dwTimeCaps.wPeriodMin < 10 )
+       {
+               CTimer::timerType = TIMERTYPE_TIMEGTTIME;
+               ::timeBeginPeriod( dwTimeCaps.wPeriodMin );
+               return true;
+       }
+       return false;
+}
+
+bool   CTimer::SetC_OldTimer()
+{
+       CTimer::timerType = TIMERTYPE_GETTICKCOUNT;
+       dwTimeCaps.wPeriodMax = 1;      // \90¸\93x1ms\81i\82Ì\82Â\82à\82è\82É\82È\82é\81j
+       return true;
+}
+
+void   CTimer::Term()
+{
+       // \8eQ\8fÆ\83J\83E\83\93\83g\8c¸\8f­
+       this->nRefCount --;
+
+       // \83C\83\93\83X\83^\83\93\83X\82ª\82È\82­\82È\82Á\82½\82ç\8fI\97¹\8f\88\97\9d
+       if( this->nRefCount == 0 )
+       {
+               if( this->timerType == TIMERTYPE_TIMEGTTIME )
+                       ::timeEndPeriod( this->dwTimeCaps.wPeriodMin );
+       }
+}
+
+void   CTimer::Reset()
+{
+       this->dbOffsetTime = this->dbPauseTime = GetTime();
+       this->nPaused = 0;
+}
+
+double CTimer::Get()
+{
+       return ( this->nPaused > 0 ) ? (this->dbPauseTime - this->dbOffsetTime) : (GetTime() - this->dbOffsetTime);
+}
+
+void   CTimer::Set( double dbTime )
+{
+       this->dbOffsetTime = ( this->nPaused > 0 ) ? (this->dbPauseTime - dbTime) : (GetTime() - dbTime);
+}
+
+void   CTimer::Pause()
+{
+       if ( this->nPaused++ == 0 )
+               this->dbPauseTime = GetTime();
+}
+
+void   CTimer::Restart()
+{
+       if ( --this->nPaused == 0)
+               this->dbOffsetTime += GetTime() - this->dbPauseTime;
+}
+
+double CTimer::GetTime()
+{
+       switch( CTimer::timerType )
+       {
+       case TIMERTYPE_GETTICKCOUNT:
+               return (double) ::GetTickCount();
+
+       case TIMERTYPE_TIMEGTTIME:
+               return (double) ::timeGetTime();
+
+       case TIMERTYPE_PERFORMANCECOUNTER:
+               {
+                       double dbNow = 0.0;
+                       if( liFrequency.QuadPart != 0 )
+                       {
+                               LARGE_INTEGER liNowTime;
+                               ::QueryPerformanceCounter( &liNowTime );
+                               dbNow = liNowTime.QuadPart / ( liFrequency.QuadPart / 1000.0 );
+                       }
+                       return dbNow;
+               }
+       }
+       return 0.0;
+}
+
+       }//General
+}//FDK
\ No newline at end of file