Corrections post-code review for audio handler

separation-create-menu
thmaillarb 3 years ago
parent faf47fa469
commit abab37ca05

@ -67,6 +67,7 @@
*/ */
#define MACRO_TO_SFX_PATH(P) "rsrc/sfx/"#P".wav", #define MACRO_TO_SFX_PATH(P) "rsrc/sfx/"#P".wav",
// TODO faire dans 2 enums
/** /**
* \enum EnumAudios * \enum EnumAudios
* \brief Lists audios used in the program. * \brief Lists audios used in the program.
@ -121,14 +122,14 @@ void freeAudioHandler(AudioHandler* audioHandler);
* \param[in] music The music to be played. Should be a value of #EnumAudios starting with MUSIC_. * \param[in] music The music to be played. Should be a value of #EnumAudios starting with MUSIC_.
* \param[in] audioHandler The AudioHandler used to store the musics. * \param[in] audioHandler The AudioHandler used to store the musics.
*/ */
void playMusic(EnumAudios music, AudioHandler audioHandler); void playMusic(EnumAudios music, const AudioHandler* audioHandler);
/** /**
* \brief Plays a sound effect. * \brief Plays a sound effect.
* \param[in] sfx The SFX to be played. Should be a value of #EnumAudios starting with SFX_. * \param[in] sfx The SFX to be played. Should be a value of #EnumAudios starting with SFX_.
* \param[in] audioHandler The AudioHandler used to store the SFX. * \param[in] audioHandler The AudioHandler used to store the SFX.
*/ */
void playSFX(EnumAudios sfx, AudioHandler audioHandler); void playSFX(EnumAudios sfx, const AudioHandler* audioHandler);
#endif // AUDIO_H #endif // AUDIO_H

