Bläddra i källkod

Rewrote OpenAL, SoundManager, added system folder

Thomas Buck 10 år sedan
förälder
incheckning
ad4fa38113

+ 5
- 0
ChangeLog.md Visa fil

@@ -2,6 +2,11 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20141201 ]
6
+    * Moved Font, Sound, Window modules into system subfolder
7
+    * Rewrote OpenAL sound implementation, separated sourcing and buffering
8
+    * LoaderTR2 loads all sound-related data into SoundManager
9
+
5 10
     [ 20141129 ]
6 11
     * LoaderTR2 now supports loading animated textiles
7 12
     * Clearing textures and textiles before loading a new level

+ 0
- 107
include/Sound.h Visa fil

@@ -1,107 +0,0 @@
1
-/*!
2
- * \file include/Sound.h
3
- * \brief This is the audio manager Header
4
- *
5
- * \author Mongoose
6
- * \author xythobuz
7
- */
8
-
9
-#ifndef _SOUND_H_
10
-#define _SOUND_H_
11
-
12
-/*!
13
- * \brief This is the audio manager for OpenRaider
14
- */
15
-class Sound {
16
-  public:
17
-
18
-    typedef enum {
19
-        SoundFlagsNone = 0,       //!< No effect
20
-        SoundFlagsLoop = (1 << 0) //!< Enable looping during playback
21
-    } SoundFlags;
22
-
23
-    /*!
24
-     * \brief Deconstructs an object of Sound
25
-     */
26
-    virtual ~Sound();
27
-
28
-    /*!
29
-     * \brief Initialize sound system
30
-     * \returns 0 on success or < 0 error flags
31
-     */
32
-    virtual int initialize() = 0;
33
-
34
-    virtual void setEnabled(bool on) = 0;
35
-
36
-    virtual bool getEnabled() = 0;
37
-
38
-    /*!
39
-     * \brief Set the volume
40
-     * \param vol new source gain
41
-     */
42
-    virtual void setVolume(float vol) = 0;
43
-
44
-    virtual float getVolume() = 0;
45
-
46
-    /*!
47
-     * \brief Get number of registered sources
48
-     * \returns number of registered sources
49
-     */
50
-    virtual unsigned long registeredSources() = 0;
51
-
52
-    /*!
53
-     * \brief Remove all loaded sounds
54
-     */
55
-    virtual void clear() = 0;
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]) = 0;
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(unsigned long source, float pos[3]) = 0;
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, unsigned long* source, unsigned int flags) = 0;
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, unsigned long* source,
89
-                        unsigned int flags) = 0;
90
-
91
-    /*!
92
-     * \brief Play sound source
93
-     * \param source sound source to play
94
-     */
95
-    virtual void play(unsigned long source) = 0;
96
-
97
-    /*!
98
-     * \brief Stop playing sound source
99
-     * \param source sound source to stop
100
-     */
101
-    virtual void stop(unsigned long source) = 0;
102
-};
103
-
104
-Sound& getSound();
105
-
106
-#endif
107
-

+ 0
- 117
include/SoundAL.h Visa fil

@@ -1,117 +0,0 @@
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
-    virtual bool getEnabled() { return mEnabled; }
41
-
42
-    /*!
43
-     * \brief Set the volume
44
-     * \param vol new source gain
45
-     */
46
-    virtual void setVolume(float vol);
47
-
48
-    virtual float getVolume() { return mVolume; }
49
-
50
-    /*!
51
-     * \brief Get number of registered sources
52
-     * \returns number of registered sources
53
-     */
54
-    virtual unsigned long registeredSources();
55
-
56
-    /*!
57
-     * \brief Remove all loaded sounds
58
-     */
59
-    virtual void clear();
60
-
61
-    /*!
62
-     * \brief Move listener and repositions them
63
-     * \param pos New position for listener
64
-     * \param angle New orientation for listener
65
-     */
66
-    virtual void listenAt(float pos[3], float angle[3]);
67
-
68
-    /*!
69
-     * \brief Move sound source to position
70
-     * \param source valid source id
71
-     * \param pos new position for source
72
-     */
73
-    virtual void sourceAt(unsigned long source, float pos[3]);
74
-
75
-    /*!
76
-     * \brief Load wav file from disk
77
-     * \param filename not NULL!
78
-     * \param source not NULL! Returns new source ID or -1 on error.
79
-     * \param flags set options. Use SoundFlags enum bitwise OR-ed
80
-     * \returns 0 for no error or < 0 error flag
81
-     */
82
-    virtual int addFile(const char* filename, unsigned long* source, unsigned int flags);
83
-
84
-    /*!
85
-     * \brief Load wav file from buffer
86
-     * \param wav not NULL! Is a valid waveform buffer!
87
-     * \param length length of wav buffer
88
-     * \param source not NULL! Returns new source ID or -1 on error.
89
-     * \param flags set options. Use SoundFlags enum bitwise OR-ed
90
-     * \returns 0 for no error or < 0 error flag
91
-     */
92
-    virtual int addWave(unsigned char* wav, unsigned int length, unsigned long* source,
93
-                        unsigned int flags);
94
-
95
-    /*!
96
-     * \brief Play sound source
97
-     * \param source sound source to play
98
-     */
99
-    virtual void play(unsigned long source);
100
-
101
-    /*!
102
-     * \brief Stop playing sound source
103
-     * \param source sound source to stop
104
-     */
105
-    virtual void stop(unsigned long source);
106
-
107
-  private:
108
-
109
-    bool mEnabled;
110
-    bool mInit;                         //!< Guard to ensure ausio system is active
111
-    float mVolume;                      //!< Listener gain
112
-    std::vector<unsigned int> mBuffer; //!< Audio buffer id list
113
-    std::vector<unsigned int> mSource; //!< Audio source id list
114
-};
115
-
116
-#endif
117
-

+ 17
- 12
include/SoundManager.h Visa fil

