Fixed OpenGL version string being parsed incorrectly on some platforms. Fixes #1249

This commit is contained in:
binary1248 2018-03-18 10:57:49 +01:00 committed by Lukas Dürrenberger
parent 15d0f789c7
commit a1065737db

View File

@ -38,6 +38,7 @@
#include <set>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cassert>
#if !defined(SFML_OPENGL_ES)
@ -199,6 +200,26 @@ namespace
// Supported OpenGL extensions
std::vector<std::string> extensions;
// Helper to parse OpenGL version strings
bool parseVersionString(const char* version, const char* prefix, unsigned int &major, unsigned int &minor)
{
std::size_t prefixLength = std::strlen(prefix);
if ((std::strlen(version) >= (prefixLength + 3)) &&
(std::strncmp(version, prefix, prefixLength) == 0) &&
std::isdigit(version[prefixLength]) &&
(version[prefixLength + 1] == '.') &&
std::isdigit(version[prefixLength + 2]))
{
major = version[prefixLength] - '0';
minor = version[prefixLength + 2] - '0';
return true;
}
return false;
}
}
@ -582,18 +603,30 @@ void GlContext::initialize(const ContextSettings& requestedSettings)
else
{
// Try the old way
const GLubyte* version = glGetString(GL_VERSION);
// If we can't get the version number, assume 1.1
m_settings.majorVersion = 1;
m_settings.minorVersion = 1;
const char* version = reinterpret_cast<const char*>(glGetString(GL_VERSION));
if (version)
{
// The beginning of the returned string is "major.minor" (this is standard)
m_settings.majorVersion = version[0] - '0';
m_settings.minorVersion = version[2] - '0';
// OpenGL ES Common Lite profile: The beginning of the returned string is "OpenGL ES-CL major.minor"
// OpenGL ES Common profile: The beginning of the returned string is "OpenGL ES-CM major.minor"
// OpenGL ES Full profile: The beginning of the returned string is "OpenGL ES major.minor"
// Desktop OpenGL: The beginning of the returned string is "major.minor"
if (!parseVersionString(version, "OpenGL ES-CL ", m_settings.majorVersion, m_settings.minorVersion) &&
!parseVersionString(version, "OpenGL ES-CM ", m_settings.majorVersion, m_settings.minorVersion) &&
!parseVersionString(version, "OpenGL ES ", m_settings.majorVersion, m_settings.minorVersion) &&
!parseVersionString(version, "", m_settings.majorVersion, m_settings.minorVersion))
{
err() << "Unable to parse OpenGL version string: \"" << version << "\", defaulting to 1.1" << std::endl;
}
}
else
{
// Can't get the version number, assume 1.1
m_settings.majorVersion = 1;
m_settings.minorVersion = 1;
err() << "Unable to retrieve OpenGL version string, defaulting to 1.1" << std::endl;
}
}