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