Pārlūkot izejas kodu

Sound interface now abstract

Thomas Buck 10 gadus atpakaļ
vecāks
revīzija
780989b3c7
7 mainītis faili ar 410 papildinājumiem un 289 dzēšanām
  1. 12
    25
      include/Sound.h
  2. 112
    0
      include/SoundAL.h
  3. 10
    2
      src/CMakeLists.txt
  4. 1
    1
      src/Game.cpp
  5. 0
    260
      src/Sound.cpp
  6. 272
    0
      src/SoundAL.cpp
  7. 3
    1
      src/main.cpp

+ 12
- 25
include/Sound.h Parādīt failu

@@ -9,8 +9,6 @@
9 9
 #ifndef _SOUND_H_
10 10
 #define _SOUND_H_
11 11
 
12
-#include <vector>
13
-
14 12
 /*!
15 13
  * \brief This is the audio manager for OpenRaider
16 14
  */
@@ -23,53 +21,48 @@ public:
23 21
     } SoundFlags;
24 22
 
25 23
     /*!
26
-     * \brief Constructs an object of Sound
27
-     */
28
-    Sound();
29
-
30
-    /*!
31 24
      * \brief Deconstructs an object of Sound
32 25
      */
33
-    ~Sound();
26
+    virtual ~Sound();
34 27
 
35 28
     /*!
36 29
      * \brief Initialize sound system
37 30
      * \returns 0 on success or < 0 error flags
38 31
      */
39
-    int initialize();
32
+    virtual int initialize() = 0;
40 33
 
41
-    void setEnabled(bool on);
34
+    virtual void setEnabled(bool on) = 0;
42 35
 
43 36
     /*!
44 37
      * \brief Set the volume
45 38
      * \param vol new source gain
46 39
      */
47
-    void setVolume(float vol);
40
+    virtual void setVolume(float vol) = 0;
48 41
 
49 42
     /*!
50 43
      * \brief Get number of registered sources
51 44
      * \returns number of registered sources
52 45
      */
53
-    int registeredSources();
46
+    virtual int registeredSources() = 0;
54 47
 
55 48
     /*!
56 49
      * \brief Remove all loaded sounds
57 50
      */
58
-    void clear();
51
+    virtual void clear() = 0;
59 52
 
60 53
     /*!
61 54
      * \brief Move listener and repositions them
62 55
      * \param pos New position for listener
63 56
      * \param angle New orientation for listener
64 57
      */
65
-    void listenAt(float pos[3], float angle[3]);
58
+    virtual void listenAt(float pos[3], float angle[3]) = 0;
66 59
 
67 60
     /*!
68 61
      * \brief Move sound source to position
69 62
      * \param source valid source id
70 63
      * \param pos new position for source
71 64
      */
72
-    void sourceAt(int source, float pos[3]);
65
+    virtual void sourceAt(int source, float pos[3]) = 0;
73 66
 
74 67
     /*!
75 68
      * \brief Load wav file from disk
@@ -78,7 +71,7 @@ public:
78 71
      * \param flags set options. Use SoundFlags enum bitwise OR-ed
79 72
      * \returns 0 for no error or < 0 error flag
80 73
      */
81
-    int addFile(const char *filename, int *source, unsigned int flags);
74
+    virtual int addFile(const char *filename, int *source, unsigned int flags) = 0;
82 75
 
83 76
     /*!
84 77
      * \brief Load wav file from buffer
@@ -88,27 +81,21 @@ public:
88 81
      * \param flags set options. Use SoundFlags enum bitwise OR-ed
89 82
      * \returns 0 for no error or < 0 error flag
90 83
      */
91
-    int addWave(unsigned char *wav, unsigned int length, int *source, unsigned int flags);
84
+    virtual int addWave(unsigned char *wav, unsigned int length, int *source, unsigned int flags) = 0;
92 85
 
93 86
     /*!
94 87
      * \brief Play sound source
95 88
      * \param source sound source to play
96 89
      */
97
-    void play(int source);
90
+    virtual void play(int source) = 0;
98 91
 
99 92
     /*!
100 93
      * \brief Stop playing sound source
101 94
      * \param source sound source to stop
102 95
      */
103
-    void stop(int source);
96
+    virtual void stop(int source) = 0;
104 97
 
105 98
 private:
