3 changed files with 0 additions and 879 deletions
@ -1,365 +0,0 @@
|
||||
#include "diablo.h" |
||||
#include "../3rdParty/Storm/Source/storm.h" |
||||
|
||||
DEVILUTION_BEGIN_NAMESPACE |
||||
|
||||
BYTE *sgpBackBuf; |
||||
LPDIRECTDRAW lpDDInterface; |
||||
IDirectDrawPalette *lpDDPalette; |
||||
int sgdwLockCount; |
||||
BYTE *gpBuffer; |
||||
IDirectDrawSurface *lpDDSBackBuf; |
||||
IDirectDrawSurface *lpDDSPrimary; |
||||
#ifdef _DEBUG |
||||
int locktbl[256]; |
||||
#endif |
||||
#ifdef __cplusplus |
||||
static CCritSect sgMemCrit; |
||||
#endif |
||||
char gbBackBuf; |
||||
char gbEmulate; |
||||
HMODULE ghDiabMod; |
||||
|
||||
void dx_init(HWND hWnd) |
||||
{ |
||||
HRESULT hDDVal; |
||||
int winw, winh; |
||||
BOOL bSuccess; |
||||
GUID *lpGUID; |
||||
|
||||
/// ASSERT: assert(! gpBuffer);
|
||||
/// ASSERT: assert(! sgdwLockCount);
|
||||
/// ASSERT: assert(! sgpBackBuf);
|
||||
|
||||
SetFocus(hWnd); |
||||
ShowWindow(hWnd, SW_SHOWNORMAL); |
||||
|
||||
lpGUID = NULL; |
||||
if (gbEmulate) { |
||||
lpGUID = (GUID *)DDCREATE_EMULATIONONLY; |
||||
} |
||||
hDDVal = dx_DirectDrawCreate(lpGUID, &lpDDInterface, NULL); |
||||
if (hDDVal != DD_OK) { |
||||
ErrDlg(IDD_DIALOG1, hDDVal, __FILE__, __LINE__); |
||||
} |
||||
|
||||
#ifdef COLORFIX |
||||
#ifdef __DDRAWI_INCLUDED__ |
||||
((LPDDRAWI_DIRECTDRAW_INT)lpDDInterface)->lpLcl->dwAppHackFlags |= 0x800; |
||||
#else |
||||
((DWORD **)lpDDInterface)[1][18] |= 0x800; |
||||
#endif |
||||
#endif |
||||
|
||||
#ifndef _DEBUG |
||||
fullscreen = TRUE; |
||||
#endif |
||||
if (!fullscreen) { |
||||
#ifdef __cplusplus |
||||
hDDVal = lpDDInterface->SetCooperativeLevel(hWnd, DDSCL_NORMAL | DDSCL_ALLOWREBOOT); |
||||
#else |
||||
hDDVal = lpDDInterface->lpVtbl->SetCooperativeLevel(lpDDInterface, hWnd, DDSCL_NORMAL | DDSCL_ALLOWREBOOT); |
||||
#endif |
||||
if (hDDVal == DDERR_EXCLUSIVEMODEALREADYSET) { |
||||
TriggerBreak(); |
||||
} else if (hDDVal != DD_OK) { |
||||
ErrDlg(IDD_DIALOG1, hDDVal, __FILE__, __LINE__); |
||||
} |
||||
SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE); |
||||
} else { |
||||
#ifdef __cplusplus |
||||
hDDVal = lpDDInterface->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT | DDSCL_FULLSCREEN); |
||||
#else |
||||
hDDVal = lpDDInterface->lpVtbl->SetCooperativeLevel(lpDDInterface, hWnd, DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT | DDSCL_FULLSCREEN); |
||||
#endif |
||||
if (hDDVal == DDERR_EXCLUSIVEMODEALREADYSET) { |
||||
TriggerBreak(); |
||||
} else if (hDDVal != DD_OK) { |
||||
ErrDlg(IDD_DIALOG1, hDDVal, __FILE__, __LINE__); |
||||
} |
||||
#ifdef __cplusplus |
||||
hDDVal = lpDDInterface->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP); |
||||
#else |
||||
hDDVal = lpDDInterface->lpVtbl->SetDisplayMode(lpDDInterface, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP); |
||||
#endif |
||||
if (hDDVal != DD_OK) { |
||||
winw = GetSystemMetrics(SM_CXSCREEN); |
||||
winh = GetSystemMetrics(SM_CYSCREEN); |
||||
#ifdef __cplusplus |
||||
hDDVal = lpDDInterface->SetDisplayMode(winw, winh, SCREEN_BPP); |
||||
#else |
||||
hDDVal = lpDDInterface->lpVtbl->SetDisplayMode(lpDDInterface, winw, winh, SCREEN_BPP); |
||||
#endif |
||||
} |
||||
if (hDDVal != DD_OK) { |
||||
ErrDlg(IDD_DIALOG1, hDDVal, __FILE__, __LINE__); |
||||
} |
||||
} |
||||
|
||||
dx_create_primary_surface(); |
||||
palette_init(); |
||||
GdiSetBatchLimit(1); |
||||
dx_create_back_buffer(); |
||||
bSuccess = SDrawManualInitialize(hWnd, lpDDInterface, lpDDSPrimary, NULL, NULL, lpDDSBackBuf, lpDDPalette, NULL); |
||||
/// ASSERT: assert(bSuccess);
|
||||
} |
||||
|
||||
void dx_create_back_buffer() |
||||
{ |
||||
DDSCAPS caps; |
||||
HRESULT error_code; |
||||
DDSURFACEDESC ddsd; |
||||
|
||||
#ifdef __cplusplus |
||||
error_code = lpDDSPrimary->GetCaps(&caps); |
||||
#else |
||||
error_code = lpDDSPrimary->lpVtbl->GetCaps(lpDDSPrimary, &caps); |
||||
#endif |
||||
if (error_code != DD_OK) |
||||
DDErrMsg(error_code, 59, "C:\\Src\\Diablo\\Source\\dx.cpp"); |
||||
|
||||
if (!gbBackBuf) { |
||||
ddsd.dwSize = sizeof(ddsd); |
||||
#ifdef __cplusplus |
||||
error_code = lpDDSPrimary->Lock(NULL, &ddsd, DDLOCK_WRITEONLY | DDLOCK_WAIT, NULL); |
||||
#else |
||||
error_code = lpDDSPrimary->lpVtbl->Lock(lpDDSPrimary, NULL, &ddsd, DDLOCK_WRITEONLY | DDLOCK_WAIT, NULL); |
||||
#endif |
||||
if (error_code == DD_OK) { |
||||
#ifdef __cplusplus |
||||
lpDDSPrimary->Unlock(NULL); |
||||
#else |
||||
lpDDSPrimary->lpVtbl->Unlock(lpDDSPrimary, NULL); |
||||
#endif |
||||
sgpBackBuf = (BYTE *)DiabloAllocPtr(BUFFER_HEIGHT * BUFFER_WIDTH); |
||||
return; |
||||
} |
||||
if (error_code != DDERR_CANTLOCKSURFACE) |
||||
ErrDlg(IDD_DIALOG1, error_code, __FILE__, __LINE__); |
||||
} |
||||
|
||||
memset(&ddsd, 0, sizeof(ddsd)); |
||||
ddsd.dwWidth = BUFFER_WIDTH; |
||||
ddsd.lPitch = BUFFER_WIDTH; |
||||
ddsd.dwSize = sizeof(ddsd); |
||||
ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS; |
||||
ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN; |
||||
ddsd.dwHeight = BUFFER_HEIGHT; |
||||
ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat); |
||||
#ifdef __cplusplus |
||||
error_code = lpDDSPrimary->GetPixelFormat(&ddsd.ddpfPixelFormat); |
||||
#else |
||||
error_code = lpDDSPrimary->lpVtbl->GetPixelFormat(lpDDSPrimary, &ddsd.ddpfPixelFormat); |
||||
#endif |
||||
if (error_code != DD_OK) |
||||
ErrDlg(IDD_DIALOG1, error_code, __FILE__, __LINE__); |
||||
#ifdef __cplusplus |
||||
error_code = lpDDInterface->CreateSurface(&ddsd, &lpDDSBackBuf, NULL); |
||||
#else |
||||
error_code = lpDDInterface->lpVtbl->CreateSurface(lpDDInterface, &ddsd, &lpDDSBackBuf, NULL); |
||||
#endif |
||||
if (error_code != DD_OK) |
||||
ErrDlg(IDD_DIALOG1, error_code, __FILE__, __LINE__); |
||||
} |
||||
|
||||
void dx_create_primary_surface() |
||||
{ |
||||
DDSURFACEDESC ddsd; |
||||
HRESULT error_code; |
||||
|
||||
memset(&ddsd, 0, sizeof(ddsd)); |
||||
ddsd.dwSize = sizeof(ddsd); |
||||
ddsd.dwFlags = DDSD_CAPS; |
||||
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; |
||||
#ifdef __cplusplus |
||||
error_code = lpDDInterface->CreateSurface(&ddsd, &lpDDSPrimary, NULL); |
||||
#else |
||||
error_code = lpDDInterface->lpVtbl->CreateSurface(lpDDInterface, &ddsd, &lpDDSPrimary, NULL); |
||||
#endif |
||||
if (error_code != DD_OK) |
||||
ErrDlg(IDD_DIALOG1, error_code, __FILE__, __LINE__); |
||||
} |
||||
|
||||
HRESULT dx_DirectDrawCreate(LPGUID guid, LPDIRECTDRAW *lplpDD, LPUNKNOWN pUnkOuter) |
||||
{ |
||||
HRESULT(WINAPI * DirectDrawCreate) |
||||
(LPGUID lpGuid, LPDIRECTDRAW * lplpDD, LPUNKNOWN pUnkOuter); |
||||
|
||||
if (ghDiabMod == NULL) { |
||||
ghDiabMod = LoadLibrary("ddraw.dll"); |
||||
if (ghDiabMod == NULL) { |
||||
ErrDlg(IDD_DIALOG4, GetLastError(), __FILE__, __LINE__); |
||||
} |
||||
} |
||||
|
||||
DirectDrawCreate = (HRESULT(WINAPI *)(LPGUID, LPDIRECTDRAW *, LPUNKNOWN))GetProcAddress(ghDiabMod, "DirectDrawCreate"); |
||||
if (DirectDrawCreate == NULL) { |
||||
ErrDlg(IDD_DIALOG4, GetLastError(), __FILE__, __LINE__); |
||||
} |
||||
return DirectDrawCreate(guid, lplpDD, pUnkOuter); |
||||
} |
||||
|
||||
void lock_buf(BYTE idx) |
||||
{ |
||||
#ifdef _DEBUG |
||||
++locktbl[idx]; |
||||
#endif |
||||
lock_buf_priv(); |
||||
} |
||||
|
||||
void lock_buf_priv() |
||||
{ |
||||
DDSURFACEDESC ddsd; |
||||
HRESULT error_code; |
||||
|
||||
#ifdef __cplusplus |
||||
sgMemCrit.Enter(); |
||||
#endif |
||||
if (sgpBackBuf != NULL) { |
||||
gpBuffer = sgpBackBuf; |
||||
sgdwLockCount++; |
||||
return; |
||||
} |
||||
|
||||
if (lpDDSBackBuf == NULL) { |
||||
Sleep(20000); |
||||
app_fatal("lock_buf_priv"); |
||||
sgdwLockCount++; |
||||
return; |
||||
} |
||||
|
||||
if (sgdwLockCount != 0) { |
||||
sgdwLockCount++; |
||||
return; |
||||
} |
||||
ddsd.dwSize = sizeof(ddsd); |
||||
#ifdef __cplusplus |
||||
error_code = lpDDSBackBuf->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL); |
||||
#else |
||||
error_code = lpDDSBackBuf->lpVtbl->Lock(lpDDSBackBuf, NULL, &ddsd, DDLOCK_WAIT, NULL); |
||||
#endif |
||||
if (error_code != DD_OK) |
||||
DDErrMsg(error_code, 235, "C:\\Src\\Diablo\\Source\\dx.cpp"); |
||||
|
||||
gpBufEnd += (size_t)ddsd.lpSurface; |
||||
gpBuffer = (BYTE *)ddsd.lpSurface; |
||||
sgdwLockCount++; |
||||
} |
||||
|
||||
void unlock_buf(BYTE idx) |
||||
{ |
||||
#ifdef _DEBUG |
||||
if (!locktbl[idx]) |
||||
app_fatal("Draw lock underflow: 0x%x", idx); |
||||
--locktbl[idx]; |
||||
#endif |
||||
unlock_buf_priv(); |
||||
} |
||||
|
||||
void unlock_buf_priv() |
||||
{ |
||||
HRESULT error_code; |
||||
|
||||
if (sgdwLockCount == 0) |
||||
app_fatal("draw main unlock error"); |
||||
if (gpBuffer == NULL) |
||||
app_fatal("draw consistency error"); |
||||
|
||||
sgdwLockCount--; |
||||
if (sgdwLockCount == 0) { |
||||
gpBufEnd -= (size_t)gpBuffer; |
||||
gpBuffer = NULL; |
||||
if (sgpBackBuf == NULL) { |
||||
#ifdef __cplusplus |
||||
error_code = lpDDSBackBuf->Unlock(NULL); |
||||
#else |
||||
error_code = lpDDSBackBuf->lpVtbl->Unlock(lpDDSBackBuf, NULL); |
||||
#endif |
||||
if (error_code != DD_OK) |
||||
DDErrMsg(error_code, 273, "C:\\Src\\Diablo\\Source\\dx.cpp"); |
||||
} |
||||
} |
||||
#ifdef __cplusplus |
||||
sgMemCrit.Leave(); |
||||
#endif |
||||
} |
||||
|
||||
void dx_cleanup() |
||||
{ |
||||
if (ghMainWnd) |
||||
ShowWindow(ghMainWnd, SW_HIDE); |
||||
SDrawDestroy(); |
||||
#ifdef __cplusplus |
||||
sgMemCrit.Enter(); |
||||
#endif |
||||
if (sgpBackBuf != NULL) { |
||||
MemFreeDbg(sgpBackBuf); |
||||
} else if (lpDDSBackBuf != NULL) { |
||||
#ifdef __cplusplus |
||||
lpDDSBackBuf->Release(); |
||||
#else |
||||
lpDDSBackBuf->lpVtbl->Release(lpDDSBackBuf); |
||||
#endif |
||||
lpDDSBackBuf = NULL; |
||||
} |
||||
sgdwLockCount = 0; |
||||
gpBuffer = NULL; |
||||
#ifdef __cplusplus |
||||
sgMemCrit.Leave(); |
||||
#endif |
||||
if (lpDDSPrimary) { |
||||
#ifdef __cplusplus |
||||
lpDDSPrimary->Release(); |
||||
#else |
||||
lpDDSPrimary->lpVtbl->Release(lpDDSPrimary); |
||||
#endif |
||||
lpDDSPrimary = NULL; |
||||
} |
||||
if (lpDDPalette) { |
||||
#ifdef __cplusplus |
||||
lpDDPalette->Release(); |
||||
#else |
||||
lpDDPalette->lpVtbl->Release(lpDDPalette); |
||||
#endif |
||||
lpDDPalette = NULL; |
||||
} |
||||
if (lpDDInterface) { |
||||
#ifdef __cplusplus |
||||
lpDDInterface->Release(); |
||||
#else |
||||
lpDDInterface->lpVtbl->Release(lpDDInterface); |
||||
#endif |
||||
lpDDInterface = NULL; |
||||
} |
||||
} |
||||
|
||||
void dx_reinit() |
||||
{ |
||||
int lockCount; |
||||
|
||||
#ifdef __cplusplus |
||||
sgMemCrit.Enter(); |
||||
#endif |
||||
ClearCursor(); |
||||
lockCount = sgdwLockCount; |
||||
|
||||
while (sgdwLockCount != 0) |
||||
unlock_buf_priv(); |
||||
|
||||
dx_cleanup(); |
||||
|
||||
drawpanflag = 255; |
||||
|
||||
dx_init(ghMainWnd); |
||||
|
||||
while (lockCount != 0) { |
||||
lock_buf_priv(); |
||||
lockCount--; |
||||
} |
||||
|
||||
#ifdef __cplusplus |
||||
sgMemCrit.Leave(); |
||||
#endif |
||||
} |
||||
|
||||
DEVILUTION_END_NAMESPACE |
||||
@ -1,512 +0,0 @@
|
||||
#include "diablo.h" |
||||
#include "../3rdParty/Storm/Source/storm.h" |
||||
|
||||
DEVILUTION_BEGIN_NAMESPACE |
||||
|
||||
LPDIRECTSOUNDBUFFER DSBs[8]; |
||||
LPDIRECTSOUND sglpDS; |
||||
BOOLEAN gbSndInited; |
||||
int sglMusicVolume; |
||||
int sglSoundVolume; |
||||
HMODULE hDsound_dll; |
||||
HANDLE sgpMusicTrack; |
||||
LPDIRECTSOUNDBUFFER sglpDSB; |
||||
|
||||
/* data */ |
||||
|
||||
BOOLEAN gbMusicOn = TRUE; |
||||
BOOLEAN gbSoundOn = TRUE; |
||||
BOOLEAN gbDupSounds = TRUE; |
||||
int sgnMusicTrack = 6; |
||||
char *sgszMusicTracks[NUM_MUSIC] = { |
||||
#ifdef SPAWN |
||||
"Music\\sTowne.wav", |
||||
"Music\\sLvlA.wav", |
||||
"Music\\sintro.wav" |
||||
#else |
||||
"Music\\DTowne.wav", |
||||
"Music\\DLvlA.wav", |
||||
"Music\\DLvlB.wav", |
||||
"Music\\DLvlC.wav", |
||||
"Music\\DLvlD.wav", |
||||
"Music\\Dintro.wav" |
||||
#endif |
||||
}; |
||||
char unk_volume[4][2] = { |
||||
{ 15, -16 }, |
||||
{ 15, -16 }, |
||||
{ 30, -31 }, |
||||
{ 30, -31 } |
||||
}; |
||||
|
||||
void snd_update(BOOL bStopAll) |
||||
{ |
||||
DWORD dwStatus, i; |
||||
|
||||
for (i = 0; i < 8; i++) { |
||||
if (!DSBs[i]) |
||||
continue; |
||||
|
||||
#ifdef __cplusplus |
||||
if (!bStopAll && DSBs[i]->GetStatus(&dwStatus) == DS_OK && dwStatus == DSBSTATUS_PLAYING) |
||||
continue; |
||||
|
||||
DSBs[i]->Stop(); |
||||
DSBs[i]->Release(); |
||||
#else |
||||
if (!bStopAll && DSBs[i]->lpVtbl->GetStatus(DSBs[i], &dwStatus) == DS_OK && dwStatus == DSBSTATUS_PLAYING) |
||||
continue; |
||||
|
||||
DSBs[i]->lpVtbl->Stop(DSBs[i]); |
||||
DSBs[i]->lpVtbl->Release(DSBs[i]); |
||||
#endif |
||||
|
||||
DSBs[i] = NULL; |
||||
} |
||||
} |
||||
|
||||
void snd_stop_snd(TSnd *pSnd) |
||||
{ |
||||
if (pSnd && pSnd->DSB) |
||||
#ifdef __cplusplus |
||||
pSnd->DSB->Stop(); |
||||
#else |
||||
pSnd->DSB->lpVtbl->Stop(pSnd->DSB); |
||||
#endif |
||||
} |
||||
|
||||
BOOL snd_playing(TSnd *pSnd) |
||||
{ |
||||
DWORD dwStatus; |
||||
|
||||
if (!pSnd) |
||||
return FALSE; |
||||
|
||||
if (pSnd->DSB == NULL) |
||||
return FALSE; |
||||
|
||||
#ifdef __cplusplus |
||||
if (pSnd->DSB->GetStatus(&dwStatus) != DS_OK) |
||||
#else |
||||
if (pSnd->DSB->lpVtbl->GetStatus(pSnd->DSB, &dwStatus) != DS_OK) |
||||
#endif |
||||
return FALSE; |
||||
|
||||
return dwStatus == DSBSTATUS_PLAYING; |
||||
} |
||||
|
||||
void snd_play_snd(TSnd *pSnd, int lVolume, int lPan) |
||||
{ |
||||
LPDIRECTSOUNDBUFFER DSB; |
||||
DWORD tc; |
||||
HRESULT error_code; |
||||
|
||||
if (!pSnd || !gbSoundOn) { |
||||
return; |
||||
} |
||||
|
||||
DSB = pSnd->DSB; |
||||
if (!DSB) { |
||||
return; |
||||
} |
||||
|
||||
tc = GetTickCount(); |
||||
if (tc - pSnd->start_tc < 80) { |
||||
GetTickCount(); // BUGFIX: unnecessary GetTickCount
|
||||
return; |
||||
} |
||||
|
||||
if (snd_playing(pSnd)) { |
||||
DSB = sound_dup_channel(pSnd->DSB); |
||||
if (DSB == NULL) { |
||||
return; |
||||
} |
||||
} |
||||
|
||||
lVolume += sglSoundVolume; |
||||
if (lVolume < VOLUME_MIN) { |
||||
lVolume = VOLUME_MIN; |
||||
} else if (lVolume > VOLUME_MAX) { |
||||
lVolume = VOLUME_MAX; |
||||
} |
||||
#ifdef __cplusplus |
||||
DSB->SetVolume(lVolume); |
||||
DSB->SetPan(lPan); |
||||
|
||||
error_code = DSB->Play(0, 0, 0); |
||||
#else |
||||
DSB->lpVtbl->SetVolume(DSB, lVolume); |
||||
DSB->lpVtbl->SetPan(DSB, lPan); |
||||
|
||||
error_code = DSB->lpVtbl->Play(DSB, 0, 0, 0); |
||||
#endif |
||||
|
||||
if (error_code != DSERR_BUFFERLOST) { |
||||
if (error_code != DS_OK) { |
||||
DSErrMsg(error_code, 261, "C:\\Src\\Diablo\\Source\\SOUND.CPP"); |
||||
} |
||||
} else if (sound_file_reload(pSnd, DSB)) { |
||||
#ifdef __cplusplus |
||||
DSB->Play(0, 0, 0); |
||||
#else |
||||
DSB->lpVtbl->Play(DSB, 0, 0, 0); |
||||
#endif |
||||
} |
||||
|
||||
pSnd->start_tc = tc; |
||||
} |
||||
|
||||
LPDIRECTSOUNDBUFFER sound_dup_channel(LPDIRECTSOUNDBUFFER DSB) |
||||
{ |
||||
DWORD i; |
||||
|
||||
if (!gbDupSounds) { |
||||
return NULL; |
||||
} |
||||
|
||||
for (i = 0; i < 8; i++) { |
||||
if (!DSBs[i]) { |
||||
#ifdef __cplusplus |
||||
if (sglpDS->DuplicateSoundBuffer(DSB, &DSBs[i]) != DS_OK) { |
||||
#else |
||||
if (sglpDS->lpVtbl->DuplicateSoundBuffer(sglpDS, DSB, &DSBs[i]) != DS_OK) { |
||||
#endif |
||||
return NULL; |
||||
} |
||||
|
||||
return DSBs[i]; |
||||
} |
||||
} |
||||
|
||||
return NULL; |
||||
} |
||||
|
||||
BOOL sound_file_reload(TSnd *sound_file, LPDIRECTSOUNDBUFFER DSB) |
||||
{ |
||||
HANDLE file; |
||||
LPVOID buf1, buf2; |
||||
DWORD size1, size2; |
||||
BOOL rv; |
||||
|
||||
#ifdef __cplusplus |
||||
if (DSB->Restore() != DS_OK) |
||||
#else |
||||
if (DSB->lpVtbl->Restore(DSB) != DS_OK) |
||||
#endif |
||||
return FALSE; |
||||
|
||||
rv = FALSE; |
||||
|
||||
WOpenFile(sound_file->sound_path, &file, FALSE); |
||||
WSetFilePointer(file, sound_file->chunk.dwOffset, NULL, 0); |
||||
|
||||
#ifdef __cplusplus |
||||
if (DSB->Lock(0, sound_file->chunk.dwSize, &buf1, &size1, &buf2, &size2, 0) == DS_OK) { |
||||
WReadFile(file, buf1, size1); |
||||
if (DSB->Unlock(buf1, size1, buf2, size2) == DS_OK) |
||||
rv = TRUE; |
||||
} |
||||
#else |
||||
if (DSB->lpVtbl->Lock(DSB, 0, sound_file->chunk.dwSize, &buf1, &size1, &buf2, &size2, 0) == DS_OK) { |
||||
WReadFile(file, buf1, size1); |
||||
if (DSB->lpVtbl->Unlock(DSB, buf1, size1, buf2, size2) == DS_OK) |
||||
rv = TRUE; |
||||
} |
||||
#endif |
||||
|
||||
WCloseFile(file); |
||||
|
||||
return rv; |
||||
} |
||||
|
||||
TSnd *sound_file_load(char *path) |
||||
{ |
||||
HANDLE file; |
||||
BYTE *wave_file; |
||||
TSnd *pSnd; |
||||
LPVOID buf1, buf2; |
||||
DWORD size1, size2; |
||||
HRESULT error_code; |
||||
|
||||
if (!sglpDS) |
||||
return NULL; |
||||
|
||||
WOpenFile(path, &file, FALSE); |
||||
pSnd = (TSnd *)DiabloAllocPtr(sizeof(TSnd)); |
||||
memset(pSnd, 0, sizeof(TSnd)); |
||||
pSnd->sound_path = path; |
||||
pSnd->start_tc = GetTickCount() - 81; |
||||
|
||||
wave_file = LoadWaveFile(file, &pSnd->fmt, &pSnd->chunk); |
||||
if (!wave_file) |
||||
app_fatal("Invalid sound format on file %s", pSnd->sound_path); |
||||
|
||||
sound_CreateSoundBuffer(pSnd); |
||||
|
||||
#ifdef __cplusplus |
||||
error_code = pSnd->DSB->Lock(0, pSnd->chunk.dwSize, &buf1, &size1, &buf2, &size2, 0); |
||||
#else |
||||
error_code = pSnd->DSB->lpVtbl->Lock(pSnd->DSB, 0, pSnd->chunk.dwSize, &buf1, &size1, &buf2, &size2, 0); |
||||
#endif |
||||
if (error_code != DS_OK) |
||||
DSErrMsg(error_code, 318, "C:\\Src\\Diablo\\Source\\SOUND.CPP"); |
||||
|
||||
memcpy(buf1, wave_file + pSnd->chunk.dwOffset, size1); |
||||
|
||||
#ifdef __cplusplus |
||||
error_code = pSnd->DSB->Unlock(buf1, size1, buf2, size2); |
||||
#else |
||||
error_code = pSnd->DSB->lpVtbl->Unlock(pSnd->DSB, buf1, size1, buf2, size2); |
||||
#endif |
||||
if (error_code != DS_OK) |
||||
DSErrMsg(error_code, 325, "C:\\Src\\Diablo\\Source\\SOUND.CPP"); |
||||
|
||||
mem_free_dbg((void *)wave_file); |
||||
WCloseFile(file); |
||||
|
||||
return pSnd; |
||||
} |
||||
|
||||
void sound_CreateSoundBuffer(TSnd *sound_file) |
||||
{ |
||||
DSBUFFERDESC DSB; |
||||
HRESULT error_code; |
||||
memset(&DSB, 0, sizeof(DSBUFFERDESC)); |
||||
|
||||
DSB.dwBufferBytes = sound_file->chunk.dwSize; |
||||
DSB.lpwfxFormat = &sound_file->fmt; |
||||
DSB.dwSize = sizeof(DSBUFFERDESC); |
||||
DSB.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN | DSBCAPS_STATIC; |
||||
|
||||
#ifdef __cplusplus |
||||
error_code = sglpDS->CreateSoundBuffer(&DSB, &sound_file->DSB, NULL); |
||||
#else |
||||
error_code = sglpDS->lpVtbl->CreateSoundBuffer(sglpDS, &DSB, &sound_file->DSB, NULL); |
||||
#endif |
||||
if (error_code != ERROR_SUCCESS) |
||||
DSErrMsg(error_code, 282, "C:\\Src\\Diablo\\Source\\SOUND.CPP"); |
||||
} |
||||
|
||||
void sound_file_cleanup(TSnd *sound_file) |
||||
{ |
||||
if (sound_file) { |
||||
if (sound_file->DSB) { |
||||
#ifdef __cplusplus |
||||
sound_file->DSB->Stop(); |
||||
sound_file->DSB->Release(); |
||||
#else |
||||
sound_file->DSB->lpVtbl->Stop(sound_file->DSB); |
||||
sound_file->DSB->lpVtbl->Release(sound_file->DSB); |
||||
#endif |
||||
sound_file->DSB = NULL; |
||||
} |
||||
|
||||
mem_free_dbg(sound_file); |
||||
} |
||||
} |
||||
|
||||
void snd_init(HWND hWnd) |
||||
{ |
||||
sound_load_volume("Sound Volume", &sglSoundVolume); |
||||
gbSoundOn = sglSoundVolume > VOLUME_MIN; |
||||
|
||||
sound_load_volume("Music Volume", &sglMusicVolume); |
||||
gbMusicOn = sglMusicVolume > VOLUME_MIN; |
||||
|
||||
if (sound_DirectSoundCreate(NULL, &sglpDS, NULL) != DS_OK) |
||||
sglpDS = NULL; |
||||
|
||||
#ifdef __cplusplus |
||||
if (sglpDS && sglpDS->SetCooperativeLevel(hWnd, DSSCL_EXCLUSIVE) == DS_OK) |
||||
#else |
||||
if (sglpDS && sglpDS->lpVtbl->SetCooperativeLevel(sglpDS, hWnd, DSSCL_EXCLUSIVE) == DS_OK) |
||||
#endif |
||||
sound_create_primary_buffer(NULL); |
||||
|
||||
SVidInitialize(sglpDS); |
||||
SFileDdaInitialize(sglpDS); |
||||
|
||||
gbSndInited = sglpDS != NULL; |
||||
} |
||||
|
||||
void sound_load_volume(char *value_name, int *value) |
||||
{ |
||||
int v = *value; |
||||
if (!SRegLoadValue("Diablo", value_name, 0, &v)) { |
||||
v = VOLUME_MAX; |
||||
} |
||||
*value = v; |
||||
|
||||
if (*value < VOLUME_MIN) { |
||||
*value = VOLUME_MIN; |
||||
} else if (*value > VOLUME_MAX) { |
||||
*value = VOLUME_MAX; |
||||
} |
||||
*value -= *value % 100; |
||||
} |
||||
|
||||
void sound_create_primary_buffer(HANDLE music_track) |
||||
{ |
||||
HRESULT error_code; |
||||
DSBUFFERDESC dsbuf; |
||||
WAVEFORMATEX format; |
||||
|
||||
if (!music_track) { |
||||
memset(&dsbuf, 0, sizeof(DSBUFFERDESC)); |
||||
dsbuf.dwSize = sizeof(DSBUFFERDESC); |
||||
dsbuf.dwFlags = DSBCAPS_PRIMARYBUFFER; |
||||
|
||||
#ifdef __cplusplus |
||||
error_code = sglpDS->CreateSoundBuffer(&dsbuf, &sglpDSB, NULL); |
||||
#else |
||||
error_code = sglpDS->lpVtbl->CreateSoundBuffer(sglpDS, &dsbuf, &sglpDSB, NULL); |
||||
#endif |
||||
if (error_code != DS_OK) |
||||
DSErrMsg(error_code, 375, "C:\\Src\\Diablo\\Source\\SOUND.CPP"); |
||||
} |
||||
|
||||
if (sglpDSB) { |
||||
DSCAPS dsbcaps; |
||||
dsbcaps.dwSize = sizeof(DSCAPS); |
||||
|
||||
#ifdef __cplusplus |
||||
error_code = sglpDS->GetCaps(&dsbcaps); |
||||
#else |
||||
error_code = sglpDS->lpVtbl->GetCaps(sglpDS, &dsbcaps); |
||||
#endif |
||||
if (error_code != DS_OK) |
||||
DSErrMsg(error_code, 383, "C:\\Src\\Diablo\\Source\\SOUND.CPP"); |
||||
|
||||
if (!music_track || !LoadWaveFormat(music_track, &format)) { |
||||
memset(&format, 0, sizeof(WAVEFORMATEX)); |
||||
format.wFormatTag = WAVE_FORMAT_PCM; |
||||
format.nSamplesPerSec = 22050; |
||||
format.wBitsPerSample = 16; |
||||
format.cbSize = 0; |
||||
} |
||||
|
||||
format.nChannels = 2; |
||||
format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8; |
||||
format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign; |
||||
|
||||
#ifdef __cplusplus |
||||
sglpDSB->SetFormat(&format); |
||||
#else |
||||
sglpDSB->lpVtbl->SetFormat(sglpDSB, &format); |
||||
#endif |
||||
} |
||||
} |
||||
|
||||
HRESULT sound_DirectSoundCreate(LPGUID lpGuid, LPDIRECTSOUND *ppDS, LPUNKNOWN pUnkOuter) |
||||
{ |
||||
HRESULT(WINAPI * DirectSoundCreate) |
||||
(LPGUID lpGuid, LPDIRECTSOUND * ppDS, LPUNKNOWN pUnkOuter); |
||||
|
||||
if (hDsound_dll == NULL) { |
||||
hDsound_dll = LoadLibrary("dsound.dll"); |
||||
if (hDsound_dll == NULL) { |
||||
ErrDlg(IDD_DIALOG5, GetLastError(), "C:\\Src\\Diablo\\Source\\SOUND.CPP", 422); |
||||
} |
||||
} |
||||
|
||||
DirectSoundCreate = (HRESULT(WINAPI *)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN))GetProcAddress(hDsound_dll, "DirectSoundCreate"); |
||||
if (DirectSoundCreate == NULL) { |
||||
ErrDlg(IDD_DIALOG5, GetLastError(), "C:\\Src\\Diablo\\Source\\SOUND.CPP", 427); |
||||
} |
||||
return DirectSoundCreate(lpGuid, ppDS, pUnkOuter); |
||||
} |
||||
|
||||
void sound_cleanup() |
||||
{ |
||||
snd_update(TRUE); |
||||
SVidDestroy(); |
||||
SFileDdaDestroy(); |
||||
|
||||
if (sglpDS) { |
||||
#ifdef __cplusplus |
||||
sglpDS->Release(); |
||||
#else |
||||
sglpDS->lpVtbl->Release(sglpDS); |
||||
#endif |
||||
sglpDS = NULL; |
||||
} |
||||
|
||||
if (gbSndInited) { |
||||
gbSndInited = FALSE; |
||||
sound_store_volume("Sound Volume", sglSoundVolume); |
||||
sound_store_volume("Music Volume", sglMusicVolume); |
||||
} |
||||
} |
||||
|
||||
void sound_store_volume(char *key, int value) |
||||
{ |
||||
SRegSaveValue("Diablo", key, 0, value); |
||||
} |
||||
|
||||
void music_stop() |
||||
{ |
||||
if (sgpMusicTrack) { |
||||
SFileDdaEnd(sgpMusicTrack); |
||||
SFileCloseFile(sgpMusicTrack); |
||||
sgpMusicTrack = NULL; |
||||
sgnMusicTrack = 6; |
||||
} |
||||
} |
||||
|
||||
void music_start(int nTrack) |
||||
{ |
||||
BOOL success; |
||||
|
||||
/// ASSERT: assert((DWORD) nTrack < NUM_MUSIC);
|
||||
music_stop(); |
||||
if (sglpDS && gbMusicOn) { |
||||
#ifdef _DEBUG |
||||
SFileEnableDirectAccess(FALSE); |
||||
#endif |
||||
success = SFileOpenFile(sgszMusicTracks[nTrack], &sgpMusicTrack); |
||||
#ifdef _DEBUG |
||||
SFileEnableDirectAccess(TRUE); |
||||
#endif |
||||
sound_create_primary_buffer(sgpMusicTrack); |
||||
if (!success) { |
||||
sgpMusicTrack = NULL; |
||||
} else { |
||||
SFileDdaBeginEx(sgpMusicTrack, 0x40000, 0x40000, 0, sglMusicVolume, 0, 0); |
||||
sgnMusicTrack = nTrack; |
||||
} |
||||
} |
||||
} |
||||
|
||||
void sound_disable_music(BOOL disable) |
||||
{ |
||||
if (disable) { |
||||
music_stop(); |
||||
} else if (sgnMusicTrack != 6) { |
||||
music_start(sgnMusicTrack); |
||||
} |
||||
} |
||||
|
||||
int sound_get_or_set_music_volume(int volume) |
||||
{ |
||||
if (volume == 1) |
||||
return sglMusicVolume; |
||||
|
||||
sglMusicVolume = volume; |
||||
|
||||
if (sgpMusicTrack) |
||||
SFileDdaSetVolume(sgpMusicTrack, volume, 0); |
||||
|
||||
return sglMusicVolume; |
||||
} |
||||
|
||||
int sound_get_or_set_sound_volume(int volume) |
||||
{ |
||||
if (volume == 1) |
||||
return sglSoundVolume; |
||||
|
||||
sglSoundVolume = volume; |
||||
|
||||
return sglSoundVolume; |
||||
} |
||||
|
||||
DEVILUTION_END_NAMESPACE |
||||
Loading…
Reference in new issue