Browse Source

Reorder sound.cpp

pull/914/head
Anders Jenbo 5 years ago
parent
commit
2927a64d4d
  1. 206
      Source/sound.cpp
  2. 12
      Source/sound.h

206
Source/sound.cpp

@ -78,6 +78,74 @@ void snd_update(BOOL bStopAll)
}
}
static LPDIRECTSOUNDBUFFER sound_dup_channel(LPDIRECTSOUNDBUFFER DSB)
{
DWORD i;
if (!gbDupSounds) {
return NULL;
}
for (i = 0; i < 8; i++) {
if (!DSBs[i]) {
if (sglpDS->DuplicateSoundBuffer(DSB, &DSBs[i]) != DS_OK) {
return NULL;
}
return DSBs[i];
}
}
return NULL;
}
static void snd_get_volume(const char *value_name, int *value)
{
int v = *value;
if (!SRegLoadValue(APP_NAME, 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;
}
static void snd_set_volume(const char *key, int value)
{
SRegSaveValue(APP_NAME, key, 0, value);
}
static BOOL sound_file_reload(TSnd *sound_file, LPDIRECTSOUNDBUFFER DSB)
{
HANDLE file;
LPVOID buf1, buf2;
DWORD size1, size2;
BOOL rv;
if (DSB->Restore() != DS_OK)
return FALSE;
rv = FALSE;
WOpenFile(sound_file->sound_path, &file, FALSE);
WSetFilePointer(file, sound_file->chunk.dwOffset, NULL, FILE_BEGIN);
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;
}
WCloseFile(file);
return rv;
}
void snd_stop_snd(TSnd *pSnd)
{
if (pSnd && pSnd->DSB)
@ -150,51 +218,19 @@ void snd_play_snd(TSnd *pSnd, int lVolume, int lPan)
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]) {
if (sglpDS->DuplicateSoundBuffer(DSB, &DSBs[i]) != DS_OK) {
return NULL;
}
return DSBs[i];
}
}
return NULL;
}
BOOL sound_file_reload(TSnd *sound_file, LPDIRECTSOUNDBUFFER DSB)
static void sound_CreateSoundBuffer(TSnd *sound_file)
{
HANDLE file;
LPVOID buf1, buf2;
DWORD size1, size2;
BOOL rv;
if (DSB->Restore() != DS_OK)
return FALSE;
rv = FALSE;
WOpenFile(sound_file->sound_path, &file, FALSE);
WSetFilePointer(file, sound_file->chunk.dwOffset, NULL, FILE_BEGIN);
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;
}
WCloseFile(file);
DSBUFFERDESC DSB;
HRESULT error_code;
memset(&DSB, 0, sizeof(DSBUFFERDESC));
return rv;
DSB.dwSize = sizeof(DSBUFFERDESC);
DSB.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN | DSBCAPS_STATIC;
DSB.dwBufferBytes = sound_file->chunk.dwSize;
DSB.lpwfxFormat = &sound_file->fmt;
error_code = sglpDS->CreateSoundBuffer(&DSB, &sound_file->DSB, NULL);
if (error_code != ERROR_SUCCESS)
DSErrMsg(error_code, 282, "C:\\Src\\Diablo\\Source\\SOUND.CPP");
}
TSnd *sound_file_load(const char *path)
@ -237,21 +273,6 @@ TSnd *sound_file_load(const char *path)
return pSnd;
}
void sound_CreateSoundBuffer(TSnd *sound_file)
{
DSBUFFERDESC DSB;
HRESULT error_code;
memset(&DSB, 0, sizeof(DSBUFFERDESC));
DSB.dwSize = sizeof(DSBUFFERDESC);
DSB.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN | DSBCAPS_STATIC;
DSB.dwBufferBytes = sound_file->chunk.dwSize;
DSB.lpwfxFormat = &sound_file->fmt;
error_code = sglpDS->CreateSoundBuffer(&DSB, &sound_file->DSB, NULL);
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) {
@ -265,45 +286,7 @@ void sound_file_cleanup(TSnd *sound_file)
}
}
void snd_init(HWND hWnd)
{
int error_code;
snd_get_volume("Sound Volume", &sglSoundVolume);
gbSoundOn = sglSoundVolume > VOLUME_MIN;
snd_get_volume("Music Volume", &sglMusicVolume);
gbMusicOn = sglMusicVolume > VOLUME_MIN;
error_code = sound_DirectSoundCreate(NULL, &sglpDS, NULL);
if (error_code != DS_OK)
sglpDS = NULL;
if (sglpDS && sglpDS->SetCooperativeLevel(hWnd, DSSCL_EXCLUSIVE) == DS_OK)
sound_create_primary_buffer(NULL);
SVidInitialize(sglpDS);
SFileDdaInitialize(sglpDS);
gbSndInited = sglpDS != NULL;
}
void snd_get_volume(const char *value_name, int *value)
{
int v = *value;
if (!SRegLoadValue(APP_NAME, 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)
static void sound_create_primary_buffer(HANDLE music_track)
{
HRESULT error_code;
DSBUFFERDESC dsbuf;
@ -343,7 +326,7 @@ void sound_create_primary_buffer(HANDLE music_track)
}
}
HRESULT sound_DirectSoundCreate(LPGUID lpGuid, LPDIRECTSOUND *ppDS, LPUNKNOWN pUnkOuter)
static HRESULT sound_DirectSoundCreate(LPGUID lpGuid, LPDIRECTSOUND *ppDS, LPUNKNOWN pUnkOuter)
{
HRESULT(WINAPI * DirectSoundCreate)
(LPGUID lpGuid, LPDIRECTSOUND * ppDS, LPUNKNOWN pUnkOuter);
@ -362,6 +345,28 @@ HRESULT sound_DirectSoundCreate(LPGUID lpGuid, LPDIRECTSOUND *ppDS, LPUNKNOWN pU
return DirectSoundCreate(lpGuid, ppDS, pUnkOuter);
}
void snd_init(HWND hWnd)
{
int error_code;
snd_get_volume("Sound Volume", &sglSoundVolume);
gbSoundOn = sglSoundVolume > VOLUME_MIN;
snd_get_volume("Music Volume", &sglMusicVolume);
gbMusicOn = sglMusicVolume > VOLUME_MIN;
error_code = sound_DirectSoundCreate(NULL, &sglpDS, NULL);
if (error_code != DS_OK)
sglpDS = NULL;
if (sglpDS && sglpDS->SetCooperativeLevel(hWnd, DSSCL_EXCLUSIVE) == DS_OK)
sound_create_primary_buffer(NULL);
SVidInitialize(sglpDS);
SFileDdaInitialize(sglpDS);
gbSndInited = sglpDS != NULL;
}
void sound_cleanup()
{
snd_update(TRUE);
@ -380,11 +385,6 @@ void sound_cleanup()
}
}
void snd_set_volume(const char *key, int value)
{
SRegSaveValue(APP_NAME, key, 0, value);
}
void music_stop()
{
if (sghMusic) {

12
Source/sound.h

@ -6,38 +6,26 @@
#ifndef __SOUND_H__
#define __SOUND_H__
extern IDirectSoundBuffer *DSBs[8];
extern BOOLEAN gbSndInited;
extern HMODULE hDsound_dll;
void snd_update(BOOL bStopAll);
void snd_stop_snd(TSnd *pSnd);
BOOL snd_playing(TSnd *pSnd);
void snd_play_snd(TSnd *pSnd, int lVolume, int lPan);
IDirectSoundBuffer *sound_dup_channel(IDirectSoundBuffer *DSB);
BOOL sound_file_reload(TSnd *sound_file, IDirectSoundBuffer *DSB);
TSnd *sound_file_load(const char *path);
void sound_CreateSoundBuffer(TSnd *sound_file);
void sound_file_cleanup(TSnd *sound_file);
void snd_init(HWND hWnd);
void snd_get_volume(const char *value_name, int *value);
void sound_create_primary_buffer(HANDLE music_track);
HRESULT sound_DirectSoundCreate(LPGUID lpGuid, LPDIRECTSOUND *ppDS, LPUNKNOWN pUnkOuter);
void sound_cleanup();
void snd_set_volume(const char *key, int value);
void music_stop();
void music_start(int nTrack);
void sound_disable_music(BOOL disable);
int sound_get_or_set_music_volume(int volume);
int sound_get_or_set_sound_volume(int volume);
/* rdata */
/* data */
extern BOOLEAN gbMusicOn;
extern BOOLEAN gbSoundOn;
extern BOOLEAN gbDupSounds;
extern char unk_volume[4][2];
#endif /* __SOUND_H__ */

Loading…
Cancel
Save