106
-
107
-    bool mEnabled;
108
-    bool mInit;                        //!< Guard to ensure ausio system is active
109
-    float mVolume;                     //!< Listener gain
110
-    std::vector<unsigned int> mBuffer; //!< Audio buffer id list
111
-    std::vector<unsigned int> mSource; //!< Audio source id list
112 99
 };
113 100
 
114 101
 #endif

+ 112
- 0
include/SoundAL.h Parādīt failu

@@ -0,0 +1,112 @@
1
+/*!
2
+ * \file include/SoundAL.h
3
+ * \brief This is the OpenAL audio manager Header
4
+ *
5
+ * \author Mongoose
6
+ * \author xythobuz
7
+ */
8
+
9
+#ifndef _SOUND_AL_H_
10
+#define _SOUND_AL_H_
11
+
12
+#include <vector>
13
+
14
+#include "Sound.h"
15
+
16
+/*!
17
+ * \brief This is the OpenAL audio manager
18
+ */
19
+class SoundAL : public Sound {
20
+public:
21
+
22
+    /*!
23
+     * \brief Constructs an object of SoundAL
24
+     */
25
+    SoundAL();
26
+
27
+    /*!
28
+     * \brief Deconstructs an object of SoundAL
29
+     */
30
+    virtual ~SoundAL();
31
+
32
+    /*!
33
+     * \brief Initialize sound system
34
+     * \returns 0 on success or < 0 error flags
35
+     */
36
+    virtual int initialize();
37
+
38
+    virtual void setEnabled(bool on);
39
+
40
+    /*!
41
+     * \brief Set the volume
42
+     * \param vol new source gain
43
+     */
44
+    virtual void setVolume(float vol);
45
+
46
+    /*!
47
+     * \brief Get number of registered sources
48
+     * \returns number of registered sources
49
+     */
50
+    virtual int registeredSources();
51
+
52
+    /*!
53
+     * \brief Remove all loaded sounds
54
+     */
55
+    virtual void clear();
56
+
57
+    /*!
58
+     * \brief Move listener and repositions them
59
+     * \param pos New position for listener
60
+     * \param angle New orientation for listener
61
+     */
62
+    virtual void listenAt(float pos[3], float angle[3]);
63
+
64
+    /*!
65
+     * \brief Move sound source to position
66
+     * \param source valid source id
67
+     * \param pos new position for source
68
+     */
69
+    virtual void sourceAt(int source, float pos[3]);
70
+
71
+    /*!
72
+     * \brief Load wav file from disk
73
+     * \param filename not NULL!
74
+     * \param source not NULL! Returns new source ID or -1 on error.
75
+     * \param flags set options. Use SoundFlags enum bitwise OR-ed
76
+     * \returns 0 for no error or < 0 error flag
77
+     */
78
+    virtual int addFile(const char *filename, int *source, unsigned int flags);
79
+
80
+    /*!
81
+     * \brief Load wav file from buffer
82
+     * \param wav not NULL! Is a valid waveform buffer!
83
+     * \param length length of wav buffer
84
+     * \param source not NULL! Returns new source ID or -1 on error.
85
+     * \param flags set options. Use SoundFlags enum bitwise OR-ed
86
+     * \returns 0 for no error or < 0 error flag
87
+     */
88
+    virtual int addWave(unsigned char *wav, unsigned int length, int *source, unsigned int flags);
89
+
90
+    /*!
91
+     * \brief Play sound source
92
+     * \param source sound source to play
93
+     */
94
+    virtual void play(int source);
95
+
96
+    /*!
97
+     * \brief Stop playing sound source
98
+     * \param source sound source to stop
99
+     */
100
+    virtual void stop(int source);
101
+
102
+private:
103
+
104
+    bool mEnabled;
105
+    bool mInit;                        //!< Guard to ensure ausio system is active
106
+    float mVolume;                     //!< Listener gain
107
+    std::vector<unsigned int> mBuffer; //!< Audio buffer id list
108
+    std::vector<unsigned int> mSource; //!< Audio source id list
109
+};
110
+
111
+#endif
112
+

+ 10
- 2
src/CMakeLists.txt Parādīt failu

@@ -14,12 +14,12 @@ include_directories (SYSTEM ${OPENGL_INCLUDE_DIRS})
14 14
 set (LIBS ${LIBS} ${OPENGL_LIBRARIES})
15 15
 
