comparison Applications/Platforms/Sdl/SdlOpenGLContext.cpp @ 1591:5887a4f8594b

moving platform-specific files out of the "OrthancStone" folder
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 23 Oct 2020 13:15:03 +0200
parents OrthancStone/Sources/OpenGL/SdlOpenGLContext.cpp@244ad1e4e76a
children 4fb8fdf03314
comparison
equal deleted inserted replaced
1590:7b963bccafef 1591:5887a4f8594b
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-2020 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 "../../../OrthancStone/Sources/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 <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 LOG(ERROR) << glewGetErrorString(err);
72 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
73 "Cannot initialize glew");
74 }
75
76 isGlewInitialized_ = true;
77 }
78 }
79 #endif
80 }
81
82
83 SdlOpenGLContext::~SdlOpenGLContext()
84 {
85 SDL_GL_DeleteContext(context_);
86 }
87
88 void SdlOpenGLContext::MakeCurrent()
89 {
90 if (SDL_GL_MakeCurrent(window_.GetObject(), context_) != 0)
91 {
92 const char* errText = SDL_GetError();
93 std::stringstream ss;
94 ss << "Cannot set current OpenGL context. SDL error text: " << errText;
95 std::string errStr = ss.str();
96 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, errStr.c_str());
97 }
98
99 // This makes our buffer swap synchronized with the monitor's vertical refresh
100 SDL_GL_SetSwapInterval(1);
101 }
102
103
104 void SdlOpenGLContext::SwapBuffer()
105 {
106 // Swap our buffer to display the current contents of buffer on screen
107 SDL_GL_SwapWindow(window_.GetObject());
108 }
109
110
111 unsigned int SdlOpenGLContext::GetCanvasWidth() const
112 {
113 int w = 0;
114 SDL_GL_GetDrawableSize(window_.GetObject(), &w, NULL);
115 return static_cast<unsigned int>(w);
116 }
117
118
119 unsigned int SdlOpenGLContext::GetCanvasHeight() const
120 {
121 int h = 0;
122 SDL_GL_GetDrawableSize(window_.GetObject(), NULL, &h);
123 return static_cast<unsigned int>(h);
124 }
125 }
126
127 #endif