From d00ab43ea6654ad057043154a2287bccb0e43ef4 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Tue, 2 Apr 2024 18:09:10 -0600 Subject: [PATCH] Return `nullptr` when `sf::Context::getFunction` is called on Windows without an active context This converts termination in Debug and a SEGFAULT in Release builds into defined behavior. --- include/SFML/Window/Context.hpp | 3 +++ src/SFML/Window/Win32/WglContext.cpp | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/SFML/Window/Context.hpp b/include/SFML/Window/Context.hpp index ca1befeb0..1457eab01 100644 --- a/include/SFML/Window/Context.hpp +++ b/include/SFML/Window/Context.hpp @@ -131,6 +131,9 @@ public: //////////////////////////////////////////////////////////// /// \brief Get the address of an OpenGL function /// + /// On Windows when not using OpenGL ES, a context must be + /// active for this function to succeed. + /// /// \param name Name of the function to get the address of /// /// \return Address of the OpenGL function, 0 on failure diff --git a/src/SFML/Window/Win32/WglContext.cpp b/src/SFML/Window/Win32/WglContext.cpp index 357a74f7e..5eabc4a5b 100644 --- a/src/SFML/Window/Win32/WglContext.cpp +++ b/src/SFML/Window/Win32/WglContext.cpp @@ -203,8 +203,8 @@ WglContext::~WglContext() //////////////////////////////////////////////////////////// GlFunctionPointer WglContext::getFunction(const char* name) { - assert(WglContextImpl::currentContext != nullptr && - "Current WGL context cannot be null. Call WglContext::makeCurrent() to initialize it."); + if (WglContextImpl::currentContext == nullptr) + return nullptr; // If we are using the generic GDI implementation, skip to loading directly from OpenGL32.dll since it doesn't support extensions if (!WglContextImpl::currentContext->m_isGeneric)