diff --git a/.gitignore b/.gitignore index 4c61493..5a06f6b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .vscode *.out main +jail_audio \ No newline at end of file diff --git a/jail_audio.cpp b/jail_audio.cpp index 0293d51..29e0628 100644 --- a/jail_audio.cpp +++ b/jail_audio.cpp @@ -204,10 +204,8 @@ void JA_PlayMusic(JA_Music_t *music, const int loop) { if (!JA_musicEnabled) return; - if (current_music != NULL) { - current_music->pos = 0; - current_music->state = JA_MUSIC_STOPPED; - } + JA_StopMusic(); + current_music = music; current_music->pos = 0; current_music->state = JA_MUSIC_PLAYING; @@ -217,6 +215,7 @@ void JA_PlayMusic(JA_Music_t *music, const int loop) if (!SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length)) printf("[ERROR] SDL_PutAudioStreamData failed!\n"); SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume); if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) printf("[ERROR] SDL_BindAudioStream failed!\n"); + SDL_ResumeAudioStreamDevice(current_music->stream); } void JA_PauseMusic() @@ -272,7 +271,7 @@ void JA_DeleteMusic(JA_Music_t *music) { if (current_music == music) current_music = nullptr; SDL_free(music->buffer); - SDL_DestroyAudioStream(music->stream); + if (music->stream) SDL_DestroyAudioStream(music->stream); delete music; } @@ -337,6 +336,7 @@ int JA_PlaySound(JA_Sound_t *sound, const int loop) int channel = 0; while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE) { channel++; } if (channel == JA_MAX_SIMULTANEOUS_CHANNELS) channel = 0; + JA_StopChannel(channel); channels[channel].sound = sound; channels[channel].times = loop; @@ -354,7 +354,8 @@ int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop) { if (!JA_soundEnabled) return -1; - if (channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return -1; + if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return -1; + JA_StopChannel(channel); channels[channel].sound = sound; channels[channel].times = loop; @@ -430,18 +431,18 @@ void JA_StopChannel(const int channel) if (channel == -1) { for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) { + if (channels[i].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[i].stream); channels[i].state = JA_CHANNEL_FREE; channels[i].pos = 0; channels[i].sound = NULL; - SDL_DestroyAudioStream(channels[i].stream); } } else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) { + if (channels[channel].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[channel].stream); channels[channel].state = JA_CHANNEL_FREE; channels[channel].pos = 0; channels[channel].sound = NULL; - SDL_DestroyAudioStream(channels[channel].stream); } } diff --git a/main.cpp b/main.cpp index 33ffe11..87aac8a 100644 --- a/main.cpp +++ b/main.cpp @@ -17,7 +17,7 @@ int main(int argc, char **argv) { int channel = -1; JA_PlayMusic(music, -1); - int volume = 128; + float volume = 1.0f; bool should_exit = false; while(!should_exit) { while(SDL_PollEvent(&event)) { @@ -49,10 +49,10 @@ int main(int argc, char **argv) { JA_FadeOutMusic(1000); break; case SDL_SCANCODE_UP: - volume = JA_SetVolume(volume+16); + volume = JA_SetVolume(volume+0.1f); break; case SDL_SCANCODE_DOWN: - volume = JA_SetVolume(volume-16); + volume = JA_SetVolume(volume-0.1f); break; case SDL_SCANCODE_ESCAPE: should_exit = true;