From 49c0208b2ef177e7420e38564751924f20438f49 Mon Sep 17 00:00:00 2001
From: Laurent Gomila <laurent.gom@gmail.com>
Date: Wed, 4 May 2011 22:35:15 +0200
Subject: [PATCH] The version returned by Window::GetSettings() is now the
 actual version of the context

---
 src/SFML/Window/GlContext.cpp        | 46 +++++++++++++++++++++-------
 src/SFML/Window/GlContext.hpp        | 12 ++++++--
 src/SFML/Window/GlResource.cpp       |  4 +--
 src/SFML/Window/Linux/GlxContext.cpp |  9 ------
 src/SFML/Window/OSX/SFContext.hpp    |  7 -----
 src/SFML/Window/OSX/SFContext.mm     | 45 ---------------------------
 src/SFML/Window/Win32/WglContext.cpp |  9 ------
 7 files changed, 47 insertions(+), 85 deletions(-)

diff --git a/src/SFML/Window/GlContext.cpp b/src/SFML/Window/GlContext.cpp
index 3c5b4ef9..b1803576 100644
--- a/src/SFML/Window/GlContext.cpp
+++ b/src/SFML/Window/GlContext.cpp
@@ -102,7 +102,7 @@ namespace sf
 namespace priv
 {
 ////////////////////////////////////////////////////////////
-void GlContext::Initialize()
+void GlContext::GlobalInit()
 {
     // Create the shared context
     sharedContext = new ContextType(NULL);
@@ -115,7 +115,7 @@ void GlContext::Initialize()
 
 
 ////////////////////////////////////////////////////////////
-void GlContext::Cleanup()
+void GlContext::GlobalCleanup()
 {
     // Destroy the shared context
     delete sharedContext;
@@ -141,7 +141,10 @@ void GlContext::EnsureContext()
 ////////////////////////////////////////////////////////////
 GlContext* GlContext::New()
 {
-    return new ContextType(sharedContext);
+    GlContext* context = new ContextType(sharedContext);
+    context->Initialize();
+
+    return context;
 }
 
 
@@ -153,10 +156,7 @@ GlContext* GlContext::New(const ContextSettings& settings, const WindowImpl* own
 
     // Create the context
     GlContext* context = new ContextType(sharedContext, settings, owner, bitsPerPixel);
-
-    // Enable antialiasing if needed
-    if (context->GetSettings().AntialiasingLevel > 0)
-        glEnable(GL_MULTISAMPLE_ARB);
+    context->Initialize();
 
     return context;
 }
@@ -170,10 +170,7 @@ GlContext* GlContext::New(const ContextSettings& settings, unsigned int width, u
 
     // Create the context
     GlContext* context = new ContextType(sharedContext, settings, width, height);
-
-    // Enable antialiasing if needed
-    if (context->GetSettings().AntialiasingLevel > 0)
-        glEnable(GL_MULTISAMPLE_ARB);
+    context->Initialize();
 
     return context;
 }
@@ -253,6 +250,33 @@ int GlContext::EvaluateFormat(unsigned int bitsPerPixel, const ContextSettings&
            std::abs(static_cast<int>(settings.AntialiasingLevel - antialiasing));
 }
 
+
+////////////////////////////////////////////////////////////
+void GlContext::Initialize()
+{
+    // Activate the context
+    SetActive(true);
+
+    // Retrieve the context version number
+    const GLubyte* version = glGetString(GL_VERSION);
+    if (version)
+    {
+        // The beginning of the returned string is "major.minor" (this is standard)
+        mySettings.MajorVersion = version[0] - '0';
+        mySettings.MinorVersion = version[2] - '0';
+    }
+    else
+    {
+        // Can't get the version number, assume 2.0
+        mySettings.MajorVersion = 2;
+        mySettings.MinorVersion = 0;
+    }
+
+    // Enable antialiasing if needed
+    if (mySettings.AntialiasingLevel > 0)
+        glEnable(GL_MULTISAMPLE_ARB);
+}
+
 } // namespace priv
 
 } // namespace sf
diff --git a/src/SFML/Window/GlContext.hpp b/src/SFML/Window/GlContext.hpp
index dab762f3..f6eb193a 100644
--- a/src/SFML/Window/GlContext.hpp
+++ b/src/SFML/Window/GlContext.hpp
@@ -57,7 +57,7 @@ public :
     /// can be called only once.
     ///
     ////////////////////////////////////////////////////////////
-    static void Initialize();
+    static void GlobalInit();
 
     ////////////////////////////////////////////////////////////
     /// \brief Perform the global cleanup
@@ -69,7 +69,7 @@ public :
     /// can be called only once.
     ///
     ////////////////////////////////////////////////////////////
-    static void Cleanup();
+    static void GlobalCleanup();
 
     ////////////////////////////////////////////////////////////
     /// \brief Ensures that an OpenGL context is active in the current thread
@@ -217,6 +217,14 @@ protected :
     // Member data
     ////////////////////////////////////////////////////////////
     ContextSettings mySettings; ///< Creation settings of the context
+
+private:
+
+    ////////////////////////////////////////////////////////////
+    /// \brief Perform various initializations after the context construction
+    ///
+    ////////////////////////////////////////////////////////////
+    void Initialize();
 };
 
 } // namespace priv
