]> git.jsancho.org Git - lugaru.git/blob - Source/Sounds.cpp
c2d4eb714863b86e334d5a86aa51e0b94318f843
[lugaru.git] / Source / 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 "Quaternions.h"
21 #include "Sounds.h"
22 #include "openal_wrapper.h"
23
24 struct OPENAL_SAMPLE *samp[sounds_count];
25
26 extern XYZ envsound[30];
27 extern float envsoundvol[30];
28 extern int numenvsounds;
29 extern float envsoundlife[30];
30
31 int footstepsound, footstepsound2, footstepsound3, footstepsound4;
32
33 int channels[100];
34
35 static const char *sound_data[sounds_count] = {
36 #define DECLARE_SOUND(id, filename) filename,
37 #include "Sounds.def"
38 #undef DECLARE_SOUND
39 };
40
41 // FIXME: dimensionality is not a property of the sound sample.
42 // This should be decided at the time of playback
43 static int snd_mode(int snd)
44 {
45     switch (snd) {
46     case alarmsound:
47     case consolefailsound:
48     case consolesuccesssound:
49     case firestartsound:
50     case fireendsound:
51         return OPENAL_2D;
52     default:
53         return OPENAL_HW3D;
54     }
55 }
56
57 void loadAllSounds()
58 {
59     for (int i = 0; i < sounds_count; i++) {
60         char buf[64];
61         snprintf(buf, 64, ":Data:Sounds:%s", sound_data[i]);
62         samp[i] = OPENAL_Sample_Load(OPENAL_FREE,
63                                      ConvertFileName(buf),
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, NULL);
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, NULL);
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 }