Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Platforms/Sdl/SdlOpenGLContext.cpp @ 1899:917500c46fe0
moved the Platform folder from the Applications folder to the Stone library itself
author | Alain Mazy <am@osimis.io> |
---|---|
date | Sat, 29 Jan 2022 12:47:32 +0100 |
parents | |
children | 184b0aeae1af |
comparison
equal
deleted
inserted
replaced
1898:a5e54bd87b25 | 1899:917500c46fe0 |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium | |
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium | |
7 * | |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Lesser General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
19 * License along with this program. If not, see | |
20 * <http://www.gnu.org/licenses/>. | |
21 **/ | |
22 | |
23 #include "SdlOpenGLContext.h" | |
24 #include "../../../OrthancStone/Sources/StoneException.h" | |
25 | |
26 #if ORTHANC_ENABLE_SDL == 1 | |
27 | |
28 #if !defined(ORTHANC_ENABLE_GLEW) | |
29 # error Macro ORTHANC_ENABLE_GLEW must be defined | |
30 #endif | |
31 | |
32 #if ORTHANC_ENABLE_GLEW == 1 | |
33 # include <GL/glew.h> | |
34 #endif | |
35 | |
36 #include <Logging.h> | |
37 #include <OrthancException.h> | |
38 | |
39 #include <boost/thread/mutex.hpp> | |
40 | |
41 namespace OrthancStone | |
42 { | |
43 SdlOpenGLContext::SdlOpenGLContext(const char* title, | |
44 unsigned int width, | |
45 unsigned int height, | |
46 bool allowDpiScaling) | |
47 : window_(title, width, height, true /* enable OpenGL */, allowDpiScaling) | |
48 , context_(NULL) | |
49 { | |
50 context_ = SDL_GL_CreateContext(window_.GetObject()); | |
51 | |
52 if (context_ == NULL) | |
53 { | |
54 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, | |
55 "Cannot initialize OpenGL"); | |
56 } | |
57 | |
58 #if ORTHANC_ENABLE_GLEW == 1 | |
59 // The initialization function of glew (i.e. "glewInit()") can | |
60 // only be called once an OpenGL is setup. | |
61 // https://stackoverflow.com/a/45033669/881731 | |
62 { | |
63 static boost::mutex mutex_; | |
64 static bool isGlewInitialized_ = false; | |
65 | |
66 boost::mutex::scoped_lock lock(mutex_); | |
67 | |
68 if (!isGlewInitialized_) | |
69 { | |
70 LOG(INFO) << "Initializing glew"; | |
71 | |
72 GLenum err = glewInit(); | |
73 if (GLEW_OK != err) | |
74 { | |
75 LOG(ERROR) << glewGetErrorString(err); | |
76 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, | |
77 "Cannot initialize glew"); | |
78 } | |
79 | |
80 isGlewInitialized_ = true; | |
81 } | |
82 } | |
83 #endif | |
84 } | |
85 | |
86 | |
87 SdlOpenGLContext::~SdlOpenGLContext() | |
88 { | |
89 SDL_GL_DeleteContext(context_); | |
90 } | |
91 | |
92 void SdlOpenGLContext::MakeCurrent() | |
93 { | |
94 if (SDL_GL_MakeCurrent(window_.GetObject(), context_) != 0) | |
95 { | |
96 const char* errText = SDL_GetError(); | |
97 std::stringstream ss; | |
98 ss << "Cannot set current OpenGL context. SDL error text: " << errText; | |
99 std::string errStr = ss.str(); | |
100 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, errStr.c_str()); | |
101 } | |
102 | |
103 // This makes our buffer swap synchronized with the monitor's vertical refresh | |
104 SDL_GL_SetSwapInterval(1); | |
105 } | |
106 | |
107 | |
108 void SdlOpenGLContext::SwapBuffer() | |
109 { | |
110 // Swap our buffer to display the current contents of buffer on screen | |
111 SDL_GL_SwapWindow(window_.GetObject()); | |
112 } | |
113 | |
114 | |
115 unsigned int SdlOpenGLContext::GetCanvasWidth() const | |
116 { | |
117 int w = 0; | |
118 SDL_GL_GetDrawableSize(window_.GetObject(), &w, NULL); | |
119 return static_cast<unsigned int>(w); | |
120 } | |
121 | |
122 | |
123 unsigned int SdlOpenGLContext::GetCanvasHeight() const | |
124 { | |
125 int h = 0; | |
126 SDL_GL_GetDrawableSize(window_.GetObject(), NULL, &h); | |
127 return static_cast<unsigned int>(h); | |
128 } | |
129 } | |
130 | |
131 #endif |