Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
80 changes: 37 additions & 43 deletions src/audio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,23 @@
#include "utils.h"
#include "preference.h"

/*** Variable globales ***/
/*************************/
extern sNewPreference Pref;
extern int currentTime;

/*** Constructeur et Destructeur ***/
/***********************************/
Audio::~Audio()
{
if (N) {
Mix_HaltChannel(-1);
for (int i = 0; i < N; i++) {
if (Son[i]) {
Mix_FreeChunk(Son[i]);
if (Sound[i]) {
Mix_FreeChunk(Sound[i]);
}
}
delete[] Son;
delete[] Sound;
}
Mix_CloseAudio();
}

/*** Initialise l'Audio ***/
/**************************/
bool Audio::Init()
{
char PathFile[512];
Expand All @@ -62,46 +56,46 @@ bool Audio::Init()
return false;
}

/*** Allocation de la mémoire ***/
N = sFin;
Son = new Mix_Chunk *[sFin];
/*** Memory allocation ***/
N = sSize;
Sound = new Mix_Chunk *[sSize];

/*** Chargement des sons ***/
strcpy(PathFile, "Sounds/clic.wav");
/*** Loading sound effects ***/
strcpy(PathFile, "Sounds/click.wav");
Utils::GetPath(PathFile);
Son[sClic] = Mix_LoadWAV(PathFile);
Sound[sClick] = Mix_LoadWAV(PathFile);

strcpy(PathFile, "Sounds/speed.wav");
Utils::GetPath(PathFile);
Son[sSpeed] = Mix_LoadWAV(PathFile);
Sound[sSpeed] = Mix_LoadWAV(PathFile);

strcpy(PathFile, "Sounds/crash.wav");
Utils::GetPath(PathFile);
Son[sCrash] = Mix_LoadWAV(PathFile);
Sound[sCrash] = Mix_LoadWAV(PathFile);

strcpy(PathFile, "Sounds/end.wav");
Utils::GetPath(PathFile);
Son[sEnd] = Mix_LoadWAV(PathFile);
Sound[sEnd] = Mix_LoadWAV(PathFile);

strcpy(PathFile, "Sounds/lose.wav");
Utils::GetPath(PathFile);
Son[sLose] = Mix_LoadWAV(PathFile);
Sound[sLose] = Mix_LoadWAV(PathFile);

strcpy(PathFile, "Sounds/etire.wav");
strcpy(PathFile, "Sounds/expand.wav");
Utils::GetPath(PathFile);
Son[sEtire] = Mix_LoadWAV(PathFile);
Sound[sExpand] = Mix_LoadWAV(PathFile);

strcpy(PathFile, "Sounds/wagon.wav");
strcpy(PathFile, "Sounds/car.wav");
Utils::GetPath(PathFile);
Son[sWagon] = Mix_LoadWAV(PathFile);
Sound[sCar] = Mix_LoadWAV(PathFile);

strcpy(PathFile, "Sounds/reduit.wav");
strcpy(PathFile, "Sounds/shrink.wav");
Utils::GetPath(PathFile);
Son[sReduit] = Mix_LoadWAV(PathFile);
Sound[sShrink] = Mix_LoadWAV(PathFile);

strcpy(PathFile, "Sounds/live.wav");
Utils::GetPath(PathFile);
Son[sLive] = Mix_LoadWAV(PathFile);
Sound[sLive] = Mix_LoadWAV(PathFile);

strcpy(PathFile, "Sounds/menu.mod");
Utils::GetPath(PathFile);
Expand All @@ -110,8 +104,8 @@ bool Audio::Init()
return true;
}

/*** Charge une music 0=menu 1,2,3,4 = game ***/
/**********************************************/
/*** Loads a music track, 0 = menu music 1,2,3,4=game music tracks ***/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is a valid one, the behaviour cannot be explained just with the name, it makes sense to have this comment

