From 1101077a226b09854650662759bf12caf7c84f8d Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Wed, 24 Apr 2002 00:46:05 +0200 Subject: [PATCH] rnd-20020424-1-src --- src/game.c | 7 +- src/init.c | 20 +- src/libgame/misc.c | 18 +- src/libgame/sdl.c | 12 +- src/libgame/sdl.h | 1 + src/libgame/sound.c | 447 ++++++++++++++++++++++++++----------------- src/libgame/sound.h | 74 ++++--- src/libgame/system.c | 7 +- src/libgame/system.h | 6 +- src/libgame/x11.h | 1 + src/main.c | 15 -- src/main.h | 2 - src/timestamp.h | 2 +- 13 files changed, 355 insertions(+), 257 deletions(-) diff --git a/src/game.c b/src/game.c index fa1175c0..06d84b98 100644 --- a/src/game.c +++ b/src/game.c @@ -831,8 +831,8 @@ void InitGame() OpenDoor(DOOR_OPEN_ALL); - if (setup.sound_music && num_bg_loops) - PlayMusic(level_nr % num_bg_loops); + if (setup.sound_music) + PlayMusic(level_nr); KeyboardAutoRepeatOff(); @@ -6548,8 +6548,7 @@ static void HandleGameButtons(struct GadgetInfo *gi) else if (audio.music_available) { setup.sound = setup.sound_music = TRUE; - if (num_bg_loops) - PlayMusic(level_nr % num_bg_loops); + PlayMusic(level_nr); } break; diff --git a/src/init.c b/src/init.c index 5923322a..eee87022 100644 --- a/src/init.c +++ b/src/init.c @@ -157,21 +157,12 @@ void InitSound() OpenAudio(); - for(i=0; i 4 && - strcmp(&filename[strlen(filename) - 4], ".wav") == 0) + if (strlen(basename) > 4 && + strcmp(&basename[strlen(basename) - 4], ".wav") == 0) return TRUE; return FALSE; } -boolean FileIsMusic(char *filename) +boolean FileIsMusic(char *basename) { - if (strlen(filename) > 4 && - (strcmp(&filename[strlen(filename) - 4], ".mod") == 0 || - strcmp(&filename[strlen(filename) - 4], ".MOD") == 0 || - strncmp(filename, "mod.", 4) == 0 || - strncmp(filename, "MOD.", 4) == 0)) + if (strlen(basename) > 4 && + (strcmp(&basename[strlen(basename) - 4], ".mod") == 0 || + strcmp(&basename[strlen(basename) - 4], ".MOD") == 0 || + strncmp(basename, "mod.", 4) == 0 || + strncmp(basename, "MOD.", 4) == 0)) return TRUE; return FALSE; diff --git a/src/libgame/sdl.c b/src/libgame/sdl.c index ef68a08e..f10dfd6d 100644 --- a/src/libgame/sdl.c +++ b/src/libgame/sdl.c @@ -888,13 +888,11 @@ inline void SDLOpenAudio(void) /* determine number of available channels */ audio.channels = Mix_AllocateChannels(MIX_CHANNELS); - if (!audio.mods_available) /* reserve first channel for music loops */ - { - if (Mix_ReserveChannels(1) == 1) - audio.music_channel = 0; - else - audio.music_available = FALSE; - } + /* reserve first channel for music loops */ + if (Mix_ReserveChannels(1) == 1) + audio.music_channel = 0; + else + audio.music_available = FALSE; Mix_Volume(-1, SOUND_MAX_VOLUME); Mix_VolumeMusic(SOUND_MAX_VOLUME); diff --git a/src/libgame/sdl.h b/src/libgame/sdl.h index 85073986..a4ce8d64 100644 --- a/src/libgame/sdl.h +++ b/src/libgame/sdl.h @@ -61,6 +61,7 @@ typedef int Colormap; struct SDLSurfaceInfo { char *source_filename; + int width, height; SDL_Surface *surface; SDL_Surface *surface_masked; diff --git a/src/libgame/sound.c b/src/libgame/sound.c index 52fdcbfb..744c4bd8 100644 --- a/src/libgame/sound.c +++ b/src/libgame/sound.c @@ -24,12 +24,9 @@ #include "setup.h" +static SoundInfo **Sound = NULL; +static MusicInfo **Music = NULL; static int num_sounds = 0, num_music = 0; -static struct SampleInfo *Sound = NULL; -#if defined(TARGET_SDL) -static int num_mods = 0; -static struct SampleInfo *Mod = NULL; -#endif /* ========================================================================= */ @@ -39,7 +36,7 @@ static int playing_sounds = 0; static struct SoundControl playlist[MAX_SOUNDS_PLAYING]; static struct SoundControl emptySoundControl = { - -1,0,0, FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE, 0,0L,0L,NULL + -1,0,0, FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE, 0,0, 0,NULL }; #if defined(PLATFORM_UNIX) @@ -178,7 +175,7 @@ void InitPlaylist(void) { int i; - for(i=0;i> PSND_MAX_VOLUME_BITS; @@ -358,10 +367,10 @@ void SoundServer(void) if (stereo) { int middle_pos = PSND_MAX_LEFT2RIGHT/2; - int left_volume = stereo_volume[middle_pos+playlist[i].stereo]; - int right_volume = stereo_volume[middle_pos-playlist[i].stereo]; + int left_volume = stereo_volume[middle_pos +playlist[i].stereo]; + int right_volume = stereo_volume[middle_pos -playlist[i].stereo]; - for(j=0;jlongest) + if (i != audio.music_channel && !playlist[i].loop && actual > longest) { - longest=actual; - longest_nr=i; + longest = actual; + longest_nr = i; } } #if defined(PLATFORM_MSDOS) @@ -562,18 +576,17 @@ static void SoundServer_InsertNewSound(struct SoundControl snd_ctrl) } /* check if sound is already being played (and how often) */ - for(k=0,i=0;i=1 && snd_ctrl.loop) + if (k >= 1 && snd_ctrl.loop) { - for(i=0;i=2) + if (k >= 2) { - int longest=0, longest_nr=0; + int longest = 0, longest_nr = 0; /* look for oldest equal sound */ - for(i=0;i=longest) + if (actual >= longest) { - longest=actual; - longest_nr=i; + longest = actual; + longest_nr = i; } } @@ -620,18 +636,27 @@ static void SoundServer_InsertNewSound(struct SoundControl snd_ctrl) playing_sounds--; } - /* neuen Sound in Liste packen */ - for(i=0;idata_ptr; + snd_ctrl.data_len = snd_info->data_len; + playlist[i] = snd_ctrl; playing_sounds++; #if defined(PLATFORM_MSDOS) - playlist[i].voice = allocate_voice(Sound[snd_ctrl.nr].sample_ptr); - if(snd_ctrl.loop) + playlist[i].voice = allocate_voice(playlist[i].data_ptr); + + if (snd_ctrl.loop) voice_set_playmode(playlist[i].voice, PLAYMODE_LOOP); + voice_set_volume(playlist[i].voice, snd_ctrl.volume); voice_set_pan(playlist[i].voice, snd_ctrl.stereo); voice_start(playlist[i].voice); @@ -658,15 +683,18 @@ void SoundServer_FadeSound(int nr) #if !defined(PLATFORM_WIN32) #if defined(PLATFORM_MSDOS) -static void SoundServer_StopSound(int nr) +static void SoundServer_StopSound(struct SoundControl snd_ctrl) { + int nr = snd_ctrl.nr; int i; if (!playing_sounds) return; - for(i=0;idata_len = 0; - snd_info->data_ptr = NULL; + snd_info = checked_calloc(sizeof(SoundInfo)); #if defined(TARGET_SDL) - if ((snd_info->mix_chunk = Mix_LoadWAV(filename)) == NULL) + if ((snd_info->data_ptr = Mix_LoadWAV(filename)) == NULL) { Error(ERR_WARN, "cannot read sound file '%s'", filename); - return FALSE; + free(snd_info); + return NULL; } -#elif defined(PLATFORM_UNIX) +#elif defined(TARGET_ALLEGRO) + + if ((snd_info->data_ptr = load_sample(filename)) == NULL) + { + Error(ERR_WARN, "cannot read sound file '%s'", filename); + free(snd_info); + return NULL; + } + +#else /* PLATFORM_UNIX */ if ((file = fopen(filename, MODE_READ)) == NULL) { Error(ERR_WARN, "cannot open sound file '%s'", filename); - return FALSE; + free(snd_info); + return NULL; } /* read chunk id "RIFF" */ @@ -983,7 +1018,8 @@ static boolean Load_WAV(char *filename) { Error(ERR_WARN, "missing 'RIFF' chunk of sound file '%s'", filename); fclose(file); - return FALSE; + free(snd_info); + return NULL; } /* read "RIFF" type id "WAVE" */ @@ -992,16 +1028,12 @@ static boolean Load_WAV(char *filename) { Error(ERR_WARN, "missing 'WAVE' type ID of sound file '%s'", filename); fclose(file); - return FALSE; + free(snd_info); + return NULL; } while (getFileChunk(file, chunk_name, &chunk_size, BYTE_ORDER_LITTLE_ENDIAN)) { -#if 0 - printf("DEBUG: file '%s', chunk id '%s', chunk size '%d' [%d]\n", - filename, chunk_name, chunk_size, feof(file)); -#endif - if (strcmp(chunk_name, "fmt ") == 0) { /* read header information */ @@ -1022,7 +1054,9 @@ static boolean Load_WAV(char *filename) { Error(ERR_WARN,"cannot read 'data' chunk of sound file '%s'",filename); fclose(file); - return FALSE; + free(snd_info->data_ptr); + free(snd_info); + return NULL; } /* check for odd number of sample bytes (data chunk is word aligned) */ @@ -1038,110 +1072,119 @@ static boolean Load_WAV(char *filename) if (snd_info->data_ptr == NULL) { Error(ERR_WARN, "missing 'data' chunk of sound file '%s'", filename); - return FALSE; + free(snd_info); + return NULL; } for (i=0; idata_len; i++) - snd_info->data_ptr[i] = snd_info->data_ptr[i] ^ 0x80; - -#else /* PLATFORM_MSDOS */ + ((byte *)snd_info->data_ptr)[i] = ((byte *)snd_info->data_ptr)[i] ^ 0x80; - snd_info->sample_ptr = load_sample(filename); - if (!snd_info->sample_ptr) - { - Error(ERR_WARN, "cannot read sound file '%s'", filename); - return FALSE; - } +#endif /* PLATFORM_UNIX */ -#endif + snd_info->type = SND_TYPE_WAV; + snd_info->source_filename = getStringCopy(filename); - return TRUE; + return snd_info; } -boolean LoadCustomSound(char *basename) +SoundInfo *LoadCustomSound(char *basename) { char *filename = getCustomSoundFilename(basename); if (filename == NULL) { - Error(ERR_WARN, "cannot find sound file '%s' -- no sounds", filename); + Error(ERR_WARN, "cannot find sound file '%s'", basename); return FALSE; } return Load_WAV(filename); } -static boolean Load_MOD(char *mod_name) +void InitSoundList(int num_list_entries) { -#if defined(TARGET_SDL) - struct SampleInfo *mod_info; - char *filename; + Sound = checked_calloc(num_list_entries * sizeof(SoundInfo *)); + num_sounds = num_list_entries; +} - num_mods++; - Mod = checked_realloc(Mod, num_mods * sizeof(struct SampleInfo)); +void LoadSoundToList(char *basename, int list_pos) +{ + if (Sound == NULL || list_pos >= num_sounds) + return; - mod_info = &Mod[num_mods - 1]; - mod_info->name = mod_name; + if (Sound[list_pos]) + FreeSound(Sound[list_pos]); - filename = getPath2(options.music_directory, mod_info->name); + Sound[list_pos] = LoadCustomSound(basename); +} - if ((mod_info->mix_music = Mix_LoadMUS(filename)) == NULL) +static MusicInfo *Load_MOD(char *filename) +{ +#if defined(TARGET_SDL) + MusicInfo *mod_info; + + if (!audio.sound_available) + return NULL; + + mod_info = checked_calloc(sizeof(MusicInfo)); + + if ((mod_info->data_ptr = Mix_LoadMUS(filename)) == NULL) { - Error(ERR_WARN, "cannot read music file '%s' -- no music", filename); - free(filename); - return FALSE; + Error(ERR_WARN, "cannot read music file '%s'", filename); + free(mod_info); + return NULL; } - free(filename); + mod_info->type = MUS_TYPE_MOD; + mod_info->source_filename = getStringCopy(filename); - return TRUE; + return mod_info; #else - return FALSE; + return NULL; #endif } -int LoadCustomMusic(void) +void LoadCustomMusic(void) { char *music_directory = getCustomMusicDirectory(); DIR *dir; struct dirent *dir_entry; - int num_wav_music = 0; - int num_mod_music = 0; if (!audio.sound_available) - return 0; + return; if ((dir = opendir(music_directory)) == NULL) { Error(ERR_WARN, "cannot read music directory '%s'", music_directory); audio.music_available = FALSE; - return 0; + return; } while ((dir_entry = readdir(dir)) != NULL) /* loop until last dir entry */ { - char *filename = getPath2(music_directory, dir_entry->d_name); + char *basename = dir_entry->d_name; + char *filename = getPath2(music_directory, basename); + MusicInfo *mus_info = NULL; - if (FileIsSound(filename) && Load_WAV(filename)) - num_wav_music++; - else if (FileIsMusic(filename) && Load_MOD(filename)) - num_mod_music++; + if (FileIsSound(filename)) + mus_info = Load_WAV(filename); + else if (FileIsMusic(filename)) + mus_info = Load_MOD(filename); free(filename); + + if (mus_info) + { + num_music++; + Music = checked_realloc(Music, num_music * sizeof(MusicInfo *)); + Music[num_music -1] = mus_info; + } } closedir(dir); - if (num_wav_music == 0 && num_mod_music == 0) + if (num_music == 0) Error(ERR_WARN, "cannot find any valid music files in directory '%s'", - options.music_directory); - - num_music = (num_mod_music > 0 ? num_mod_music : num_wav_music); - - audio.mods_available = (num_mod_music > 0); - audio.music_available = (num_music > 0); - - return num_music; + music_directory); } void PlayMusic(int nr) @@ -1149,23 +1192,23 @@ void PlayMusic(int nr) if (!audio.music_available) return; - if (!audio.mods_available) - nr = num_sounds - num_music + nr; - #if defined(TARGET_SDL) - if (audio.mods_available) /* play MOD music */ + + if (Music[nr]->type == MUS_TYPE_MOD) { - Mix_PlayMusic(Mod[nr].mix_music, -1); + Mix_PlayMusic(Music[nr]->data_ptr, -1); Mix_VolumeMusic(SOUND_MAX_VOLUME); /* must be _after_ Mix_PlayMusic()! */ } else /* play WAV music loop */ { Mix_Volume(audio.music_channel, SOUND_MAX_VOLUME); - Mix_PlayChannel(audio.music_channel, Sound[nr].mix_chunk, -1); + Mix_PlayChannel(audio.music_channel, Music[nr]->data_ptr, -1); } + #else - audio.music_nr = nr; - PlaySoundLoop(nr); + + PlaySoundMusic(nr); + #endif } @@ -1184,7 +1227,12 @@ void PlaySoundLoop(int nr) PlaySoundExt(nr, PSND_MAX_VOLUME, PSND_MIDDLE, PSND_LOOP); } -void PlaySoundExt(int nr, int volume, int stereo, boolean loop) +void PlaySoundMusic(int nr) +{ + PlaySoundExt(nr, PSND_MAX_VOLUME, PSND_MIDDLE, PSND_MUSIC); +} + +void PlaySoundExt(int nr, int volume, int stereo, boolean loop_type) { struct SoundControl snd_ctrl = emptySoundControl; @@ -1193,27 +1241,31 @@ void PlaySoundExt(int nr, int volume, int stereo, boolean loop) audio.sound_deactivated) return; - if (volumePSND_MAX_VOLUME) + else if (volume > PSND_MAX_VOLUME) volume = PSND_MAX_VOLUME; - if (stereoPSND_MAX_RIGHT) + else if (stereo > PSND_MAX_RIGHT) stereo = PSND_MAX_RIGHT; snd_ctrl.nr = nr; snd_ctrl.volume = volume; snd_ctrl.stereo = stereo; - snd_ctrl.loop = loop; + snd_ctrl.loop = (loop_type != PSND_NO_LOOP); + snd_ctrl.music = (loop_type == PSND_MUSIC); snd_ctrl.active = TRUE; + +#if 0 snd_ctrl.data_ptr = Sound[nr].data_ptr; snd_ctrl.data_len = Sound[nr].data_len; +#endif #if defined(TARGET_SDL) Mix_Volume(-1, SOUND_MAX_VOLUME); - Mix_PlayChannel(-1, Sound[nr].mix_chunk, (loop ? -1 : 0)); + Mix_PlayChannel(-1, Sound[nr]->data_ptr, (loop_type ? -1 : 0)); #elif defined(PLATFORM_UNIX) if (audio.soundserver_pid == 0) /* we are child process */ return; @@ -1231,16 +1283,14 @@ void PlaySoundExt(int nr, int volume, int stereo, boolean loop) void FadeMusic(void) { -#if defined(TARGET_SDL) if (!audio.sound_available) return; - if (audio.mods_available) - Mix_FadeOutMusic(SOUND_FADING_INTERVAL); - else - Mix_FadeOutChannel(audio.music_channel, SOUND_FADING_INTERVAL); +#if defined(TARGET_SDL) + Mix_FadeOutMusic(SOUND_FADING_INTERVAL); + Mix_FadeOutChannel(audio.music_channel, SOUND_FADING_INTERVAL); #else - FadeSound(audio.music_nr); + StopSoundExt(-1, SSND_FADE_MUSIC); #endif } @@ -1252,7 +1302,7 @@ void FadeSound(int nr) void FadeSounds() { FadeMusic(); - StopSoundExt(-1, SSND_FADE_ALL_SOUNDS); + StopSoundExt(-1, SSND_FADE_ALL); } void StopMusic(void) @@ -1261,12 +1311,10 @@ void StopMusic(void) if (!audio.sound_available) return; - if (audio.mods_available) - Mix_HaltMusic(); - else - Mix_HaltChannel(audio.music_channel); + Mix_HaltMusic(); + Mix_HaltChannel(audio.music_channel); #else - StopSound(audio.music_nr); + StopSoundExt(-1, SSND_STOP_MUSIC); #endif } @@ -1277,7 +1325,7 @@ void StopSound(int nr) void StopSounds() { - StopSoundExt(-1, SSND_STOP_ALL_SOUNDS); + StopSoundExt(-1, SSND_STOP_ALL); } void StopSoundExt(int nr, int method) @@ -1287,27 +1335,30 @@ void StopSoundExt(int nr, int method) if (!audio.sound_available) return; - if (SSND_FADING(method)) + if (method & SSND_FADING) snd_ctrl.fade_sound = TRUE; - if (SSND_ALL(method)) + if (method & SSND_ALL) snd_ctrl.stop_all_sounds = TRUE; else { - snd_ctrl.nr = nr; snd_ctrl.stop_sound = TRUE; + snd_ctrl.nr = nr; } + if (method & SSND_MUSIC) + snd_ctrl.music = TRUE; + #if defined(TARGET_SDL) - if (SSND_FADING(method)) + if (method & SSND_FADING) { int i; for (i=0; idata_ptr) + { +#if defined(TARGET_SDL) + Mix_FreeChunk(sound->data_ptr); +#elif defined(TARGET_ALLEGRO) + destroy_sample(sound->data_ptr); +#else /* PLATFORM_UNIX */ + free(sound->data_ptr); +#endif + } + + free(sound); +} + +void FreeMusic(MusicInfo *music) +{ + if (music == NULL) return; - for(i=0; idata_ptr) + { #if defined(TARGET_SDL) - free(Sound[i].mix_chunk); -#elif !defined(PLATFORM_MSDOS) - free(Sound[i].data_ptr); -#else - destroy_sample(Sound[i].sample_ptr); + if (music->type == MUS_TYPE_MOD) + Mix_FreeMusic(music->data_ptr); + else + Mix_FreeChunk(music->data_ptr); +#elif defined(TARGET_ALLEGRO) + destroy_sample(music->data_ptr); +#else /* PLATFORM_UNIX */ + free(music->data_ptr); #endif + } + + free(music); +} + +void FreeAllSounds() +{ + int i; + + if (Sound == NULL) + return; + + for(i=0; isource_filename = getStringCopy(filename); + if (new_bitmap) + new_bitmap->source_filename = getStringCopy(filename); return new_bitmap; } @@ -737,9 +738,8 @@ inline void OpenAudio(void) audio.sound_available = FALSE; audio.music_available = FALSE; audio.loops_available = FALSE; - audio.mods_available = FALSE; - audio.sound_enabled = FALSE; + audio.sound_enabled = FALSE; audio.sound_deactivated = FALSE; audio.soundserver_pipe[0] = audio.soundserver_pipe[1] = 0; @@ -749,7 +749,6 @@ inline void OpenAudio(void) audio.channels = 0; audio.music_channel = 0; - audio.music_nr = 0; #if defined(TARGET_SDL) SDLOpenAudio(); diff --git a/src/libgame/system.h b/src/libgame/system.h index f3569763..316de68e 100644 --- a/src/libgame/system.h +++ b/src/libgame/system.h @@ -230,11 +230,10 @@ struct VideoSystemInfo struct AudioSystemInfo { boolean sound_available; - boolean music_available; boolean loops_available; - boolean mods_available; - boolean sound_enabled; + boolean music_available; + boolean sound_enabled; boolean sound_deactivated; /* for temporarily disabling sound */ int soundserver_pipe[2]; @@ -244,7 +243,6 @@ struct AudioSystemInfo int channels; int music_channel; - int music_nr; }; struct GfxInfo diff --git a/src/libgame/x11.h b/src/libgame/x11.h index 43013b93..3ccac795 100644 --- a/src/libgame/x11.h +++ b/src/libgame/x11.h @@ -60,6 +60,7 @@ typedef XClientMessageEvent ClientMessageEvent; struct X11DrawableInfo { char *source_filename; + int width, height; Drawable drawable; Drawable clip_mask; diff --git a/src/main.c b/src/main.c index 8533d336..cf78c0cb 100644 --- a/src/main.c +++ b/src/main.c @@ -136,21 +136,6 @@ char *sound_name[NUM_SOUNDS] = "gate.wav" }; -/* background music */ -int background_loop[] = -{ -#if 0 - SND_ALCHEMY, - SND_CHASE, - SND_NETWORK, - SND_CZARDASZ, - SND_TYGER, - SND_VOYAGER, - SND_TWILIGHT -#endif -}; -int num_bg_loops = sizeof(background_loop)/sizeof(int); - char *element_info[] = { "empty space", /* 0 */ diff --git a/src/main.h b/src/main.h index 68cac8cd..4769e69b 100644 --- a/src/main.h +++ b/src/main.h @@ -398,8 +398,6 @@ extern struct GameInfo game; extern struct GlobalInfo global; extern char *sound_name[]; -extern int background_loop[]; -extern int num_bg_loops; extern char *element_info[]; extern int num_element_info; diff --git a/src/timestamp.h b/src/timestamp.h index d672394d..427ae052 100644 --- a/src/timestamp.h +++ b/src/timestamp.h @@ -1 +1 @@ -#define COMPILE_DATE_STRING "[2002-04-22 02:18]" +#define COMPILE_DATE_STRING "[2002-04-23 22:56]" -- 2.34.1