Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SFML unity builds #1787

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/SFML/Audio/AlResource.cpp
Expand Up @@ -34,13 +34,13 @@
namespace
{
// OpenAL resources counter and its mutex
unsigned int count = 0;
sf::Mutex mutex;
unsigned int sfmlAlResourceCount = 0;
sf::Mutex sfmlAlResourceMutex;

// 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;
sf::priv::AudioDevice* sfmlAlGlobalDevice;
}


Expand All @@ -50,29 +50,29 @@ namespace sf
AlResource::AlResource()
{
// Protect from concurrent access
Lock lock(mutex);
Lock lock(sfmlAlResourceMutex);

// If this is the very first resource, trigger the global device initialization
if (count == 0)
globalDevice = new priv::AudioDevice;
if (sfmlAlResourceCount == 0)
sfmlAlGlobalDevice = new priv::AudioDevice;

// Increment the resources counter
count++;
sfmlAlResourceCount++;
}


////////////////////////////////////////////////////////////
AlResource::~AlResource()
{
// Protect from concurrent access
Lock lock(mutex);
Lock lock(sfmlAlResourceMutex);

// Decrement the resources counter
count--;
sfmlAlResourceCount--;

// If there's no more resource alive, we can destroy the device
if (count == 0)
delete globalDevice;
if (sfmlAlResourceCount == 0)
delete sfmlAlGlobalDevice;
}

} // namespace sf
26 changes: 13 additions & 13 deletions src/SFML/Audio/SoundRecorder.cpp
Expand Up @@ -40,7 +40,7 @@

namespace
{
ALCdevice* captureDevice = NULL;
ALCdevice* sfmlALCDeviceCaptureDevice = NULL;
}

namespace sf
Expand Down Expand Up @@ -81,7 +81,7 @@ bool SoundRecorder::start(unsigned int sampleRate)
}

