Debian Codegraph @

src/cdogs/music.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2013-2016, 2019, 2021, 2024 Cong Xu
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29 #include "music.h"
30
31 #include <string.h>
32
33 #include <tinydir/tinydir.h>
34
35 #include "config.h"
36 #include "gamedata.h"
37 #include "log.h"
38
39 static void LoadMusic(CArray *tracks, const char *path);
40 void MusicPlayerInit(MusicPlayer *mp)
41 {
42 memset(mp, 0, sizeof *mp);
43
44 // Load music
45 LoadMusic(&mp->generalTracks[MUSIC_MENU], "music/menu");
46 LoadMusic(&mp->generalTracks[MUSIC_BRIEFING], "music/briefing");
47 LoadMusic(&mp->generalTracks[MUSIC_GAME], "music/game");
48 LoadMusic(&mp->generalTracks[MUSIC_END], "music/end");
49 LoadMusic(&mp->generalTracks[MUSIC_LOSE], "music/lose");
50 LoadMusic(&mp->generalTracks[MUSIC_VICTORY], "music/victory");
51 }
52 static void LoadMusic(CArray *tracks, const char *path)
53 {
54 CArrayInit(tracks, sizeof(Mix_Music *));
55 tinydir_dir dir;
56 char buf[CDOGS_PATH_MAX];
57 GetDataFilePath(buf, path);
58 if (tinydir_open(&dir, buf) == -1)
59 {
60 LOG(LM_MAIN, LL_ERROR, "Cannot open music dir %s: %s", buf,
61 strerror(errno));
62 return;
63 }
64
65 for (; dir.has_next; tinydir_next(&dir))
66 {
67 Mix_Music *m;
68 tinydir_file file;
69 if (tinydir_readfile(&dir, &file) == -1)
70 {
71 goto bail;
72 }
73 if (!file.is_reg)
74 {
75 continue;
76 }
77
78 m = MusicLoad(file.path);
79 if (m == NULL)
80 {
81 continue;
82 }
83 CArrayPushBack(tracks, &m);
84 }
85
86 bail:
87 tinydir_close(&dir);
88 }
89
90 static void UnloadMusic(CArray *tracks);
91 void MusicPlayerTerminate(MusicPlayer *mp)
92 {
93 for (MusicType type = MUSIC_MENU; type < MUSIC_COUNT; type++)
94 {
95 UnloadMusic(&mp->generalTracks[type]);
96 }
97 }
98 static void UnloadMusic(CArray *tracks)
99 {
100 CA_FOREACH(Mix_Music *, m, *tracks)
101 Mix_FreeMusic(*m);
102 CA_FOREACH_END()
103 CArrayTerminate(tracks);
104 }
105
106 Mix_Music *MusicLoad(const char *path)
107 {
108 // Only load music from known extensions
109 const char *ext = strrchr(path, '.');
110 if (ext == NULL ||
111 !(strcmp(ext, ".it") == 0 || strcmp(ext, ".IT") == 0 ||
112 strcmp(ext, ".mod") == 0 || strcmp(ext, ".MOD") == 0 ||
113 strcmp(ext, ".ogg") == 0 || strcmp(ext, ".OGG") == 0 ||
114 strcmp(ext, ".s3m") == 0 || strcmp(ext, ".S3M") == 0 ||
115 strcmp(ext, ".xm") == 0 || strcmp(ext, ".XM") == 0))
116 {
117 return NULL;
118 }
119 LOG(LM_MAIN, LL_TRACE, "loading music file %s", path);
120 return Mix_LoadMUS(path);
121 }
122
123 static void PlayMusic(MusicPlayer *mp)
124 {
125 MusicResume(mp);
126
127 if (ConfigGetInt(&gConfig, "Sound.MusicVolume") == 0)
128 {
129 MusicPause(mp);
130 }
131 }
132
133 static bool Play(MusicPlayer *mp, const char *path)
134 {
135 if (!mp->isInitialised)
136 {
137 return true;
138 }
139
140 if (path == NULL || strlen(path) == 0)
141 {
142 LOG(LM_SOUND, LL_WARN, "Attempting to play song with empty name");
143 return false;
144 }
145
146 mp->type = MUSIC_SRC_DYNAMIC;
147 mp->u.dynamic = MusicLoad(path);
148 if (mp->u.dynamic == NULL)
149 {
150 strcpy(mp->errorMessage, SDL_GetError());
151 return false;
152 }
153 mp->errorMessage[0] = '\0';
154 PlayMusic(mp);
155 return true;
156 }
157
158 void MusicPlayGeneral(MusicPlayer *mp, const MusicType type)
159 {
160 MusicStop(mp);
161 mp->type = MUSIC_SRC_GENERAL;
162 CArray *tracks = &mp->generalTracks[type];
163 if (tracks->size == 0)
164 {
165 return;
166 }
167 mp->u.general = *(Mix_Music **)CArrayGet(tracks, 0);
168 // Shuffle tracks
169 if (tracks->size > 1)
170 {
171 while (mp->u.general == *(Mix_Music **)CArrayGet(tracks, 0))
172 {
173 CArrayShuffle(tracks);
174 }
175 }
176 PlayMusic(mp);
177 }
178 void MusicPlayFile(
179 MusicPlayer *mp, const MusicType type, const char *missionPath,
180 const char *music)
181 {
182 MusicStop(mp);
183 bool played = false;
184 if (music != NULL && strlen(music) != 0)
185 {
186 char buf[CDOGS_PATH_MAX];
187 // First, try to play music from the same directory
188 // This may be a new-style directory campaign
189 GetDataFilePath(buf, missionPath);
190 strcat(buf, "/");
191 strcat(buf, music);
192 played = Play(mp, buf);
193 if (!played)
194 {
195 char buf2[CDOGS_PATH_MAX];
196 GetDataFilePath(buf2, missionPath);
197 PathGetDirname(buf, buf2);
198 strcat(buf, music);
199 played = Play(mp, buf);
200 }
201 }
202 if (!played)
203 {
204 MusicPlayGeneral(mp, type);
205 }
206 }
207 void MusicPlayChunk(MusicPlayer *mp, const MusicType type, MusicChunk *chunk)
208 {
209 MusicStop(mp);
210 if (chunk != NULL)
211 {
212 if (chunk->isMusic)
213 {
214 mp->type = MUSIC_SRC_DYNAMIC;
215 mp->u.dynamic = chunk->u.Music;
216 // Mark music as null so it can be dynamically loaded again
217 chunk->u.Music = NULL;
218 PlayMusic(mp);
219 }
220 else
221 {
222 mp->type = MUSIC_SRC_CHUNK;
223 mp->u.chunk.chunk = chunk->u.Chunk;
224 mp->u.chunk.channel = Mix_PlayChannel(-1, chunk->u.Chunk, -1);
225 }
226 }
227 else
228 {
229 MusicPlayGeneral(mp, type);
230 }
231 }
232
233 void MusicStop(MusicPlayer *mp)
234 {
235 switch (mp->type)
236 {
237 case MUSIC_SRC_GENERAL:
238 Mix_HaltMusic();
239 mp->u.general = NULL;
240 break;
241 case MUSIC_SRC_DYNAMIC:
242 Mix_HaltMusic();
243 if (mp->u.dynamic != NULL)
244 {
245 Mix_FreeMusic(mp->u.dynamic);
246 }
247 mp->u.dynamic = NULL;
248 break;
249 case MUSIC_SRC_CHUNK:
250 if (mp->u.chunk.chunk != NULL)
251 {
252 Mix_HaltChannel(mp->u.chunk.channel);
253 }
254 mp->u.chunk.chunk = NULL;
255 break;
256 }
257 }
258
259 void MusicPause(MusicPlayer *mp)
260 {
261 switch (mp->type)
262 {
263 case MUSIC_SRC_GENERAL:
264 case MUSIC_SRC_DYNAMIC:
265 if (Mix_PlayingMusic())
266 {
267 Mix_PauseMusic();
268 }
269 break;
270 case MUSIC_SRC_CHUNK:
271 Mix_Pause(mp->u.chunk.channel);
272 break;
273 }
274 }
275
276 void MusicResume(MusicPlayer *mp)
277 {
278 switch (mp->type)
279 {
280 case MUSIC_SRC_GENERAL:
281 if (mp->u.general == NULL)
282 {
283 return;
284 }
285 if (Mix_PausedMusic())
286 {
287 Mix_ResumeMusic();
288 }
289 else if (!Mix_PlayingMusic())
290 {
291 Mix_PlayMusic(mp->u.general, -1);
292 }
293 break;
294 case MUSIC_SRC_DYNAMIC:
295 if (mp->u.dynamic == NULL)
296 {
297 return;
298 }
299 if (Mix_PausedMusic())
300 {
301 Mix_ResumeMusic();
302 }
303 else if (!Mix_PlayingMusic())
304 {
305 Mix_PlayMusic(mp->u.dynamic, -1);
306 }
307 break;
308 case MUSIC_SRC_CHUNK:
309 Mix_Resume(mp->u.chunk.channel);
310 break;
311 }
312 }
313
314 void MusicSetPlaying(MusicPlayer *mp, const bool isPlaying)
315 {
316 if (isPlaying)
317 {
318 MusicResume(mp);
319 }
320 else
321 {
322 MusicPause(mp);
323 }
324 }
325
326 const char *MusicGetErrorMessage(const MusicPlayer *mp)
327 {
328 return mp->errorMessage;
329 }
330
331 void MusicChunkTerminate(MusicChunk *chunk)
332 {
333 if (!chunk->isMusic && chunk->u.Chunk)
334 {
335 Mix_FreeChunk(chunk->u.Chunk);
336 }
337 CFREE(chunk->Data);
338 memset(chunk, 0, sizeof *chunk);
339 }
340
341 void MusicPlayFromChunk(
342 MusicPlayer *mp, const MusicType type, MusicChunk *chunk)
343 {
344 if ((chunk->isMusic ? chunk->u.Music == NULL : chunk->u.Chunk == NULL) &&
345 chunk->GetData)
346 {
347 chunk->isMusic = chunk->GetData(chunk, chunk->Data);
348 // Note: don't free the data; we need to reload it after it is freed in MusicStop
349 }
350 MusicPlayChunk(mp, type, chunk);
351 }