-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made OpenAL context management more intelligent, in analogy to OpenGL…
… context management. OpenAL contexts now only exist as long as AlResources require them and are destroyed when they are no longer required. Fixes #30.
- Loading branch information
1 parent
fc850ed
commit 0ad401c
Showing
14 changed files
with
377 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//////////////////////////////////////////////////////////// | ||
// | ||
// SFML - Simple and Fast Multimedia Library | ||
// Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com) | ||
// | ||
// This software is provided 'as-is', without any express or implied warranty. | ||
// In no event will the authors be held liable for any damages arising from the use of this software. | ||
// | ||
// Permission is granted to anyone to use this software for any purpose, | ||
// including commercial applications, and to alter it and redistribute it freely, | ||
// subject to the following restrictions: | ||
// | ||
// 1. The origin of this software must not be misrepresented; | ||
// you must not claim that you wrote the original software. | ||
// If you use this software in a product, an acknowledgment | ||
// in the product documentation would be appreciated but is not required. | ||
// | ||
// 2. Altered source versions must be plainly marked as such, | ||
// and must not be misrepresented as being the original software. | ||
// | ||
// 3. This notice may not be removed or altered from any source distribution. | ||
// | ||
//////////////////////////////////////////////////////////// | ||
|
||
#ifndef SFML_ALRESOURCE_HPP | ||
#define SFML_ALRESOURCE_HPP | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Headers | ||
//////////////////////////////////////////////////////////// | ||
#include <SFML/Audio/Export.hpp> | ||
|
||
|
||
namespace sf | ||
{ | ||
//////////////////////////////////////////////////////////// | ||
/// \brief Base class for classes that require an OpenAL context | ||
/// | ||
//////////////////////////////////////////////////////////// | ||
class SFML_AUDIO_API AlResource | ||
{ | ||
protected: | ||
|
||
//////////////////////////////////////////////////////////// | ||
/// \brief Default constructor | ||
/// | ||
//////////////////////////////////////////////////////////// | ||
AlResource(); | ||
|
||
//////////////////////////////////////////////////////////// | ||
/// \brief Destructor | ||
/// | ||
//////////////////////////////////////////////////////////// | ||
~AlResource(); | ||
}; | ||
|
||
} // namespace sf | ||
|
||
|
||
#endif // SFML_ALRESOURCE_HPP | ||
|
||
//////////////////////////////////////////////////////////// | ||
/// \class sf::AlResource | ||
/// \ingroup audio | ||
/// | ||
/// This class is for internal use only, it must be the base | ||
/// of every class that requires a valid OpenAL context in | ||
/// order to work. | ||
/// | ||
//////////////////////////////////////////////////////////// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//////////////////////////////////////////////////////////// | ||
// | ||
// SFML - Simple and Fast Multimedia Library | ||
// Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com) | ||
// | ||
// This software is provided 'as-is', without any express or implied warranty. | ||
// In no event will the authors be held liable for any damages arising from the use of this software. | ||
// | ||
// Permission is granted to anyone to use this software for any purpose, | ||
// including commercial applications, and to alter it and redistribute it freely, | ||
// subject to the following restrictions: | ||
// | ||
// 1. The origin of this software must not be misrepresented; | ||
// you must not claim that you wrote the original software. | ||
// If you use this software in a product, an acknowledgment | ||
// in the product documentation would be appreciated but is not required. | ||
// | ||
// 2. Altered source versions must be plainly marked as such, | ||
// and must not be misrepresented as being the original software. | ||
// | ||
// 3. This notice may not be removed or altered from any source distribution. | ||
// | ||
//////////////////////////////////////////////////////////// | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Headers | ||
//////////////////////////////////////////////////////////// | ||
#include <SFML/Audio/AlResource.hpp> | ||
#include <SFML/Audio/AudioDevice.hpp> | ||
#include <SFML/System/Mutex.hpp> | ||
#include <SFML/System/Lock.hpp> | ||
|
||
|
||
namespace | ||
{ | ||
// OpenAL resources counter and its mutex | ||
unsigned int count = 0; | ||
sf::Mutex mutex; | ||
|
||
// The audio device is instantiated on demand rather than at global startup, | ||
// which solves a lot of weird crashes and errors. | ||
// It is destroyed when it is no longer needed. | ||
sf::priv::AudioDevice* globalDevice; | ||
} | ||
|
||
|
||
namespace sf | ||
{ | ||
//////////////////////////////////////////////////////////// | ||
AlResource::AlResource() | ||
{ | ||
// Protect from concurrent access | ||
Lock lock(mutex); | ||
|
||
// If this is the very first resource, trigger the global device initialization | ||
if (count == 0) | ||
globalDevice = new priv::AudioDevice; | ||
|
||
// Increment the resources counter | ||
count++; | ||
} | ||
|
||
|
||
//////////////////////////////////////////////////////////// | ||
AlResource::~AlResource() | ||
{ | ||
// Protect from concurrent access | ||
Lock lock(mutex); | ||
|
||
// Decrement the resources counter | ||
count--; | ||
|
||
// If there's no more resource alive, we can destroy the device | ||
if (count == 0) | ||
delete globalDevice; | ||
} | ||
|
||
} // namespace sf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,12 +29,18 @@ | |
#include <SFML/Audio/ALCheck.hpp> | ||
#include <SFML/Audio/Listener.hpp> | ||
#include <SFML/System/Err.hpp> | ||
#include <memory> | ||
|
||
|
||
namespace | ||
{ | ||
ALCdevice* audioDevice = NULL; | ||
ALCcontext* audioContext = NULL; | ||
|
||
float listenerVolume = 100.f; | ||
sf::Vector3f listenerPosition (0.f, 0.f, 0.f); | ||
sf::Vector3f listenerDirection(0.f, 0.f, -1.f); | ||
sf::Vector3f listenerUpVector (0.f, 1.f, 0.f); | ||
} | ||
|
||
namespace sf | ||
|
@@ -56,6 +62,17 @@ AudioDevice::AudioDevice() | |
{ | ||
// Set the context as the current one (we'll only need one) | ||
alcMakeContextCurrent(audioContext); | ||
|
||
// Apply the listener properties the user might have set | ||
float orientation[] = {listenerDirection.x, | ||
listenerDirection.y, | ||
listenerDirection.z, | ||
listenerUpVector.x, | ||
listenerUpVector.y, | ||
listenerUpVector.z}; | ||
alCheck(alListenerf(AL_GAIN, listenerVolume * 0.01f)); | ||
alCheck(alListener3f(AL_POSITION, listenerPosition.x, listenerPosition.y, listenerPosition.z)); | ||
alCheck(alListenerfv(AL_ORIENTATION, orientation)); | ||
} | ||
else | ||
{ | ||
|
@@ -86,7 +103,10 @@ AudioDevice::~AudioDevice() | |
//////////////////////////////////////////////////////////// | ||
bool AudioDevice::isExtensionSupported(const std::string& extension) | ||
{ | ||
ensureALInit(); | ||
// Create a temporary audio device in case none exists yet. | ||
std::auto_ptr<AudioDevice> device; | ||
if (!audioDevice) | ||
device = std::auto_ptr<AudioDevice>(new AudioDevice); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
binary1248
Author
Member
|
||
|
||
if ((extension.length() > 2) && (extension.substr(0, 3) == "ALC")) | ||
return alcIsExtensionPresent(audioDevice, extension.c_str()) != AL_FALSE; | ||
|
@@ -98,7 +118,10 @@ bool AudioDevice::isExtensionSupported(const std::string& extension) | |
//////////////////////////////////////////////////////////// | ||
int AudioDevice::getFormatFromChannelCount(unsigned int channelCount) | ||
{ | ||
ensureALInit(); | ||
// Create a temporary audio device in case none exists yet. | ||
std::auto_ptr<AudioDevice> device; | ||
if (!audioDevice) | ||
device = std::auto_ptr<AudioDevice>(new AudioDevice); | ||
|
||
// Find the good format according to the number of channels | ||
int format = 0; | ||
|
@@ -120,6 +143,92 @@ int AudioDevice::getFormatFromChannelCount(unsigned int channelCount) | |
return format; | ||
} | ||
|
||
|
||
//////////////////////////////////////////////////////////// | ||
void AudioDevice::setGlobalVolume(float volume) | ||
{ | ||
if (volume != listenerVolume) | ||
{ | ||
if (audioContext) | ||
alCheck(alListenerf(AL_GAIN, volume * 0.01f)); | ||
|
||
listenerVolume = volume; | ||
} | ||
} | ||
|
||
|
||
//////////////////////////////////////////////////////////// | ||
float AudioDevice::getGlobalVolume() | ||
{ | ||
return listenerVolume; | ||
} | ||
|
||
|
||
//////////////////////////////////////////////////////////// | ||
void AudioDevice::setPosition(const Vector3f& position) | ||
{ | ||
if (position != listenerPosition) | ||
{ | ||
if (audioContext) | ||
alCheck(alListener3f(AL_POSITION, position.x, position.y, position.z)); | ||
|
||
listenerPosition = position; | ||
} | ||
} | ||
|
||
|
||
//////////////////////////////////////////////////////////// | ||
Vector3f AudioDevice::getPosition() | ||
{ | ||
return listenerPosition; | ||
} | ||
|
||
|
||
//////////////////////////////////////////////////////////// | ||
void AudioDevice::setDirection(const Vector3f& direction) | ||
{ | ||
if (direction != listenerDirection) | ||
{ | ||
if (audioContext) | ||
{ | ||
float orientation[] = {direction.x, direction.y, direction.z, listenerUpVector.x, listenerUpVector.y, listenerUpVector.z}; | ||
alCheck(alListenerfv(AL_ORIENTATION, orientation)); | ||
} | ||
|
||
listenerDirection = direction; | ||
} | ||
} | ||
|
||
|
||
//////////////////////////////////////////////////////////// | ||
Vector3f AudioDevice::getDirection() | ||
{ | ||
return listenerDirection; | ||
} | ||
|
||
|
||
//////////////////////////////////////////////////////////// | ||
void AudioDevice::setUpVector(const Vector3f& upVector) | ||
{ | ||
if (upVector != listenerUpVector) | ||
{ | ||
if (audioContext) | ||
{ | ||
float orientation[] = {listenerDirection.x, listenerDirection.y, listenerDirection.z, upVector.x, upVector.y, upVector.z}; | ||
alCheck(alListenerfv(AL_ORIENTATION, orientation)); | ||
} | ||
|
||
listenerUpVector = upVector; | ||
} | ||
} | ||
|
||
|
||
//////////////////////////////////////////////////////////// | ||
Vector3f AudioDevice::getUpVector() | ||
{ | ||
return listenerUpVector; | ||
} | ||
|
||
} // namespace priv | ||
|
||
} // namespace sf |
Oops, something went wrong.
A bit strange... why not reset()?
If the idea is just to have a local variable, why not simply declare one (without auto_ptr)?