Debian Codegraph @

engine/sound/music.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name music.cpp - Background music support */
11 //
12 // (c) Copyright 2002-2008 by Lutz Sammer, Nehal Mistry, and Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 /*----------------------------------------------------------------------------
32 -- Includes
33 ----------------------------------------------------------------------------*/
34
35 #include "stratagus.h"
36
37 #include <stdio.h>
38
39 #include "SDL.h"
40
41 #include "sound_server.h"
42 #include "script.h"
43
44 /*----------------------------------------------------------------------------
45 -- Declaration
46 ----------------------------------------------------------------------------*/
47
48 #define SoundFrequency 44100 // sample rate of dsp
49
50 /*----------------------------------------------------------------------------
51 -- Variables
52 ----------------------------------------------------------------------------*/
53
54 static SDL_mutex *MusicFinishedMutex; /// Mutex for MusicFinished
55 static bool MusicFinished; /// Music ended and we need a new file
56
57 bool CallbackMusic; /// flag true callback ccl if stops
58
59 /*----------------------------------------------------------------------------
60 -- Functions
61 ----------------------------------------------------------------------------*/
62
63 /**
64 ** Callback for when music has finished
65 ** Note: we are in the sdl audio thread
66 */
67 static void MusicFinishedCallback(void)
68 {
69 SDL_LockMutex(MusicFinishedMutex);
70 MusicFinished = true;
71 SDL_UnlockMutex(MusicFinishedMutex);
72 }
73
74 /**
75 ** Check if music is finished and play the next song
76 */
77 void CheckMusicFinished(bool force)
78 {
79 bool proceed;
80
81 SDL_LockMutex(MusicFinishedMutex);
82 proceed = MusicFinished;
83 MusicFinished = false;
84 SDL_UnlockMutex(MusicFinishedMutex);
85
86 if ((proceed || force) && SoundEnabled() && IsMusicEnabled() && CallbackMusic) {
87 lua_pushstring(Lua, "MusicStopped");
88 lua_gettable(Lua, LUA_GLOBALSINDEX);
89 if (!lua_isfunction(Lua, -1)) {
90 fprintf(stderr, "No MusicStopped function in Lua\n");
91 StopMusic();
92 } else {
93 LuaCall(0, 1);
94 }
95 }
96 }
97
98 /**
99 ** Init music
100 */
101 void InitMusic(void)
102 {
103 MusicFinished = false;
104 MusicFinishedMutex = SDL_CreateMutex();
105 SetMusicFinishedCallback(MusicFinishedCallback);
106 }
107
108 //@}