]> git.jsancho.org Git - lugaru.git/blob - Source/Audio/Sounds.cpp
Added braces to all statements with clang-tidy and ran clang-format again
[lugaru.git] / Source / Audio / Sounds.cpp
1 /*
2 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "Audio/Sounds.hpp"
21
22 #include "Audio/openal_wrapper.hpp"
23 #include "Utils/Folders.hpp"
24
25 struct OPENAL_SAMPLE* samp[sounds_count];
26
27 extern XYZ envsound[30];
28 extern float envsoundvol[30];
29 extern int numenvsounds;
30 extern float envsoundlife[30];
31
32 int footstepsound, footstepsound2, footstepsound3, footstepsound4;
33
34 int channels[100];
35
36 static const char* sound_data[sounds_count] = {
37 #define DECLARE_SOUND(id, filename) filename,
38 #include "Sounds.def"
39 #undef DECLARE_SOUND
40 };
41
42 // FIXME: dimensionality is not a property of the sound sample.
43 // This should be decided at the time of playback
44 static int snd_mode(int snd)
45 {
46     switch (snd) {
47         case alarmsound:
48         case consolefailsound:
49         case consolesuccesssound:
50         case firestartsound:
51         case fireendsound:
52             return OPENAL_2D;
53         default:
54             return OPENAL_HW3D;
55     }
56 }
57
58 void loadAllSounds()
59 {
60     for (int i = 0; i < sounds_count; i++) {
61         std::string buf = std::string("Sounds/") + sound_data[i];
62         samp[i] = OPENAL_Sample_Load(OPENAL_FREE,
63                                      Folders::getResourcePath(buf).c_str(),
64                                      snd_mode(i),
65                                      0, 0);
66     }
67     footstepsound = footstepsn1;
68     footstepsound2 = footstepsn2;
69     footstepsound3 = footstepst1;
70     footstepsound4 = footstepst2;
71     // Huh?
72     // OPENAL_Sample_SetMode(samp[whooshsound], OPENAL_LOOP_NORMAL);
73     for (int i = stream_firesound; i <= stream_menutheme; i++) {
74         OPENAL_Stream_SetMode(samp[i], OPENAL_LOOP_NORMAL);
75     }
76 }
77
78 void addEnvSound(XYZ coords, float vol, float life)
79 {
80     envsound[numenvsounds] = coords;
81     envsoundvol[numenvsounds] = vol;
82     envsoundlife[numenvsounds] = life;
83     numenvsounds++;
84 }
85
86 void emit_sound_at(int soundid, const XYZ& pos, float vol)
87 {
88     PlaySoundEx(soundid, samp[soundid], NULL, true);
89     OPENAL_3D_SetAttributes_(channels[soundid], pos);
90     OPENAL_SetVolume(channels[soundid], vol);
91     OPENAL_SetPaused(channels[soundid], false);
92 }
93
94 void emit_sound_np(int soundid, float vol)
95 {
96     PlaySoundEx(soundid, samp[soundid], NULL, true);
97     OPENAL_SetVolume(channels[soundid], vol);
98     OPENAL_SetPaused(channels[soundid], false);
99 }
100
101 void emit_stream_at(int soundid, const XYZ& pos, float vol)
102 {
103     PlayStreamEx(soundid, samp[soundid], NULL, true);
104     OPENAL_3D_SetAttributes_(channels[soundid], pos);
105     OPENAL_SetVolume(channels[soundid], vol);
106     OPENAL_SetPaused(channels[soundid], false);
107 }
108
109 void emit_stream_np(int soundid, float vol)
110 {
111     PlayStreamEx(soundid, samp[soundid], NULL, true);
112     OPENAL_SetVolume(channels[soundid], vol);
113     OPENAL_SetPaused(channels[soundid], false);
114 }
115
116 void resume_stream(int soundid)
117 {
118     OPENAL_SetPaused(channels[soundid], false);
119 }
120
121 void pause_sound(int soundid)
122 {
123     OPENAL_SetPaused(channels[soundid], true);
124 }