@@ -27,22 +27,27 @@ struct SoundDetail {
27 27
 
28 28
 class SoundManager {
29 29
   public:
30
-    void clear();
31
-    void addSoundSource(float x, float y, float z, int id, int flags);
32
-    void addSoundMapEntry(int id);
33
-    void addSoundDetail(int sample, float volume);
34
-    void addSampleIndex(int index);
30
+    static void clear();
31
+    static int prepareSources();
35 32
 
36
-    int playSound(int index);
33
+    static void addSoundSource(float x, float y, float z, int id, int flags);
34
+    static void addSoundMapEntry(int id);
35
+    static void addSoundDetail(int sample, float volume);
36
+    static void addSampleIndex(int index);
37
+
38
+    static int sizeSoundMap();
39
+
40
+    static int getIndex(int index, float* volume = nullptr);
41
+
42
+    // index --> SoundMap --> SoundDetails --> SampleIndices --> play
43
+    static int playSound(int index);
37 44
 
38 45
   private:
39
-    std::vector<SoundSource> soundSources;
40
-    std::vector<int> soundMap;
41
-    std::vector<SoundDetail> soundDetails;
42
-    std::vector<int> sampleIndices;
46
+    static std::vector<SoundSource> soundSources;
47
+    static std::vector<int> soundMap;
48
+    static std::vector<SoundDetail> soundDetails;
49
+    static std::vector<int> sampleIndices;
43 50
 };
44 51
 
45
-SoundManager& getSoundManager();
46
-
47 52
 #endif
48 53
 

+ 0
- 109
include/SoundNull.h Visa fil

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

include/Font.h → include/system/Font.h Visa fil


include/FontImGui.h → include/system/FontImGui.h Visa fil


include/FontSDL.h → include/system/FontSDL.h Visa fil


include/FontTRLE.h → include/system/FontTRLE.h Visa fil


+ 37
- 0
include/system/Sound.h Visa fil

@@ -0,0 +1,37 @@
1
+/*!
2
+ * \file include/system/Sound.h
3
+ * \brief This is the audio manager Header
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _SOUND_H_
9
+#define _SOUND_H_
10
+
11
+class Sound {
12
+  public:
13
+    static int initialize();
14
+    static void shutdown();
15
+    static void clear();
16
+
17
+    static int numBuffers();
18
+    static int loadBuffer(unsigned char* buffer, unsigned int length);
19
+
20
+    static int numSources(bool atListener = false);
21
+    static int addSource(int buffer, float volume = 1.0f, bool atListener = false, bool loop = false);
22
+
23
+    static int sourceAt(int source, float pos[3]);
24
+    static void listenAt(float pos[3], float orientation[6]);
25
+
26
+    static void play(int source, bool atListener = false);
27
+    static void stopAll();
28
+
29
+    static void setEnabled(bool on = true);
30
+    static bool getEnabled();
31
+
32
+    static void setVolume(float vol = 1.0f);
33
+    static float getVolume();
34
+};
35
+
36
+#endif
37
+

+ 50
- 0
include/system/SoundAL.h Visa fil

@@ -0,0 +1,50 @@
1
+/*!
2
+ * \file include/system/SoundAL.h
3
+ * \brief This is the OpenAL audio manager Header
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _SOUND_AL_H_
9
+#define _SOUND_AL_H_
10
+
11
+#include <vector>
12
+
13
+class SoundAL {
14
+  public:
15
+    static int initialize();
16
+    static void shutdown();
17
+    static void clear();
18
+
19
+    static int numBuffers();
20
+    static int loadBuffer(unsigned char* buffer, unsigned int length);
21
+
22
+    static int numSources(bool atListener);
23
+    static int addSource(int buffer, float volume, bool atListener, bool loop);
24
+
25
+    static int sourceAt(int source, float pos[3]);
26
+    static void listenAt(float pos[3], float orientation[6]);
27
+
28
+    static void play(int source, bool atListener);
29
+    static void stopAll();
30
+
31
+    static void setEnabled(bool on);
32
+    static bool getEnabled();
33
+
34
+    static void setVolume(float vol);
35
+    static float getVolume();
36
+
37
+  private:
38
+    static bool init;
39
+    static bool enabled;
40
+    static float volume;
41
+
42
+    static std::vector<unsigned int> buffers;
43
+    static std::vector<unsigned int> sources;
44
+    static std::vector<unsigned int> listenerSources;
45
+
46
+    static float lastPosition[3];
47
+};
48
+
49
+#endif
50
+

include/Window.h → include/system/Window.h Visa fil


include/WindowGLUT.h → include/system/WindowGLUT.h Visa fil

@@ -8,7 +8,7 @@
8 8
 #ifndef _WINDOW_GLUT_H_
9 9
 #define _WINDOW_GLUT_H_
10 10
 
11
-#include "Window.h"
11
+#include "system/Window.h"
12 12
 
13 13
 /*!
14 14
  * \brief GLUT windowing implementation

include/WindowSDL.h → include/system/WindowSDL.h Visa fil

@@ -10,7 +10,7 @@
10 10
 
11 11
 #include "SDL.h"
12 12
 
13
-#include "Window.h"
13
+#include "system/Window.h"
14 14
 
15 15
 /*!
16 16
  * \brief SDL windowing implementation

+ 2
- 10
src/CMakeLists.txt Visa fil

@@ -62,9 +62,6 @@ set (SRCS ${SRCS} "Camera.cpp")
62 62
 set (SRCS ${SRCS} "Console.cpp")
63 63
 set (SRCS ${SRCS} "Entity.cpp")
64 64
 set (SRCS ${SRCS} "Exception.cpp")
65
-set (SRCS ${SRCS} "Font.cpp")
66
-set (SRCS ${SRCS} "FontImGui.cpp")
67
-set (SRCS ${SRCS} "FontTRLE.cpp")
68 65
 set (SRCS ${SRCS} "Game.cpp")
69 66
 set (SRCS ${SRCS} "Log.cpp")
70 67
 set (SRCS ${SRCS} "main.cpp")
@@ -77,7 +74,6 @@ set (SRCS ${SRCS} "RoomData.cpp")
77 74
 set (SRCS ${SRCS} "RunTime.cpp")
78 75
 set (SRCS ${SRCS} "Script.cpp")
79 76
 set (SRCS ${SRCS} "SkeletalModel.cpp")
80
-set (SRCS ${SRCS} "Sound.cpp")
81 77
 set (SRCS ${SRCS} "SoundManager.cpp")
82 78
 set (SRCS ${SRCS} "Sprite.cpp")
83 79
 set (SRCS ${SRCS} "StaticMesh.cpp")
@@ -85,30 +81,24 @@ set (SRCS ${SRCS} "TextureManager.cpp")
85 81
 set (SRCS ${SRCS} "TombRaider.cpp")
86 82
 set (SRCS ${SRCS} "UI.cpp")
87 83
 set (SRCS ${SRCS} "ViewVolume.cpp")
88
-set (SRCS ${SRCS} "Window.cpp")
89 84
 set (SRCS ${SRCS} "World.cpp")
90 85
 
91 86
 # Select available Sound library
92 87
 if (OPENAL_FOUND AND ALUT_FOUND AND ENABLE_AUDIO)
93
-    set (SRCS ${SRCS} "SoundAL.cpp")
94 88
     set (USING_AL TRUE)
95 89
 else (OPENAL_FOUND AND ALUT_FOUND AND ENABLE_AUDIO)
96
-    set (SRCS ${SRCS} "SoundNull.cpp")
97 90
     set (USING_AL FALSE)
98 91
     message (STATUS "Disabled Sound support (no OpenAL and ALUT)!")
99 92
 endif (OPENAL_FOUND AND ALUT_FOUND AND ENABLE_AUDIO)
100 93
 
101 94
 # Select available Windowing library
102 95
 if (SDL2_FOUND AND NOT FORCE_GLUT)
103
-    set (SRCS ${SRCS} "WindowSDL.cpp")
104 96
     set (OpenRaider_CXX_FLAGS "${OpenRaider_CXX_FLAGS} -DUSING_SDL")
105 97
     if (SDL2TTF_FOUND)
106
-        set (SRCS ${SRCS} "FontSDL.cpp")
107 98
         set (OpenRaider_CXX_FLAGS "${OpenRaider_CXX_FLAGS} -DUSING_SDL_FONT")
108 99
     endif (SDL2TTF_FOUND)
109 100
 else (SDL2_FOUND AND NOT FORCE_GLUT)
110 101
     if (FREEGLUT_FOUND)
111
-        set (SRCS ${SRCS} "WindowGLUT.cpp")
112 102
         set (OpenRaider_CXX_FLAGS "${OpenRaider_CXX_FLAGS} -DUSING_GLUT")
113 103
     else (FREEGLUT_FOUND)
114 104
         message (FATAL_ERROR "SDL2 or freeGLUT are required!")
@@ -230,6 +220,7 @@ add_subdirectory ("commands")
230 220
 add_subdirectory ("deps")
231 221
 add_subdirectory ("loader")
232 222
 add_subdirectory ("math")
223
+add_subdirectory ("system")
233 224
 add_subdirectory ("utils")
234 225
 
235 226
 # Add Executable
@@ -238,6 +229,7 @@ add_executable (OpenRaider MACOSX_BUNDLE ${RESRCS}
238 229
     $<TARGET_OBJECTS:OpenRaider_all> $<TARGET_OBJECTS:OpenRaider_commands>
239 230
     $<TARGET_OBJECTS:OpenRaider_deps> $<TARGET_OBJECTS:OpenRaider_math>
240 231
     $<TARGET_OBJECTS:OpenRaider_utils> $<TARGET_OBJECTS:OpenRaider_loader>
232
+    $<TARGET_OBJECTS:OpenRaider_system>
241 233
 )
242 234
 
243 235
 #################################################################

+ 6
- 4
src/Game.cpp Visa fil

@@ -16,9 +16,9 @@
16 16
 #include "loader/Loader.h"
17 17
 #include "Log.h"
18 18
 #include "Render.h"
19
-#include "Sound.h"
20 19
 #include "SoundManager.h"
21 20
 #include "StaticMesh.h"
21
+#include "system/Sound.h"
22 22
 #include "TextureManager.h"
23 23
 #include "World.h"
24 24
 #include "utils/strings.h"
@@ -55,8 +55,8 @@ void Game::destroy() {
55 55
 
56 56
     getWorld().destroy();
57 57
     getRender().ClearWorld();
58
-    getSound().clear(); // Remove all previously loaded sounds
59
-    getSoundManager().clear();
58
+    Sound::clear(); // Remove all previously loaded sounds
59
+    SoundManager::clear();
60 60
     getTextureManager().clear();
61 61
 }
62 62
 
@@ -80,6 +80,8 @@ int Game::loadLevel(const char* level) {
80 80
         if (error != 0) {
81 81
             getLog() << "Error while trying new loader (" << error << ")..." << Log::endl;
82 82
             destroy();
83
+        } else {
84
+            SoundManager::prepareSources();
83 85
         }
84 86
     }
85 87
 
@@ -228,7 +230,7 @@ void Game::processPakSounds() {
228 230
     for (i = 0; i < mTombRaider.getSoundSamplesCount(); ++i) {
229 231
         mTombRaider.getSoundSample(i, &riffSz, &riff);
230 232
 
231
-        getSound().addWave(riff, riffSz, &id, Sound::SoundFlagsNone);
233
+        id = Sound::loadBuffer(riff, riffSz);
232 234
 
233 235
         //if (((i + 1) == TR_SOUND_F_PISTOL) && (id > 0))
234 236
         //{

+ 2
- 2
src/Menu.cpp Visa fil

@@ -6,11 +6,11 @@
6 6
  */
7 7
 
8 8
 #include "global.h"
9
-#include "Font.h"
10 9
 #include "Log.h"
11
-#include "Window.h"
12 10
 #include "Menu.h"
13 11
 #include "MenuFolder.h"
12
+#include "system/Font.h"
13
+#include "system/Window.h"
14 14
 
15 15
 void Menu::showDialog(std::string msg, std::string btn1, std::string btn2,
16 16
                       std::function<int (bool state)> callback) {

+ 2
- 2
src/MenuFolder.cpp Visa fil

@@ -6,13 +6,13 @@
6 6
  */
7 7
 
8 8
 #include "global.h"
9
-#include "Font.h"
10 9
 #include "Game.h"
11 10
 #include "loader/Loader.h"
12 11
 #include "Log.h"
13 12
 #include "RunTime.h"
14
-#include "Window.h"
15 13
 #include "MenuFolder.h"
14
+#include "system/Font.h"
15
+#include "system/Window.h"
16 16
 
17 17
 MenuFolder::MenuFolder() {
18 18
     mCursor = 0;

+ 1
- 1
src/Render.cpp Visa fil

@@ -19,8 +19,8 @@
19 19
 #include "Render.h"
20 20
 #include "utils/strings.h"
21 21
 #include "utils/tga.h"
22
-#include "Window.h"
23 22
 #include "World.h"
23
+#include "system/Window.h"
24 24
 
25 25
 Render::Render() {
26 26
     mSkyMesh = -1;

+ 0
- 13
src/Sound.cpp Visa fil

@@ -1,13 +0,0 @@
1
-/*!
2
- * \file src/Sound.cpp
3
- * \brief This is the audio manager Implementation
4
- *
5
- * \author xythobuz
6
- */
7
-
8
-#include "global.h"
9
-#include "Sound.h"
10
-
11
-Sound::~Sound() {
12
-}
13
-

+ 0
- 263
src/SoundAL.cpp Visa fil

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

+ 65
- 16
src/SoundManager.cpp Visa fil

@@ -6,9 +6,14 @@
6 6
  */
7 7
 
8 8
 #include "global.h"
9
-#include "Sound.h"
9
+#include "system/Sound.h"
10 10
 #include "SoundManager.h"
11 11
 
12
+std::vector<SoundSource> SoundManager::soundSources;
13
+std::vector<int> SoundManager::soundMap;
14
+std::vector<SoundDetail> SoundManager::soundDetails;
15
+std::vector<int> SoundManager::sampleIndices;
16
+
12 17
 void SoundManager::clear() {
13 18
     soundSources.clear();
14 19
     soundMap.clear();
@@ -16,6 +21,28 @@ void SoundManager::clear() {
16 21
     sampleIndices.clear();
17 22
 }
18 23
 
24
+int SoundManager::prepareSources() {
25
+    for (int i = 0; i < soundSources.size(); i++) {
26
+        float vol;
27
+        int index = getIndex(soundSources.at(i).id, &vol);
28
+        int ret = Sound::addSource(index, vol, false, true);
29
+        assert(ret == i);
30
+        float pos[3] = { soundSources.at(i).x, soundSources.at(i).y, soundSources.at(i).z };
31
+        ret = Sound::sourceAt(i, pos);
32
+        assert(ret == 0);
33
+        Sound::play(i, false);
34
+    }
35
+
36
+    for (int i = 0; i < soundMap.size(); i++) {
37
+        float vol;
38
+        int index = getIndex(i, &vol);
39
+        if (index >= 0) {
40
+            int ret = Sound::addSource(index, vol, true, false);
41
+            assert(ret >= 0);
42
+        }
43
+    }
44
+}
45
+
19 46
 void SoundManager::addSoundSource(float x, float y, float z, int id, int flags) {
20 47
     soundSources.emplace_back(x, y, z, id, flags);
21 48
 }
@@ -32,36 +59,58 @@ void SoundManager::addSampleIndex(int index) {
32 59
     sampleIndices.push_back(index);
33 60
 }
34 61
 
35
-int SoundManager::playSound(int index) {
62
+int SoundManager::sizeSoundMap() {
63
+    return soundMap.size();
64
+}
65
+
66
+int SoundManager::getIndex(int index, float* volume) {
67
+    if (index <= -1)
68
+        return -1;
69
+
36 70
     if (index >= soundMap.size())
37
-        return -1; // SoundMap not big enough
71
+        return -2; // SoundMap not big enough
38 72
 
39 73
     index = soundMap.at(index);
40 74
 
41
-    if (index == -1)
42
-        return -2; // SoundMap has no entry here (-1)
75
+    if (index <= -1)
76
+        return -3; // SoundMap has no entry here (-1)
43 77
 
44 78
     if (index >= soundDetails.size())
45
-        return -3; // SoundMap entry is bigger than SoundDetails
79
+        return -4; // SoundMap entry is bigger than SoundDetails
46 80
 
47 81
     SoundDetail s = soundDetails.at(index);
48 82
 
49
-    if (s.sample == -1)
50
-        return -4; // SoundDetail has no entry here (-1)
83
+    if (volume != nullptr)
84
+        *volume = s.volume;
85
+
86
+    if (s.sample <= -1)
87
+        return -5; // SoundDetail has no entry here (-1)
51 88
 
52 89
     if (s.sample >= sampleIndices.size())
53
-        return -5; // SoundDetail entry is bigger than SampleIndices
90
+        return -6; // SoundDetail entry is bigger than SampleIndices
54 91
 
55 92
     index = sampleIndices.at(s.sample);
56 93
 
57
-    if (index == -1)
58
-        return -6; // SampleIndices has no entry here (-1)
59
-
60
-    if (index >= getSound().registeredSources())
61
-        return -7; // SampleIndices entry is bigger than number of sounds loaded
94
+    if (index <= -1)
95
+        return -7; // SampleIndices has no entry here (-1)
62 96
 
63
-    // TODO play sound _index_ with matching volume
97
+    return index;
98
+}
64 99
 
65
-    return 0;
100
+int SoundManager::playSound(int index) {
101
+    if ((index >= 0) && (index < soundMap.size())) {
102
+        if (soundMap.at(index) == -1)
103
+            return 0;
104
+
105
+        int c = 1;
106
+        for (int i = 0; i < index; i++)
107
+            if (soundMap.at(i) != -1)
108
+                c++;
109
+
110
+        Sound::play(c, true);
111
+        return 0;
112
+    } else {
113
+        return -1;
114
+    }
66 115
 }
67 116
 

+ 0
- 63
src/SoundNull.cpp Visa fil

@@ -1,63 +0,0 @@
1
-/*!
2
- * \file src/SoundNull.cpp
3
- * \brief This is the null audio manager Implementation
4
- *
5
- * \author xythobuz
6
- */
7
-
8
-#include "global.h"
9
-#include "SoundNull.h"
10
-
11
-SoundNull::SoundNull() {
12
-    sources = 0;
13
-}
14
-
15
-SoundNull::~SoundNull() {
16
-}
17
-
18
-int SoundNull::initialize() {
19
-    return 0;
20
-}
21
-
22
-void SoundNull::setEnabled(bool on) {
23
-}
24
-
25
-void SoundNull::setVolume(float vol) {
26
-}
27
-
28
-unsigned long SoundNull::registeredSources() {
29
-    return sources;
30
-}
31
-
32
-void SoundNull::clear() {
33
-    sources = 0;
34
-}
35
-
36
-void SoundNull::listenAt(float pos[3], float angle[3]) {
37
-}
38
-
39
-void SoundNull::sourceAt(unsigned long source, float pos[3]) {
40
-    assert(source < sources);
41
-}
42
-
43
-int SoundNull::addFile(const char* filename, unsigned long* source, unsigned int flags) {
44
-    *source = sources;
45
-    sources++;
46
-    return 0;
47
-}
48
-
49
-int SoundNull::addWave(unsigned char* wav, unsigned int length, unsigned long* source,
50
-                       unsigned int flags) {
51
-    *source = sources;
52
-    sources++;
53
-    return 0;
54
-}
55
-
56
-void SoundNull::play(unsigned long source) {
57
-    assert(source < sources);
58
-}
59
-
60
-void SoundNull::stop(unsigned long source) {
61
-    assert(source < sources);
62
-}
63
-

+ 21
- 16
src/UI.cpp Visa fil

@@ -15,10 +15,11 @@
15 15
 #include "Menu.h"
16 16
 #include "Render.h"
17 17
 #include "RunTime.h"
18
-#include "Sound.h"
18
+#include "SoundManager.h"
19 19
 #include "TextureManager.h"
20
-#include "Window.h"
21 20
 #include "commands/Command.h"
21
+#include "system/Sound.h"
22
+#include "system/Window.h"
22 23
 #include "utils/time.h"
23 24
 #include "UI.h"
24 25
 
@@ -178,6 +179,8 @@ void UI::eventsFinished() {
178 179
     bool input = !(visible || getMenu().isVisible());
179 180
     if (getWindow().getMousegrab() != input)
180 181
         getWindow().setMousegrab(input);
182
+
183
+    io.MouseWheel = 0;
181 184
 }
182 185
 
183 186
 void UI::display() {
@@ -219,9 +222,9 @@ void UI::display() {
219 222
                 getRunTime().setRunning(running);
220 223
             }
221 224
             ImGui::SameLine();
222
-            bool sound = getSound().getEnabled();
225
+            bool sound = Sound::getEnabled();
223 226
             if (ImGui::Checkbox("Sound##runtime", &sound)) {
224
-                getSound().setEnabled(sound);
227
+                Sound::setEnabled(sound);
225 228
             }
226 229
             ImGui::SameLine();
227 230
             bool fullscreen = getWindow().getFullscreen();
@@ -229,14 +232,14 @@ void UI::display() {
229 232
                 getWindow().setFullscreen(fullscreen);
230 233
             }
231 234
 
232
-            float vol = getSound().getVolume();
235
+            float vol = Sound::getVolume();
233 236
             if (ImGui::InputFloat("Volume##runtime", &vol, 0.0f, 0.0f, 3,
234 237
                                   ImGuiInputTextFlags_EnterReturnsTrue)) {
235 238
                 if (vol < 0.0f)
236 239
                     vol = 0.0f;
237 240
                 if (vol > 1.0f)
238 241
                     vol = 1.0f;
239
-                getSound().setVolume(vol);
242
+                Sound::setVolume(vol);
240 243
             }
241 244
 
242 245
             int w = getWindow().getWidth();
@@ -461,22 +464,22 @@ void UI::display() {
461 464
             }
462 465
         }
463 466
 
464
-        if (ImGui::CollapsingHeader("SoundSample Player")) {
465
-            if (!getSound().getEnabled()) {
466
-                ImGui::Text("Please enable Sound before loading a level!");
467
+        if (ImGui::CollapsingHeader("SoundManager Player")) {
468
+            if (!Sound::getEnabled()) {
469
+                ImGui::Text("Please enable Sound first!");
467 470
                 if (ImGui::Button("Enable Sound!")) {
468
-                    getSound().setEnabled(true);
471
+                    Sound::setEnabled(true);
469 472
                 }
470
-            } else if (getSound().registeredSources() == 0) {
473
+            } else if (Sound::numBuffers() == 0) {
471 474
                 ImGui::Text("Please load a level!");
472 475
             } else {
473 476
                 static int index = 0;
474 477
                 ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
475
-                ImGui::SliderInt("##soundslide", &index, 0, getSound().registeredSources() - 1);
478
+                ImGui::SliderInt("##soundslide", &index, 0, SoundManager::sizeSoundMap() - 1);
476 479
                 ImGui::PopItemWidth();
477 480
                 ImGui::SameLine();
478 481
                 if (ImGui::Button("+##soundplus", ImVec2(0, 0), true)) {
479
-                    if (index < (getSound().registeredSources() - 1))
482
+                    if (index < (SoundManager::sizeSoundMap() - 1))
480 483
                         index++;
481 484
                     else
482 485
                         index = 0;
@@ -486,12 +489,14 @@ void UI::display() {
486 489
                     if (index > 0)
487 490
                         index--;
488 491
                     else
489
-                        index = getSound().registeredSources() - 1;
492
+                        index = SoundManager::sizeSoundMap() - 1;
490 493
                 }
491 494
                 ImGui::SameLine();
492 495
                 if (ImGui::Button("Play##soundplay")) {
493
-                    getSound().play(index);
496
+                    SoundManager::playSound(index);
494 497
                 }
498
+
499
+                ImGui::Text("Index: %d", SoundManager::getIndex(index));
495 500
             }
496 501
         }
497 502
 
@@ -558,7 +563,7 @@ void UI::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
558 563
 
559 564
 void UI::handleMouseScroll(int xrel, int yrel) {
560 565
     ImGuiIO& io = ImGui::GetIO();
561
-    io.MouseWheel = (yrel != 0) ? yrel > 0 ? 1 : -1 : 0;
566
+    io.MouseWheel += yrel;
562 567
 
563 568
     scrollEvents.push_back(std::make_tuple(xrel, yrel));
564 569
 }

+ 7
- 7
src/commands/CommandSet.cpp Visa fil

@@ -7,11 +7,11 @@
7 7
 
8 8
 #include "global.h"
9 9
 #include "Camera.h"
10
-#include "Font.h"
11 10
 #include "Log.h"
12 11
 #include "RunTime.h"
13
-#include "Sound.h"
14
-#include "Window.h"
12
+#include "system/Font.h"
13
+#include "system/Sound.h"
14
+#include "system/Window.h"
15 15
 #include "utils/strings.h"
16 16
 #include "commands/CommandSet.h"
17 17
 
@@ -89,14 +89,14 @@ int CommandSet::execute(std::istream& args) {
89 89
             getLog() << "set-audio-Error: Invalid value" << Log::endl;
90 90
             return -4;
91 91
         }
92
-        getSound().setEnabled(audio);
92
+        Sound::setEnabled(audio);
93 93
     } else if (var.compare("volume") == 0) {
94 94
         float vol = 1.0f;
95 95
         if (!(args >> vol)) {
96 96
             getLog() << "set-volume-Error: Invalid value" << Log::endl;
97 97
             return -5;
98 98
         }
99
-        getSound().setVolume(vol);
99
+        Sound::setVolume(vol);
100 100
     } else if (var.compare("mouse_x") == 0) {
101 101
         float sense = 1.0f;
102 102
         if (!(args >> sense)) {
@@ -183,9 +183,9 @@ int CommandGet::execute(std::istream& args) {
183 183
     } else if (var.compare("fullscreen") == 0) {
184 184
         getLog() << getWindow().getFullscreen() << Log::endl;
185 185
     } else if (var.compare("audio") == 0) {
186
-        getLog() << getSound().getEnabled() << Log::endl;
186
+        getLog() << Sound::getEnabled() << Log::endl;
187 187
     } else if (var.compare("volume") == 0) {
188
-        getLog() << getSound().getVolume() << Log::endl;
188
+        getLog() << Sound::getVolume() << Log::endl;
189 189
     } else if (var.compare("mouse_x") == 0) {
190 190
         getLog() << OR_RAD_TO_DEG(getCamera().getSensitivityX()) << Log::endl;
191 191
     } else if (var.compare("mouse_y") == 0) {

+ 15
- 6
src/loader/LoaderTR2.cpp Visa fil

@@ -13,9 +13,9 @@
13 13
 #include "Log.h"
14 14
 #include "Mesh.h"
15 15
 #include "Room.h"
16
-#include "Sound.h"
17 16
 #include "SoundManager.h"
18 17
 #include "TextureManager.h"
18
+#include "system/Sound.h"
19 19
 #include "utils/pixel.h"
20 20
 #include "loader/LoaderTR2.h"
21 21
 
@@ -626,13 +626,16 @@ void LoaderTR2::loadSoundSources() {
626 626
         // Unknown, 0x40, 0x80 or 0xC0
627 627
         uint16_t flags = file.readU16();
628 628
 
629
-        getSoundManager().addSoundSource(x, y, z, soundID, flags);
629
+        SoundManager::addSoundSource(x, y, z, soundID, flags);
630 630
     }
631
+
632
+    if (numSoundSources > 0)
633
+        getLog() << "LoaderTR2: Found " << numSoundSources << " SoundSources" << Log::endl;
631 634
 }
632 635
 
633 636
 void LoaderTR2::loadSoundMap() {
634 637
     for (int i = 0; i < 370; i++) {
635
-        getSoundManager().addSoundMapEntry(file.read16());
638
+        SoundManager::addSoundMapEntry(file.read16());
636 639
     }
637 640
 }
638 641
 
@@ -650,15 +653,21 @@ void LoaderTR2::loadSoundDetails() {
650 653
         // Bits 0-1: channel number?
651 654
         uint16_t unknown2 = file.readU16();
652 655
 
653
-        getSoundManager().addSoundDetail(sample, ((float)volume) / 32767.0f);
656
+        SoundManager::addSoundDetail(sample, ((float)volume) / 32767.0f);
654 657
     }
658
+
659
+    if (numSoundDetails > 0)
660
+        getLog() << "LoaderTR2: Found " << numSoundDetails << " SoundDetails" << Log::endl;
655 661
 }
656 662
 
657 663
 void LoaderTR2::loadSampleIndices() {
658 664
     uint32_t numSampleIndices = file.readU32();
659 665
     for (unsigned int i = 0; i < numSampleIndices; i++) {
660
-        getSoundManager().addSampleIndex(file.readU32());
666
+        SoundManager::addSampleIndex(file.readU32());
661 667
     }
668
+
669
+    if (numSampleIndices > 0)
670
+        getLog() << "LoaderTR2: Found " << numSampleIndices << " SampleIndices" << Log::endl;
662 671
 }
663 672
 
664 673
 void LoaderTR2::loadExternalSoundFile(std::string f) {
@@ -697,7 +706,7 @@ void LoaderTR2::loadExternalSoundFile(std::string f) {
697 706
             buff[i] = sfx.readU8();
698 707
 
699 708
         unsigned long src;
700
-        int ret = getSound().addWave(buff, riffSize + 8, &src, 0);
709
+        int ret = Sound::loadBuffer(buff, riffSize + 8);
701 710
         assert(ret >= 0);
702 711
 
703 712
         riffCount++;

+ 7
- 28
src/main.cpp Visa fil

@@ -18,7 +18,6 @@
18 18
 #ifndef UNIT_TEST
19 19
 
20 20
 #include "Camera.h"
21
-#include "Font.h"
22 21
 #include "Game.h"
23 22
 #include "Log.h"
24 23
 #include "MenuFolder.h"
@@ -27,19 +26,15 @@
27 26
 #include "SoundManager.h"
28 27
 #include "TextureManager.h"
29 28
 #include "UI.h"
30
-#include "Window.h"
31 29
 #include "World.h"
32
-
33
-#ifdef USING_AL
34
-#include "SoundAL.h"
35
-#else
36
-#include "SoundNull.h"
37
-#endif
30
+#include "system/Font.h"
31
+#include "system/Sound.h"
32
+#include "system/Window.h"
38 33
 
39 34
 #ifdef USING_SDL
40
-#include "WindowSDL.h"
35
+#include "system/WindowSDL.h"
41 36
 #elif defined(USING_GLUT)
42
-#include "WindowGLUT.h"
37
+#include "system/WindowGLUT.h"
43 38
 #else
44 39
 #error No Windowing Library selected!
45 40
 #endif
@@ -52,8 +47,6 @@ static std::shared_ptr<Log> gLog;
52 47
 static std::shared_ptr<MenuFolder> gMenu;
53 48
 static std::shared_ptr<Render> gRender;
54 49
 static std::shared_ptr<RunTime> gRunTime;
55
-static std::shared_ptr<Sound> gSound;
56
-static std::shared_ptr<SoundManager> gSoundManager;
57 50
 static std::shared_ptr<TextureManager> gTextureManager;
58 51
 static std::shared_ptr<Window> gWindow;
59 52
 static std::shared_ptr<World> gWorld;
@@ -82,14 +75,6 @@ RunTime& getRunTime() {
82 75
     return *gRunTime;
83 76
 }
84 77
 
85
-Sound& getSound() {
86
-    return *gSound;
87
-}
88
-
89
-SoundManager& getSoundManager() {
90
-    return *gSoundManager;
91
-}
92
-
93 78
 TextureManager& getTextureManager() {
94 79
     return *gTextureManager;
95 80
 }
@@ -120,16 +105,9 @@ int main(int argc, char* argv[]) {
120 105
     gLog.reset(new Log());
121 106
     gMenu.reset(new MenuFolder());
122 107
     gRender.reset(new Render());
123
-    gSoundManager.reset(new SoundManager());
124 108
     gTextureManager.reset(new TextureManager());
125 109
     gWorld.reset(new World());
126 110
 
127
-#ifdef USING_AL
128
-    gSound.reset(new SoundAL());
129
-#else
130
-    gSound.reset(new SoundNull());
131
-#endif
132
-
133 111
 #ifdef USING_SDL
134 112
     gWindow.reset(new WindowSDL());
135 113
 #elif defined(USING_GLUT)
@@ -176,7 +154,7 @@ int main(int argc, char* argv[]) {
176 154
     }
177 155
 
178 156
     // Initialize Sound
179
-    error = getSound().initialize();
157
+    error = Sound::initialize();
180 158
     if (error != 0) {
181 159
         std::cout << "Could not initialize Sound (" << error << ")!" << std::endl;
182 160
         return -4;
@@ -222,6 +200,7 @@ int main(int argc, char* argv[]) {
222 200
 
223 201
     UI::shutdown();
224 202
     Font::shutdown();
203
+    Sound::shutdown();
225 204
 
226 205
 #ifdef DEBUG
227 206
     std::cout << std::endl;

+ 31
- 0
src/system/CMakeLists.txt Visa fil

@@ -0,0 +1,31 @@
1
+# Source files
2
+set (SYS_SRCS ${SYS_SRCS} "Font.cpp")
3
+set (SYS_SRCS ${SYS_SRCS} "FontImGui.cpp")
4
+set (SYS_SRCS ${SYS_SRCS} "FontTRLE.cpp")
5
+set (SYS_SRCS ${SYS_SRCS} "Sound.cpp")
6
+set (SYS_SRCS ${SYS_SRCS} "Window.cpp")
7
+
8
+# Select available Sound library
9
+if (OPENAL_FOUND AND ALUT_FOUND AND ENABLE_AUDIO)
10
+    set (SYS_SRCS ${SYS_SRCS} "SoundAL.cpp")
11
+else (OPENAL_FOUND AND ALUT_FOUND AND ENABLE_AUDIO)
12
+    message (STATUS "Disabled Sound support (no OpenAL and ALUT)!")
13
+endif (OPENAL_FOUND AND ALUT_FOUND AND ENABLE_AUDIO)
14
+
15
+# Select available Windowing library
16
+if (SDL2_FOUND AND NOT FORCE_GLUT)
17
+    set (SYS_SRCS ${SYS_SRCS} "WindowSDL.cpp")
18
+    if (SDL2TTF_FOUND)
19
+        set (SYS_SRCS ${SYS_SRCS} "FontSDL.cpp")
20
+    endif (SDL2TTF_FOUND)
21
+else (SDL2_FOUND AND NOT FORCE_GLUT)
22
+    if (FREEGLUT_FOUND)
23
+        set (SYS_SRCS ${SYS_SRCS} "WindowGLUT.cpp")
24
+    else (FREEGLUT_FOUND)
25
+        message (FATAL_ERROR "SDL2 or freeGLUT are required!")
26
+    endif (FREEGLUT_FOUND)
27
+endif (SDL2_FOUND AND NOT FORCE_GLUT)
28
+
29
+# Add library
30
+add_library (OpenRaider_system OBJECT ${SYS_SRCS})
31
+

src/Font.cpp → src/system/Font.cpp Visa fil

@@ -8,13 +8,13 @@
8 8
 #include "global.h"
9 9
 #include "Log.h"
10 10
 #include "utils/strings.h"
11
-#include "Window.h"
12
-#include "Font.h"
13
-#include "FontImGui.h"
14
-#include "FontTRLE.h"
11
+#include "system/Window.h"
12
+#include "system/Font.h"
13
+#include "system/FontImGui.h"
14
+#include "system/FontTRLE.h"
15 15
 
16 16
 #ifdef USING_SDL_FONT
17
-#include "FontSDL.h"
17
+#include "system/FontSDL.h"
18 18
 #endif
19 19
 
20 20
 bool Font::isInit = false;

src/FontImGui.cpp → src/system/FontImGui.cpp Visa fil

@@ -7,7 +7,7 @@
7 7
 
8 8
 #include "global.h"
9 9
 #include "UI.h"
10
-#include "FontImGui.h"
10
+#include "system/FontImGui.h"
11 11
 
12 12
 #define SCALE_CALC 1.0f
13 13
 #define SCALE_DRAW 20.0f

src/FontSDL.cpp → src/system/FontSDL.cpp Visa fil

@@ -8,7 +8,7 @@
8 8
 #include <iostream>
9 9
 
10 10
 #include "global.h"
11
-#include "FontSDL.h"
11
+#include "system/FontSDL.h"
12 12
 
13 13
 bool FontSDL::mFontInit = false;
14 14
 TTF_Font* FontSDL::mFont = nullptr;

src/FontTRLE.cpp → src/system/FontTRLE.cpp Visa fil

@@ -12,7 +12,7 @@
12 12
 
13 13
 #include "global.h"
14 14
 #include "utils/strings.h"
15
-#include "FontTRLE.h"
15
+#include "system/FontTRLE.h"
16 16
 
17 17
 #define SCALING 2.0f
18 18
 

+ 120
- 0
src/system/Sound.cpp Visa fil

@@ -0,0 +1,120 @@
1
+/*!
2
+ * \file src/system/Sound.cpp
3
+ * \brief This is the audio manager Implementation
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include "global.h"
9
+#include "system/Sound.h"
10
+
11
+#ifdef USING_AL
12
+#include "system/SoundAL.h"
13
+#endif
14
+
15
+int Sound::initialize() {
16
+#ifdef USING_AL
17
+    return SoundAL::initialize();
18
+#else
19
+    return 0;
20
+#endif
21
+}
22
+
23
+void Sound::shutdown() {
24
+#ifdef USING_AL
25
+    SoundAL::shutdown();
26
+#endif
27
+}
28
+
29
+void Sound::clear() {
30
+#ifdef USING_AL
31
+    SoundAL::clear();
32
+#endif
33
+}
34
+
35
+int Sound::numBuffers() {
36
+#ifdef USING_AL
37
+    return SoundAL::numBuffers();
38
+#else
39
+    return 0;
40
+#endif
41
+}
42
+
43
+int Sound::loadBuffer(unsigned char* buffer, unsigned int length) {
44
+#ifdef USING_AL
45
+    return SoundAL::loadBuffer(buffer, length);
46
+#else
47
+    return 0;
48
+#endif
49
+}
50
+
51
+int Sound::numSources(bool atListener) {
52
+#ifdef USING_AL
53
+    return SoundAL::numSources(atListener);
54
+#else
55
+    return 0;
56
+#endif
57
+}
58
+
59
+int Sound::addSource(int buffer, float volume, bool atListener, bool loop) {
60
+#ifdef USING_AL
61
+    return SoundAL::addSource(buffer, volume, atListener, loop);
62
+#else
63
+    return 0;
64
+#endif
65
+}
66
+
67
+int Sound::sourceAt(int source, float pos[3]) {
68
+#ifdef USING_AL
69
+    return SoundAL::sourceAt(source, pos);
70
+#else
71
+    return 0;
72
+#endif
73
+}
74
+
75
+void Sound::listenAt(float pos[3], float orientation[6]) {
76
+#ifdef USING_AL
77
+    SoundAL::listenAt(pos, orientation);
78
+#endif
79
+}
80
+
81
+void Sound::play(int source, bool atListener) {
82
+#ifdef USING_AL
83
+    SoundAL::play(source, atListener);
84
+#endif
85
+}
86
+
87
+void Sound::stopAll() {
88
+#ifdef USING_AL
89
+    SoundAL::stopAll();
90
+#endif
91
+}
92
+
93
+void Sound::setEnabled(bool on) {
94
+#ifdef USING_AL
95
+    SoundAL::setEnabled(on);
96
+#endif
97
+}
98
+
99
+bool Sound::getEnabled() {
100
+#ifdef USING_AL
101
+    return SoundAL::getEnabled();
102
+#else
103
+    return false;
104
+#endif
105
+}
106
+
107
+void Sound::setVolume(float vol) {
108
+#ifdef USING_AL
109
+    SoundAL::setVolume(vol);
110
+#endif
111
+}
112
+
113
+float Sound::getVolume() {
114
+#ifdef USING_AL
115
+    return SoundAL::getVolume();
116
+#else
117
+    return 0.0f;
118
+#endif
119
+}
120
+

+ 215
- 0
src/system/SoundAL.cpp Visa fil

@@ -0,0 +1,215 @@
1
+/*!
2
+ * \file src/system/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 "global.h"
20
+#include "Log.h"
21
+#include "system/SoundAL.h"
22
+
23
+bool SoundAL::init = false;
24
+bool SoundAL::enabled = true;
25
+float SoundAL::volume = 1.0f;
26
+std::vector<unsigned int> SoundAL::buffers;
27
+std::vector<unsigned int> SoundAL::sources;
28
+std::vector<unsigned int> SoundAL::listenerSources;
29
+float SoundAL::lastPosition[3] = { 0.0f, 0.0f, 0.0f };
30
+
31
+int SoundAL::initialize() {
32
+    if (init)
33
+        return 0;
34
+
35
+    ALCdevice* device = alcOpenDevice(NULL);
36
+    ALCcontext* context = alcCreateContext(device, NULL);
37
+    alcMakeContextCurrent(context);
38
+
39
+    if (alutInitWithoutContext(nullptr, nullptr) == AL_FALSE) {
40
+        getLog() << "SoundAL Error: " << alutGetErrorString(alutGetError()) << Log::endl;
41
+        return -1;
42
+    }
43
+
44
+    init = true;
45
+    setVolume(volume);
46
+    return 0;
47
+}
48
+
49
+void SoundAL::shutdown() {
50
+    if (!init)
51
+        return;
52
+
53
+    clear();
54
+    if (alutExit() == AL_FALSE)
55
+        getLog() << "SoundAL Error: " << alutGetErrorString(alutGetError()) << Log::endl;
56
+    init = false;
57
+}
58
+
59
+void SoundAL::clear() {
60
+    if (!init)
61
+        return;
62
+
63
+    stopAll();
64
+
65
+    alGetError();
66
+    alDeleteSources(sources.size(), &sources[0]);
67
+    sources.clear();
68
+    if (alGetError() != AL_NO_ERROR) {
69
+        getLog() << "SoundAL: Error while deleting sources!" << Log::endl;
70
+    }
71
+
72
+    alGetError();
73
+    alDeleteSources(listenerSources.size(), &listenerSources[0]);
74
+    listenerSources.clear();
75
+    if (alGetError() != AL_NO_ERROR) {
76
+        getLog() << "SoundAL: Error while deleting listener sources!" << Log::endl;
77
+    }
78
+
79
+    alGetError();
80
+    alDeleteBuffers(buffers.size(), &buffers[0]);
81
+    if (alGetError() != AL_NO_ERROR) {
82
+        getLog() << "SoundAL: Error while deleting buffers!" << Log::endl;
83
+    }
84
+
85
+    for (int i = 0; i < 3; i++)
86
+        lastPosition[i] = 0.0f;
87
+}
88
+
89
+int SoundAL::numBuffers() {
90
+    return buffers.size();
91
+}
92
+
93
+int SoundAL::loadBuffer(unsigned char* buffer, unsigned int length) {
94
+    if (!init)
95
+        return -1;
96
+
97
+    alGetError();
98
+    unsigned int r = alutCreateBufferFromFileImage(buffer, length);
99
+    if (r == AL_NONE) {
100
+        getLog() << "SoundAL Error: " << alutGetErrorString(alutGetError()) << Log::endl;
101
+        return -2;
102
+    }
103
+    buffers.push_back(r);
104
+    return r;
105
+}
106
+
107
+int SoundAL::numSources(bool atListener) {
108
+    if (atListener)
109
+        return listenerSources.size();
110
+    else
111
+        return sources.size();
112
+}
113
+
114
+int SoundAL::addSource(int buffer, float volume, bool atListener, bool loop) {
115
+    if ((!init) || (buffer < 0) || (buffer >= buffers.size()))
116
+        return -1;
117
+
118
+    unsigned int id;
119
+
120
+    alGetError();
121
+    alGenSources(1, &id);
122
+    if (alGetError() != AL_NO_ERROR) {
123
+        getLog() << "SoundAL Error: Could not create source!" << Log::endl;
124
+        return -2;
125
+    }
126
+
127
+    alSourcei(id, AL_BUFFER, buffers.at(buffer));
128
+    alSourcef(id, AL_GAIN, volume);
129
+
130
+    if (loop)
131
+        alSourcei(id, AL_LOOPING, AL_TRUE);
132
+
133
+    if (atListener) {
134
+        alSourcefv(id, AL_POSITION, lastPosition);
135
+        listenerSources.push_back(id);
136
+        return listenerSources.size() - 1;
137
+    } else {
138
+        sources.push_back(id);
139
+        return sources.size() - 1;
140
+    }
141
+}
142
+
143
+int SoundAL::sourceAt(int source, float pos[3]) {
144
+    if (!init)
145
+        return -1;
146
+
147
+    if ((source < 0) || (source >= sources.size()) || (pos == nullptr)) {
148
+        getLog() << "SoundAL: Can't position non-existing source!" << Log::endl;
149
+        return -2;
150
+    }
151
+
152
+    alSourcefv(sources.at(source), AL_POSITION, pos);
153
+
154
+    return 0;
155
+}
156
+
157
+void SoundAL::listenAt(float pos[3], float orientation[6]) {
158
+    if ((!init) || (pos == nullptr) || (orientation == nullptr))
159
+        return;
160
+
161
+    alListenerfv(AL_POSITION, pos);
162
+    alListenerfv(AL_ORIENTATION, orientation);
163
+
164
+    for (auto& s : listenerSources) {
165
+        alSourcefv(s, AL_POSITION, pos);
166
+    }
167
+
168
+    for (int i = 0; i < 3; i++)
169
+        lastPosition[i] = pos[i];
170
+}
171
+
172
+void SoundAL::play(int source, bool atListener) {
173
+    if ((!init) || (!enabled))
174
+        return;
175
+
176
+    if (atListener) {
177
+        if ((source >= 0) && (source < listenerSources.size()))
178
+            alSourcePlay(listenerSources.at(source));
179
+        else
180
+            getLog() << "SoundAL: Can't play non-existing listener source!" << Log::endl;
181
+    } else {
182
+        if ((source >= 0) && (source < sources.size()))
183
+            alSourcePlay(sources.at(source));
184
+        else
185
+            getLog() << "SoundAL: Can't play non-existing source!" << Log::endl;
186
+    }
187
+}
188
+
189
+void SoundAL::stopAll() {
190
+    if (!init)
191
+        return;
192
+
193
+    alSourceStopv(sources.size(), &sources[0]);
194
+    alSourceStopv(listenerSources.size(), &listenerSources[0]);
195
+}
196
+
197
+void SoundAL::setEnabled(bool on) {
198
+    enabled = on;
199
+}
200
+
201
+bool SoundAL::getEnabled() {
202
+    return enabled;
203
+}
204
+
205
+void SoundAL::setVolume(float vol) {
206
+    volume = vol;
207
+
208
+    if (init)
209
+        alListenerf(AL_GAIN, volume);
210
+}
211
+
212
+float SoundAL::getVolume() {
213
+    return volume;
214
+}
215
+

src/Window.cpp → src/system/Window.cpp Visa fil

@@ -13,7 +13,7 @@
13 13
 #include "global.h"
14 14
 #include "math/math.h"
15 15
 #include "utils/strings.h"
16
-#include "Window.h"
16
+#include "system/Window.h"
17 17
 
18 18
 int Window::initializeGL() {
19 19
     // Print driver support information

src/WindowGLUT.cpp → src/system/WindowGLUT.cpp Visa fil

@@ -13,7 +13,7 @@
13 13
 #include "RunTime.h"
14 14
 #include "UI.h"
15 15
 #include "utils/strings.h"
16
-#include "WindowGLUT.h"
16
+#include "system/WindowGLUT.h"
17 17
 
18 18
 //! \todo Modifier keys currently don't create keyboard events...
19 19
 

src/WindowSDL.cpp → src/system/WindowSDL.cpp Visa fil

@@ -11,7 +11,7 @@
11 11
 #include "RunTime.h"
12 12
 #include "UI.h"
13 13
 #include "utils/strings.h"
14
-#include "WindowSDL.h"
14
+#include "system/WindowSDL.h"
15 15
 
16 16
 #define SUBSYSTEMS_USED (SDL_INIT_VIDEO | SDL_INIT_EVENTS)
17 17
 

Laddar…
Avbryt
Spara