00001
00002
00003
00004
00005
00006
00007
00008
00009 #define STRICT
00010
#include <windows.h>
00011
#include <mmsystem.h>
00012
#include <tchar.h>
00013
#include <stdio.h>
00014
#include <dxerr9.h>
00015
#include <stdarg.h>
00016
#include "DXUtil.h"
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 const TCHAR*
DXUtil_GetDXSDKMediaPath()
00027 {
00028
static TCHAR strNull[2] = _T(
"");
00029
static TCHAR strPath[MAX_PATH];
00030 DWORD dwType;
00031 DWORD dwSize = MAX_PATH;
00032 HKEY hKey;
00033
00034
00035 LONG lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
00036 _T(
"Software\\Microsoft\\DirectX"),
00037 0, KEY_READ, &hKey );
00038
if( ERROR_SUCCESS != lResult )
00039
return strNull;
00040
00041 lResult = RegQueryValueEx( hKey, _T(
"DX8SDK Samples Path"), NULL,
00042 &dwType, (BYTE*)strPath, &dwSize );
00043 RegCloseKey( hKey );
00044
00045
if( ERROR_SUCCESS != lResult )
00046
return strNull;
00047
00048 _tcscat( strPath, _T(
"\\Media\\") );
00049
00050
return strPath;
00051 }
00052
00053
00054
00055
00056
00057
00058
00059
00060 HRESULT
DXUtil_FindMediaFile( TCHAR* strPath, TCHAR* strFilename )
00061 {
00062 HANDLE file;
00063
00064
if( NULL==strFilename || NULL==strPath )
00065
return E_INVALIDARG;
00066
00067
00068 _tcscpy( strPath, strFilename );
00069
00070 file = CreateFile( strPath, GENERIC_READ, FILE_SHARE_READ, NULL,
00071 OPEN_EXISTING, 0, NULL );
00072
if( INVALID_HANDLE_VALUE != file )
00073 {
00074 CloseHandle( file );
00075
return S_OK;
00076 }
00077
00078
00079 _stprintf( strPath, _T(
"%s%s"),
DXUtil_GetDXSDKMediaPath(), strFilename );
00080
00081 file = CreateFile( strPath, GENERIC_READ, FILE_SHARE_READ, NULL,
00082 OPEN_EXISTING, 0, NULL );
00083
if( INVALID_HANDLE_VALUE != file )
00084 {
00085 CloseHandle( file );
00086
return S_OK;
00087 }
00088
00089
00090 _tcscpy( strPath, strFilename );
00091
return E_FAIL;
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101 HRESULT
DXUtil_ReadStringRegKey( HKEY hKey, TCHAR* strRegName, TCHAR* strValue,
00102 DWORD dwLength, TCHAR* strDefault )
00103 {
00104 DWORD dwType;
00105
00106
if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
00107 (BYTE*)strValue, &dwLength ) )
00108 {
00109 _tcscpy( strValue, strDefault );
00110 }
00111
00112
return S_OK;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122 HRESULT
DXUtil_WriteStringRegKey( HKEY hKey, TCHAR* strRegName,
00123 TCHAR* strValue )
00124 {
00125
if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_SZ,
00126 (BYTE*)strValue,
00127 (_tcslen(strValue)+1)*
sizeof(TCHAR) ) )
00128
return E_FAIL;
00129
00130
return S_OK;
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140 HRESULT
DXUtil_ReadIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD* pdwValue,
00141 DWORD dwDefault )
00142 {
00143 DWORD dwType;
00144 DWORD dwLength =
sizeof(DWORD);
00145
00146
if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
00147 (BYTE*)pdwValue, &dwLength ) )
00148 {
00149 *pdwValue = dwDefault;
00150 }
00151
00152
return S_OK;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162 HRESULT
DXUtil_WriteIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD dwValue )
00163 {
00164
if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD,
00165 (BYTE*)&dwValue,
sizeof(DWORD) ) )
00166
return E_FAIL;
00167
00168
return S_OK;
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178 HRESULT
DXUtil_ReadBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL* pbValue,
00179 BOOL bDefault )
00180 {
00181 DWORD dwType;
00182 DWORD dwLength =
sizeof(BOOL);
00183
00184
if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
00185 (BYTE*)pbValue, &dwLength ) )
00186 {
00187 *pbValue = bDefault;
00188 }
00189
00190
return S_OK;
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200 HRESULT
DXUtil_WriteBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL bValue )
00201 {
00202
if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD,
00203 (BYTE*)&bValue,
sizeof(BOOL) ) )
00204
return E_FAIL;
00205
00206
return S_OK;
00207 }
00208
00209
00210
00211
00212
00213
00214
00215
00216 HRESULT
DXUtil_ReadGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID* pGuidValue,
00217 GUID& guidDefault )
00218 {
00219 DWORD dwType;
00220 DWORD dwLength =
sizeof(GUID);
00221
00222
if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
00223 (LPBYTE) pGuidValue, &dwLength ) )
00224 {
00225 *pGuidValue = guidDefault;
00226 }
00227
00228
return S_OK;
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238 HRESULT
DXUtil_WriteGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID guidValue )
00239 {
00240
if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_BINARY,
00241 (BYTE*)&guidValue,
sizeof(GUID) ) )
00242
return E_FAIL;
00243
00244
return S_OK;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 FLOAT __stdcall
DXUtil_Timer( TIMER_COMMAND command )
00263 {
00264
static BOOL m_bTimerInitialized = FALSE;
00265
static BOOL m_bUsingQPF = FALSE;
00266
static LONGLONG m_llQPFTicksPerSec = 0;
00267
00268
00269
if( FALSE == m_bTimerInitialized )
00270 {
00271 m_bTimerInitialized = TRUE;
00272
00273
00274
00275 LARGE_INTEGER qwTicksPerSec;
00276 m_bUsingQPF = QueryPerformanceFrequency( &qwTicksPerSec );
00277
if( m_bUsingQPF )
00278 m_llQPFTicksPerSec = qwTicksPerSec.QuadPart;
00279 }
00280
00281
if( m_bUsingQPF )
00282 {
00283
static LONGLONG m_llStopTime = 0;
00284
static LONGLONG m_llLastElapsedTime = 0;
00285
static LONGLONG m_llBaseTime = 0;
00286
double fTime;
00287
double fElapsedTime;
00288 LARGE_INTEGER qwTime;
00289
00290
00291
00292
if( m_llStopTime != 0 && command !=
TIMER_START && command !=
TIMER_GETABSOLUTETIME)
00293 qwTime.QuadPart = m_llStopTime;
00294
else
00295 QueryPerformanceCounter( &qwTime );
00296
00297
00298
if( command ==
TIMER_GETELAPSEDTIME )
00299 {
00300 fElapsedTime = (
double) ( qwTime.QuadPart - m_llLastElapsedTime ) / (
double) m_llQPFTicksPerSec;
00301 m_llLastElapsedTime = qwTime.QuadPart;
00302
return (FLOAT) fElapsedTime;
00303 }
00304
00305
00306
if( command ==
TIMER_GETAPPTIME )
00307 {
00308
double fAppTime = (
double) ( qwTime.QuadPart - m_llBaseTime ) / (
double) m_llQPFTicksPerSec;
00309
return (FLOAT) fAppTime;
00310 }
00311
00312
00313
if( command ==
TIMER_RESET )
00314 {
00315 m_llBaseTime = qwTime.QuadPart;
00316 m_llLastElapsedTime = qwTime.QuadPart;
00317
return 0.0f;
00318 }
00319
00320
00321
if( command ==
TIMER_START )
00322 {
00323 m_llBaseTime += qwTime.QuadPart - m_llStopTime;
00324 m_llStopTime = 0;
00325 m_llLastElapsedTime = qwTime.QuadPart;
00326
return 0.0f;
00327 }
00328
00329
00330
if( command ==
TIMER_STOP )
00331 {
00332 m_llStopTime = qwTime.QuadPart;
00333 m_llLastElapsedTime = qwTime.QuadPart;
00334
return 0.0f;
00335 }
00336
00337
00338
if( command ==
TIMER_ADVANCE )
00339 {
00340 m_llStopTime += m_llQPFTicksPerSec/10;
00341
return 0.0f;
00342 }
00343
00344
if( command ==
TIMER_GETABSOLUTETIME )
00345 {
00346 fTime = qwTime.QuadPart / (
double) m_llQPFTicksPerSec;
00347
return (FLOAT) fTime;
00348 }
00349
00350
return -1.0f;
00351 }
00352
else
00353 {
00354
00355
static double m_fLastElapsedTime = 0.0;
00356
static double m_fBaseTime = 0.0;
00357
static double m_fStopTime = 0.0;
00358
double fTime;
00359
double fElapsedTime;
00360
00361
00362
00363
if( m_fStopTime != 0.0 && command !=
TIMER_START && command !=
TIMER_GETABSOLUTETIME)
00364 fTime = m_fStopTime;
00365
else
00366 fTime = timeGetTime() * 0.001;
00367
00368
00369
if( command ==
TIMER_GETELAPSEDTIME )
00370 {
00371 fElapsedTime = (
double) (fTime - m_fLastElapsedTime);
00372 m_fLastElapsedTime = fTime;
00373
return (FLOAT) fElapsedTime;
00374 }
00375
00376
00377
if( command ==
TIMER_GETAPPTIME )
00378 {
00379
return (FLOAT) (fTime - m_fBaseTime);
00380 }
00381
00382
00383
if( command ==
TIMER_RESET )
00384 {
00385 m_fBaseTime = fTime;
00386 m_fLastElapsedTime = fTime;
00387
return 0.0f;
00388 }
00389
00390
00391
if( command ==
TIMER_START )
00392 {
00393 m_fBaseTime += fTime - m_fStopTime;
00394 m_fStopTime = 0.0f;
00395 m_fLastElapsedTime = fTime;
00396
return 0.0f;
00397 }
00398
00399
00400
if( command ==
TIMER_STOP )
00401 {
00402 m_fStopTime = fTime;
00403
return 0.0f;
00404 }
00405
00406
00407
if( command ==
TIMER_ADVANCE )
00408 {
00409 m_fStopTime += 0.1f;
00410
return 0.0f;
00411 }
00412
00413
if( command ==
TIMER_GETABSOLUTETIME )
00414 {
00415
return (FLOAT) fTime;
00416 }
00417
00418
return -1.0f;
00419 }
00420 }
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431 VOID
DXUtil_ConvertAnsiStringToWide( WCHAR* wstrDestination,
const CHAR* strSource,
00432
int cchDestChar )
00433 {
00434
if( wstrDestination==NULL || strSource==NULL )
00435
return;
00436
00437
if( cchDestChar == -1 )
00438 cchDestChar = strlen(strSource)+1;
00439
00440 MultiByteToWideChar( CP_ACP, 0, strSource, -1,
00441 wstrDestination, cchDestChar-1 );
00442
00443 wstrDestination[cchDestChar-1] = 0;
00444 }
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455 VOID
DXUtil_ConvertWideStringToAnsi( CHAR* strDestination,
const WCHAR* wstrSource,
00456
int cchDestChar )
00457 {
00458
if( strDestination==NULL || wstrSource==NULL )
00459
return;
00460
00461
if( cchDestChar == -1 )
00462 cchDestChar = wcslen(wstrSource)+1;
00463
00464 WideCharToMultiByte( CP_ACP, 0, wstrSource, -1, strDestination,
00465 cchDestChar-1, NULL, NULL );
00466
00467 strDestination[cchDestChar-1] = 0;
00468 }
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479 VOID
DXUtil_ConvertGenericStringToAnsi( CHAR* strDestination,
const TCHAR* tstrSource,
00480
int cchDestChar )
00481 {
00482
if( strDestination==NULL || tstrSource==NULL )
00483
return;
00484
00485
#ifdef _UNICODE
00486
DXUtil_ConvertWideStringToAnsi( strDestination, tstrSource, cchDestChar );
00487
#else
00488
if( cchDestChar == -1 )
00489 strcpy( strDestination, tstrSource );
00490
else
00491 strncpy( strDestination, tstrSource, cchDestChar );
00492
#endif
00493
}
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504 VOID
DXUtil_ConvertGenericStringToWide( WCHAR* wstrDestination,
const TCHAR* tstrSource,
00505
int cchDestChar )
00506 {
00507
if( wstrDestination==NULL || tstrSource==NULL )
00508
return;
00509
00510
#ifdef _UNICODE
00511
if( cchDestChar == -1 )
00512 wcscpy( wstrDestination, tstrSource );
00513
else
00514 wcsncpy( wstrDestination, tstrSource, cchDestChar );
00515
#else
00516
DXUtil_ConvertAnsiStringToWide( wstrDestination, tstrSource, cchDestChar );
00517
#endif
00518
}
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529 VOID
DXUtil_ConvertAnsiStringToGeneric( TCHAR* tstrDestination,
const CHAR* strSource,
00530
int cchDestChar )
00531 {
00532
if( tstrDestination==NULL || strSource==NULL )
00533
return;
00534
00535
#ifdef _UNICODE
00536
DXUtil_ConvertAnsiStringToWide( tstrDestination, strSource, cchDestChar );
00537
#else
00538
if( cchDestChar == -1 )
00539 strcpy( tstrDestination, strSource );
00540
else
00541 strncpy( tstrDestination, strSource, cchDestChar );
00542
#endif
00543
}
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554 VOID
DXUtil_ConvertWideStringToGeneric( TCHAR* tstrDestination,
const WCHAR* wstrSource,
00555
int cchDestChar )
00556 {
00557
if( tstrDestination==NULL || wstrSource==NULL )
00558
return;
00559
00560
#ifdef _UNICODE
00561
if( cchDestChar == -1 )
00562 wcscpy( tstrDestination, wstrSource );
00563
else
00564 wcsncpy( tstrDestination, wstrSource, cchDestChar );
00565
#else
00566
DXUtil_ConvertWideStringToAnsi( tstrDestination, wstrSource, cchDestChar );
00567
#endif
00568
}
00569
00570
00571
00572
00573
00574
00575
00576
00577 HRESULT
_DbgOut( TCHAR* strFile, DWORD dwLine, HRESULT hr, TCHAR* strMsg )
00578 {
00579 TCHAR buffer[256];
00580 wsprintf( buffer, _T(
"%s(%ld): "), strFile, dwLine );
00581 OutputDebugString( buffer );
00582 OutputDebugString( strMsg );
00583
00584
if( hr )
00585 {
00586 wsprintf( buffer, _T(
"(hr=%08lx)\n"), hr );
00587 OutputDebugString( buffer );
00588 OutputDebugString( (
char*)DXGetErrorDescription9(hr) );
00589 }
00590
00591 OutputDebugString( _T(
"\n") );
00592
00593
return hr;
00594 }
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604 VOID
DXUtil_Trace( TCHAR* strMsg, ... )
00605 {
00606
#if defined(DEBUG) | defined(_DEBUG)
00607
TCHAR strBuffer[512];
00608
00609 va_list args;
00610 va_start(args, strMsg);
00611 _vsntprintf( strBuffer, 512, strMsg, args );
00612 va_end(args);
00613
00614 OutputDebugString( strBuffer );
00615
#endif
00616
}
00617
00618
00619