comparison Applications/Sdl/SdlOpenGLContext.cpp @ 891:0aff28f15ea2

new abstraction: IViewport
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jul 2019 18:18:42 +0200
parents Applications/Sdl/SdlOpenGLWindow.cpp@002d9562c8f5
children 1091b2adeb5a
comparison
equal deleted inserted replaced
890:77c96ba899f9 891:0aff28f15ea2
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
24 #if ORTHANC_ENABLE_SDL == 1
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
34 #include <Core/OrthancException.h>
35
36 namespace OrthancStone
37 {
38 SdlOpenGLContext::SdlOpenGLContext(const char* title,
39 unsigned int width,
40 unsigned int height,
41 bool allowDpiScaling) :
42 window_(title, width, height, true /* enable OpenGL */, allowDpiScaling)
43 {
44 context_ = SDL_GL_CreateContext(window_.GetObject());
45
46 if (context_ == NULL)
47 {
48 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
49 "Cannot initialize OpenGL");
50 }
51
52 #if ORTHANC_ENABLE_GLEW == 1
53 // The initialization function of glew (i.e. "glewInit()") can
54 // only be called once an OpenGL is setup.
55 // https://stackoverflow.com/a/45033669/881731
56 {
57 static boost::mutex mutex_;
58 static bool isGlewInitialized_ = false;
59
60 boost::mutex::scoped_lock lock(mutex_);
61
62 if (!isGlewInitialized_)
63 {
64 LOG(INFO) << "Initializing glew";
65
66 GLenum err = glewInit();
67 if (GLEW_OK != err)
68 {
69 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
70 "Cannot initialize glew");
71 }
72
73 isGlewInitialized_ = true;
74 }
75 }
76 #endif
77 }
78
79
80 SdlOpenGLContext::~SdlOpenGLContext()
81 {
82 SDL_GL_DeleteContext(context_);
83 }
84
85
86 void SdlOpenGLContext::MakeCurrent()
87 {
88 if (SDL_GL_MakeCurrent(window_.GetObject(), context_) != 0)
89 {
90 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
91 "Cannot set current OpenGL context");
92 }
93
94 // This makes our buffer swap syncronized with the monitor's vertical refresh
95 SDL_GL_SetSwapInterval(1);
96 }
97
98
99 void SdlOpenGLContext::SwapBuffer()
100 {
101 // Swap our buffer to display the current contents of buffer on screen
102 SDL_GL_SwapWindow(window_.GetObject());
103 }
104
105
106 unsigned int SdlOpenGLContext::GetCanvasWidth() const
107 {
108 int w = 0;
109 SDL_GL_GetDrawableSize(window_.GetObject(), &w, NULL);
110 return static_cast<unsigned int>(w);
111 }
112
113
114 unsigned int SdlOpenGLContext::GetCanvasHeight() const
115 {
116 int h = 0;
117 SDL_GL_GetDrawableSize(window_.GetObject(), NULL, &h);
118 return static_cast<unsigned int>(h);
119 }
120 }
121
122 #endif