]> git.jsancho.org Git - lugaru.git/blob - Source/Audio/Sounds.cpp
a259864ccbd2fc1809c6fa4f8bb7c7f12a146a16
[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 void addEnvSound(XYZ coords, float vol, float life)
78 {
79     envsound[numenvsounds] = coords;
80     envsoundvol[numenvsounds] = vol;
81     envsoundlife[numenvsounds] = life;
82     numenvsounds++;
83 }
84
85 void emit_sound_at(int soundid, const XYZ& pos, float vol)
86 {
87     PlaySoundEx(soundid, samp[soundid], NULL, true);
88     OPENAL_3D_SetAttributes_(channels[soundid], pos);
89     OPENAL_SetVolume(channels[soundid], vol);
90     OPENAL_SetPaused(channels[soundid], false);
91 }
92
93 void emit_sound_np(int soundid, float vol)
94 {
95     PlaySoundEx(soundid, samp[soundid], NULL, true);
96     OPENAL_SetVolume(channels[soundid], vol);
97     OPENAL_SetPaused(channels[soundid], false);
98 }
99
100 void emit_stream_at(int soundid, const XYZ& pos, float vol)
101 {
102     PlayStreamEx(soundid, samp[soundid], NULL, true);
103     OPENAL_3D_SetAttributes_(channels[soundid], pos);
104     OPENAL_SetVolume(channels[soundid], vol);
105     OPENAL_SetPaused(channels[soundid], false);
106 }
107
108 void emit_stream_np(int soundid, float vol)
109 {
110     PlayStreamEx(soundid, samp[soundid], NULL, true);
111     OPENAL_SetVolume(channels[soundid], vol);
112     OPENAL_SetPaused(channels[soundid], false);
113 }
114
115 void resume_stream(int soundid)
116 {
117     OPENAL_SetPaused(channels[soundid], false);
118 }
119
120 void pause_sound(int soundid)
121 {
122     OPENAL_SetPaused(channels[soundid], true);
123 }