@ -1,4 +1,5 @@
#include "engine/AudioHandler.h" #include "engine/AudioHandler.h"
#include <SDL2/SDL.h>
// A channel represents the number of SFX we can play at the same time. // A channel represents the number of SFX we can play at the same time.
// We normally should use only 1 channel, and we add one for safety. // We normally should use only 1 channel, and we add one for safety.
@ -13,7 +14,7 @@
* but should actually be a pointer to Mix_Music. * but should actually be a pointer to Mix_Music.
* It is the music we want to play next; * It is the music we want to play next;
*/ */
int fadeOut(void* args) { int switchMusic_impl(void* args) {
// Since args is a pointer to void // Since args is a pointer to void
// (the way C handles undefined types), // (the way C handles undefined types),
// casting args to a pointer to Mix_Music // casting args to a pointer to Mix_Music
@ -21,9 +22,7 @@ int fadeOut(void* args) {
int ret; int ret;
if(Mix_FadeOutMusic(500) == 1) { // Starting the fadeout if(Mix_FadeOutMusic(500) == 1) { // Starting the fadeout
while (Mix_PlayingMusic()) { SDL_Delay(500); // Waiting until it's done
; // Waiting until it's done
}
} else { } else {
fprintf(stderr,"WARNING: couldn't fade out"); fprintf(stderr,"WARNING: couldn't fade out");
Mix_HaltMusic(); Mix_HaltMusic();
@ -40,7 +39,6 @@ int fadeOut(void* args) {
} }
// Global functions // Global functions
AudioHandler newAudioHandler(int masterVol, int volMusic, int volSFX) { AudioHandler newAudioHandler(int masterVol, int volMusic, int volSFX) {
AudioHandler audioHandler; AudioHandler audioHandler;
int nb_SFX = NB_AUDIO_DEFINED - NB_MUSIC_DEFINED - 1; int nb_SFX = NB_AUDIO_DEFINED - NB_MUSIC_DEFINED - 1;
@ -48,11 +46,11 @@ AudioHandler newAudioHandler(int masterVol, int volMusic, int volSFX) {
// Generating paths to musics and SFX files using macros // Generating paths to musics and SFX files using macros
char* musicsPaths[] = {MACRO_FOR_ALL_MUSICS(MACRO_TO_MUSIC_PATH)}; char* musicsPaths[] = {MACRO_FOR_ALL_MUSICS(MACRO_TO_MUSIC_PATH)};
char* sfxPaths[] = {MACRO_FOR_ALL_SFX(MACRO_TO_SFX_PATH)}; char* sfxPaths[] = {MACRO_FOR_ALL_SFX(MACRO_TO_SFX_PATH)};
audioHandler.canPlayAudio = true;
// Initialize + verify opening audio device // Initialize + verify opening audio device
if (0 != Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)) { if (0 != Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)) {
fprintf(stderr,"Error when initializing audio.\n"); fprintf(stderr,"Error when initializing audio: %s.\n", Mix_GetError());
audioHandler.canPlayAudio = false;
return audioHandler; return audioHandler;
} }
@ -87,7 +85,6 @@ AudioHandler newAudioHandler(int masterVol, int volMusic, int volSFX) {
} }
changeSFXVol(&audioHandler, volSFX); changeSFXVol(&audioHandler, volSFX);
changeMasterVol(&audioHandler, masterVol);
return audioHandler; return audioHandler;
} }
@ -142,7 +139,7 @@ void freeAudioHandler(AudioHandler* audioHandler) {
audioHandler->canPlayAudio = false; audioHandler->canPlayAudio = false;
} }
void playMusic(EnumAudios music, AudioHandler audioHandler) { void playMusic(EnumAudios music, const AudioHandler* audioHandler) {
// music should be a value of EnumAudios that starts by MUSIC_ // music should be a value of EnumAudios that starts by MUSIC_
// In other words, it should be an enumerator before NB_MUSIC_DEFINED. // In other words, it should be an enumerator before NB_MUSIC_DEFINED.
if (music >= NB_MUSIC_DEFINED) { if (music >= NB_MUSIC_DEFINED) {
@ -151,7 +148,7 @@ void playMusic(EnumAudios music, AudioHandler audioHandler) {
} }
// Checking if audio has been opened. // Checking if audio has been opened.
if (!(audioHandler.canPlayAudio)) { if (!(audioHandler->canPlayAudio)) {
fprintf(stderr,"WARNING: tried to play a music with an unusable AudioHandler\n"); fprintf(stderr,"WARNING: tried to play a music with an unusable AudioHandler\n");
return; return;
} }
@ -159,7 +156,7 @@ void playMusic(EnumAudios music, AudioHandler audioHandler) {
// If another music is playing, fading the previous one out // If another music is playing, fading the previous one out
if (Mix_PlayingMusic()) { if (Mix_PlayingMusic()) {
// Creating the thread, passing the music as parameter // Creating the thread, passing the music as parameter
SDL_Thread* thread = SDL_CreateThread(&fadeOut, "Fade out", audioHandler.musics[music]); SDL_Thread* thread = SDL_CreateThread(&switchMusic_impl, "Fade out", audioHandler->musics[music]);
if (thread == NULL) { if (thread == NULL) {
fprintf(stderr,"WARNING: couldn't create thread to fade out music\n"); fprintf(stderr,"WARNING: couldn't create thread to fade out music\n");
} }
@ -167,13 +164,13 @@ void playMusic(EnumAudios music, AudioHandler audioHandler) {
SDL_DetachThread(thread); SDL_DetachThread(thread);
// No other music is playing: starting a music normally. // No other music is playing: starting a music normally.
} else { } else {
if (Mix_PlayMusic(audioHandler.musics[music],-1) != 0) { if (Mix_PlayMusic(audioHandler->musics[music],-1) != 0) {
fprintf(stderr,"WARNING: %s\n",Mix_GetError()); fprintf(stderr,"WARNING: %s\n",Mix_GetError());
} }
} }
} }
void playSFX(EnumAudios sfx, AudioHandler audioHandler) { void playSFX(EnumAudios sfx, const AudioHandler* audioHandler) {
int channel; int channel;
Mix_Chunk* chunkSFX; Mix_Chunk* chunkSFX;
@ -185,13 +182,13 @@ void playSFX(EnumAudios sfx, AudioHandler audioHandler) {
} }
// Checking if SFX has been opened. // Checking if SFX has been opened.
if (!(audioHandler.canPlayAudio)) { if (!(audioHandler->canPlayAudio)) {
fprintf(stderr,"WARNING: tried to play an SFX with an unusable AudioHandler\n"); fprintf(stderr,"WARNING: tried to play an SFX with an unusable AudioHandler\n");
return; return;
} }
// Getting actual chunk // Getting actual chunk
chunkSFX = audioHandler.sfx[sfx - NB_MUSIC_DEFINED - 1]; chunkSFX = audioHandler->sfx[sfx - NB_MUSIC_DEFINED - 1];
// Getting first available channel // Getting first available channel
channel = Mix_GroupAvailable(-1); channel = Mix_GroupAvailable(-1);
if (channel == -1) { if (channel == -1) {

Loading…
Cancel
Save