diff --git a/src/SFML/Window/GlResource.cpp b/src/SFML/Window/GlResource.cpp
index d06a8fc9..a0fa41aa 100644
--- a/src/SFML/Window/GlResource.cpp
+++ b/src/SFML/Window/GlResource.cpp
@@ -54,7 +54,7 @@ GlResource::GlResource()
 
         // If this is the very first resource, trigger the global context initialization
         if (count == 0)
-            priv::GlContext::Initialize();
+            priv::GlContext::GlobalInit();
 
         // Increment the resources counter
         count++;
@@ -76,7 +76,7 @@ GlResource::~GlResource()
 
     // If there's no more resource alive, we can trigger the global context cleanup
     if (count == 0)
-        priv::GlContext::Cleanup();
+        priv::GlContext::GlobalCleanup();
 }
 
 
diff --git a/src/SFML/Window/Linux/GlxContext.cpp b/src/SFML/Window/Linux/GlxContext.cpp
index 36ee48ab..08c62aca 100644
--- a/src/SFML/Window/Linux/GlxContext.cpp
+++ b/src/SFML/Window/Linux/GlxContext.cpp
@@ -60,9 +60,6 @@ myOwnsWindow(true)
 
     // Create the context
     CreateContext(shared, VideoMode::GetDesktopMode().BitsPerPixel, ContextSettings());
-
-    // Activate the context
-    SetActive(true);
 }
 
 
@@ -81,9 +78,6 @@ myOwnsWindow(false)
     // Create the context
     if (myWindow)
         CreateContext(shared, bitsPerPixel, settings);
-
-    // Activate the context
-    SetActive(true);
 }
 
 
@@ -110,9 +104,6 @@ myOwnsWindow(true)
 
     // Create the context
     CreateContext(shared, VideoMode::GetDesktopMode().BitsPerPixel, settings);
-
-    // Activate the context
-    SetActive(true);
 }
 
 
diff --git a/src/SFML/Window/OSX/SFContext.hpp b/src/SFML/Window/OSX/SFContext.hpp
index dcc38ebd..c49e482f 100644
--- a/src/SFML/Window/OSX/SFContext.hpp
+++ b/src/SFML/Window/OSX/SFContext.hpp
@@ -150,13 +150,6 @@ private:
                        unsigned int bitsPerPixel, 
                        const ContextSettings& settings);
     
