X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fsound.c;h=b71ed27fe3d7ef4076494e33f9d641e6cb9c62ec;hb=87af02f45b63b849773c7f830d69136d130acca0;hp=4b62e341fbd0e8d7d3029df0d8ab82ec3913b0cd;hpb=696d50b659131ba46ea7f4c44ca4d29475e8cb4f;p=rocksndiamonds.git diff --git a/src/libgame/sound.c b/src/libgame/sound.c index 4b62e341..b71ed27f 100644 --- a/src/libgame/sound.c +++ b/src/libgame/sound.c @@ -651,6 +651,7 @@ static void sound_handler(struct SoundControl snd_ctrl) #if !defined(PLATFORM_WIN32) static void SoundServer_InsertNewSound(struct SoundControl snd_ctrl) { + SoundInfo *snd_info; int i, k; if (snd_ctrl.music) @@ -658,6 +659,15 @@ static void SoundServer_InsertNewSound(struct SoundControl snd_ctrl) else if (snd_ctrl.nr >= num_sounds) return; + snd_info = (snd_ctrl.music ? Music[snd_ctrl.nr] : Sound[snd_ctrl.nr]); + if (snd_info == NULL) + { +#if 1 + printf("sound/music %d undefined\n", snd_ctrl.nr); +#endif + return; + } + /* if playlist is full, remove oldest sound */ if (playing_sounds == MAX_SOUNDS_PLAYING) { @@ -752,9 +762,6 @@ static void SoundServer_InsertNewSound(struct SoundControl snd_ctrl) if (!playlist[i].active || (snd_ctrl.music && i == audio.music_channel)) { - SoundInfo *snd_info = - (snd_ctrl.music ? Music[snd_ctrl.nr] : Sound[snd_ctrl.nr]); - snd_ctrl.data_ptr = snd_info->data_ptr; snd_ctrl.data_len = snd_info->data_len; snd_ctrl.format = snd_info->format; @@ -1123,7 +1130,7 @@ static SoundInfo *Load_WAV(char *filename) if (!audio.sound_available) return NULL; -#if 1 +#if 0 printf("loading WAV file '%s'\n", filename); #endif @@ -1230,69 +1237,87 @@ static SoundInfo *Load_WAV(char *filename) return snd_info; } -static void LoadCustomSound(SoundInfo **snd_info, char *basename) +static void deleteSoundEntry(SoundInfo **snd_info) { - char *filename = getCustomSoundFilename(basename); - ListNode *node; - - if (filename == NULL) /* (should never happen) */ - { - Error(ERR_WARN, "cannot find sound file '%s'", basename); - return; - } - if (*snd_info) { - char *filename_old = (*snd_info)->source_filename; - - if (strcmp(filename, filename_old) == 0) - { - /* The old and new sound are the same (have the same filename and path). - This usually means that this sound does not exist in this sound set - and a fallback to the existing sound is done. */ - -#if 1 - printf("[sound '%s' already exists (same list entry)]\n", filename); -#endif - - return; - } + char *filename = (*snd_info)->source_filename; #if 1 - printf("[decrementing reference counter of sound '%s']\n", filename_old); + printf("[decrementing reference counter of sound '%s']\n", filename); #endif if (--(*snd_info)->num_references <= 0) { #if 1 - printf("[deleting sound '%s']\n", filename_old); + printf("[deleting sound '%s']\n", filename); #endif /* FreeSound(*snd_info); */ - deleteNodeFromList(&SoundFileList, filename_old, FreeSound); + deleteNodeFromList(&SoundFileList, filename, FreeSound); } + + *snd_info = NULL; } +} + +static void replaceSoundEntry(SoundInfo **snd_info, char *filename) +{ + ListNode *node; - /* check if this sound already exists in the list of sounds */ - node = getNodeFromKey(SoundFileList, filename); - if (node) + /* check if the old and the new sound file are the same */ + if (*snd_info && strcmp((*snd_info)->source_filename, filename) == 0) { -#if 1 + /* The old and new sound are the same (have the same filename and path). + This usually means that this sound does not exist in this sound set + and a fallback to the existing sound is done. */ + +#if 0 + printf("[sound '%s' already exists (same list entry)]\n", filename); +#endif + + return; + } + + /* delete existing sound file entry */ + deleteSoundEntry(snd_info); + + /* check if the new sound file already exists in the list of sounds */ + if ((node = getNodeFromKey(SoundFileList, filename)) != NULL) + { +#if 0 printf("[sound '%s' already exists (other list entry)]\n", filename); #endif *snd_info = (SoundInfo *)node->content; (*snd_info)->num_references++; + } + else if ((*snd_info = Load_WAV(filename)) != NULL) /* load new sound */ + { + (*snd_info)->num_references = 1; + addNodeToList(&SoundFileList, (*snd_info)->source_filename, *snd_info); + } +} - return; +static void LoadCustomSound(SoundInfo **snd_info, char *basename) +{ + char *filename = getCustomSoundFilename(basename); + + if (strcmp(basename, SND_FILE_UNDEFINED) == 0) + { + deleteSoundEntry(snd_info); + return; } - *snd_info = Load_WAV(filename); - (*snd_info)->num_references = 1; + if (filename == NULL) + { + Error(ERR_WARN, "cannot find sound file '%s'", basename); + return; + } - addNodeToList(&SoundFileList, (*snd_info)->source_filename, *snd_info); + replaceSoundEntry(snd_info, filename); } void InitSoundList(struct SoundEffectInfo *sounds_list, int num_list_entries) @@ -1309,13 +1334,17 @@ void LoadSoundToList(char *basename, int list_pos) if (Sound == NULL || list_pos >= num_sounds) return; +#if 0 printf("loading sound '%s' ... [%d]\n", basename, getNumNodes(SoundFileList)); +#endif LoadCustomSound(&Sound[list_pos], basename); +#if 0 printf("loading sound '%s' done [%d]\n", basename, getNumNodes(SoundFileList)); +#endif } static MusicInfo *Load_MOD(char *filename) @@ -1701,7 +1730,7 @@ static void ReloadCustomSounds() { int i; -#if 1 +#if 0 printf("DEBUG: reloading sounds '%s' ...\n", artwork.sounds_set_current); #endif @@ -1718,12 +1747,15 @@ static void ReloadCustomSounds() /* printf("list size == %d\n", getNumNodes(SoundFileList)); */ + +#if 0 dumpList(SoundFileList); +#endif } static void ReloadCustomMusic() { -#if 1 +#if 0 printf("DEBUG: reloading music '%s' ...\n", artwork.music_set_current); #endif