Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

D:/Programming/GUI Editor (Source)/Timer.cpp

00001 /* 00002 CTimer.cpp 00003 00004 Timer class under Microsoft Windows. 00005 00006 Author: Brett Porter 00007 Email: brettporter@yahoo.com 00008 Website: http://rsn.gamedev.net/pl3d 00009 Copyright (C)2000, 2001, Brett Porter. All Rights Reserved. 00010 This source code is released under the LGPL. See license.txt for details. 00011 00012 Created: 18 July 2000 00013 Last Edited: 6 Febraury 2001 00014 00015 Please see the file ChangeLog.html for a revision history. 00016 */ 00017 00018 #include "stdafx.h" 00019 #define WIN32_LEAN_AND_MEAN 00020 #include <windows.h> 00021 #include "Timer.h" 00022 00023 #ifndef _WIN32 00024 # error This file should only be used in a Microsoft Windows build. 00025 #endif 00026 00027 // Disable warning about non-standard extension in Microsoft's header files 00028 #pragma warning( disable : 4201 ) 00029 #include <mmsystem.h> 00030 #pragma warning( default : 4201 ) 00031 #pragma comment( lib, "winmm.lib" ) 00032 00033 00034 CTimer::CTimer() 00035 { 00036 if ( QueryPerformanceFrequency(( LARGE_INTEGER* )&m_dwFrequency ) == false ) 00037 { 00038 // No performance counter available 00039 m_bUsePerformanceCounter = false; 00040 00041 m_dwTimerStart = timeGetTime(); 00042 } 00043 else 00044 { 00045 // Performance counter is available, use it instead of the multimedia timer 00046 m_bUsePerformanceCounter = true; 00047 00048 // Get the current time and store it in pcTimerStart 00049 QueryPerformanceCounter(( LARGE_INTEGER* )&m_dwTimerStart ); 00050 00051 // Calculate the timer resolution using the timer frequency 00052 m_fResolution = ( float )( 1.0/( double )m_dwFrequency )*1000.0f; 00053 } 00054 00055 m_iPauseCount = 0; 00056 m_fPauseTime = 0; 00057 } 00058 00059 CTimer::~CTimer() 00060 { 00061 00062 } 00063 00064 void CTimer::Reset() 00065 { 00066 if ( m_bUsePerformanceCounter == true ) 00067 QueryPerformanceCounter(( LARGE_INTEGER* )&m_dwTimerStart ); 00068 else 00069 m_dwTimerStart = timeGetTime(); 00070 00071 m_fPauseTime = GetTime(); 00072 } 00073 00074 double CTimer::GetTime() 00075 { 00076 if ( m_iPauseCount > 0 ) 00077 return m_fPauseTime; 00078 00079 __int64 timeElapsed; 00080 00081 if ( m_bUsePerformanceCounter == true ) 00082 { 00083 QueryPerformanceCounter(( LARGE_INTEGER* )&timeElapsed ); 00084 timeElapsed -= m_dwTimerStart; 00085 return timeElapsed*m_fResolution; 00086 } 00087 else 00088 { 00089 timeElapsed = timeGetTime()-m_dwTimerStart; 00090 return ( double )timeElapsed; 00091 } 00092 } 00093 00094 void CTimer::Pause() 00095 { 00096 if ( m_iPauseCount == 0 ) 00097 m_fPauseTime = GetTime(); 00098 00099 m_iPauseCount++; 00100 } 00101 00102 00103 double CTimer::GetAbsoluteTime() 00104 { 00105 __int64 timeElapsed; 00106 if ( m_bUsePerformanceCounter == true ) 00107 QueryPerformanceCounter(( LARGE_INTEGER* )&timeElapsed ); 00108 00109 else 00110 timeElapsed = timeGetTime(); 00111 00112 return timeElapsed*m_fResolution; 00113 } 00114 00115 void CTimer::Unpause() 00116 { 00117 if(m_iPauseCount > 0 ) 00118 m_iPauseCount--; 00119 00120 if ( m_iPauseCount == 0 ) 00121 { 00122 if ( m_bUsePerformanceCounter == true ) 00123 { 00124 __int64 time; 00125 QueryPerformanceCounter(( LARGE_INTEGER* )&time ); 00126 m_dwTimerStart = time - ( __int64 )( m_fPauseTime/m_fResolution ); 00127 } 00128 else 00129 { 00130 m_dwTimerStart = ( unsigned )( timeGetTime() - m_fPauseTime ); 00131 } 00132 } 00133 } 00134

Generated on Sun Jul 17 21:34:28 2005 for OpenGL GUI by doxygen 1.3.8