comparison 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
comparison
equal deleted inserted replaced
612:ba72bffceb7d 613:412a2d01a189
21 21
22 #include "SdlOpenGLWindow.h" 22 #include "SdlOpenGLWindow.h"
23 23
24 #if ORTHANC_ENABLE_SDL == 1 24 #if ORTHANC_ENABLE_SDL == 1
25 25
26 #if !defined(ORTHANC_ENABLE_GLEW)
27 # error Macro ORTHANC_ENABLE_GLEW must be defined
28 #endif
29
30 #if ORTHANC_ENABLE_GLEW == 1
31 # include <GL/glew.h>
32 #endif
33
26 #include <Core/OrthancException.h> 34 #include <Core/OrthancException.h>
27 35
28 namespace OrthancStone 36 namespace OrthancStone
29 { 37 {
38 static boost::mutex globalMutex_;
39 static bool globalIsGlewInitialized_ = false;
40
30 SdlOpenGLWindow::SdlOpenGLWindow(const char* title, 41 SdlOpenGLWindow::SdlOpenGLWindow(const char* title,
31 unsigned int width, 42 unsigned int width,
32 unsigned int height) : 43 unsigned int height) :
33 window_(title, width, height, true /* enable OpenGL */) 44 window_(title, width, height, true /* enable OpenGL */)
34 { 45 {
37 if (context_ == NULL) 48 if (context_ == NULL)
38 { 49 {
39 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, 50 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
40 "Cannot initialize OpenGL"); 51 "Cannot initialize OpenGL");
41 } 52 }
53
54 #if ORTHANC_ENABLE_GLEW == 1
55 // The initialization function of glew (i.e. "glewInit()") can
56 // only be called once an OpenGL is setup.
57 // https://stackoverflow.com/a/45033669/881731
58 {
59 boost::mutex::scoped_lock lock(globalMutex_);
60
61 if (!globalIsGlewInitialized_)
62 {
63 LOG(INFO) << "Initializing glew";
64
65 GLenum err = glewInit();
66 if (GLEW_OK != err)
67 {
68 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
69 "Cannot initialize glew");
70 }
71
72 globalIsGlewInitialized_ = true;
73 }
74 }
75 #endif
42 } 76 }
43 77
44 78
45 SdlOpenGLWindow::~SdlOpenGLWindow() 79 SdlOpenGLWindow::~SdlOpenGLWindow()
46 { 80 {