Fix sf::Context::setActive bug

This commit is contained in:
Chris Thrasher 2023-12-07 16:56:01 -07:00
parent 4e2f6ae655
commit cc5dc0f08c
2 changed files with 34 additions and 11 deletions

View File

@ -68,12 +68,14 @@ Context::~Context()
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool Context::setActive(bool active) bool Context::setActive(bool active)
{ {
const bool result = m_context->setActive(active); if (!m_context->setActive(active))
return false;
if (result) if (active)
ContextImpl::currentContext = (active ? this : nullptr); ContextImpl::currentContext = this;
else if (this == ContextImpl::currentContext)
return result; ContextImpl::currentContext = nullptr;
return true;
} }

View File

@ -29,19 +29,40 @@ TEST_CASE("[Window] sf::Context", runDisplayTests())
{ {
const sf::Context context; const sf::Context context;
CHECK(context.getSettings().majorVersion > 0); CHECK(context.getSettings().majorVersion > 0);
CHECK(context.getActiveContext() == &context); CHECK(sf::Context::getActiveContext() == &context);
CHECK(context.getActiveContextId() != 0); CHECK(sf::Context::getActiveContextId() != 0);
} }
SECTION("setActive()") SECTION("setActive()")
{ {
sf::Context context; sf::Context context;
const auto contextId = sf::Context::getActiveContextId();
// Set inactive
CHECK(context.setActive(false)); CHECK(context.setActive(false));
CHECK(context.getActiveContext() == nullptr); CHECK(sf::Context::getActiveContext() == nullptr);
CHECK(context.getActiveContextId() == 0); CHECK(sf::Context::getActiveContextId() == 0);
// Set active
CHECK(context.setActive(true)); CHECK(context.setActive(true));
CHECK(context.getActiveContext() == &context); CHECK(sf::Context::getActiveContext() == &context);
CHECK(context.getActiveContextId() != 0); 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()") SECTION("getActiveContext()/getActiveContextId()")