Skip to content

Commit

Permalink
Fixed OpenGL version string being parsed incorrectly on some platforms.
Browse files Browse the repository at this point in the history
Fixes #1249
  • Loading branch information
binary1248 committed Mar 19, 2018
1 parent 8b7a50a commit 607aa46
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions src/SFML/Window/GlContext.cpp
Expand Up @@ -38,6 +38,7 @@
#include <set>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cassert>

#if !defined(SFML_OPENGL_ES)
Expand Down Expand Up @@ -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 versionPosition = std::strlen(prefix);

if ((std::strlen(version) >= (versionPosition + 3)) &&
!std::strncmp(version, prefix, std::strlen(prefix)) &&
std::isdigit(version[versionPosition]) &&
(version[versionPosition + 1] == '.') &&
std::isdigit(version[versionPosition + 2]))
{
major = version[versionPosition] - '0';
minor = version[versionPosition + 2] - '0';

return true;
}

return false;
}
}


Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 607aa46

Please sign in to comment.