0001:
0002:
0003:
0004:
0005:
0006: #define STRICT
0007: #include <windows.h>
0008: #include <mmsystem.h>
0009: #include <tchar.h>
0010: #include <stdio.h>
0011: #include <stdarg.h>
0012: #include "DXUtil.h"
0013:
0014:
0015:
0016:
0017:
0018:
0019:
0020:
0021: const TCHAR* DXUtil_GetDXSDKMediaPath()
0022: {
0023: static TCHAR strNull[2] = _T("");
0024: static TCHAR strPath[MAX_PATH];
0025: DWORD dwType;
0026: DWORD dwSize = MAX_PATH;
0027: HKEY hKey;
0028:
0029:
0030: LONG lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
0031: _T("Software\\Microsoft\\DirectX SDK"),
0032: 0, KEY_READ, &hKey );
0033: if( ERROR_SUCCESS != lResult )
0034: return strNull;
0035:
0036: lResult = RegQueryValueEx( hKey, _T("DX81SDK Samples Path"), NULL,
0037: &dwType, (BYTE*)strPath, &dwSize );
0038: RegCloseKey( hKey );
0039:
0040: if( ERROR_SUCCESS != lResult )
0041: return strNull;
0042:
0043: _tcscat( strPath, _T("\\Media\\") );
0044:
0045: return strPath;
0046: }
0047:
0048:
0049:
0050:
0051:
0052:
0053:
0054:
0055: HRESULT DXUtil_FindMediaFile( TCHAR* strPath, TCHAR* strFilename )
0056: {
0057: HANDLE file;
0058: TCHAR strFullPath[1024];
0059: TCHAR *strShortName;
0060: DWORD cchPath;
0061:
0062: if( NULL==strFilename || NULL==strPath )
0063: return E_INVALIDARG;
0064:
0065:
0066: cchPath = GetFullPathName(strFilename, sizeof(strFullPath)/sizeof(TCHAR), strFullPath, &strShortName);
0067: if ((cchPath == 0) || (sizeof(strFullPath)/sizeof(TCHAR) <= cchPath))
0068: return E_FAIL;
0069:
0070:
0071: file = CreateFile( strFullPath, GENERIC_READ, FILE_SHARE_READ, NULL,
0072: OPEN_EXISTING, 0, NULL );
0073: if( INVALID_HANDLE_VALUE != file )
0074: {
0075: _tcscpy( strPath, strFullPath );
0076: CloseHandle( file );
0077: return S_OK;
0078: }
0079:
0080:
0081: file = CreateFile( strShortName, GENERIC_READ, FILE_SHARE_READ, NULL,
0082: OPEN_EXISTING, 0, NULL );
0083: if( INVALID_HANDLE_VALUE != file )
0084: {
0085: _tcscpy( strPath, strShortName );
0086: CloseHandle( file );
0087: return S_OK;
0088: }
0089:
0090:
0091: _stprintf( strPath, _T("%s%s"), DXUtil_GetDXSDKMediaPath(), strShortName );
0092:
0093: file = CreateFile( strPath, GENERIC_READ, FILE_SHARE_READ, NULL,
0094: OPEN_EXISTING, 0, NULL );
0095: if( INVALID_HANDLE_VALUE != file )
0096: {
0097: CloseHandle( file );
0098: return S_OK;
0099: }
0100:
0101:
0102: _tcscpy( strPath, strFilename );
0103: return E_FAIL;
0104: }
0105:
0106:
0107:
0108:
0109:
0110:
0111:
0112:
0113: HRESULT DXUtil_ReadStringRegKey( HKEY hKey, TCHAR* strRegName, TCHAR* strValue,
0114: DWORD dwLength, TCHAR* strDefault )
0115: {
0116: DWORD dwType;
0117:
0118: if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
0119: (BYTE*)strValue, &dwLength ) )
0120: {
0121: _tcscpy( strValue, strDefault );
0122: }
0123:
0124: return S_OK;
0125: }
0126:
0127:
0128:
0129:
0130:
0131:
0132:
0133:
0134: HRESULT DXUtil_WriteStringRegKey( HKEY hKey, TCHAR* strRegName,
0135: TCHAR* strValue )
0136: {
0137: if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_SZ,
0138: (BYTE*)strValue,
0139: (_tcslen(strValue)+1)*sizeof(TCHAR) ) )
0140: return E_FAIL;
0141:
0142: return S_OK;
0143: }
0144:
0145:
0146:
0147:
0148:
0149:
0150:
0151:
0152: HRESULT DXUtil_ReadIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD* pdwValue,
0153: DWORD dwDefault )
0154: {
0155: DWORD dwType;
0156: DWORD dwLength = sizeof(DWORD);
0157:
0158: if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
0159: (BYTE*)pdwValue, &dwLength ) )
0160: {
0161: *pdwValue = dwDefault;
0162: }
0163:
0164: return S_OK;
0165: }
0166:
0167:
0168:
0169:
0170:
0171:
0172:
0173:
0174: HRESULT DXUtil_WriteIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD dwValue )
0175: {
0176: if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD,
0177: (BYTE*)&dwValue, sizeof(DWORD) ) )
0178: return E_FAIL;
0179:
0180: return S_OK;
0181: }
0182:
0183:
0184:
0185:
0186:
0187:
0188:
0189:
0190: HRESULT DXUtil_ReadBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL* pbValue,
0191: BOOL bDefault )
0192: {
0193: DWORD dwType;
0194: DWORD dwLength = sizeof(BOOL);
0195:
0196: if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
0197: (BYTE*)pbValue, &dwLength ) )
0198: {
0199: *pbValue = bDefault;
0200: }
0201:
0202: return S_OK;
0203: }
0204:
0205:
0206:
0207:
0208:
0209:
0210:
0211:
0212: HRESULT DXUtil_WriteBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL bValue )
0213: {
0214: if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD,
0215: (BYTE*)&bValue, sizeof(BOOL) ) )
0216: return E_FAIL;
0217:
0218: return S_OK;
0219: }
0220:
0221:
0222:
0223:
0224:
0225:
0226:
0227:
0228: HRESULT DXUtil_ReadGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID* pGuidValue,
0229: GUID& guidDefault )
0230: {
0231: DWORD dwType;
0232: DWORD dwLength = sizeof(GUID);
0233:
0234: if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
0235: (LPBYTE) pGuidValue, &dwLength ) )
0236: {
0237: *pGuidValue = guidDefault;
0238: }
0239:
0240: return S_OK;
0241: }
0242:
0243:
0244:
0245:
0246:
0247:
0248:
0249:
0250: HRESULT DXUtil_WriteGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID guidValue )
0251: {
0252: if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_BINARY,
0253: (BYTE*)&guidValue, sizeof(GUID) ) )
0254: return E_FAIL;
0255:
0256: return S_OK;
0257: }
0258:
0259:
0260:
0261:
0262:
0263:
0264:
0265:
0266:
0267:
0268:
0269:
0270:
0271:
0272:
0273:
0274: FLOAT __stdcall DXUtil_Timer( TIMER_COMMAND command )
0275: {
0276: static BOOL m_bTimerInitialized = FALSE;
0277: static BOOL m_bUsingQPF = FALSE;
0278: static BOOL m_bTimerStopped = TRUE;
0279: static LONGLONG m_llQPFTicksPerSec = 0;
0280:
0281:
0282: if( FALSE == m_bTimerInitialized )
0283: {
0284: m_bTimerInitialized = TRUE;
0285:
0286:
0287:
0288: LARGE_INTEGER qwTicksPerSec;
0289: m_bUsingQPF = QueryPerformanceFrequency( &qwTicksPerSec );
0290: if( m_bUsingQPF )
0291: m_llQPFTicksPerSec = qwTicksPerSec.QuadPart;
0292: }
0293:
0294: if( m_bUsingQPF )
0295: {
0296: static LONGLONG m_llStopTime = 0;
0297: static LONGLONG m_llLastElapsedTime = 0;
0298: static LONGLONG m_llBaseTime = 0;
0299: double fTime;
0300: double fElapsedTime;
0301: LARGE_INTEGER qwTime;
0302:
0303:
0304:
0305: if( m_llStopTime != 0 && command != TIMER_START && command != TIMER_GETABSOLUTETIME)
0306: qwTime.QuadPart = m_llStopTime;
0307: else
0308: QueryPerformanceCounter( &qwTime );
0309:
0310:
0311: if( command == TIMER_GETELAPSEDTIME )
0312: {
0313: fElapsedTime = (double) ( qwTime.QuadPart - m_llLastElapsedTime ) / (double) m_llQPFTicksPerSec;
0314: m_llLastElapsedTime = qwTime.QuadPart;
0315: return (FLOAT) fElapsedTime;
0316: }
0317:
0318:
0319: if( command == TIMER_GETAPPTIME )
0320: {
0321: double fAppTime = (double) ( qwTime.QuadPart - m_llBaseTime ) / (double) m_llQPFTicksPerSec;
0322: return (FLOAT) fAppTime;
0323: }
0324:
0325:
0326: if( command == TIMER_RESET )
0327: {
0328: m_llBaseTime = qwTime.QuadPart;
0329: m_llLastElapsedTime = qwTime.QuadPart;
0330: m_llStopTime = 0;
0331: m_bTimerStopped = FALSE;
0332: return 0.0f;
0333: }
0334:
0335:
0336: if( command == TIMER_START )
0337: {
0338: if( m_bTimerStopped )
0339: m_llBaseTime += qwTime.QuadPart - m_llStopTime;
0340: m_llStopTime = 0;
0341: m_llLastElapsedTime = qwTime.QuadPart;
0342: m_bTimerStopped = FALSE;
0343: return 0.0f;
0344: }
0345:
0346:
0347: if( command == TIMER_STOP )
0348: {
0349: m_llStopTime = qwTime.QuadPart;
0350: m_llLastElapsedTime = qwTime.QuadPart;
0351: m_bTimerStopped = TRUE;
0352: return 0.0f;
0353: }
0354:
0355:
0356: if( command == TIMER_ADVANCE )
0357: {
0358: m_llStopTime += m_llQPFTicksPerSec/10;
0359: return 0.0f;
0360: }
0361:
0362: if( command == TIMER_GETABSOLUTETIME )
0363: {
0364: fTime = qwTime.QuadPart / (double) m_llQPFTicksPerSec;
0365: return (FLOAT) fTime;
0366: }
0367:
0368: return -1.0f;
0369: }
0370: else
0371: {
0372:
0373: static double m_fLastElapsedTime = 0.0;
0374: static double m_fBaseTime = 0.0;
0375: static double m_fStopTime = 0.0;
0376: double fTime;
0377: double fElapsedTime;
0378:
0379:
0380:
0381: if( m_fStopTime != 0.0 && command != TIMER_START && command != TIMER_GETABSOLUTETIME)
0382: fTime = m_fStopTime;
0383: else
0384: fTime = timeGetTime() * 0.001;
0385:
0386:
0387: if( command == TIMER_GETELAPSEDTIME )
0388: {
0389: fElapsedTime = (double) (fTime - m_fLastElapsedTime);
0390: m_fLastElapsedTime = fTime;
0391: return (FLOAT) fElapsedTime;
0392: }
0393:
0394:
0395: if( command == TIMER_GETAPPTIME )
0396: {
0397: return (FLOAT) (fTime - m_fBaseTime);
0398: }
0399:
0400:
0401: if( command == TIMER_RESET )
0402: {
0403: m_fBaseTime = fTime;
0404: m_fLastElapsedTime = fTime;
0405: m_fStopTime = 0;
0406: m_bTimerStopped = FALSE;
0407: return 0.0f;
0408: }
0409:
0410:
0411: if( command == TIMER_START )
0412: {
0413: if( m_bTimerStopped )
0414: m_fBaseTime += fTime - m_fStopTime;
0415: m_fStopTime = 0.0f;
0416: m_fLastElapsedTime = fTime;
0417: m_bTimerStopped = FALSE;
0418: return 0.0f;
0419: }
0420:
0421:
0422: if( command == TIMER_STOP )
0423: {
0424: m_fStopTime = fTime;
0425: m_fLastElapsedTime = fTime;
0426: m_bTimerStopped = TRUE;
0427: return 0.0f;
0428: }
0429:
0430:
0431: if( command == TIMER_ADVANCE )
0432: {
0433: m_fStopTime += 0.1f;
0434: return 0.0f;
0435: }
0436:
0437: if( command == TIMER_GETABSOLUTETIME )
0438: {
0439: return (FLOAT) fTime;
0440: }
0441:
0442: return -1.0f;
0443: }
0444: }
0445:
0446:
0447:
0448:
0449:
0450:
0451:
0452:
0453:
0454:
0455: VOID DXUtil_ConvertAnsiStringToWide( WCHAR* wstrDestination, const CHAR* strSource,
0456: int cchDestChar )
0457: {
0458: if( wstrDestination==NULL || strSource==NULL )
0459: return;
0460:
0461: if( cchDestChar == -1 )
0462: cchDestChar = strlen(strSource)+1;
0463:
0464: MultiByteToWideChar( CP_ACP, 0, strSource, -1,
0465: wstrDestination, cchDestChar-1 );
0466:
0467: wstrDestination[cchDestChar-1] = 0;
0468: }
0469:
0470:
0471:
0472:
0473:
0474:
0475:
0476:
0477:
0478:
0479: VOID DXUtil_ConvertWideStringToAnsi( CHAR* strDestination, const WCHAR* wstrSource,
0480: int cchDestChar )
0481: {
0482: if( strDestination==NULL || wstrSource==NULL )
0483: return;
0484:
0485: if( cchDestChar == -1 )
0486: cchDestChar = wcslen(wstrSource)+1;
0487:
0488: WideCharToMultiByte( CP_ACP, 0, wstrSource, -1, strDestination,
0489: cchDestChar-1, NULL, NULL );
0490:
0491: strDestination[cchDestChar-1] = 0;
0492: }
0493:
0494:
0495:
0496:
0497:
0498:
0499:
0500:
0501:
0502:
0503: VOID DXUtil_ConvertGenericStringToAnsi( CHAR* strDestination, const TCHAR* tstrSource,
0504: int cchDestChar )
0505: {
0506: if( strDestination==NULL || tstrSource==NULL || cchDestChar == 0 )
0507: return;
0508:
0509: #ifdef _UNICODE
0510: DXUtil_ConvertWideStringToAnsi( strDestination, tstrSource, cchDestChar );
0511: #else
0512: if( cchDestChar == -1 )
0513: {
0514: strcpy( strDestination, tstrSource );
0515: }
0516: else
0517: {
0518: strncpy( strDestination, tstrSource, cchDestChar );
0519: strDestination[cchDestChar-1] = '\0';
0520: }
0521: #endif
0522: }
0523:
0524:
0525:
0526:
0527:
0528:
0529:
0530:
0531:
0532:
0533: VOID DXUtil_ConvertGenericStringToWide( WCHAR* wstrDestination, const TCHAR* tstrSource,
0534: int cchDestChar )
0535: {
0536: if( wstrDestination==NULL || tstrSource==NULL || cchDestChar == 0 )
0537: return;
0538:
0539: #ifdef _UNICODE
0540: if( cchDestChar == -1 )
0541: {
0542: wcscpy( wstrDestination, tstrSource );
0543: }
0544: else
0545: {
0546: wcsncpy( wstrDestination, tstrSource, cchDestChar );
0547: wstrDestination[cchDestChar-1] = L'\0';
0548: }
0549: #else
0550: DXUtil_ConvertAnsiStringToWide( wstrDestination, tstrSource, cchDestChar );
0551: #endif
0552: }
0553:
0554:
0555:
0556:
0557:
0558:
0559:
0560:
0561:
0562:
0563: VOID DXUtil_ConvertAnsiStringToGeneric( TCHAR* tstrDestination, const CHAR* strSource,
0564: int cchDestChar )
0565: {
0566: if( tstrDestination==NULL || strSource==NULL || cchDestChar == 0 )
0567: return;
0568:
0569: #ifdef _UNICODE
0570: DXUtil_ConvertAnsiStringToWide( tstrDestination, strSource, cchDestChar );
0571: #else
0572: if( cchDestChar == -1 )
0573: {
0574: strcpy( tstrDestination, strSource );
0575: }
0576: else
0577: {
0578: strncpy( tstrDestination, strSource, cchDestChar );
0579: tstrDestination[cchDestChar-1] = '\0';
0580: }
0581: #endif
0582: }
0583:
0584:
0585:
0586:
0587:
0588:
0589:
0590:
0591:
0592:
0593: VOID DXUtil_ConvertWideStringToGeneric( TCHAR* tstrDestination, const WCHAR* wstrSource,
0594: int cchDestChar )
0595: {
0596: if( tstrDestination==NULL || wstrSource==NULL || cchDestChar == 0 )
0597: return;
0598:
0599: #ifdef _UNICODE
0600: if( cchDestChar == -1 )
0601: {
0602: wcscpy( tstrDestination, wstrSource );
0603: }
0604: else
0605: {
0606: wcsncpy( tstrDestination, wstrSource, cchDestChar );
0607: tstrDestination[cchDestChar-1] = L'\0';
0608: }
0609: #else
0610: DXUtil_ConvertWideStringToAnsi( tstrDestination, wstrSource, cchDestChar );
0611: #endif
0612: }
0613:
0614:
0615:
0616:
0617:
0618:
0619:
0620:
0621: HRESULT _DbgOut( TCHAR* strFile, DWORD dwLine, HRESULT hr, TCHAR* strMsg )
0622: {
0623: TCHAR buffer[256];
0624: wsprintf( buffer, _T("%s(%ld): "), strFile, dwLine );
0625: OutputDebugString( buffer );
0626: OutputDebugString( strMsg );
0627:
0628: if( hr )
0629: {
0630: wsprintf( buffer, _T("(hr=%08lx)\n"), hr );
0631: OutputDebugString( buffer );
0632: }
0633:
0634: OutputDebugString( _T("\n") );
0635:
0636: return hr;
0637: }
0638:
0639:
0640:
0641:
0642:
0643:
0644:
0645:
0646:
0647: VOID DXUtil_Trace( TCHAR* strMsg, ... )
0648: {
0649: #if defined(DEBUG) | defined(_DEBUG)
0650: TCHAR strBuffer[512];
0651:
0652: va_list args;
0653: va_start(args, strMsg);
0654: _vsntprintf( strBuffer, 512, strMsg, args );
0655: va_end(args);
0656:
0657: OutputDebugString( strBuffer );
0658: #else
0659: UNREFERENCED_PARAMETER(strMsg);
0660: #endif
0661: }
0662:
0663:
0664:
0665:
0666:
0667:
0668:
0669:
0670: BOOL DXUtil_ConvertStringToGUID( const TCHAR* strIn, GUID* pGuidOut )
0671: {
0672: UINT aiTmp[10];
0673:
0674: if( _stscanf( strIn, TEXT("{%8X-%4X-%4X-%2X%2X-%2X%2X%2X%2X%2X%2X}"),
0675: &pGuidOut->Data1,
0676: &aiTmp[0], &aiTmp[1],
0677: &aiTmp[2], &aiTmp[3],
0678: &aiTmp[4], &aiTmp[5],
0679: &aiTmp[6], &aiTmp[7],
0680: &aiTmp[8], &aiTmp[9] ) != 11 )
0681: {
0682: ZeroMemory( pGuidOut, sizeof(GUID) );
0683: return FALSE;
0684: }
0685: else
0686: {
0687: pGuidOut->Data2 = (USHORT) aiTmp[0];
0688: pGuidOut->Data3 = (USHORT) aiTmp[1];
0689: pGuidOut->Data4[0] = (BYTE) aiTmp[2];
0690: pGuidOut->Data4[1] = (BYTE) aiTmp[3];
0691: pGuidOut->Data4[2] = (BYTE) aiTmp[4];
0692: pGuidOut->Data4[3] = (BYTE) aiTmp[5];
0693: pGuidOut->Data4[4] = (BYTE) aiTmp[6];
0694: pGuidOut->Data4[5] = (BYTE) aiTmp[7];
0695: pGuidOut->Data4[6] = (BYTE) aiTmp[8];
0696: pGuidOut->Data4[7] = (BYTE) aiTmp[9];
0697: return TRUE;
0698: }
0699: }
0700:
0701:
0702:
0703:
0704:
0705:
0706:
0707:
0708: VOID DXUtil_ConvertGUIDToString( const GUID* pGuidIn, TCHAR* strOut )
0709: {
0710: _stprintf( strOut, TEXT("{%0.8X-%0.4X-%0.4X-%0.2X%0.2X-%0.2X%0.2X%0.2X%0.2X%0.2X%0.2X}"),
0711: pGuidIn->Data1, pGuidIn->Data2, pGuidIn->Data3,
0712: pGuidIn->Data4[0], pGuidIn->Data4[1],
0713: pGuidIn->Data4[2], pGuidIn->Data4[3],
0714: pGuidIn->Data4[4], pGuidIn->Data4[5],
0715: pGuidIn->Data4[6], pGuidIn->Data4[7] );
0716: }
0717: