diff Applications/Sdl/SdlOpenGLWindow.cpp @ 613:412a2d01a189

automatic initialization of glew
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 30 Apr 2019 16:44:23 +0200
parents 6e471e6cf09b
children 4ec32d4d0867
line wrap: on
line diff
--- a/Applications/Sdl/SdlOpenGLWindow.cpp	Tue Apr 30 16:18:46 2019 +0200
+++ b/Applications/Sdl/SdlOpenGLWindow.cpp	Tue Apr 30 16:44:23 2019 +0200
@@ -23,10 +23,21 @@
 
 #if ORTHANC_ENABLE_SDL == 1
 
+#if !defined(ORTHANC_ENABLE_GLEW)
+#  error Macro ORTHANC_ENABLE_GLEW must be defined
+#endif
+
+#if ORTHANC_ENABLE_GLEW == 1
+#  include <GL/glew.h>
+#endif
+
 #include <Core/OrthancException.h>
 
 namespace OrthancStone
 {
+  static boost::mutex  globalMutex_;
+  static bool          globalIsGlewInitialized_ = false;
+  
   SdlOpenGLWindow::SdlOpenGLWindow(const char* title,
                                    unsigned int width,
                                    unsigned int height) :
@@ -39,6 +50,29 @@
       throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
                                       "Cannot initialize OpenGL");
     }
+
+#if ORTHANC_ENABLE_GLEW == 1
+    // The initialization function of glew (i.e. "glewInit()") can
+    // only be called once an OpenGL is setup.
+    // https://stackoverflow.com/a/45033669/881731
+    {
+      boost::mutex::scoped_lock lock(globalMutex_);
+
+      if (!globalIsGlewInitialized_)
+      {
+        LOG(INFO) << "Initializing glew";
+        
+        GLenum err = glewInit();
+        if (GLEW_OK != err)
+        {
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
+                                          "Cannot initialize glew");
+        }
+
+        globalIsGlewInitialized_ = true;
+      }
+    }    
+#endif
   }