/*********************************************************************/
void Audio::LoadMusic(int Num)
{
char Provi[512] = "Sounds/jeu1.xm";
Expand All @@ -124,12 +118,12 @@ void Audio::LoadMusic(int Num)

if (Music) {
PauseMusic(true);
Mix_HaltMusic(); // Arrete la music
Mix_HaltMusic(); // Stops the music
Mix_FreeMusic(Music);
Music = nullptr;
}

if (Num == 0) { // Si music du menu
if (Num == 0) { // if menu music
strcpy(Provi, "Sounds/menu.mod");
Utils::GetPath(Provi);
Music = Mix_LoadMUS(Provi);
Expand All @@ -142,8 +136,8 @@ void Audio::LoadMusic(int Num)
PlayMusic();
}

/*** Passe à la music de jeu suivante ***/
/****************************************/
/*** Switch to next game track ***/
/********************************/
void Audio::NextMusic()
{
NMus++;
Expand All @@ -153,26 +147,26 @@ void Audio::NextMusic()
LoadMusic(NMus);
}

/*** Fait la lecture d'un son ***/
/********************************/
void Audio::Play(eSon So)
/*** Plays a sound effect ***/
/****************************/
void Audio::Play(eSound index)
{
if (!N) {
return;
}

if (So == sClic) {
if (index == sClick) {
if (currentTime - MemorizedTime <= 120) {
return;
}
MemorizedTime = currentTime;
}

Mix_PlayChannel(-1, Son[So], 0);
Mix_PlayChannel(-1, Sound[index], 0);
}

/*** Joue la music ***/
/*********************/
/*** Plays the music ***/
/***********************/
void Audio::PlayMusic() const
{
if (Music && N) {
Expand All @@ -181,22 +175,22 @@ void Audio::PlayMusic() const
}
}

void Audio::PauseMusic(bool Et) const
void Audio::PauseMusic(bool IsMusicPlaying) const
{
if (!N) {
return;
}

if (Et) {
if (IsMusicPlaying) {
Mix_PauseMusic();
}
else {
Mix_ResumeMusic();
}
}

/*** Valide les Volumes ***/
/**************************/
/*** Handles sound volumes ***/
/*****************************/
void Audio::DoVolume() const
{
if (!N) {
Expand Down
44 changes: 20 additions & 24 deletions src/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,44 @@

#include <SDL2/SDL_mixer.h>

/*** Enumération des sons ***/
/****************************/
enum eSon {
sClic = 0,
/*** Sound effects enum ***/
/**************************/
enum eSound {
sClick = 0,
sSpeed,
sCrash,
sEnd,
sLose,
sEtire,
sWagon,
sReduit,
sExpand,
sCar,
sShrink,
sLive,
sFin
sSize
};

/*** Définition de la classe Audio ***/
/*************************************/
class Audio
{
public:
Audio() = default;
~Audio();

/*** Fonctions ***/
bool Init(); // Initialise et charge les fichiers audio
void LoadMusic(int Num); // Charge une music, 0 = music du menu 1,2,3,4=Jeu
void NextMusic(); // Passe à la music suivante
bool Init(); // Initializes and loads audio files
void LoadMusic(int Num); // Loads a music track, 0 = menu music 1,2,3,4=game music tracks
void NextMusic(); // Switch to next game track

void Play(eSon); // Joue un son
void PlayMusic() const; // Joue la music
void Play(eSound);
void PlayMusic() const;

void PauseMusic(bool Etat) const; // Met ou no la music en pause
void PauseMusic(bool IsMusicPlaying) const; // Pauses/Resumes music

void DoVolume() const; // Valide les volumes audio
Mix_Music *Music { nullptr }; // Pointe sur les musics
void DoVolume() const; // Handles sound volumes
Mix_Music *Music { nullptr }; // Pointer to music tracks

private:
/*** Variables ***/
int N { 0 }; // Nombre d'échantillon audio
int NMus { 0 }; // Numéro de la music en cours
int MemorizedTime { 0 }; // Mémorise l'horloge pour les clics
Mix_Chunk **Son { nullptr }; // Pointe sur les sons
int N { 0 }; // Number/Amount of sound effects
int NMus { 0 }; // Number of the current music
int MemorizedTime { 0 }; // Memorizes time for clicks
Mix_Chunk **Sound { nullptr }; // Pointer to sound effects
};

#endif
Loading