From cc5dc0f08ce13100f53ed199dad15521362257e6 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Thu, 7 Dec 2023 16:56:01 -0700 Subject: [PATCH] Fix `sf::Context::setActive` bug --- src/SFML/Window/Context.cpp | 12 +++++++----- test/Window/Context.test.cpp | 33 +++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/SFML/Window/Context.cpp b/src/SFML/Window/Context.cpp index 740a6543b..6ee6f5b3e 100644 --- a/src/SFML/Window/Context.cpp +++ b/src/SFML/Window/Context.cpp @@ -68,12 +68,14 @@ Context::~Context() //////////////////////////////////////////////////////////// bool Context::setActive(bool active) { - const bool result = m_context->setActive(active); + if (!m_context->setActive(active)) + return false; - if (result) - ContextImpl::currentContext = (active ? this : nullptr); - - return result; + if (active) + ContextImpl::currentContext = this; + else if (this == ContextImpl::currentContext) + ContextImpl::currentContext = nullptr; + return true; } diff --git a/test/Window/Context.test.cpp b/test/Window/Context.test.cpp index 8fd6719a1..4c9067775 100644 --- a/test/Window/Context.test.cpp +++ b/test/Window/Context.test.cpp @@ -29,19 +29,40 @@ TEST_CASE("[Window] sf::Context", runDisplayTests()) { const sf::Context context; CHECK(context.getSettings().majorVersion > 0); - CHECK(context.getActiveContext() == &context); - CHECK(context.getActiveContextId() != 0); + CHECK(sf::Context::getActiveContext() == &context); + CHECK(sf::Context::getActiveContextId() != 0); } SECTION("setActive()") { sf::Context context; + const auto contextId = sf::Context::getActiveContextId(); + + // Set inactive CHECK(context.setActive(false)); - CHECK(context.getActiveContext() == nullptr); - CHECK(context.getActiveContextId() == 0); + CHECK(sf::Context::getActiveContext() == nullptr); + CHECK(sf::Context::getActiveContextId() == 0); + + // Set active CHECK(context.setActive(true)); - CHECK(context.getActiveContext() == &context); - CHECK(context.getActiveContextId() != 0); + CHECK(sf::Context::getActiveContext() == &context); + CHECK(sf::Context::getActiveContextId() == contextId); + + // Create new context which becomes active automatically + const sf::Context newContext; + CHECK(sf::Context::getActiveContext() == &newContext); + const auto newContextId = sf::Context::getActiveContextId(); + CHECK(newContextId != 0); + + // Set old context as inactive but new context remains active + CHECK(context.setActive(false)); + CHECK(sf::Context::getActiveContext() == &newContext); + CHECK(sf::Context::getActiveContextId() == newContextId); + + // Set old context as active again + CHECK(context.setActive(true)); + CHECK(sf::Context::getActiveContext() == &context); + CHECK(sf::Context::getActiveContextId() == contextId); } SECTION("getActiveContext()/getActiveContextId()")