From 0d082ba3f2611eec43404e5b612472a12c692006 Mon Sep 17 00:00:00 2001 From: spacy51 Date: Sun, 9 Mar 2008 19:11:28 +0000 Subject: [PATCH] Change initialization of DirectSound and XAudio2 to load DLLs at runtime instead of linking statically to them. Replace timeGetTime() with GetTickCount() in order to eliminate the need of additionally linking against winmm.lib. git-svn-id: https://svn.code.sf.net/p/vbam/code/trunk@458 a31d4220-a93d-0410-bf67-fe4944624d44 --- VBA2008.vcproj | 4 ++-- src/dmg/GB.cpp | 6 +++--- src/win32/AVIWrite.cpp | 2 +- src/win32/Direct3D.cpp | 6 +++--- src/win32/DirectInput.cpp | 36 +++++++++++++++------------------- src/win32/DirectSound.cpp | 22 ++++++++++++--------- src/win32/FullscreenSettings.h | 2 +- src/win32/OpenGL.cpp | 2 +- src/win32/VBA.cpp | 6 +++--- src/win32/XAudio2.cpp | 29 ++++++++++++++++++++++----- src/win32/stdafx.h | 1 - 11 files changed, 67 insertions(+), 49 deletions(-) diff --git a/VBA2008.vcproj b/VBA2008.vcproj index b82cdded..a03d2cca 100644 --- a/VBA2008.vcproj +++ b/VBA2008.vcproj @@ -85,7 +85,7 @@ /> -#pragma comment(lib, "Dinput8") +#include +#pragma comment( lib, "dinput8" ) +#pragma comment( lib, "dxguid" ) #ifdef _DEBUG @@ -608,7 +609,8 @@ BOOL checkKey(KeyList &k) } DirectInput::DirectInput() -{} +{ +} DirectInput::~DirectInput() { @@ -631,27 +633,21 @@ DirectInput::~DirectInput() } } - bool DirectInput::initialize() { - - HRESULT hr; - - hr = DirectInput8Create( - AfxGetInstanceHandle(), - DIRECTINPUT_VERSION, - IID_IDirectInput8, - (LPVOID*)&pDirectInput, - NULL ); - - if ( hr != DI_OK ) { - return false; - } + HRESULT hr; + + hr = DirectInput8Create( + GetModuleHandle( NULL ), + DIRECTINPUT_VERSION, + IID_IDirectInput8, + (LPVOID *)&pDirectInput, + NULL ); + ASSERT( hr == DI_OK ); + if( hr != DI_OK ) return false; - - - hr = pDirectInput->EnumDevices(DI8DEVCLASS_GAMECTRL, + hr = pDirectInput->EnumDevices(DI8DEVCLASS_GAMECTRL, DIEnumDevicesCallback2, NULL, DIEDFL_ATTACHEDONLY); diff --git a/src/win32/DirectSound.cpp b/src/win32/DirectSound.cpp index 2b8b333f..5ad5067b 100644 --- a/src/win32/DirectSound.cpp +++ b/src/win32/DirectSound.cpp @@ -32,9 +32,7 @@ #include "../Sound.h" // DirectSound8 -#include -#pragma comment( lib, "Dsound" ) -#pragma comment( lib, "Dxguid" ) +#include extern bool soundBufferLow; extern void setsystemSoundOn(bool value); @@ -64,7 +62,10 @@ public: DirectSound::DirectSound() { - CoInitialize( NULL ); + if( S_OK != CoInitializeEx( NULL, COINIT_MULTITHREADED ) ) { + systemMessage( IDS_COM_FAILURE, NULL ); + return; + } pDirectSound = NULL; dsbPrimary = NULL; @@ -114,14 +115,17 @@ bool DirectSound::init() DSBUFFERDESC dsbdesc; int i; - - // Initialize DirectSound - if( FAILED( hr = DirectSoundCreate8( &DSDEVID_DefaultPlayback, &pDirectSound, NULL ) ) ) { - systemMessage( IDS_CANNOT_CREATE_DIRECTSOUND, _T("Cannot create DirectSound %08x"), hr ); - pDirectSound = NULL; + hr = CoCreateInstance( CLSID_DirectSound8, NULL, CLSCTX_INPROC_SERVER, IID_IDirectSound8, (LPVOID *)&pDirectSound ); + if( hr != S_OK ) { + systemMessage( IDS_CANNOT_CREATE_DIRECTSOUND, NULL, hr ); return false; } + pDirectSound->Initialize( &DSDEVID_DefaultPlayback ); + if( hr != DS_OK ) { + systemMessage( IDS_CANNOT_CREATE_DIRECTSOUND, NULL, hr ); + return false; + } if( FAILED( hr = pDirectSound->SetCooperativeLevel( theApp.m_pMainWnd->GetSafeHwnd(), DSSCL_EXCLUSIVE ) ) ) { systemMessage( IDS_CANNOT_SETCOOPERATIVELEVEL, _T("Cannot SetCooperativeLevel %08x"), hr ); diff --git a/src/win32/FullscreenSettings.h b/src/win32/FullscreenSettings.h index 9edc8714..e297bb13 100644 --- a/src/win32/FullscreenSettings.h +++ b/src/win32/FullscreenSettings.h @@ -23,7 +23,7 @@ #ifndef NO_D3D -#pragma comment( lib, "d3d9.lib" ) +#pragma comment( lib, "d3d9" ) #ifdef _DEBUG #define D3D_DEBUG_INFO #endif diff --git a/src/win32/OpenGL.cpp b/src/win32/OpenGL.cpp index 3d9c6e61..0bc61b16 100644 --- a/src/win32/OpenGL.cpp +++ b/src/win32/OpenGL.cpp @@ -20,7 +20,7 @@ #ifndef NO_OGL //OpenGL library -#pragma comment( lib, "opengl32.lib" ) +#pragma comment( lib, "OpenGL32" ) // MFC #include "stdafx.h" diff --git a/src/win32/VBA.cpp b/src/win32/VBA.cpp index 11ce90cb..7b1e3a7c 100644 --- a/src/win32/VBA.cpp +++ b/src/win32/VBA.cpp @@ -2624,7 +2624,7 @@ bool Sm60FPS_CanSkipFrame() if( Sm60FPS::nFrameCnt == 0 ) { Sm60FPS::nFrameCnt = 0; Sm60FPS::dwTimeElapse = 0; - Sm60FPS::dwTime0 = timeGetTime(); + Sm60FPS::dwTime0 = GetTickCount(); } else { if( Sm60FPS::nFrameCnt >= 10 ) { Sm60FPS::nFrameCnt = 0; @@ -2644,7 +2644,7 @@ bool Sm60FPS_CanSkipFrame() } } } else { // between frame 1-10 - Sm60FPS::dwTime1 = timeGetTime(); + Sm60FPS::dwTime1 = GetTickCount(); Sm60FPS::dwTimeElapse += (Sm60FPS::dwTime1 - Sm60FPS::dwTime0); Sm60FPS::dwTime0 = Sm60FPS::dwTime1; if( !Sm60FPS::bLastSkip && @@ -2668,7 +2668,7 @@ bool Sm60FPS_CanSkipFrame() void Sm60FPS_Sleep() { if( theApp.autoFrameSkip ) { - u32 dwTimePass = Sm60FPS::dwTimeElapse + (timeGetTime() - Sm60FPS::dwTime0); + u32 dwTimePass = Sm60FPS::dwTimeElapse + (GetTickCount() - Sm60FPS::dwTime0); u32 dwTimeShould = (u32)(Sm60FPS::nFrameCnt * Sm60FPS::K_fDT); if( dwTimeShould > dwTimePass ) { Sleep(dwTimeShould - dwTimePass); diff --git a/src/win32/XAudio2.cpp b/src/win32/XAudio2.cpp index 3a7d2987..a95df28f 100644 --- a/src/win32/XAudio2.cpp +++ b/src/win32/XAudio2.cpp @@ -155,16 +155,35 @@ bool XAudio2_Output::init() { if( failed || initialized ) return false; + // Initialize XAudio2 using COM HRESULT hr; - UINT32 flags = 0; + hr = CoCreateInstance( #ifdef _DEBUG - flags |= XAUDIO2_DEBUG_ENGINE; + __uuidof( XAudio2_Debug ), +#else + __uuidof( XAudio2 ), #endif + NULL, + CLSCTX_INPROC_SERVER, + __uuidof( IXAudio2 ), + (LPVOID *)&xaud + ); + if( hr != S_OK ) { + systemMessage( IDS_XAUDIO2_FAILURE, NULL ); + failed = true; + return false; + } - hr = XAudio2Create( &xaud, flags ); - - if( FAILED( hr ) ) { + hr = xaud->Initialize( +#ifdef _DEBUG + XAUDIO2_DEBUG_ENGINE, +#else + 0, +#endif + XAUDIO2_DEFAULT_PROCESSOR + ); + if( hr != S_OK ) { systemMessage( IDS_XAUDIO2_FAILURE, NULL ); failed = true; return false; diff --git a/src/win32/stdafx.h b/src/win32/stdafx.h index 77c15e38..fcadc242 100644 --- a/src/win32/stdafx.h +++ b/src/win32/stdafx.h @@ -30,7 +30,6 @@ // Enable STRICT type checking #define STRICT - #include #include #include