16 16
 # Add OpenAL Library
17
-find_package (OpenAL REQUIRED)
17
+find_package (OpenAL)
18 18
 include_directories (SYSTEM ${OPENAL_INCLUDE_DIRS})
19 19
 set (LIBS ${LIBS} ${OPENAL_LIBRARIES})
20 20
 
21 21
 # Add ALUT Library
22
-find_package (ALUT REQUIRED)
22
+find_package (ALUT)
23 23
 include_directories (SYSTEM ${ALUT_INCLUDE_DIRS})
24 24
 set (LIBS ${LIBS} ${ALUT_LIBRARIES})
25 25
 
@@ -49,6 +49,14 @@ set (SRCS ${SRCS} "ViewVolume.cpp")
49 49
 set (SRCS ${SRCS} "Window.cpp")
50 50
 set (SRCS ${SRCS} "World.cpp")
51 51
 
52
+# Select available Sound library
53
+if (OPENAL_FOUND AND ALUT_FOUND)
54
+    set (SRCS ${SRCS} "SoundAL.cpp")
55
+else (OPENAL_FOUND AND ALUT_FOUND)
56
+    # Currently only OpenAL support
57
+    message (FATAL_ERROR "OpenAL and ALUT are required!")
58
+endif (OPENAL_FOUND AND ALUT_FOUND)
59
+
52 60
 # Select available Windowing library
53 61
 if (SDL2_FOUND AND SDL2TTF_FOUND)
54 62
     set (SRCS ${SRCS} "WindowSDL.cpp")

+ 1
- 1
src/Game.cpp Parādīt failu

