Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
SDLWrapper.cpp
Go to the documentation of this file.
1
2
4
9
10#include <SDL2/SDL_mixer.h>
11
12namespace Divide {
13
14namespace
15{
17};
18
19void musicFinishedHook() noexcept
20{
21 if (g_sfxDevice)
22 {
23 g_sfxDevice->musicFinished();
24 }
25}
26
28 : AudioAPIWrapper("SDL", context)
29{
30}
31
33{
34 constexpr I32 flags = MIX_INIT_OGG | MIX_INIT_MP3 | MIX_INIT_FLAC/* | MIX_INIT_MOD*/;
35
36 const I32 ret = Mix_Init(flags);
37 if ((ret & flags) == flags)
38 {
39 // Try HiFi sound
40 if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096) == -1)
41 {
42 Console::errorfn("{}", Mix_GetError());
43 // Try lower quality
44 if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 2048) == -1)
45 {
46 Console::errorfn("{}", Mix_GetError());
48 }
49 }
50
51 g_sfxDevice = &_context.sfx();
52 Mix_HookMusicFinished(musicFinishedHook);
53 return ErrorCode::NO_ERR;
54 }
55
56 Console::errorfn("{}", Mix_GetError());
58}
59
61{
62 Mix_HaltMusic();
63 for (const MusicMap::value_type& it : _musicMap)
64 {
65 Mix_FreeMusic(it.second);
66 }
67 for (const SoundMap::value_type& it : _soundMap)
68 {
69 Mix_FreeChunk(it.second);
70 }
71 Mix_CloseAudio();
72 Mix_Quit();
73 g_sfxDevice = nullptr;
74}
75
76void SDL_API::stopMusic() noexcept
77{
78 Mix_HaltMusic();
79}
80
82{
83}
84
86{
87 if (music != INVALID_HANDLE<AudioDescriptor>)
88 {
89 ResourcePtr<AudioDescriptor> musicPtr = Get(music);
90
91 Mix_Music* mixMusicPtr = nullptr;
92 const MusicMap::iterator it = _musicMap.find(music._data);
93 if (it == std::cend(_musicMap))
94 {
95 mixMusicPtr = Mix_LoadMUS((musicPtr->assetLocation() / musicPtr->assetName()).string().c_str() );
96 insert(_musicMap, music._data, mixMusicPtr);
97 }
98 else
99 {
100
101 if ( musicPtr->dirty())
102 {
103 mixMusicPtr = Mix_LoadMUS( (musicPtr->assetLocation() / musicPtr->assetName()).string().c_str() );
104 Mix_FreeMusic(it->second);
105 it->second = mixMusicPtr;
106 musicPtr->clean();
107 }
108 else
109 {
110 mixMusicPtr = it->second;
111 }
112 }
113
114 if( mixMusicPtr )
115 {
116 Mix_VolumeMusic( musicPtr->volume());
117 if (Mix_PlayMusic( mixMusicPtr, musicPtr->isLooping() ? -1 : 0) == -1)
118 {
119 Console::errorfn("{}", Mix_GetError());
120 }
121 }
122 else
123 {
124 Console::errorfn(LOCALE_STR("ERROR_SDL_LOAD_SOUND"), Get( music )->resourceName().c_str());
125 }
126 }
127}
128
130{
131 if (sound != INVALID_HANDLE<AudioDescriptor> )
132 {
133 ResourcePtr<AudioDescriptor> soundPtr = Get( sound );
134
135 Mix_Chunk* mixSoundPtr = nullptr;
136 const SoundMap::iterator it = _soundMap.find(sound._data);
137 if (it == std::cend(_soundMap))
138 {
139 mixSoundPtr = Mix_LoadWAV( (soundPtr->assetLocation() / soundPtr->assetName()).string().c_str() );
140 insert(_soundMap, sound._data, mixSoundPtr );
141 }
142 else
143 {
144
145 if ( soundPtr->dirty())
146 {
147 mixSoundPtr = Mix_LoadWAV( (soundPtr->assetLocation() / soundPtr->assetName()).string().c_str() );
148 Mix_FreeChunk(it->second);
149 it->second = mixSoundPtr;
150 soundPtr->clean();
151 }
152 else
153 {
154 mixSoundPtr = it->second;
155 }
156 }
157
158 if ( mixSoundPtr )
159 {
160 Mix_Volume( soundPtr->channelID(), soundPtr->volume());
161 if (Mix_PlayChannel( soundPtr->channelID(), mixSoundPtr, soundPtr->isLooping() ? -1 : 0) == -1)
162 {
163 Console::errorfn(LOCALE_STR("ERROR_SDL_CANT_PLAY"), soundPtr->resourceName().c_str(), Mix_GetError());
164 }
165 }
166 else
167 {
168 Console::errorfn(LOCALE_STR("ERROR_SDL_LOAD_SOUND"), soundPtr->resourceName().c_str());
169 }
170 }
171}
172
173}; //namespace Divide
#define LOCALE_STR(X)
Definition: Localization.h:91
struct _Mix_Music Mix_Music
Definition: SDLWrapper.h:38
Audio Programming Interface.
SFXDevice & sfx() noexcept
void playMusic(const Handle< AudioDescriptor > music) override
Definition: SDLWrapper.cpp:85
SDL_API(PlatformContext &context)
Definition: SDLWrapper.cpp:27
SoundMap _soundMap
Definition: SDLWrapper.h:68
void closeAudioAPI() override
Definition: SDLWrapper.cpp:60
MusicMap _musicMap
Definition: SDLWrapper.h:67
ErrorCode initAudioAPI() override
Definition: SDLWrapper.cpp:32
void musicFinished() noexcept override
Definition: SDLWrapper.cpp:81
void playSound(const Handle< AudioDescriptor > sound) override
Definition: SDLWrapper.cpp:129
void stopMusic() noexcept override
Definition: SDLWrapper.cpp:76
void musicFinished() noexcept override
Definition: SFXDevice.cpp:153
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
void insert(eastl::vector< T, A1 > &target, const eastl::vector< T, A2 > &source)
Definition: Vector.h:97
int32_t I32
T * ResourcePtr
Definition: Resource.h:112
FORCE_INLINE T * Get(const Handle< T > handle)
void musicFinishedHook() noexcept
Definition: SDLWrapper.cpp:19
static NO_INLINE void errorfn(const char *format, T &&... args)