File: JSInterface_Sound.cpp

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (154 lines) | stat: -rw-r--r-- 4,687 bytes parent folder | download | duplicates (2)
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
/* Copyright (C) 2021 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. 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 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. 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 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "precompiled.h"

#include "JSInterface_Sound.h"

#include "lib/config2.h"
#include "lib/utf8.h"
#include "maths/Vector3D.h"
#include "ps/Filesystem.h"
#include "scriptinterface/FunctionWrapper.h"
#include "scriptinterface/ScriptRequest.h"
#include "soundmanager/SoundManager.h"

#include <sstream>

namespace JSI_Sound
{
#if CONFIG2_AUDIO

	void StartMusic()
	{
		if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager)
			sndManager->SetMusicEnabled(true);
	}

	void StopMusic()
	{
		if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager)
			sndManager->SetMusicEnabled(false);
	}

	void ClearPlaylist()
	{
		if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager)
			sndManager->ClearPlayListItems();
	}

	void AddPlaylistItem(const std::wstring& filename)
	{
		if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager)
			sndManager->AddPlayListItem(VfsPath(filename));
	}

	void StartPlaylist(bool looping)
	{
		if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager)
			sndManager->StartPlayList(looping );
	}

	void PlayMusic(const std::wstring& filename, bool looping)
	{
		if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager)
			sndManager->PlayAsMusic(filename, looping);
	}

	void PlayUISound(const std::wstring& filename, bool looping)
	{
		if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager)
			sndManager->PlayAsUI(filename, looping);
	}

	void PlayAmbientSound(const std::wstring& filename, bool looping)
	{
		if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager)
			sndManager->PlayAsAmbient(filename, looping);
	}

	bool MusicPlaying()
	{
		return true;
	}

	void SetMasterGain(float gain)
	{
		if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager)
			sndManager->SetMasterGain(gain);
	}

	void SetMusicGain(float gain)
	{
		if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager)
			sndManager->SetMusicGain(gain);
	}

	void SetAmbientGain(float gain)
	{
		if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager)
			sndManager->SetAmbientGain(gain);
	}

	void SetActionGain(float gain)
	{
		if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager)
			sndManager->SetActionGain(gain);
	}

	void SetUIGain(float gain)
	{
		if (CSoundManager* sndManager = (CSoundManager*)g_SoundManager)
			sndManager->SetUIGain(gain);
	}

#else

	bool MusicPlaying( ){ return false; }
	void PlayAmbientSound(const std::wstring& UNUSED(filename), bool UNUSED(looping) ){}
	void PlayUISound(const std::wstring& UNUSED(filename), bool UNUSED(looping) ) {}
	void PlayMusic(const std::wstring& UNUSED(filename), bool UNUSED(looping) ) {}
	void StartPlaylist(bool UNUSED(looping) ){}
	void AddPlaylistItem(const std::wstring& UNUSED(filename) ){}
	void ClearPlaylist( ){}
	void StopMusic( ){}
	void StartMusic( ){}
	void SetMasterGain(float UNUSED(gain)){}
	void SetMusicGain(float UNUSED(gain)){}
	void SetAmbientGain(float UNUSED(gain)){}
	void SetActionGain(float UNUSED(gain)){}
	void SetUIGain(float UNUSED(gain)){}

#endif

	void RegisterScriptFunctions(const ScriptRequest& rq)
	{
		ScriptFunction::Register<&StartMusic>(rq, "StartMusic");
		ScriptFunction::Register<&StopMusic>(rq, "StopMusic");
		ScriptFunction::Register<&ClearPlaylist>(rq, "ClearPlaylist");
		ScriptFunction::Register<&AddPlaylistItem>(rq, "AddPlaylistItem");
		ScriptFunction::Register<&StartPlaylist>(rq, "StartPlaylist");
		ScriptFunction::Register<&PlayMusic>(rq, "PlayMusic");
		ScriptFunction::Register<&PlayUISound>(rq, "PlayUISound");
		ScriptFunction::Register<&PlayAmbientSound>(rq, "PlayAmbientSound");
		ScriptFunction::Register<&MusicPlaying>(rq, "MusicPlaying");
		ScriptFunction::Register<&SetMasterGain>(rq, "SetMasterGain");
		ScriptFunction::Register<&SetMusicGain>(rq, "SetMusicGain");
		ScriptFunction::Register<&SetAmbientGain>(rq, "SetAmbientGain");
		ScriptFunction::Register<&SetActionGain>(rq, "SetActionGain");
		ScriptFunction::Register<&SetUIGain>(rq, "SetUIGain");
	}
}