// Check that another capture is not already running
if (captureDevice)
if (sfmlALCDeviceCaptureDevice)
{
err() << "Trying to start audio capture, but another capture is already running" << std::endl;
return false;
Expand All @@ -91,8 +91,8 @@ bool SoundRecorder::start(unsigned int sampleRate)
ALCenum format = (m_channelCount == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;

// Open the capture device for capturing 16 bits samples
captureDevice = alcCaptureOpenDevice(m_deviceName.c_str(), sampleRate, format, sampleRate);
if (!captureDevice)
sfmlALCDeviceCaptureDevice = alcCaptureOpenDevice(m_deviceName.c_str(), sampleRate, format, sampleRate);
if (!sfmlALCDeviceCaptureDevice)
{
err() << "Failed to open the audio capture device with the name: " << m_deviceName << std::endl;
return false;
Expand All @@ -108,7 +108,7 @@ bool SoundRecorder::start(unsigned int sampleRate)
if (onStart())
{
// Start the capture
alcCaptureStart(captureDevice);
alcCaptureStart(sfmlALCDeviceCaptureDevice);

// Start the capture in a new thread, to avoid blocking the main thread
m_isCapturing = true;
Expand Down Expand Up @@ -188,8 +188,8 @@ bool SoundRecorder::setDevice(const std::string& name)
ALCenum format = (m_channelCount == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;

// Open the requested capture device for capturing 16 bits samples
captureDevice = alcCaptureOpenDevice(m_deviceName.c_str(), m_sampleRate, format, m_sampleRate);
if (!captureDevice)
sfmlALCDeviceCaptureDevice = alcCaptureOpenDevice(m_deviceName.c_str(), m_sampleRate, format, m_sampleRate);
if (!sfmlALCDeviceCaptureDevice)
{
// Notify derived class
onStop();
Expand All @@ -199,7 +199,7 @@ bool SoundRecorder::setDevice(const std::string& name)
}

// Start the capture
alcCaptureStart(captureDevice);
alcCaptureStart(sfmlALCDeviceCaptureDevice);

// Start the capture in a new thread, to avoid blocking the main thread
m_isCapturing = true;
Expand Down Expand Up @@ -295,13 +295,13 @@ void SoundRecorder::processCapturedSamples()
{
// Get the number of samples available
ALCint samplesAvailable;
alcGetIntegerv(captureDevice, ALC_CAPTURE_SAMPLES, 1, &samplesAvailable);
alcGetIntegerv(sfmlALCDeviceCaptureDevice, ALC_CAPTURE_SAMPLES, 1, &samplesAvailable);

if (samplesAvailable > 0)
{
// Get the recorded samples
m_samples.resize(samplesAvailable * getChannelCount());
alcCaptureSamples(captureDevice, &m_samples[0], samplesAvailable);
alcCaptureSamples(sfmlALCDeviceCaptureDevice, &m_samples[0], samplesAvailable);

// Forward them to the derived class
if (!onProcessSamples(&m_samples[0], m_samples.size()))
Expand All @@ -317,14 +317,14 @@ void SoundRecorder::processCapturedSamples()
void SoundRecorder::cleanup()
{
// Stop the capture
alcCaptureStop(captureDevice);
alcCaptureStop(sfmlALCDeviceCaptureDevice);

// Get the samples left in the buffer
processCapturedSamples();

// Close the device
alcCaptureCloseDevice(captureDevice);
captureDevice = NULL;
alcCaptureCloseDevice(sfmlALCDeviceCaptureDevice);
sfmlALCDeviceCaptureDevice = NULL;
}

} // namespace sf
3 changes: 2 additions & 1 deletion src/SFML/Graphics/GLExtensions.cpp
Expand Up @@ -25,10 +25,11 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#define SF_GLAD_GL_IMPLEMENTATION
#include <SFML/Graphics/GLExtensions.hpp>
#include <SFML/Window/Context.hpp>
#include <SFML/System/Err.hpp>
#define SF_GLAD_GL_IMPLEMENTATION
#include <glad/gl.h>

#if !defined(GL_MAJOR_VERSION)
#define GL_MAJOR_VERSION 0x821B
Expand Down
10 changes: 5 additions & 5 deletions src/SFML/Graphics/RenderTarget.cpp
Expand Up @@ -53,13 +53,13 @@
namespace
{
// Mutex to protect ID generation and our context-RenderTarget-map
sf::Mutex mutex;
sf::Mutex sfmlRenderTargetMutex;

// Unique identifier, used for identifying RenderTargets when
// tracking the currently active RenderTarget within a given context
sf::Uint64 getUniqueId()
sf::Uint64 sfmlRenderTargetGetUniqueId()
{
sf::Lock lock(mutex);
sf::Lock lock(sfmlRenderTargetMutex);

static sf::Uint64 id = 1; // start at 1, zero is "no RenderTarget"

Expand Down Expand Up @@ -416,7 +416,7 @@ bool RenderTarget::setActive(bool active)
{
// Mark this RenderTarget as active or no longer active in the tracking map
{
sf::Lock lock(mutex);
sf::Lock lock(sfmlRenderTargetMutex);

Uint64 contextId = Context::getActiveContextId();

Expand Down Expand Up @@ -574,7 +574,7 @@ void RenderTarget::initialize()

// Generate a unique ID for this RenderTarget to track
// whether it is active within a specific context
m_id = getUniqueId();
m_id = sfmlRenderTargetGetUniqueId();
}


Expand Down
26 changes: 14 additions & 12 deletions src/SFML/Graphics/RenderTextureImplFBO.cpp
Expand Up @@ -40,15 +40,15 @@ namespace
// Set to track all active FBO mappings
// This is used to free active FBOs while their owning
// RenderTextureImplFBO is still alive
std::set<std::map<sf::Uint64, unsigned int>*> frameBuffers;
std::set<std::map<sf::Uint64, unsigned int>*> sfmlRenderTextureImplFBOFrameBuffers;

// Set to track all stale FBOs
// This is used to free stale FBOs after their owning
// RenderTextureImplFBO has already been destroyed
// An FBO cannot be destroyed until it's containing context
// becomes active, so the destruction of the RenderTextureImplFBO
// has to be decoupled from the destruction of the FBOs themselves
std::set<std::pair<sf::Uint64, unsigned int> > staleFrameBuffers;
std::set<std::pair<sf::Uint64, unsigned int> > sfmlRenderTextureImplStaleFrameBuffers;

// Mutex to protect both active and stale frame buffer sets
sf::Mutex mutex;
Expand All @@ -60,14 +60,15 @@ namespace
{
sf::Uint64 contextId = sf::Context::getActiveContextId();

for (std::set<std::pair<sf::Uint64, unsigned int> >::iterator iter = staleFrameBuffers.begin(); iter != staleFrameBuffers.end();)
for (std::set<std::pair<sf::Uint64, unsigned int> >::iterator iter = sfmlRenderTextureImplStaleFrameBuffers.begin();
iter != sfmlRenderTextureImplStaleFrameBuffers.end();)
{
if (iter->first == contextId)
{
GLuint frameBuffer = static_cast<GLuint>(iter->second);
glCheck(GLEXT_glDeleteFramebuffers(1, &frameBuffer));

staleFrameBuffers.erase(iter++);
sfmlRenderTextureImplStaleFrameBuffers.erase(iter++);
}
else
{
Expand All @@ -84,7 +85,8 @@ namespace
sf::Uint64 contextId = sf::Context::getActiveContextId();

// Destroy active frame buffer objects
for (std::set<std::map<sf::Uint64, unsigned int>*>::iterator frameBuffersIter = frameBuffers.begin(); frameBuffersIter != frameBuffers.end(); ++frameBuffersIter)
for (std::set<std::map<sf::Uint64, unsigned int>*>::iterator frameBuffersIter = sfmlRenderTextureImplFBOFrameBuffers.begin();
frameBuffersIter != sfmlRenderTextureImplFBOFrameBuffers.end(); ++frameBuffersIter)
{
for (std::map<sf::Uint64, unsigned int>::iterator iter = (*frameBuffersIter)->begin(); iter != (*frameBuffersIter)->end(); ++iter)
{
Expand Down Expand Up @@ -128,8 +130,8 @@ m_stencil (false)
registerContextDestroyCallback(contextDestroyCallback, 0);

// Insert the new frame buffer mapping into the set of all active mappings
frameBuffers.insert(&m_frameBuffers);
frameBuffers.insert(&m_multisampleFrameBuffers);
sfmlRenderTextureImplFBOFrameBuffers.insert(&m_frameBuffers);
sfmlRenderTextureImplFBOFrameBuffers.insert(&m_multisampleFrameBuffers);
}


Expand All @@ -141,8 +143,8 @@ RenderTextureImplFBO::~RenderTextureImplFBO()
Lock lock(mutex);

// Remove the frame buffer mapping from the set of all active mappings
frameBuffers.erase(&m_frameBuffers);
frameBuffers.erase(&m_multisampleFrameBuffers);
sfmlRenderTextureImplFBOFrameBuffers.erase(&m_frameBuffers);
sfmlRenderTextureImplFBOFrameBuffers.erase(&m_multisampleFrameBuffers);

// Destroy the color buffer
if (m_colorBuffer)
Expand All @@ -160,10 +162,10 @@ RenderTextureImplFBO::~RenderTextureImplFBO()

// Move all frame buffer objects to stale set
for (std::map<Uint64, unsigned int>::iterator iter = m_frameBuffers.begin(); iter != m_frameBuffers.end(); ++iter)
staleFrameBuffers.insert(std::make_pair(iter->first, iter->second));
sfmlRenderTextureImplStaleFrameBuffers.insert(std::make_pair(iter->first, iter->second));

for (std::map<Uint64, unsigned int>::iterator iter = m_multisampleFrameBuffers.begin(); iter != m_multisampleFrameBuffers.end(); ++iter)
staleFrameBuffers.insert(std::make_pair(iter->first, iter->second));
sfmlRenderTextureImplStaleFrameBuffers.insert(std::make_pair(iter->first, iter->second));

// Clean up FBOs
destroyStaleFBOs();
Expand Down Expand Up @@ -538,7 +540,7 @@ bool RenderTextureImplFBO::activate(bool active)
Lock lock(mutex);

std::map<Uint64, unsigned int>::iterator iter;

if (m_multisample)
{
iter = m_multisampleFrameBuffers.find(contextId);
Expand Down
10 changes: 5 additions & 5 deletions src/SFML/Graphics/Shader.cpp
Expand Up @@ -56,8 +56,8 @@

namespace
{
sf::Mutex maxTextureUnitsMutex;
sf::Mutex isAvailableMutex;
sf::Mutex sfmlShaderMaxTextureUnitsMutex;
sf::Mutex sfmlShaderIsAvailableMutex;

GLint checkMaxTextureUnits()
{
Expand All @@ -71,7 +71,7 @@ namespace
GLint getMaxTextureUnits()
{
// TODO: Remove this lock when it becomes unnecessary in C++11
sf::Lock lock(maxTextureUnitsMutex);
sf::Lock lock(sfmlShaderMaxTextureUnitsMutex);

static GLint maxUnits = checkMaxTextureUnits();

Expand Down Expand Up @@ -773,7 +773,7 @@ void Shader::bind(const Shader* shader)
////////////////////////////////////////////////////////////
bool Shader::isAvailable()
{
Lock lock(isAvailableMutex);
Lock lock(sfmlShaderIsAvailableMutex);

static bool checked = false;
static bool available = false;
Expand Down Expand Up @@ -801,7 +801,7 @@ bool Shader::isAvailable()
////////////////////////////////////////////////////////////
bool Shader::isGeometryAvailable()
{
Lock lock(isAvailableMutex);
Lock lock(sfmlShaderIsAvailableMutex);

static bool checked = false;
static bool available = false;
Expand Down