-    ////////////////////////////////////////////////////////////
-    /// \brief Finish updating our settings
-    /// \note The context must be active in order to get the OpenGL version.
-    ///
-    ////////////////////////////////////////////////////////////
-    void UpdateOpenGLVersion();
-    
     ////////////////////////////////////////////////////////////
     // Member data
     ////////////////////////////////////////////////////////////
diff --git a/src/SFML/Window/OSX/SFContext.mm b/src/SFML/Window/OSX/SFContext.mm
index dc35f87d..4f05b0df 100644
--- a/src/SFML/Window/OSX/SFContext.mm
+++ b/src/SFML/Window/OSX/SFContext.mm
@@ -57,12 +57,6 @@ SFContext::SFContext(SFContext* shared)
     
     // Create the context
     CreateContext(shared, VideoMode::GetDesktopMode().BitsPerPixel, ContextSettings(0, 0, 0));
-    
-    // Activate the context
-    SetActive(true);
-    
-    // Finish updating settings.
-    UpdateOpenGLVersion();
 }
 
     
@@ -79,12 +73,6 @@ SFContext::SFContext(SFContext* shared, const ContextSettings& settings,
     // Apply context.
     WindowImplCocoa const * ownerCocoa = static_cast<WindowImplCocoa const *>(owner);
     ownerCocoa->ApplyContext(myContext);
-    
-    // Activate the context
-    SetActive(true);
-    
-    // Finish updating settings.
-    UpdateOpenGLVersion();
 }
 
 
@@ -109,12 +97,6 @@ SFContext::SFContext(SFContext* shared, const ContextSettings& settings,
     [myWindow setContentView:myView];
     [myView setOpenGLContext:myContext];
     [myContext setView:myView];
-    
-    // Activate the context
-    SetActive(true);
-    
-    // Finish updating settings.
-    UpdateOpenGLVersion();
 }
 
 
@@ -224,33 +206,6 @@ void SFContext::CreateContext(SFContext* shared,
     mySettings = settings;
 }
 
-
-////////////////////////////////////////////////////////////
-void SFContext::UpdateOpenGLVersion()
-{
-    // Update the OpenGL version in the settings.
-    // NB : the context muste be active to get its version.
-
-    GLubyte const* versionString = glGetString(GL_VERSION);
-    
-    if (versionString == 0) {
-
-        // (Warning) Couldn't get the OpenGL version of the context.
-        // This happens sometimes (but not always) when creating the first
-        // context for no apparent reason.
-        
-        // We assume we can get at least a 2.0 valid context.
-        mySettings.MajorVersion = 2;
-        mySettings.MinorVersion = 0;
-        
-    } else {
-        
-        // versionString looks like "2.1 NVIDIA-1.6.26".
-        mySettings.MajorVersion = versionString[0];
-        mySettings.MinorVersion = versionString[2];
-    }
-}
-
 } // namespace priv
 
 } // namespace sf
diff --git a/src/SFML/Window/Win32/WglContext.cpp b/src/SFML/Window/Win32/WglContext.cpp
index ad8b3dc8..295ca2d1 100644
--- a/src/SFML/Window/Win32/WglContext.cpp
+++ b/src/SFML/Window/Win32/WglContext.cpp
@@ -56,9 +56,6 @@ myOwnsWindow   (true)
     // Create the context
     if (myDeviceContext)
         CreateContext(shared, VideoMode::GetDesktopMode().BitsPerPixel, ContextSettings());
-
-    // Activate the context
-    SetActive(true);
 }
 
 
@@ -76,9 +73,6 @@ myOwnsWindow   (false)
     // Create the context
     if (myDeviceContext)
         CreateContext(shared, bitsPerPixel, settings);
-
-    // Activate the context
-    SetActive(true);
 }
 
 
@@ -102,9 +96,6 @@ myOwnsWindow   (true)
     // Create the context
     if (myDeviceContext)
         CreateContext(shared, VideoMode::GetDesktopMode().BitsPerPixel, settings);
-
-    // Activate the context
-    SetActive(true);
 }