📄 dxutil.cpp
字号:
//-----------------------------------------------------------------------------
// File: DXUtil.cpp
//
// Desc: Shortcut macros and functions for using DX objects
//-----------------------------------------------------------------------------
#ifndef STRICT
#define STRICT
#endif // !STRICT
#include <windows.h>
#include <mmsystem.h>
#include <tchar.h>
#include <stdio.h>
#include <stdarg.h>
#include "DXUtil.h"
#ifdef UNICODE
typedef HINSTANCE (WINAPI* LPShellExecute)(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
#else
typedef HINSTANCE (WINAPI* LPShellExecute)(HWND hwnd, LPCSTR lpOperation, LPCSTR lpFile, LPCSTR lpParameters, LPCSTR lpDirectory, INT nShowCmd);
#endif
#ifndef UNDER_CE
//-----------------------------------------------------------------------------
// Name: DXUtil_GetDXSDKMediaPathCch()
// Desc: Returns the DirectX SDK media path
// cchDest is the size in TCHARs of strDest. Be careful not to
// pass in sizeof(strDest) on UNICODE builds.
//-----------------------------------------------------------------------------
HRESULT DXUtil_GetDXSDKMediaPathCch( TCHAR* strDest, int cchDest )
{
if( strDest == NULL || cchDest < 1 )
return E_INVALIDARG;
lstrcpy( strDest, TEXT("") );
// Open the appropriate registry key
HKEY hKey;
LONG lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
_T("Software\\Microsoft\\DirectX SDK"),
0, KEY_READ, &hKey );
if( ERROR_SUCCESS != lResult )
return E_FAIL;
DWORD dwType;
DWORD dwSize = cchDest * sizeof(TCHAR);
lResult = RegQueryValueEx( hKey, _T("DX9J3SDK Samples Path"), NULL,
&dwType, (BYTE*)strDest, &dwSize );
strDest[cchDest-1] = 0; // RegQueryValueEx doesn't NULL term if buffer too small
RegCloseKey( hKey );
if( ERROR_SUCCESS != lResult )
return E_FAIL;
const TCHAR* strMedia = _T("\\Media\\");
if( lstrlen(strDest) + lstrlen(strMedia) < cchDest )
_tcscat( strDest, strMedia );
else
return E_INVALIDARG;
return S_OK;
}
#endif // !UNDER_CE
#ifndef UNDER_CE
//-----------------------------------------------------------------------------
// Name: DXUtil_FindMediaFileCch()
// Desc: Returns a valid path to a DXSDK media file
// cchDest is the size in TCHARs of strDestPath. Be careful not to
// pass in sizeof(strDest) on UNICODE builds.
//-----------------------------------------------------------------------------
HRESULT DXUtil_FindMediaFileCch( TCHAR* strDestPath, int cchDest, TCHAR* strFilename )
{
HRESULT hr;
HANDLE file;
TCHAR* strShortNameTmp = NULL;
TCHAR strShortName[MAX_PATH];
int cchPath;
if( NULL==strFilename || NULL==strDestPath || cchDest < 1 )
return E_INVALIDARG;
lstrcpy( strDestPath, TEXT("") );
lstrcpy( strShortName, TEXT("") );
// Build full path name from strFileName (strShortName will be just the leaf filename)
cchPath = GetFullPathName(strFilename, cchDest, strDestPath, &strShortNameTmp);
if ((cchPath == 0) || (cchDest <= cchPath))
return E_FAIL;
if( strShortNameTmp )
lstrcpyn( strShortName, strShortNameTmp, MAX_PATH );
// first try to find the filename given a full path
file = CreateFile( strDestPath, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL );
if( INVALID_HANDLE_VALUE != file )
{
CloseHandle( file );
return S_OK;
}
// next try to find the filename in the current working directory (path stripped)
file = CreateFile( strShortName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL );
if( INVALID_HANDLE_VALUE != file )
{
_tcsncpy( strDestPath, strShortName, cchDest );
strDestPath[cchDest-1] = 0; // _tcsncpy doesn't NULL term if it runs out of space
CloseHandle( file );
return S_OK;
}
// last, check if the file exists in the media directory
if( FAILED( hr = DXUtil_GetDXSDKMediaPathCch( strDestPath, cchDest ) ) )
return hr;
if( lstrlen(strDestPath) + lstrlen(strShortName) < cchDest )
lstrcat( strDestPath, strShortName );
else
return E_INVALIDARG;
file = CreateFile( strDestPath, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL );
if( INVALID_HANDLE_VALUE != file )
{
CloseHandle( file );
return S_OK;
}
// On failure, just return the file as the path
_tcsncpy( strDestPath, strFilename, cchDest );
strDestPath[cchDest-1] = 0; // _tcsncpy doesn't NULL term if it runs out of space
return HRESULT_FROM_WIN32( ERROR_FILE_NOT_FOUND );
}
#endif // !UNDER_CE
//-----------------------------------------------------------------------------
// Name: DXUtil_ReadStringRegKeyCch()
// Desc: Helper function to read a registry key string
// cchDest is the size in TCHARs of strDest. Be careful not to
// pass in sizeof(strDest) on UNICODE builds.
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadStringRegKeyCch( HKEY hKey, TCHAR* strRegName, TCHAR* strDest,
DWORD cchDest, TCHAR* strDefault )
{
DWORD dwType;
DWORD cbDest = cchDest * sizeof(TCHAR);
if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
(BYTE*)strDest, &cbDest ) )
{
_tcsncpy( strDest, strDefault, cchDest );
strDest[cchDest-1] = 0;
if( dwType != REG_SZ )
return E_FAIL;
return S_OK;
}
return E_FAIL;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_WriteStringRegKey()
// Desc: Helper function to write a registry key string
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteStringRegKey( HKEY hKey, TCHAR* strRegName,
TCHAR* strValue )
{
if( NULL == strValue )
return E_INVALIDARG;
DWORD cbValue = ((DWORD)_tcslen(strValue)+1) * sizeof(TCHAR);
if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_SZ,
(BYTE*)strValue, cbValue ) )
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ReadIntRegKey()
// Desc: Helper function to read a registry key int
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD* pdwDest,
DWORD dwDefault )
{
DWORD dwType;
DWORD dwLength = sizeof(DWORD);
if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
(BYTE*)pdwDest, &dwLength ) )
{
*pdwDest = dwDefault;
if( dwType != REG_DWORD )
return E_FAIL;
return S_OK;
}
return E_FAIL;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_WriteIntRegKey()
// Desc: Helper function to write a registry key int
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD dwValue )
{
if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD,
(BYTE*)&dwValue, sizeof(DWORD) ) )
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ReadBoolRegKey()
// Desc: Helper function to read a registry key BOOL
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL* pbDest,
BOOL bDefault )
{
DWORD dwType;
DWORD dwLength = sizeof(BOOL);
if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
(BYTE*)pbDest, &dwLength ) )
{
*pbDest = bDefault;
if( dwType != REG_DWORD )
return E_FAIL;
return S_OK;
}
return E_FAIL;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_WriteBoolRegKey()
// Desc: Helper function to write a registry key BOOL
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL bValue )
{
if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD,
(BYTE*)&bValue, sizeof(BOOL) ) )
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ReadGuidRegKey()
// Desc: Helper function to read a registry key guid
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID* pGuidDest,
GUID& guidDefault )
{
DWORD dwType;
DWORD dwLength = sizeof(GUID);
if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
(LPBYTE) pGuidDest, &dwLength ) )
{
*pGuidDest = guidDefault;
if( dwType != REG_BINARY )
return E_FAIL;
return S_OK;
}
return E_FAIL;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_WriteGuidRegKey()
// Desc: Helper function to write a registry key guid
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID guidValue )
{
if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_BINARY,
(BYTE*)&guidValue, sizeof(GUID) ) )
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_Timer()
// Desc: Performs timer opertations. Use the following commands:
// TIMER_RESET - to reset the timer
// TIMER_START - to start the timer
// TIMER_STOP - to stop (or pause) the timer
// TIMER_ADVANCE - to advance the timer by 0.1 seconds
// TIMER_GETABSOLUTETIME - to get the absolute system time
// TIMER_GETAPPTIME - to get the current time
// TIMER_GETELAPSEDTIME - to get the time that elapsed between
// TIMER_GETELAPSEDTIME calls
//-----------------------------------------------------------------------------
FLOAT __stdcall DXUtil_Timer( TIMER_COMMAND command )
{
static BOOL m_bTimerInitialized = FALSE;
static BOOL m_bUsingQPF = FALSE;
static BOOL m_bTimerStopped = TRUE;
static LONGLONG m_llQPFTicksPerSec = 0;
// Initialize the timer
if( FALSE == m_bTimerInitialized )
{
m_bTimerInitialized = TRUE;
// Use QueryPerformanceFrequency() to get frequency of timer. If QPF is
// not supported, we will timeGetTime() which returns milliseconds.
LARGE_INTEGER qwTicksPerSec;
m_bUsingQPF = QueryPerformanceFrequency( &qwTicksPerSec );
if( m_bUsingQPF )
m_llQPFTicksPerSec = qwTicksPerSec.QuadPart;
}
if( m_bUsingQPF )
{
static LONGLONG m_llStopTime = 0;
static LONGLONG m_llLastElapsedTime = 0;
static LONGLONG m_llBaseTime = 0;
double fTime;
double fElapsedTime;
LARGE_INTEGER qwTime;
// Get either the current time or the stop time, depending
// on whether we're stopped and what command was sent
if( m_llStopTime != 0 && command != TIMER_START && command != TIMER_GETABSOLUTETIME)
qwTime.QuadPart = m_llStopTime;
else
QueryPerformanceCounter( &qwTime );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -