diff options
Diffstat (limited to 'lzr.c')
-rw-r--r-- | lzr.c | 140 |
1 files changed, 50 insertions, 90 deletions
@@ -8,7 +8,7 @@ # include <SDL2/SDL_image.h> #endif #ifdef LZR_ENABLE_MIXER -# include "cmixer.h" +# include <SDL2/SDL_mixer.h> #endif #ifdef LZR_ENABLE_DEVMODE # include <sys/stat.h> @@ -52,24 +52,9 @@ static int mouse_y = 0; #ifdef LZR_ENABLE_MIXER static struct { - cm_Source *ptr; + Mix_Chunk *ptr; } sounds[LZR_MAX_SOUNDS] = {0}; -static SDL_mutex *audio_mutex = NULL; -static SDL_AudioDeviceID audio_dev = 0; - -static void _lock_handler(cm_Event *e) -{ - if (e->type == CM_EVENT_LOCK) - SDL_LockMutex(audio_mutex); - else - SDL_UnlockMutex(audio_mutex); -} - -static void _audio_callback(void *udata, Uint8 *stream, int size) -{ - (void)udata; - cm_process((void *)stream, size / 2); -} +static Mix_Music *music = NULL; #endif static char *_lzrstrdup(const char *str) @@ -192,7 +177,7 @@ int LZR_Init(LZR_Config cfg) (float)config.display_width / (float)config.display_height; config.ratio /= ratio; } - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { + if (SDL_Init(SDL_INIT_VIDEO) < 0) { SDL_Log("%s", SDL_GetError()); return -1; } @@ -203,26 +188,14 @@ int LZR_Init(LZR_Config cfg) } #endif #ifdef LZR_ENABLE_MIXER - audio_mutex = SDL_CreateMutex(); - if (audio_mutex == NULL) { - SDL_Log("%s", SDL_GetError()); + if (Mix_Init(MIX_INIT_FLAC) != MIX_INIT_FLAC) { + SDL_Log("%s", Mix_GetError()); return -1; } - SDL_AudioSpec fmt = {.freq = 44100, - .format = AUDIO_S16, - .channels = 2, - .samples = 1024, - .callback = _audio_callback}; - SDL_AudioSpec got; - audio_dev = SDL_OpenAudioDevice(NULL, 0, &fmt, &got, - SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); - if (audio_dev == 0) { - SDL_Log("%s", SDL_GetError()); + if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 8, 1024) < 0) { + SDL_Log("%s", Mix_GetError()); return -1; } - cm_init(44100); - cm_set_lock(_lock_handler); - SDL_PauseAudioDevice(audio_dev, 0); #endif basepath = SDL_GetBasePath(); if (basepath == NULL) { @@ -272,30 +245,18 @@ int LZR_Init(LZR_Config cfg) void LZR_Quit(void) { #ifdef LZR_ENABLE_MIXER - for (int i = LZR_MAX_SOUNDS - 1; i >= 0; i--) - if (sounds[i].ptr != NULL) - cm_stop(sounds[i].ptr); - SDL_Delay(100); - for (int i = LZR_MAX_SOUNDS - 1; i >= 0; i--) + for (int i = 0; i < LZR_MAX_SOUNDS; i--) if (sounds[i].ptr != NULL) { - cm_destroy_source(sounds[i].ptr); + Mix_FreeChunk(sounds[i].ptr); sounds[i].ptr = NULL; - SDL_Log("destroyed sound %d", i); } - if (audio_dev != 0) { - SDL_CloseAudioDevice(audio_dev); - audio_dev = 0; - } - if (audio_mutex != NULL) { - SDL_DestroyMutex(audio_mutex); - audio_mutex = NULL; - } + Mix_CloseAudio(); + Mix_Quit(); #endif for (int i = 0; i < LZR_MAX_IMAGES; i++) { if (images[i].tex != NULL) { SDL_DestroyTexture(images[i].tex); images[i].tex = NULL; - SDL_Log("destroyed image %d", i); } if (images[i].path != NULL) { free(images[i].path); @@ -437,13 +398,13 @@ int LZR_SoundLoad(const char *path, float volume) SDL_Log("LZR_PathPrefix failed"); return -1; } - cm_Source *const chunk = cm_new_source_from_file(apath); + Mix_Chunk *const chunk = Mix_LoadWAV(apath); free(apath); if (chunk == NULL) { - SDL_Log("%s: %s", path, cm_get_error()); + SDL_Log("%s: %s", path, Mix_GetError()); return -1; } - cm_set_gain(chunk, volume); + Mix_VolumeChunk(chunk, volume); sounds[i].ptr = chunk; return i; #else @@ -832,7 +793,7 @@ int LZR_DrawTile(int id, int tile, int x, int y, double rot, int flip) return 0; } -int LZR_PlaySound(int id, int loops) +int LZR_PlaySound(int id) { #ifdef LZR_ENABLE_MIXER if (id < 0) { @@ -843,71 +804,70 @@ int LZR_PlaySound(int id, int loops) SDL_Log("no sound with id %d", id); return -1; } - cm_stop(sounds[id].ptr); - cm_set_loop(sounds[id].ptr, loops); - cm_play(sounds[id].ptr); + if (Mix_PlayChannel(-1, sounds[id].ptr, 0) < 0) { + SDL_Log("%s", Mix_GetError()); + return -1; + } return 0; #else - (void)id, (void)loops; + (void)id; SDL_Log("LZR MIXER module is disabled"); return -1; #endif } -void LZR_StopSound(int id) +int LZR_SetMusicVolume(float volume) { #ifdef LZR_ENABLE_MIXER - if (id < 0) { - SDL_Log("id is negative"); - return; - } - if (id >= LZR_MAX_SOUNDS || sounds[id].ptr == NULL) { - SDL_Log("no sound with id %d", id); - return; + if (Mix_VolumeMusic(volume * MIX_MAX_VOLUME) < 0) { + SDL_Log("%s", Mix_GetError()); + return -1; } - cm_stop(sounds[id].ptr); + return 0; #else - (void)id; + (void)volume; + SDL_Log("LZR MIXER module is disabled"); + return -1; #endif } -int LZR_SetSoundVolume(int id, float volume) +int LZR_PlayMusic(const char *path, int loops) { #ifdef LZR_ENABLE_MIXER - if (id < 0) { - SDL_Log("id is negative"); + LZR_StopMusic(); + char *const apath = LZR_PathPrefix(path); + if (apath == NULL) { + SDL_Log("LZR_PathPrefix failed"); return -1; } - if (id >= LZR_MAX_SOUNDS || sounds[id].ptr == NULL) { - SDL_Log("no sound with id %d", id); + music = Mix_LoadMUS(apath); + free(apath); + if (music == NULL) { + SDL_Log("%s: %s", path, Mix_GetError()); + return -1; + } + if (Mix_PlayMusic(music, loops) < 0) { + SDL_Log("%s", Mix_GetError()); return -1; } - cm_set_gain(sounds[id].ptr, volume); + Mix_RewindMusic(); return 0; #else - (void)id, (void)volume; + (void)path, (void)loops; SDL_Log("LZR MIXER module is disabled"); return -1; #endif } -int LZR_SetSoundPan(int id, float pan) +void LZR_StopMusic(void) { #ifdef LZR_ENABLE_MIXER - if (id < 0) { - SDL_Log("id is negative"); - return -1; + if (Mix_PlayingMusic()) + Mix_HaltMusic(); + if (music != NULL) { + Mix_FreeMusic(music); + music = NULL; } - if (id >= LZR_MAX_SOUNDS || sounds[id].ptr == NULL) { - SDL_Log("no sound with id %d", id); - return -1; - } - cm_set_pan(sounds[id].ptr, pan); - return 0; -#else - (void)id, (void)pan; - SDL_Log("LZR MIXER module is disabled"); - return -1; #endif } |