Rework modules
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include <Nazara/Core/Core.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/Log.hpp>
|
||||
#include <stdexcept>
|
||||
#include <Nazara/Audio/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
@@ -22,6 +23,32 @@ namespace Nz
|
||||
* \brief Audio class that represents the module initializer of Audio
|
||||
*/
|
||||
|
||||
Audio::Audio() :
|
||||
Module("Audio", this)
|
||||
{
|
||||
// Initialisation of OpenAL
|
||||
if (!OpenAL::Initialize())
|
||||
throw std::runtime_error("failed to initialize OpenAL");
|
||||
|
||||
if (!SoundBuffer::Initialize())
|
||||
throw std::runtime_error("failed to initialize sound buffers");
|
||||
|
||||
// Definition of the orientation by default
|
||||
SetListenerDirection(Vector3f::Forward());
|
||||
|
||||
// Loaders
|
||||
Loaders::Register_sndfile();
|
||||
}
|
||||
|
||||
Audio::~Audio()
|
||||
{
|
||||
// Loaders
|
||||
Loaders::Unregister_sndfile();
|
||||
|
||||
SoundBuffer::Uninitialize();
|
||||
OpenAL::Uninitialize();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the format of the audio
|
||||
* \return AudioFormat Enumeration type for the format
|
||||
@@ -142,59 +169,6 @@ namespace Nz
|
||||
return alGetFloat(AL_SPEED_OF_SOUND);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Initializes the Audio module
|
||||
* \return true if initialization is successful
|
||||
*
|
||||
* \remark Produces a NazaraError if initialization of modules Core, OpenAL or SoundBuffer failed
|
||||
* \remark Produces a NazaraNotice
|
||||
*/
|
||||
|
||||
bool Audio::Initialize()
|
||||
{
|
||||
if (IsInitialized())
|
||||
{
|
||||
s_moduleReferenceCounter++;
|
||||
return true; // Already initialized
|
||||
}
|
||||
|
||||
// Initialisation of dependencies
|
||||
if (!Core::Initialize())
|
||||
{
|
||||
NazaraError("Failed to initialize core module");
|
||||
return false;
|
||||
}
|
||||
|
||||
s_moduleReferenceCounter++;
|
||||
|
||||
// Initialisation of the module
|
||||
CallOnExit onExit(Audio::Uninitialize);
|
||||
|
||||
// Initialisation of OpenAL
|
||||
if (!OpenAL::Initialize())
|
||||
{
|
||||
NazaraError("Failed to initialize OpenAL");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!SoundBuffer::Initialize())
|
||||
{
|
||||
NazaraError("Failed to initialize sound buffers");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Definition of the orientation by default
|
||||
SetListenerDirection(Vector3f::Forward());
|
||||
|
||||
// Loaders
|
||||
Loaders::Register_sndfile();
|
||||
|
||||
onExit.Reset();
|
||||
|
||||
NazaraNotice("Initialized: Audio module");
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether the format is supported by the engine
|
||||
* \return true if it is the case
|
||||
@@ -210,16 +184,6 @@ namespace Nz
|
||||
return OpenAL::AudioFormat[format] != 0;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether the module is initialized
|
||||
* \return true if module is initialized
|
||||
*/
|
||||
|
||||
bool Audio::IsInitialized()
|
||||
{
|
||||
return s_moduleReferenceCounter != 0;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the factor of the doppler effect
|
||||
*
|
||||
@@ -367,37 +331,5 @@ namespace Nz
|
||||
alSpeedOfSound(speed);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Uninitializes the Audio module
|
||||
*
|
||||
* \remark Produces a NazaraNotice
|
||||
*/
|
||||
|
||||
void Audio::Uninitialize()
|
||||
{
|
||||
if (s_moduleReferenceCounter != 1)
|
||||
{
|
||||
// The module is still in use, or can not be uninitialized
|
||||
if (s_moduleReferenceCounter > 1)
|
||||
s_moduleReferenceCounter--;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Free of module
|
||||
s_moduleReferenceCounter = 0;
|
||||
|
||||
// Loaders
|
||||
Loaders::Unregister_sndfile();
|
||||
|
||||
SoundBuffer::Uninitialize();
|
||||
OpenAL::Uninitialize();
|
||||
|
||||
NazaraNotice("Uninitialized: Audio module");
|
||||
|
||||
// Free of dependencies
|
||||
Core::Uninitialize();
|
||||
}
|
||||
|
||||
unsigned int Audio::s_moduleReferenceCounter = 0;
|
||||
Audio* Audio::s_instance = nullptr;
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ namespace Nz
|
||||
m_handle = nullptr;
|
||||
});
|
||||
|
||||
m_format = Audio::GetAudioFormat(infos.channels);
|
||||
m_format = Audio::Instance()->GetAudioFormat(infos.channels);
|
||||
if (m_format == AudioFormat_Unknown)
|
||||
{
|
||||
NazaraError("Channel count not handled");
|
||||
@@ -327,7 +327,7 @@ namespace Nz
|
||||
sf_close(file);
|
||||
});
|
||||
|
||||
AudioFormat format = Audio::GetAudioFormat(info.channels);
|
||||
AudioFormat format = Audio::Instance()->GetAudioFormat(info.channels);
|
||||
if (format == AudioFormat_Unknown)
|
||||
{
|
||||
NazaraError("Channel count not handled");
|
||||
|
||||
@@ -259,7 +259,7 @@ namespace Nz
|
||||
*/
|
||||
bool SoundBuffer::IsFormatSupported(AudioFormat format)
|
||||
{
|
||||
return Audio::IsFormatSupported(format);
|
||||
return Audio::Instance()->IsFormatSupported(format);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
||||
Reference in New Issue
Block a user