| 1 | // (_||_/ |
| 2 | // ( ) |
| 3 | // ( o 0 ) |
| 4 | //-OOO°--(_)---°OOO--------------------------------------- |
| 5 | // Copyright (C) 2006 By Dominique Roux-Serret |
| 6 | // .OOOo oOOO. [email protected] |
| 7 | //-( )------( )--------------------------------------- |
| 8 | // ( ( ) / Copyright (C) 2023 By Johnny Jazeix |
| 9 | // (_) (_/ [email protected] |
| 10 | |
| 11 | // This program is free software; you can redistribute it and/or modify |
| 12 | // it under the terms of the GNU General Public License as published by |
| 13 | // the Free Software Foundation; either version 2 or version 3 of the License. |
| 14 | |
| 15 | // This program is distributed in the hope that it will be useful, |
| 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | // GNU General Public License for more details. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License along |
| 21 | // with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | |
| 24 | #include <SDL2/SDL_audio.h> // for AUDIO_S16 |
| 25 | #include <SDL2/SDL_error.h> // for SDL_GetError |
| 26 | #include <SDL2/SDL_log.h> // for SDL_LogInfo, SDL_LOG_CATEGORY_APPLICATION |
| 27 | #include <cstring> |
| 28 | |
| 29 | #include "audio.h" |
| 30 | #include "utils.h" |
| 31 | #include "preference.h" |
| 32 | |
| 33 | /*** Variable globales ***/ |
| 34 | /*************************/ |
| 35 | extern sNewPreference Pref; |
| 36 | extern int Horloge; |
| 37 | |
| 38 | /*** Constructeur et Destructeur ***/ |
| 39 | /***********************************/ |
| 40 | Audio::~Audio() |
| 41 | { |
| 42 | if (N) { |
| 43 | Mix_HaltChannel(-1); |
| 44 | for (int i = 0; i < N; i++) { |
| 45 | if (Son[i]) { |
| 46 | Mix_FreeChunk(Son[i]); |
| 47 | } |
| 48 | } |
| 49 | delete[] Son; |
| 50 | } |
| 51 | Mix_CloseAudio(); |
| 52 | } |
| 53 | |
| 54 | /*** Initialise l'Audio ***/ |
| 55 | /**************************/ |
| 56 | bool Audio::Init() |
| 57 | { |
| 58 | char PathFile[512]; |
| 59 | |
| 60 | if (Mix_OpenAudio(22050, AUDIO_S16, 1, 1024)) { |
| 61 | SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Enable to init Sound card: %s", SDL_GetError()); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | /*** Allocation de la mémoire ***/ |
| 66 | N = sFin; |
| 67 | Son = new Mix_Chunk *[sFin]; |
| 68 | |
| 69 | /*** Chargement des sons ***/ |
| 70 | strcpy(PathFile, "Sounds/clic.wav"); |
| 71 | Utils::GetPath(PathFile); |
| 72 | Son[sClic] = Mix_LoadWAV(PathFile); |
| 73 | |
| 74 | strcpy(PathFile, "Sounds/speed.wav"); |
| 75 | Utils::GetPath(PathFile); |
| 76 | Son[sSpeed] = Mix_LoadWAV(PathFile); |
| 77 | |
| 78 | strcpy(PathFile, "Sounds/crash.wav"); |
| 79 | Utils::GetPath(PathFile); |
| 80 | Son[sCrash] = Mix_LoadWAV(PathFile); |
| 81 | |
| 82 | strcpy(PathFile, "Sounds/end.wav"); |
| 83 | Utils::GetPath(PathFile); |
| 84 | Son[sEnd] = Mix_LoadWAV(PathFile); |
| 85 | |
| 86 | strcpy(PathFile, "Sounds/lose.wav"); |
| 87 | Utils::GetPath(PathFile); |
| 88 | Son[sLose] = Mix_LoadWAV(PathFile); |
| 89 | |
| 90 | strcpy(PathFile, "Sounds/etire.wav"); |
| 91 | Utils::GetPath(PathFile); |
| 92 | Son[sEtire] = Mix_LoadWAV(PathFile); |
| 93 | |
| 94 | strcpy(PathFile, "Sounds/wagon.wav"); |
| 95 | Utils::GetPath(PathFile); |
| 96 | Son[sWagon] = Mix_LoadWAV(PathFile); |
| 97 | |
| 98 | strcpy(PathFile, "Sounds/reduit.wav"); |
| 99 | Utils::GetPath(PathFile); |
| 100 | Son[sReduit] = Mix_LoadWAV(PathFile); |
| 101 | |
| 102 | strcpy(PathFile, "Sounds/live.wav"); |
| 103 | Utils::GetPath(PathFile); |
| 104 | Son[sLive] = Mix_LoadWAV(PathFile); |
| 105 | |
| 106 | strcpy(PathFile, "Sounds/menu.mod"); |
| 107 | Utils::GetPath(PathFile); |
| 108 | Music = Mix_LoadMUS(PathFile); |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | /*** Charge une music 0=menu 1,2,3,4 = game ***/ |
| 114 | /**********************************************/ |
| 115 | void Audio::LoadMusic(int Num) |
| 116 | { |
| 117 | char Provi[512] = "Sounds/jeu1.xm"; |
| 118 | |
| 119 | if (!N) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | NMus = Num; |
| 124 | |
| 125 | if (Music) { |
| 126 | PauseMusic(true); |
| 127 | Mix_HaltMusic(); // Arrete la music |
| 128 | Mix_FreeMusic(Music); |
| 129 | Music = nullptr; |
| 130 | } |
| 131 | |
| 132 | if (Num == 0) { // Si music du menu |
| 133 | strcpy(Provi, "Sounds/menu.mod"); |
| 134 | Utils::GetPath(Provi); |
| 135 | Music = Mix_LoadMUS(Provi); |
| 136 | } |
| 137 | else { |
| 138 | Provi[10] = (char)(Num) + '0'; |
| 139 | Utils::GetPath(Provi); |
| 140 | Music = Mix_LoadMUS(Provi); |
| 141 | } |
| 142 | PlayMusic(); |
| 143 | } |
| 144 | |
| 145 | /*** Passe à la music de jeu suivante ***/ |
| 146 | /****************************************/ |
| 147 | void Audio::NextMusic() |
| 148 | { |
| 149 | NMus++; |
| 150 | if (NMus > 2) { |
| 151 | NMus = 1; |
| 152 | } |
| 153 | LoadMusic(NMus); |
| 154 | } |
| 155 | |
| 156 | /*** Fait la lecture d'un son ***/ |
| 157 | /********************************/ |
| 158 | void Audio::Play(eSon So) |
| 159 | { |
| 160 | if (!N) { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | if (So == sClic) { |
| 165 | if (Horloge - MemoHorloge <= 120) { |
| 166 | return; |
| 167 | } |
| 168 | MemoHorloge = Horloge; |
| 169 | } |
| 170 | |
| 171 | Mix_PlayChannel(-1, Son[So], 0); |
| 172 | } |
| 173 | |
| 174 | /*** Joue la music ***/ |
| 175 | /*********************/ |
| 176 | void Audio::PlayMusic() const |
| 177 | { |
| 178 | if (Music && N) { |
| 179 | Mix_PlayMusic(Music, -1); |
| 180 | DoVolume(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void Audio::PauseMusic(bool Et) const |
| 185 | { |
| 186 | if (!N) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | if (Et) { |
| 191 | Mix_PauseMusic(); |
| 192 | } |
| 193 | else { |
| 194 | Mix_ResumeMusic(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /*** Valide les Volumes ***/ |
| 199 | /**************************/ |
| 200 | void Audio::DoVolume() const |
| 201 | { |
| 202 | if (!N) { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | Mix_Volume(-1, (int)Pref.Volume); |
| 207 | if (NMus) { |
| 208 | Mix_VolumeMusic((int)Pref.VolumeM); |
| 209 | } |
| 210 | else { |
| 211 | Mix_VolumeMusic((int)Pref.VolumeM / 2); |
| 212 | } |
| 213 | } |