Handle failing openal init properly, add enable_sound and sound_volume settings

master
Perttu Ahola 2012-04-06 15:30:36 +03:00
parent a67540807a
commit 6a57eabb14
5 changed files with 31 additions and 6 deletions

View File

@ -107,6 +107,9 @@
#console_color = (0,0,0) #console_color = (0,0,0)
# In-game chat console background alpha (opaqueness, between 0 and 255) # In-game chat console background alpha (opaqueness, between 0 and 255)
#console_alpha = 200 #console_alpha = 200
# Sound settings
#enable_sound = true
#sound_volume = 0.7
# #
# Server stuff # Server stuff

View File

@ -98,6 +98,8 @@ void set_default_settings(Settings *settings)
settings->setDefault("opaque_water", "false"); settings->setDefault("opaque_water", "false");
settings->setDefault("console_color", "(0,0,0)"); settings->setDefault("console_color", "(0,0,0)");
settings->setDefault("console_alpha", "200"); settings->setDefault("console_alpha", "200");
settings->setDefault("enable_sound", "true");
settings->setDefault("sound_volume", "0.8");
// Server stuff // Server stuff
// "map-dir" doesn't exist by default. // "map-dir" doesn't exist by default.

View File

@ -955,10 +955,14 @@ void the_game(
ISoundManager *sound = NULL; ISoundManager *sound = NULL;
bool sound_is_dummy = false; bool sound_is_dummy = false;
#if USE_SOUND #if USE_SOUND
infostream<<"Attempting to use OpenAL audio"<<std::endl; if(g_settings->getBool("enable_sound")){
sound = createOpenALSoundManager(&soundfetcher); infostream<<"Attempting to use OpenAL audio"<<std::endl;
if(!sound) sound = createOpenALSoundManager(&soundfetcher);
infostream<<"Failed to initialize OpenAL audio"<<std::endl; if(!sound)
infostream<<"Failed to initialize OpenAL audio"<<std::endl;
} else {
infostream<<"Sound disabled."<<std::endl;
}
#endif #endif
if(!sound){ if(!sound){
infostream<<"Using dummy audio."<<std::endl; infostream<<"Using dummy audio."<<std::endl;
@ -2082,6 +2086,7 @@ void the_game(
v3f(0,0,0), // velocity v3f(0,0,0), // velocity
camera.getDirection(), camera.getDirection(),
camera.getCameraNode()->getUpVector()); camera.getCameraNode()->getUpVector());
sound->setListenerGain(g_settings->getFloat("sound_volume"));
/* /*
Update sound maker Update sound maker

View File

@ -58,6 +58,7 @@ public:
const std::string &filedata) = 0; const std::string &filedata) = 0;
virtual void updateListener(v3f pos, v3f vel, v3f at, v3f up) = 0; virtual void updateListener(v3f pos, v3f vel, v3f at, v3f up) = 0;
virtual void setListenerGain(float gain) = 0;
// playSound functions return -1 on failure, otherwise a handle to the // playSound functions return -1 on failure, otherwise a handle to the
// sound. If name=="", call should be ignored without error. // sound. If name=="", call should be ignored without error.
@ -83,6 +84,7 @@ public:
virtual bool loadSoundData(const std::string &name, virtual bool loadSoundData(const std::string &name,
const std::string &filedata) {return true;} const std::string &filedata) {return true;}
void updateListener(v3f pos, v3f vel, v3f at, v3f up) {} void updateListener(v3f pos, v3f vel, v3f at, v3f up) {}
void setListenerGain(float gain) {}
int playSound(const std::string &name, bool loop, int playSound(const std::string &name, bool loop,
float volume) {return 0;} float volume) {return 0;}
int playSoundAt(const std::string &name, bool loop, int playSoundAt(const std::string &name, bool loop,

View File

@ -198,12 +198,14 @@ private:
std::map<int, PlayingSound*> m_sounds_playing; std::map<int, PlayingSound*> m_sounds_playing;
v3f m_listener_pos; v3f m_listener_pos;
public: public:
bool m_is_initialized;
OpenALSoundManager(OnDemandSoundFetcher *fetcher): OpenALSoundManager(OnDemandSoundFetcher *fetcher):
m_fetcher(fetcher), m_fetcher(fetcher),
m_device(NULL), m_device(NULL),
m_context(NULL), m_context(NULL),
m_can_vorbis(false), m_can_vorbis(false),
m_next_id(1) m_next_id(1),
m_is_initialized(false)
{ {
ALCenum error = ALC_NO_ERROR; ALCenum error = ALC_NO_ERROR;
@ -252,6 +254,8 @@ public:
infostream<<"Audio: Initialized: OpenAL "<<alGetString(AL_VERSION) infostream<<"Audio: Initialized: OpenAL "<<alGetString(AL_VERSION)
<<", using "<<alcGetString(m_device, ALC_DEVICE_SPECIFIER) <<", using "<<alcGetString(m_device, ALC_DEVICE_SPECIFIER)
<<std::endl; <<std::endl;
m_is_initialized = true;
} }
~OpenALSoundManager() ~OpenALSoundManager()
@ -465,6 +469,11 @@ public:
alListenerfv(AL_ORIENTATION, f); alListenerfv(AL_ORIENTATION, f);
warn_if_error(alGetError(), "updateListener"); warn_if_error(alGetError(), "updateListener");
} }
void setListenerGain(float gain)
{
alListenerf(AL_GAIN, gain);
}
int playSound(const std::string &name, bool loop, float volume) int playSound(const std::string &name, bool loop, float volume)
{ {
@ -519,6 +528,10 @@ public:
ISoundManager *createOpenALSoundManager(OnDemandSoundFetcher *fetcher) ISoundManager *createOpenALSoundManager(OnDemandSoundFetcher *fetcher)
{ {
return new OpenALSoundManager(fetcher); OpenALSoundManager *m = new OpenALSoundManager(fetcher);
if(m->m_is_initialized)
return m;
delete m;
return NULL;
}; };