2024-05-12 20:35:20 +08:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// File: DXUTMisc.cpp
|
|
|
|
//
|
|
|
|
// Shortcut macros and functions for using DX objects
|
|
|
|
//
|
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
#include "dxstdafx.h"
|
|
|
|
#undef min // use __min instead
|
|
|
|
#undef max // use __max instead
|
|
|
|
|
2024-05-12 22:41:26 +08:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Global/Static Members
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
CDXUTTimer* DXUTGetGlobalTimer()
|
|
|
|
{
|
|
|
|
// Using an accessor function gives control of the construction order
|
|
|
|
static CDXUTTimer timer;
|
|
|
|
return &timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-12 20:35:20 +08:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
CDXUTTimer::CDXUTTimer()
|
|
|
|
{
|
|
|
|
m_bUsingQPF = false;
|
|
|
|
m_bTimerStopped = true;
|
|
|
|
m_llQPFTicksPerSec = 0;
|
|
|
|
|
|
|
|
m_llStopTime = 0;
|
|
|
|
m_llLastElapsedTime = 0;
|
|
|
|
m_llBaseTime = 0;
|
|
|
|
|
|
|
|
// Use QueryPerformanceFrequency() to get frequency of timer.
|
|
|
|
LARGE_INTEGER qwTicksPerSec;
|
|
|
|
m_bUsingQPF = (bool) (QueryPerformanceFrequency( &qwTicksPerSec ) != 0);
|
|
|
|
m_llQPFTicksPerSec = qwTicksPerSec.QuadPart;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
void CDXUTTimer::Reset()
|
|
|
|
{
|
|
|
|
if( !m_bUsingQPF )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get either the current time or the stop time
|
|
|
|
LARGE_INTEGER qwTime;
|
|
|
|
if( m_llStopTime != 0 )
|
|
|
|
qwTime.QuadPart = m_llStopTime;
|
|
|
|
else
|
|
|
|
QueryPerformanceCounter( &qwTime );
|
|
|
|
|
|
|
|
m_llBaseTime = qwTime.QuadPart;
|
|
|
|
m_llLastElapsedTime = qwTime.QuadPart;
|
|
|
|
m_llStopTime = 0;
|
|
|
|
m_bTimerStopped = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
void CDXUTTimer::Start()
|
|
|
|
{
|
|
|
|
if( !m_bUsingQPF )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the current time
|
|
|
|
LARGE_INTEGER qwTime;
|
|
|
|
QueryPerformanceCounter( &qwTime );
|
|
|
|
|
|
|
|
if( m_bTimerStopped )
|
|
|
|
m_llBaseTime += qwTime.QuadPart - m_llStopTime;
|
|
|
|
m_llStopTime = 0;
|
|
|
|
m_llLastElapsedTime = qwTime.QuadPart;
|
|
|
|
m_bTimerStopped = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
void CDXUTTimer::Stop()
|
|
|
|
{
|
|
|
|
if( !m_bUsingQPF )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( !m_bTimerStopped )
|
|
|
|
{
|
|
|
|
// Get either the current time or the stop time
|
|
|
|
LARGE_INTEGER qwTime;
|
|
|
|
if( m_llStopTime != 0 )
|
|
|
|
qwTime.QuadPart = m_llStopTime;
|
|
|
|
else
|
|
|
|
QueryPerformanceCounter( &qwTime );
|
|
|
|
|
|
|
|
m_llStopTime = qwTime.QuadPart;
|
|
|
|
m_llLastElapsedTime = qwTime.QuadPart;
|
|
|
|
m_bTimerStopped = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
void CDXUTTimer::Advance()
|
|
|
|
{
|
|
|
|
if( !m_bUsingQPF )
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_llStopTime += m_llQPFTicksPerSec/10;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
double CDXUTTimer::GetAbsoluteTime()
|
|
|
|
{
|
|
|
|
if( !m_bUsingQPF )
|
|
|
|
return -1.0;
|
|
|
|
|
|
|
|
// Get either the current time or the stop time
|
|
|
|
LARGE_INTEGER qwTime;
|
|
|
|
if( m_llStopTime != 0 )
|
|
|
|
qwTime.QuadPart = m_llStopTime;
|
|
|
|
else
|
|
|
|
QueryPerformanceCounter( &qwTime );
|
|
|
|
|
|
|
|
double fTime = qwTime.QuadPart / (double) m_llQPFTicksPerSec;
|
|
|
|
|
|
|
|
return fTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
double CDXUTTimer::GetTime()
|
|
|
|
{
|
|
|
|
if( !m_bUsingQPF )
|
|
|
|
return -1.0;
|
|
|
|
|
|
|
|
// Get either the current time or the stop time
|
|
|
|
LARGE_INTEGER qwTime;
|
|
|
|
if( m_llStopTime != 0 )
|
|
|
|
qwTime.QuadPart = m_llStopTime;
|
|
|
|
else
|
|
|
|
QueryPerformanceCounter( &qwTime );
|
|
|
|
|
|
|
|
double fAppTime = (double) ( qwTime.QuadPart - m_llBaseTime ) / (double) m_llQPFTicksPerSec;
|
|
|
|
|
|
|
|
return fAppTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
double CDXUTTimer::GetElapsedTime()
|
|
|
|
{
|
|
|
|
if( !m_bUsingQPF )
|
|
|
|
return -1.0;
|
|
|
|
|
|
|
|
// Get either the current time or the stop time
|
|
|
|
LARGE_INTEGER qwTime;
|
|
|
|
if( m_llStopTime != 0 )
|
|
|
|
qwTime.QuadPart = m_llStopTime;
|
|
|
|
else
|
|
|
|
QueryPerformanceCounter( &qwTime );
|
|
|
|
|
|
|
|
double fElapsedTime = (double) ( qwTime.QuadPart - m_llLastElapsedTime ) / (double) m_llQPFTicksPerSec;
|
|
|
|
m_llLastElapsedTime = qwTime.QuadPart;
|
|
|
|
|
|
|
|
return fElapsedTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
bool CDXUTTimer::IsStopped()
|
|
|
|
{
|
|
|
|
return m_bTimerStopped;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-12 22:44:56 +08:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Returns pointer to static media search buffer
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
TCHAR* DXUTMediaSearchPath()
|
|
|
|
{
|
|
|
|
static TCHAR s_strMediaSearchPath[MAX_PATH] = {0};
|
|
|
|
return s_strMediaSearchPath;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-05-12 22:45:31 +08:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
LPCTSTR DXUTGetMediaSearchPath()
|
|
|
|
{
|
|
|
|
return DXUTMediaSearchPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-12 22:48:50 +08:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
HRESULT DXUTSetMediaSearchPath( LPCTSTR strPath )
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
TCHAR* s_strSearchPath = DXUTMediaSearchPath();
|
|
|
|
|
|
|
|
hr = StringCchCopy( s_strSearchPath, MAX_PATH, strPath );
|
|
|
|
if( SUCCEEDED(hr) )
|
|
|
|
{
|
|
|
|
// append slash if needed
|
|
|
|
size_t ch;
|
|
|
|
hr = StringCchLength( s_strSearchPath, MAX_PATH, &ch );
|
|
|
|
if( SUCCEEDED(hr) && s_strSearchPath[ch-1] != L'\\')
|
|
|
|
{
|
|
|
|
hr = StringCchCat( s_strSearchPath, MAX_PATH, "\\" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-12 22:50:11 +08:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Search a set of typical directories
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
bool DXUTFindMediaSearchTypicalDirs( TCHAR* strSearchPath, int cchSearch, LPCTSTR strLeaf,
|
|
|
|
TCHAR* strExePath, TCHAR* strExeName )
|
|
|
|
{
|
|
|
|
// Typical directories:
|
|
|
|
// .\
|
|
|
|
// ..\
|
|
|
|
// ..\..\
|
|
|
|
// %EXE_DIR%\
|
|
|
|
// %EXE_DIR%\..\
|
|
|
|
// %EXE_DIR%\..\..\
|
|
|
|
// %EXE_DIR%\..\%EXE_NAME%
|
|
|
|
// %EXE_DIR%\..\..\%EXE_NAME%
|
|
|
|
// DXSDK media path
|
|
|
|
|
|
|
|
// Search in .\
|
|
|
|
StringCchCopy( strSearchPath, cchSearch, strLeaf );
|
|
|
|
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Search in ..\
|
|
|
|
StringCchPrintf( strSearchPath, cchSearch, "..\\%s", strLeaf );
|
|
|
|
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Search in ..\..\
|
|
|
|
StringCchPrintf( strSearchPath, cchSearch, "..\\..\\%s", strLeaf );
|
|
|
|
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Search in ..\..\
|
|
|
|
StringCchPrintf( strSearchPath, cchSearch, "..\\..\\%s", strLeaf );
|
|
|
|
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Search in the %EXE_DIR%\
|
|
|
|
StringCchPrintf( strSearchPath, cchSearch, "%s\\%s", strExePath, strLeaf );
|
|
|
|
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Search in the %EXE_DIR%\..\
|
|
|
|
StringCchPrintf( strSearchPath, cchSearch, "%s\\..\\%s", strExePath, strLeaf );
|
|
|
|
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Search in the %EXE_DIR%\..\..\
|
|
|
|
StringCchPrintf( strSearchPath, cchSearch, "%s\\..\\..\\%s", strExePath, strLeaf );
|
|
|
|
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Search in "%EXE_DIR%\..\%EXE_NAME%\". This matches the DirectX SDK layout
|
|
|
|
StringCchPrintf( strSearchPath, cchSearch, "%s\\..\\%s\\%s", strExePath, strExeName, strLeaf );
|
|
|
|
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Search in "%EXE_DIR%\..\..\%EXE_NAME%\". This matches the DirectX SDK layout
|
|
|
|
StringCchPrintf( strSearchPath, cchSearch, "%s\\..\\..\\%s\\%s", strExePath, strExeName, strLeaf );
|
|
|
|
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Search in media search dir
|
|
|
|
TCHAR* s_strSearchPath = DXUTMediaSearchPath();
|
|
|
|
if( s_strSearchPath[0] != 0 )
|
|
|
|
{
|
|
|
|
StringCchPrintf( strSearchPath, cchSearch, "%s%s", s_strSearchPath, strLeaf );
|
|
|
|
if( GetFileAttributes( strSearchPath ) != 0xFFFFFFFF )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-12 22:51:13 +08:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Search parent directories starting at strStartAt, and appending strLeafName
|
|
|
|
// at each parent directory. It stops at the root directory.
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
bool DXUTFindMediaSearchParentDirs( TCHAR* strSearchPath, int cchSearch, TCHAR* strStartAt, TCHAR* strLeafName )
|
|
|
|
{
|
|
|
|
TCHAR strFullPath[MAX_PATH] = {0};
|
|
|
|
TCHAR strFullFileName[MAX_PATH] = {0};
|
|
|
|
TCHAR strSearch[MAX_PATH] = {0};
|
|
|
|
TCHAR* strFilePart = NULL;
|
|
|
|
|
|
|
|
GetFullPathName( strStartAt, MAX_PATH, strFullPath, &strFilePart );
|
|
|
|
if( strFilePart == NULL )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while( strFilePart != NULL && *strFilePart != '\0' )
|
|
|
|
{
|
|
|
|
StringCchPrintf( strFullFileName, MAX_PATH, "%s\\%s", strFullPath, strLeafName );
|
|
|
|
if( GetFileAttributes( strFullFileName ) != 0xFFFFFFFF )
|
|
|
|
{
|
|
|
|
StringCchCopy( strSearchPath, cchSearch, strFullFileName );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringCchPrintf( strSearch, MAX_PATH, "%s\\..", strFullPath );
|
|
|
|
GetFullPathName( strSearch, MAX_PATH, strFullPath, &strFilePart );
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|