From f3a180bc09a9622855a33515430fcc9383a9747d Mon Sep 17 00:00:00 2001 From: Guillaume Bertholon Date: Wed, 14 Apr 2021 12:34:34 +0200 Subject: [PATCH] Fix sf::Context::getActiveContext to stop returning inactive contexts sf::Context::getActiveContext could return an inactive context if RenderTargets activation occured since the last sf::Context activation. This is not what its documentation suggest, and was clearly an unexpected behaviour. This fix make the function return NULL if the current active context is not managed by sf::Context (e.g. because it is a RenderTarget). --- src/SFML/Window/Context.cpp | 8 ++++++-- src/SFML/Window/GlContext.cpp | 7 +++++++ src/SFML/Window/GlContext.hpp | 8 ++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/SFML/Window/Context.cpp b/src/SFML/Window/Context.cpp index 1db2e33e..d10c48c3 100644 --- a/src/SFML/Window/Context.cpp +++ b/src/SFML/Window/Context.cpp @@ -32,7 +32,7 @@ namespace { - // This per-thread variable holds the current context for each thread + // This per-thread variable holds the last activated sf::Context for each thread sf::ThreadLocalPtr currentContext(NULL); } @@ -76,7 +76,11 @@ const ContextSettings& Context::getSettings() const //////////////////////////////////////////////////////////// const Context* Context::getActiveContext() { - return currentContext; + // We have to check that the last activated sf::Context is still active (a RenderTarget activation may have deactivated it) + if (currentContext && currentContext->m_context == priv::GlContext::getActiveContext()) + return currentContext; + else + return NULL; } diff --git a/src/SFML/Window/GlContext.cpp b/src/SFML/Window/GlContext.cpp index cb33c441..62dad263 100644 --- a/src/SFML/Window/GlContext.cpp +++ b/src/SFML/Window/GlContext.cpp @@ -549,6 +549,13 @@ GlFunctionPointer GlContext::getFunction(const char* name) } +//////////////////////////////////////////////////////////// +const GlContext* GlContext::getActiveContext() +{ + return currentContext; +} + + //////////////////////////////////////////////////////////// Uint64 GlContext::getActiveContextId() { diff --git a/src/SFML/Window/GlContext.hpp b/src/SFML/Window/GlContext.hpp index 816a68b4..5ca8134f 100644 --- a/src/SFML/Window/GlContext.hpp +++ b/src/SFML/Window/GlContext.hpp @@ -157,6 +157,14 @@ public: //////////////////////////////////////////////////////////// static GlFunctionPointer getFunction(const char* name); + //////////////////////////////////////////////////////////// + /// \brief Get the currently active context + /// + /// \return The currently active context or NULL if none is active + /// + //////////////////////////////////////////////////////////// + static const GlContext* getActiveContext(); + //////////////////////////////////////////////////////////// /// \brief Get the currently active context's ID ///