comparison Framework/OpenGL/SdlOpenGLContext.cpp @ 1047:efc5b62b9539

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 09 Oct 2019 18:06:58 +0200
parents Applications/Sdl/SdlOpenGLContext.cpp@6a9300ecfa13
children bc27c199eefe 2d8ab34c8c91
comparison
equal deleted inserted replaced
1046:513a4053c54a 1047:efc5b62b9539
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-2019 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "SdlOpenGLContext.h"
23 #include "../../Framework/StoneException.h"
24
25 #if ORTHANC_ENABLE_SDL == 1
26
27 #if !defined(ORTHANC_ENABLE_GLEW)
28 # error Macro ORTHANC_ENABLE_GLEW must be defined
29 #endif
30
31 #if ORTHANC_ENABLE_GLEW == 1
32 # include <GL/glew.h>
33 #endif
34
35 #include <Core/OrthancException.h>
36
37 namespace OrthancStone
38 {
39 SdlOpenGLContext::SdlOpenGLContext(const char* title,
40 unsigned int width,
41 unsigned int height,
42 bool allowDpiScaling)
43 : window_(title, width, height, true /* enable OpenGL */, allowDpiScaling)
44 , context_(NULL)
45 {
46 context_ = SDL_GL_CreateContext(window_.GetObject());
47
48 if (context_ == NULL)
49 {
50 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
51 "Cannot initialize OpenGL");
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 static boost::mutex mutex_;
60 static bool isGlewInitialized_ = false;
61
62 boost::mutex::scoped_lock lock(mutex_);
63
64 if (!isGlewInitialized_)
65 {
66 LOG(INFO) << "Initializing glew";
67
68 GLenum err = glewInit();
69 if (GLEW_OK != err)
70 {
71 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
72 "Cannot initialize glew");
73 }
74
75 isGlewInitialized_ = true;
76 }
77 }
78 #endif
79 }
80
81
82 SdlOpenGLContext::~SdlOpenGLContext()
83 {
84 SDL_GL_DeleteContext(context_);
85 }
86
87 void SdlOpenGLContext::MakeCurrent()
88 {
89 if (SDL_GL_MakeCurrent(window_.GetObject(), context_) != 0)
90 {
91 const char* errText = SDL_GetError();
92 std::stringstream ss;
93 ss << "Cannot set current OpenGL context. SDL error text: " << errText;
94 std::string errStr = ss.str();
95 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, errStr.c_str());
96 }
97
98 // This makes our buffer swap synchronized with the monitor's vertical refresh
99 SDL_GL_SetSwapInterval(1);
100 }
101
102
103 void SdlOpenGLContext::SwapBuffer()
104 {
105 // Swap our buffer to display the current contents of buffer on screen
106 SDL_GL_SwapWindow(window_.GetObject());
107 }
108
109
110 unsigned int SdlOpenGLContext::GetCanvasWidth() const
111 {
112 int w = 0;
113 SDL_GL_GetDrawableSize(window_.GetObject(), &w, NULL);
114 return static_cast<unsigned int>(w);
115 }
116
117
118 unsigned int SdlOpenGLContext::GetCanvasHeight() const
119 {
120 int h = 0;
121 SDL_GL_GetDrawableSize(window_.GetObject(), NULL, &h);
122 return static_cast<unsigned int>(h);
123 }
124 }
125
126 #endif