From: Holger Schemel Date: Sun, 10 Dec 2000 00:21:29 +0000 (+0100) Subject: rnd-20001210-1-src X-Git-Tag: 2.0.0^2~9 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=e0560b41a1796a02b15937b0ae5453bca745e5ff;p=rocksndiamonds.git rnd-20001210-1-src --- diff --git a/src/files.c b/src/files.c index 884571eb..0fa192c3 100644 --- a/src/files.c +++ b/src/files.c @@ -456,7 +456,7 @@ void LoadLevel(int level_nr) /* always start with reliable default values */ setLevelInfoToDefaults(); - if (!(file = fopen(filename, "r"))) + if (!(file = fopen(filename, MODE_READ))) { Error(ERR_WARN, "cannot read level '%s' - creating new level", filename); return; @@ -620,7 +620,7 @@ void SaveLevel(int level_nr) char *oldest_possible_cookie; FILE *file; - if (!(file = fopen(filename, "w"))) + if (!(file = fopen(filename, MODE_WRITE))) { Error(ERR_WARN, "cannot save level file '%s'", filename); return; @@ -730,7 +730,7 @@ void LoadTape(int level_nr) /* at least one (default: the first) player participates in every tape */ num_participating_players = 1; - if (!(file = fopen(filename, "r"))) + if (!(file = fopen(filename, MODE_READ))) return; /* check file identifier */ @@ -892,7 +892,7 @@ void SaveTape(int level_nr) } } - if (!(file = fopen(filename, "w"))) + if (!(file = fopen(filename, MODE_WRITE))) { Error(ERR_WARN, "cannot save level recording file '%s'", filename); return; @@ -952,7 +952,7 @@ void LoadScore(int level_nr) highscore[i].Score = 0; } - if (!(file = fopen(filename, "r"))) + if (!(file = fopen(filename, MODE_READ))) return; /* check file identifier */ @@ -997,7 +997,7 @@ void SaveScore(int level_nr) InitScoreDirectory(leveldir_current->filename); - if (!(file = fopen(filename, "w"))) + if (!(file = fopen(filename, MODE_WRITE))) { Error(ERR_WARN, "cannot save score for level %d", level_nr); return; @@ -1304,7 +1304,7 @@ static struct SetupFileList *loadSetupFileList(char *filename) FILE *file; - if (!(file = fopen(filename, "r"))) + if (!(file = fopen(filename, MODE_READ))) { Error(ERR_WARN, "cannot open configuration file '%s'", filename); return NULL; @@ -1782,7 +1782,7 @@ static void SaveUserLevelInfo() filename = getPath2(getUserLevelDir(getLoginName()), LEVELINFO_FILENAME); - if (!(file = fopen(filename, "w"))) + if (!(file = fopen(filename, MODE_WRITE))) { Error(ERR_WARN, "cannot write level info file '%s'", filename); free(filename); @@ -1926,7 +1926,7 @@ void SaveSetup() filename = getPath2(getSetupDir(), SETUP_FILENAME); - if (!(file = fopen(filename, "w"))) + if (!(file = fopen(filename, MODE_WRITE))) { Error(ERR_WARN, "cannot write setup file '%s'", filename); free(filename); @@ -2014,7 +2014,7 @@ void SaveLevelSetup_LastSeries() filename = getPath2(getSetupDir(), LEVELSETUP_FILENAME); - if (!(file = fopen(filename, "w"))) + if (!(file = fopen(filename, MODE_WRITE))) { Error(ERR_WARN, "cannot write setup file '%s'", filename); free(filename); @@ -2159,7 +2159,7 @@ void SaveLevelSetup_SeriesInfo() filename = getPath2(getLevelSetupDir(level_subdir), LEVELSETUP_FILENAME); - if (!(file = fopen(filename, "w"))) + if (!(file = fopen(filename, MODE_WRITE))) { Error(ERR_WARN, "cannot write setup file '%s'", filename); free(filename); @@ -2178,3 +2178,5 @@ void SaveLevelSetup_SeriesInfo() chmod(filename, SETUP_PERMS); } +/* LocalWords: Rocks'n + */ diff --git a/src/game.c b/src/game.c index ab7dbb55..031fd785 100644 --- a/src/game.c +++ b/src/game.c @@ -762,7 +762,7 @@ void InitGame() OpenDoor(DOOR_OPEN_ALL); if (setup.sound_music) - PlaySoundLoop(background_loop[level_nr % num_bg_loops]); + PlayMusic(background_loop[level_nr % num_bg_loops]); KeyboardAutoRepeatOff(); @@ -6227,12 +6227,12 @@ static void HandleGameButtons(struct GadgetInfo *gi) if (setup.sound_music) { setup.sound_music = FALSE; - FadeSound(background_loop[level_nr % num_bg_loops]); + FadeMusic(); } else if (audio.loops_available) { setup.sound = setup.sound_music = TRUE; - PlaySoundLoop(background_loop[level_nr % num_bg_loops]); + PlayMusic(background_loop[level_nr % num_bg_loops]); } break; diff --git a/src/init.c b/src/init.c index 304b1037..b4efdc4e 100644 --- a/src/init.c +++ b/src/init.c @@ -132,7 +132,7 @@ void InitSound() { int i; - OpenAudio(&audio); + OpenAudio(); AllocSoundArray(NUM_SOUNDS); diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 5814e8d1..e62229a4 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -1314,7 +1314,7 @@ FILE *openErrorFile() FILE *error_file; filename = getPath2(getUserDataDir(), ERROR_FILENAME); - error_file = fopen(filename, "a"); + error_file = fopen(filename, MODE_APPEND); free(filename); return error_file; @@ -1326,7 +1326,7 @@ void dumpErrorFile() FILE *error_file; filename = getPath2(getUserDataDir(), ERROR_FILENAME); - error_file = fopen(filename, "r"); + error_file = fopen(filename, MODE_READ); free(filename); if (error_file != NULL) diff --git a/src/libgame/msdos.c b/src/libgame/msdos.c index 5b037e02..75f92a05 100644 --- a/src/libgame/msdos.c +++ b/src/libgame/msdos.c @@ -952,9 +952,15 @@ void AllegroDrawLine(Drawable d, int from_x, int from_y, int to_x, int to_y, freeze_mouse_flag = FALSE; } -Bool MSDOSOpenAudio(void) +void MSDOSOpenAudio(void) { - return allegro_init_audio(); + if (allegro_init_audio()) + { + audio.sound_available = TRUE; + audio.music_available = TRUE; + audio.loops_available = TRUE; + audio.sound_enabled = TRUE; + } } void MSDOSCloseAudio(void) diff --git a/src/libgame/msdos.h b/src/libgame/msdos.h index d071cf03..af865235 100644 --- a/src/libgame/msdos.h +++ b/src/libgame/msdos.h @@ -733,7 +733,7 @@ void XAutoRepeatOff(Display *); void AllegroDrawLine(Drawable, int, int, int, int, Pixel); -Bool MSDOSOpenAudio(void); +void MSDOSOpenAudio(void); void MSDOSCloseAudio(void); void NetworkServer(int, int); diff --git a/src/libgame/pcx.c b/src/libgame/pcx.c index 069adf7d..f50e2c9a 100644 --- a/src/libgame/pcx.c +++ b/src/libgame/pcx.c @@ -138,7 +138,7 @@ Image *Read_PCX_to_Image(char *filename) errno_pcx = PCX_Success; - if (!(file = fopen(filename, "r"))) + if (!(file = fopen(filename, MODE_READ))) { errno_pcx = PCX_OpenFailed; return NULL; diff --git a/src/libgame/sdl.c b/src/libgame/sdl.c index a4fbf7de..18de01b3 100644 --- a/src/libgame/sdl.c +++ b/src/libgame/sdl.c @@ -12,6 +12,7 @@ ***********************************************************/ #include "system.h" +#include "sound.h" #include "misc.h" @@ -705,24 +706,40 @@ Bitmap *SDLLoadImage(char *filename) /* audio functions */ /* ========================================================================= */ -inline boolean SDLOpenAudio(void) +inline void SDLOpenAudio(void) { if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { Error(ERR_WARN, "SDL_InitSubSystem() failed: %s", SDL_GetError()); - return FALSE; + return; } - if (Mix_OpenAudio(22050, AUDIO_S16, 2, 512) < 0) + if (Mix_OpenAudio(DEFAULT_AUDIO_SAMPLE_RATE, AUDIO_S16, + AUDIO_STEREO_CHANNELS, + DEFAULT_AUDIO_FRAGMENT_SIZE) < 0) { Error(ERR_WARN, "Mix_OpenAudio() failed: %s", SDL_GetError()); - return FALSE; + return; } - Mix_Volume(-1, SDL_MIX_MAXVOLUME / 4); - Mix_VolumeMusic(SDL_MIX_MAXVOLUME / 4); + audio.sound_available = TRUE; + audio.music_available = TRUE; + audio.loops_available = TRUE; + audio.sound_enabled = TRUE; + + /* 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; + } - return TRUE; + Mix_Volume(-1, SOUND_MAX_VOLUME); + Mix_VolumeMusic(SOUND_MAX_VOLUME); } inline void SDLCloseAudio(void) diff --git a/src/libgame/sdl.h b/src/libgame/sdl.h index e6543a9e..e6508859 100644 --- a/src/libgame/sdl.h +++ b/src/libgame/sdl.h @@ -326,7 +326,7 @@ void sge_Line(SDL_Surface *, Sint16, Sint16, Sint16, Sint16, Uint32); Bitmap *SDLLoadImage(char *); -inline boolean SDLOpenAudio(void); +inline void SDLOpenAudio(void); inline void SDLCloseAudio(void); #endif /* SDL_H */ diff --git a/src/libgame/sound.c b/src/libgame/sound.c index a7298a84..c690993c 100644 --- a/src/libgame/sound.c +++ b/src/libgame/sound.c @@ -15,6 +15,7 @@ #include #include +#include "system.h" #include "sound.h" #include "misc.h" @@ -81,7 +82,7 @@ int OpenAudioDevice(char *audio_device_name) return audio_fd; } -void UnixOpenAudio(struct AudioSystemInfo *audio) +void UnixOpenAudio(void) { static char *audio_device_name[] = { @@ -104,19 +105,20 @@ void UnixOpenAudio(struct AudioSystemInfo *audio) close(audio_fd); - audio->device_name = audio_device_name[i]; - audio->sound_available = TRUE; - audio->sound_enabled = TRUE; + audio.device_name = audio_device_name[i]; + audio.sound_available = TRUE; + audio.sound_enabled = TRUE; #if defined(AUDIO_STREAMING_DSP) - audio->loops_available = TRUE; + audio.music_available = TRUE; + audio.loops_available = TRUE; #endif } -void UnixCloseAudio(struct AudioSystemInfo *audio) +void UnixCloseAudio(void) { - if (audio->device_fd) - close(audio->device_fd); + if (audio.device_fd) + close(audio.device_fd); } #endif /* PLATFORM_UNIX */ @@ -202,26 +204,34 @@ void SoundServer() byte *sample_ptr; long sample_size; static long max_sample_size = 0; - static long fragment_size = 0; - /* Even if the stereo flag is used as being boolean, it must be - defined as an integer, else 'ioctl()' will fail! */ + static long fragment_size = DEFAULT_AUDIO_FRAGMENT_SIZE; + int sample_rate = DEFAULT_AUDIO_SAMPLE_RATE; int stereo = TRUE; -#if 0 - int sample_rate = 8000; -#else - int sample_rate = 22050; -#endif + /* 'ioctl()' expects pointer to integer value for stereo flag + (boolean is defined as 'char', which will not work here) */ if (playing_sounds || (audio.device_fd = OpenAudioDevice(audio.device_name)) >= 0) { if (!playing_sounds) /* we just opened the audio device */ { - /* 2 buffers / 512 bytes, giving 1/16 second resolution */ - /* (with stereo the effective buffer size will shrink to 256) */ - fragment_size = 0x00020009; + unsigned long fragment_spec = 0; + + /* determine logarithm (log2) of the fragment size */ + for (fragment_spec=0; (1 << fragment_spec) < fragment_size; + fragment_spec++); + + /* use two fragments (play one fragment, prepare the other); + one fragment would result in interrupted audio output, more + than two fragments would raise audio output latency to much */ + fragment_spec |= 0x00020000; + + /* Example for fragment specification: + - 2 buffers / 512 bytes (giving 1/16 second resolution for 8 kHz) + - (with stereo the effective buffer size will shrink to 256) + => fragment_size = 0x00020009 */ - if (ioctl(audio.device_fd,SNDCTL_DSP_SETFRAGMENT,&fragment_size) < 0) + if (ioctl(audio.device_fd,SNDCTL_DSP_SETFRAGMENT,&fragment_spec) < 0) Error(ERR_EXIT_SOUND_SERVER, "cannot set fragment size of /dev/dsp - no sounds"); @@ -262,7 +272,7 @@ void SoundServer() FD_SET(audio.soundserver_pipe[0], &sound_fdset); /* first clear the last premixing buffer */ - memset(premix_last_buffer,0,fragment_size*sizeof(int)); + memset(premix_last_buffer, 0, fragment_size * sizeof(int)); for(i=0;isound_available = FALSE; - audio->loops_available = FALSE; - audio->sound_enabled = FALSE; - audio->soundserver_pipe[0] = audio->soundserver_pipe[1] = 0; - audio->soundserver_pid = 0; - audio->device_name = NULL; - audio->device_fd = 0; + /* always start with reliable default values */ + audio.sound_available = FALSE; + audio.music_available = FALSE; + audio.loops_available = FALSE; + audio.mods_available = FALSE; + audio.sound_enabled = FALSE; + + audio.soundserver_pipe[0] = audio.soundserver_pipe[1] = 0; + audio.soundserver_pid = 0; + audio.device_name = NULL; + audio.device_fd = 0; + + audio.channels = 0; + audio.music_channel = 0; + audio.music_nr = 0; #if defined(TARGET_SDL) - if (SDLOpenAudio()) - { - audio->sound_available = TRUE; - audio->loops_available = TRUE; - audio->sound_enabled = TRUE; - } + SDLOpenAudio(); #elif defined(PLATFORM_MSDOS) - if (MSDOSOpenAudio()) - { - audio->sound_available = TRUE; - audio->loops_available = TRUE; - audio->sound_enabled = TRUE; - } + MSDOSOpenAudio(); #elif defined(PLATFORM_UNIX) - UnixOpenAudio(audio); + UnixOpenAudio(); #endif - - return audio->sound_available; } -inline void CloseAudio(struct AudioSystemInfo *audio) +inline void CloseAudio(void) { #if defined(TARGET_SDL) SDLCloseAudio(); #elif defined(PLATFORM_MSDOS) MSDOSCloseAudio(); #elif defined(PLATFORM_UNIX) - UnixCloseAudio(audio); + UnixCloseAudio(); #endif - audio->sound_available = FALSE; - audio->loops_available = FALSE; - audio->sound_enabled = FALSE; + audio.sound_enabled = FALSE; } inline void SetAudioMode(boolean enabled) diff --git a/src/libgame/system.h b/src/libgame/system.h index 3ff406f0..e9a6d190 100644 --- a/src/libgame/system.h +++ b/src/libgame/system.h @@ -30,10 +30,15 @@ /* contant definitions */ -#define DEFAULT_DEPTH 0 +/* the additional 'b' is needed for Win32 to open files in binary mode */ +#define MODE_READ "rb" +#define MODE_WRITE "wb" +#define MODE_APPEND "ab" -#define FULLSCREEN_NOT_AVAILABLE FALSE -#define FULLSCREEN_AVAILABLE TRUE +#define DEFAULT_DEPTH 0 + +#define FULLSCREEN_NOT_AVAILABLE FALSE +#define FULLSCREEN_AVAILABLE TRUE /* values for button_status */ #define MB_NOT_PRESSED FALSE @@ -166,12 +171,19 @@ struct VideoSystemInfo struct AudioSystemInfo { boolean sound_available; + boolean music_available; boolean loops_available; + boolean mods_available; boolean sound_enabled; + int soundserver_pipe[2]; int soundserver_pid; char *device_name; int device_fd; + + int channels; + int music_channel; + int music_nr; }; struct GfxInfo @@ -290,8 +302,8 @@ inline boolean ChangeVideoModeIfNeeded(boolean); Bitmap *LoadImage(char *); -inline boolean OpenAudio(struct AudioSystemInfo *); -inline void CloseAudio(struct AudioSystemInfo *); +inline void OpenAudio(void); +inline void CloseAudio(void); inline void SetAudioMode(boolean); inline void InitEventFilter(EventFilter);