@@ -377,7 +377,7 @@ void Game::processPakSounds()
377 377
     {
378 378
         mTombRaider.getSoundSample(i, &riffSz, &riff);
379 379
 
380
-        getSound().addWave(riff, riffSz, &id, getSound().SoundFlagsNone);
380
+        getSound().addWave(riff, riffSz, &id, Sound::SoundFlagsNone);
381 381
 
382 382
         //if (((i + 1) == TR_SOUND_F_PISTOL) && (id > 0))
383 383
         //{

+ 0
- 260
src/Sound.cpp Parādīt failu

@@ -2,271 +2,11 @@
2 2
  * \file src/Sound.cpp
3 3
  * \brief This is the audio manager Implementation
4 4
  *
5
- * \author Mongoose
6 5
  * \author xythobuz
7 6
  */
8 7
 
9
-#ifdef __APPLE__
10
-#include <OpenAL/al.h>
11
-#else
12
-#include <AL/al.h>
13
-#include <fcntl.h>
14
-#include <unistd.h>
15
-#endif
16
-
17
-#include <AL/alut.h>
18
-
19
-#include <cstdio>
20
-#include <cstdlib>
21
-#include <assert.h>
22
-
23
-#include "math/math.h"
24 8
 #include "Sound.h"
25 9
 
26
-Sound::Sound() {
27
-    mEnabled = false;
28
-    mInit = false;
29
-    mVolume = 1.0f;
30
-}
31
-
32 10
 Sound::~Sound() {
33
-    if (mInit)
34
-        alutExit();
35
-}
36
-
37
-int Sound::initialize() {
38
-    assert(mInit == false);
39
-
40
-    if (!mEnabled)
41
-        return 0;
42
-
43
-#ifndef __APPLE__
44
-    int fd = open("/dev/dsp", O_RDWR);
45
-    if (fd < 0) {
46
-        printf("Could not initialize Sound (/dev/dsp)\n");
47
-        return -1;
48
-    }
49
-    close(fd);
50
-#endif
51
-
52
-    ALCdevice *Device = alcOpenDevice("OSS");
53
-    ALCcontext *Context = alcCreateContext(Device, NULL);
54
-    alcMakeContextCurrent(Context);
55
-
56
-    if (alutInitWithoutContext(NULL, NULL) == AL_FALSE) {
57
-        printf("ALUT-Error: %s\n", alutGetErrorString(alutGetError()));
58
-        return -2;
59
-    }
60
-
61
-    mInit = true;
62
-
63
-    return 0;
64
-}
65
-
66
-void Sound::setEnabled(bool on) {
67
-    mEnabled = on;
68
-}
69
-
70
-void Sound::setVolume(float vol) {
71
-    assert(mSource.size() == mBuffer.size());
72
-
73
-    if (mEnabled) {
74
-        if ((mSource.size() > 0) && (!equalEpsilon(mVolume, vol))) {
75
-            // Apply new volume to old sources if needed
76
-            for (size_t i = 0; i < mSource.size(); i++)
77
-                alSourcef(mSource[i], AL_GAIN, vol);
78
-        }
79
-    }
80
-
81
-    mVolume = vol;
82
-}
83
-
84
-int Sound::registeredSources() {
85
-    assert(mSource.size() == mBuffer.size());
86
-
87
-    if ((!mEnabled) || (!mInit))
88
-        return 0;
89
-
90
-    return mSource.size();
91
-}
92
-
93
-void Sound::clear() {
94
-    assert(mSource.size() == mBuffer.size());
95
-
96
-    if ((!mEnabled) || (!mInit))
97
-        return;
98
-
99
-    for (size_t i = 0; i < mSource.size(); i++) {
100
-        alDeleteSources(1, &mSource[i]);
101
-        alDeleteBuffers(1, &mBuffer[i]);
102
-    }
103
-
104
-    mSource.clear();
105
-    mBuffer.clear();
106
-}
107
-
108
-void Sound::listenAt(float pos[3], float angle[3]) {
109
-    assert(mSource.size() == mBuffer.size());
110
-    assert(pos != NULL);
111
-    assert(angle != NULL);
112
-
113
-    if ((!mEnabled) || (!mInit))
114
-        return;
115
-
116
-    alListenerfv(AL_POSITION, pos);
117
-    alListenerfv(AL_ORIENTATION, angle);
118
-}
119
-
120
-void Sound::sourceAt(int source, float pos[3]) {
121
-    assert(mSource.size() == mBuffer.size());
122
-    assert(source >= 0);
123
-    assert(source < (int)mSource.size());
124
-    assert(pos != NULL);
125
-
126
-    if ((!mEnabled) || (!mInit))
127
-        return;
128
-
129
-    alSourcefv(mSource[source], AL_POSITION, pos);
130
-}
131
-
132
-//! \fixme Seperate sourcing and buffering, Mongoose 2002.01.04
133
-int Sound::addFile(const char *filename, int *source, unsigned int flags) {
134
-    ALsizei size;
135
-    ALfloat freq;
136
-    ALenum format;
137
-    ALvoid *data;
138
-    int id;
139
-
140
-    assert(mSource.size() == mBuffer.size());
141
-    assert(filename != NULL);
142
-    assert(filename[0] != '\0');
143
-    assert(source != NULL);
144
-
145
-    if ((!mEnabled) || (!mInit)) {
146
-        *source = 0;
147
-        return 0;
148
-    }
149
-
150
-    *source = -1;
151
-    id = mSource.size();
152
-    
153
-    mSource.push_back(0);
154
-    mBuffer.push_back(0);
155
-
156
-    alGetError();
157
-    alGenBuffers(1, &mBuffer[id]);
158
-    if (alGetError() != AL_NO_ERROR) {
159
-        printf("Could not load wav file (alGenBuffers)\n");
160
-        return -1;
161
-    }
162
-
163
-    alGetError();
164
-    alGenSources(1, &mSource[id]);
165
-    if (alGetError() != AL_NO_ERROR) {
166
-        printf("Could not load wav file (alGenSources)\n");
167
-        return -2;
168
-    }
169
-
170
-    data = alutLoadMemoryFromFile(filename, &format, &size, &freq);
171
-    if (alutGetError() != ALUT_ERROR_NO_ERROR) {
172
-        printf("Could not load %s\n", filename);
173
-        return -3;
174
-    }
175
-
176
-    alBufferData(mBuffer[id], format, data, size, static_cast<ALsizei>(freq));
177
-    alSourcei(mSource[id], AL_BUFFER, mBuffer[id]);
178
-
179
-    if (flags & SoundFlagsLoop) {
180
-        alSourcei(mSource[id], AL_LOOPING, 1);
181
-    }
182
-
183
-    alSourcef(mSource[id], AL_GAIN, mVolume);
184
-
185
-    *source = id;
186
-
187
-    return 0;
188
-}
189
-
190
-int Sound::addWave(unsigned char *wav, unsigned int length, int *source, unsigned int flags) {
191
-    ALsizei size;
192
-    ALfloat freq;
193
-    ALenum format;
194
-    ALvoid *data;
195
-    int error = 0;
196
-    int id;
197
-
198
-    assert(mSource.size() == mBuffer.size());
199
-    assert(wav != NULL);
200
-    assert(source != NULL);
201
-
202
-    if ((!mEnabled) || (!mInit)) {
203
-        *source = 0;
204
-        return 0;
205
-    }
206
-
207
-    *source = -1;
208
-    id = mSource.size();
209
-    
210
-    mSource.push_back(0);
211
-    mBuffer.push_back(0);
212
-
213
-    data = wav;
214
-
215
-    alGetError();
216
-    alGenBuffers(1, &mBuffer[id]);
217
-    if (alGetError() != AL_NO_ERROR) {
218
-        printf("Could not load wav (alGenBuffers)\n");
219
-        return -1;
220
-    }
221
-
222
-    alGetError();
223
-    alGenSources(1, &mSource[id]);
224
-    if (alGetError() != AL_NO_ERROR) {
225
-        printf("Could not load wav (alGenSources)\n");
226
-        return -2;
227
-    }
228
-
229
-    data = alutLoadMemoryFromFileImage(wav, length, &format, &size, &freq);
230
-    if (((error = alutGetError()) != ALUT_ERROR_NO_ERROR) || (data == NULL)) {
231
-        printf("Could not load wav buffer (%s)\n", alutGetErrorString(error));
232
-        return -3;
233
-    }
234
-
235
-    alBufferData(mBuffer[id], format, data, size, static_cast<ALsizei>(freq));
236
-    alSourcei(mSource[id], AL_BUFFER, mBuffer[id]);
237
-
238
-    if (flags & SoundFlagsLoop) {
239
-        alSourcei(mSource[id], AL_LOOPING, 1);
240
-    }
241
-
242
-    alSourcef(mSource[id], AL_GAIN, mVolume);
243
-
244
-    *source = id;
245
-
246
-    //! \fixme Should free alut buffer?
247
-
248
-    return 0;
249
-}
250
-
251
-void Sound::play(int source) {
252
-    assert(mSource.size() == mBuffer.size());
253
-    assert(source >= 0);
254
-    assert(source < (int)mSource.size());
255
-
256
-    if ((!mEnabled) || (!mInit))
257
-        return;
258
-
259
-    alSourcePlay(mSource[source]);
260
-}
261
-
262
-void Sound::stop(int source) {
263
-    assert(mSource.size() == mBuffer.size());
264
-    assert(source >= 0);
265
-    assert(source < (int)mSource.size());
266
-
267
-    if ((!mEnabled) || (!mInit))
268
-        return;
269
-
270
-    alSourceStop(mSource[source]);
271 11
 }
272 12
 

+ 272
- 0
src/SoundAL.cpp Parādīt failu

@@ -0,0 +1,272 @@
1
+/*!
2
+ * \file src/SoundAL.cpp
3
+ * \brief This is the OpenAL audio manager Implementation
4
+ *
5
+ * \author Mongoose
6
+ * \author xythobuz
7
+ */
8
+
9
+#ifdef __APPLE__
10
+#include <OpenAL/al.h>
11
+#else
12
+#include <AL/al.h>
13
+#include <fcntl.h>
14
+#include <unistd.h>
15
+#endif
16
+
17
+#include <AL/alut.h>
18
+
19
+#include <cstdio>
20
+#include <cstdlib>
21
+#include <assert.h>
22
+
23
+#include "math/math.h"
24
+#include "SoundAL.h"
25
+
26
+SoundAL::SoundAL() {
27
+    mEnabled = false;
28
+    mInit = false;
29
+    mVolume = 1.0f;
30
+}
31
+
32
+SoundAL::~SoundAL() {
33
+    if (mInit)
34
+        alutExit();
35
+}
36
+
37
+int SoundAL::initialize() {
38
+    assert(mInit == false);
39
+
40
+    if (!mEnabled)
41
+        return 0;
42
+
43
+#ifndef __APPLE__
44
+    int fd = open("/dev/dsp", O_RDWR);
45
+    if (fd < 0) {
46
+        printf("Could not initialize OpenAL (/dev/dsp)\n");
47
+        return -1;
48
+    }
49
+    close(fd);
50
+#endif
51
+
52
+    ALCdevice *Device = alcOpenDevice("OSS");
53
+    ALCcontext *Context = alcCreateContext(Device, NULL);
54
+    alcMakeContextCurrent(Context);
55
+
56
+    if (alutInitWithoutContext(NULL, NULL) == AL_FALSE) {
57
+        printf("ALUT-Error: %s\n", alutGetErrorString(alutGetError()));
58
+        return -2;
59
+    }
60
+
61
+    mInit = true;
62
+
63
+    return 0;
64
+}
65
+
66
+void SoundAL::setEnabled(bool on) {
67
+    mEnabled = on;
68
+}
69
+
70
+void SoundAL::setVolume(float vol) {
71
+    assert(mSource.size() == mBuffer.size());
72
+
73
+    if (mEnabled) {
74
+        if ((mSource.size() > 0) && (!equalEpsilon(mVolume, vol))) {
75
+            // Apply new volume to old sources if needed
76
+            for (size_t i = 0; i < mSource.size(); i++)
77
+                alSourcef(mSource[i], AL_GAIN, vol);
78
+        }
79
+    }
80
+
81
+    mVolume = vol;
82
+}
83
+
84
+int SoundAL::registeredSources() {
85
+    assert(mSource.size() == mBuffer.size());
86
+
87
+    if ((!mEnabled) || (!mInit))
88
+        return 0;
89
+
90
+    return mSource.size();
91
+}
92
+
93
+void SoundAL::clear() {
94
+    assert(mSource.size() == mBuffer.size());
95
+
96
+    if ((!mEnabled) || (!mInit))
97
+        return;
98
+
99
+    for (size_t i = 0; i < mSource.size(); i++) {
100
+        alDeleteSources(1, &mSource[i]);
101
+        alDeleteBuffers(1, &mBuffer[i]);
102
+    }
103
+
104
+    mSource.clear();
105
+    mBuffer.clear();
106
+}
107
+
108
+void SoundAL::listenAt(float pos[3], float angle[3]) {
109
+    assert(mSource.size() == mBuffer.size());
110
+    assert(pos != NULL);
111
+    assert(angle != NULL);
112
+
113
+    if ((!mEnabled) || (!mInit))
114
+        return;
115
+
116
+    alListenerfv(AL_POSITION, pos);
117
+    alListenerfv(AL_ORIENTATION, angle);
118
+}
119
+
120
+void SoundAL::sourceAt(int source, float pos[3]) {
121
+    assert(mSource.size() == mBuffer.size());
122
+    assert(source >= 0);
123
+    assert(source < (int)mSource.size());
124
+    assert(pos != NULL);
125
+
126
+    if ((!mEnabled) || (!mInit))
127
+        return;
128
+
129
+    alSourcefv(mSource[source], AL_POSITION, pos);
130
+}
131
+
132
+//! \fixme Seperate sourcing and buffering, Mongoose 2002.01.04
133
+int SoundAL::addFile(const char *filename, int *source, unsigned int flags) {
134
+    ALsizei size;
135
+    ALfloat freq;
136
+    ALenum format;
137
+    ALvoid *data;
138
+    int id;
139
+
140
+    assert(mSource.size() == mBuffer.size());
141
+    assert(filename != NULL);
142
+    assert(filename[0] != '\0');
143
+    assert(source != NULL);
144
+
145
+    if ((!mEnabled) || (!mInit)) {
146
+        *source = 0;
147
+        return 0;
148
+    }
149
+
150
+    *source = -1;
151
+    id = mSource.size();
152
+
153
+    mSource.push_back(0);
154
+    mBuffer.push_back(0);
155
+
156
+    alGetError();
157
+    alGenBuffers(1, &mBuffer[id]);
158
+    if (alGetError() != AL_NO_ERROR) {
159
+        printf("Could not load wav file (alGenBuffers)\n");
160
+        return -1;
161
+    }
162
+
163
+    alGetError();
164
+    alGenSources(1, &mSource[id]);
165
+    if (alGetError() != AL_NO_ERROR) {
166
+        printf("Could not load wav file (alGenSources)\n");
167
+        return -2;
168
+    }
169
+
170
+    data = alutLoadMemoryFromFile(filename, &format, &size, &freq);
171
+    if (alutGetError() != ALUT_ERROR_NO_ERROR) {
172
+        printf("Could not load %s\n", filename);
173
+        return -3;
174
+    }
175
+
176
+    alBufferData(mBuffer[id], format, data, size, static_cast<ALsizei>(freq));
177
+    alSourcei(mSource[id], AL_BUFFER, mBuffer[id]);
178
+
179
+    if (flags & SoundFlagsLoop) {
180
+        alSourcei(mSource[id], AL_LOOPING, 1);
181
+    }
182
+
183
+    alSourcef(mSource[id], AL_GAIN, mVolume);
184
+
185
+    *source = id;
186
+
187
+    return 0;
188
+}
189
+
190
+int SoundAL::addWave(unsigned char *wav, unsigned int length, int *source, unsigned int flags) {
191
+    ALsizei size;
192
+    ALfloat freq;
193
+    ALenum format;
194
+    ALvoid *data;
195
+    int error = 0;
196
+    int id;
197
+
198
+    assert(mSource.size() == mBuffer.size());
199
+    assert(wav != NULL);
200
+    assert(source != NULL);
201
+
202
+    if ((!mEnabled) || (!mInit)) {
203
+        *source = 0;
204
+        return 0;
205
+    }
206
+
207
+    *source = -1;
208
+    id = mSource.size();
209
+
210
+    mSource.push_back(0);
211
+    mBuffer.push_back(0);
212
+
213
+    data = wav;
214
+
215
+    alGetError();
216
+    alGenBuffers(1, &mBuffer[id]);
217
+    if (alGetError() != AL_NO_ERROR) {
218
+        printf("Could not load wav (alGenBuffers)\n");
219
+        return -1;
220
+    }
221
+
222
+    alGetError();
223
+    alGenSources(1, &mSource[id]);
224
+    if (alGetError() != AL_NO_ERROR) {
225
+        printf("Could not load wav (alGenSources)\n");
226
+        return -2;
227
+    }
228
+
229
+    data = alutLoadMemoryFromFileImage(wav, length, &format, &size, &freq);
230
+    if (((error = alutGetError()) != ALUT_ERROR_NO_ERROR) || (data == NULL)) {
231
+        printf("Could not load wav buffer (%s)\n", alutGetErrorString(error));
232
+        return -3;
233
+    }
234
+
235
+    alBufferData(mBuffer[id], format, data, size, static_cast<ALsizei>(freq));
236
+    alSourcei(mSource[id], AL_BUFFER, mBuffer[id]);
237
+
238
+    if (flags & SoundFlagsLoop) {
239
+        alSourcei(mSource[id], AL_LOOPING, 1);
240
+    }
241
+
242
+    alSourcef(mSource[id], AL_GAIN, mVolume);
243
+
244
+    *source = id;
245
+
246
+    //! \fixme Should free alut buffer?
247
+
248
+    return 0;
249
+}
250
+
251
+void SoundAL::play(int source) {
252
+    assert(mSource.size() == mBuffer.size());
253
+    assert(source >= 0);
254
+    assert(source < (int)mSource.size());
255
+
256
+    if ((!mEnabled) || (!mInit))
257
+        return;
258
+
259
+    alSourcePlay(mSource[source]);
260
+}
261
+
262
+void SoundAL::stop(int source) {
263
+    assert(mSource.size() == mBuffer.size());
264
+    assert(source >= 0);
265
+    assert(source < (int)mSource.size());
266
+
267
+    if ((!mEnabled) || (!mInit))
268
+        return;
269
+
270
+    alSourceStop(mSource[source]);
271
+}
272
+

+ 3
- 1
src/main.cpp Parādīt failu

@@ -13,6 +13,8 @@
13 13
 #include "config.h"
14 14
 #include "main.h"
15 15
 #include "utils/time.h"
16
+
17
+#include "SoundAL.h"
16 18
 #include "WindowSDL.h"
17 19
 
18 20
 Camera *gCamera = NULL;
@@ -134,7 +136,7 @@ int main(int argc, char *argv[]) {
134 136
     atexit(cleanupHandler);
135 137
     gOpenRaider = new OpenRaider();
136 138
     gWindow = new WindowSDL();
137
-    gSound = new Sound();
139
+    gSound = new SoundAL();
138 140
     gWorld = new World();
139 141
     gCamera = new Camera();
140 142
     gConsole = new Console();

Notiek ielāde…
Atcelt
Saglabāt