Mercurial > hg > orthanc-stone
annotate Applications/Sdl/SdlWindow.cpp @ 879:12b591d5d63c am-dev
some Qt integration (wip)
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Fri, 05 Jul 2019 14:52:43 +0200 |
parents | 794278160a3f |
children |
rev | line source |
---|---|
0 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
439 | 5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
47 | 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. | |
0 | 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 | |
47 | 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 | |
0 | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | |
20 | |
21 | |
22 #include "SdlWindow.h" | |
23 | |
24 #if ORTHANC_ENABLE_SDL == 1 | |
25 | |
212
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
26 #include <Core/Logging.h> |
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
27 #include <Core/OrthancException.h> |
0 | 28 |
656
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
29 #ifdef WIN32 |
662 | 30 #include <windows.h> // for SetProcessDpiAware |
656
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
31 #endif |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
32 // WIN32 |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
33 |
214 | 34 #include <SDL.h> |
35 | |
0 | 36 namespace OrthancStone |
37 { | |
38 SdlWindow::SdlWindow(const char* title, | |
39 unsigned int width, | |
40 unsigned int height, | |
656
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
41 bool enableOpenGl, |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
42 bool allowDpiScaling) : |
0 | 43 maximized_(false) |
44 { | |
45 // TODO Understand why, with SDL_WINDOW_OPENGL + MinGW32 + Release | |
46 // build mode, the application crashes whenever the SDL window is | |
47 // resized or maximized | |
48 | |
49 uint32_t windowFlags, rendererFlags; | |
50 if (enableOpenGl) | |
51 { | |
52 windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; | |
53 rendererFlags = SDL_RENDERER_ACCELERATED; | |
54 } | |
55 else | |
56 { | |
57 windowFlags = SDL_WINDOW_RESIZABLE; | |
58 rendererFlags = SDL_RENDERER_SOFTWARE; | |
59 } | |
656
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
60 |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
61 // TODO: probably required on MacOS X, too |
662 | 62 #if defined(WIN32) && (_WIN32_WINNT >= 0x0600) |
656
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
63 if (!allowDpiScaling) |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
64 { |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
65 // if we do NOT allow DPI scaling, it means an SDL pixel will be a real |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
66 // monitor pixel. This is needed for high-DPI applications |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
67 |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
68 // Enable high-DPI support on Windows |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
69 |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
70 // THE FOLLOWING HAS BEEN COMMENTED OUT BECAUSE IT WILL CRASH UNDER |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
71 // OLD WINDOWS VERSIONS |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
72 // ADD THIS AT THE TOP TO ENABLE IT: |
662 | 73 // |
74 //#pragma comment(lib, "Shcore.lib") THIS IS ONLY REQUIRED FOR SetProcessDpiAwareness | |
75 //#include <windows.h> | |
76 //#include <ShellScalingAPI.h> THIS IS ONLY REQUIRED FOR SetProcessDpiAwareness | |
656
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
77 //#include <comdef.h> THIS IS ONLY REQUIRED FOR SetProcessDpiAwareness |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
78 // SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
79 |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
80 // This is supported on Vista+ |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
81 SetProcessDPIAware(); |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
82 |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
83 windowFlags |= SDL_WINDOW_ALLOW_HIGHDPI; |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
84 } |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
85 #endif |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
86 // WIN32 |
0 | 87 |
88 window_ = SDL_CreateWindow(title, | |
89 SDL_WINDOWPOS_UNDEFINED, | |
90 SDL_WINDOWPOS_UNDEFINED, | |
91 width, height, windowFlags); | |
92 | |
93 if (window_ == NULL) | |
94 { | |
95 LOG(ERROR) << "Cannot create the SDL window: " << SDL_GetError(); | |
96 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
97 } | |
98 | |
99 renderer_ = SDL_CreateRenderer(window_, -1, rendererFlags); | |
100 if (!renderer_) | |
101 { | |
102 LOG(ERROR) << "Cannot create the SDL renderer: " << SDL_GetError(); | |
103 SDL_DestroyWindow(window_); | |
104 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
105 } | |
106 } | |
107 | |
108 | |
109 SdlWindow::~SdlWindow() | |
110 { | |
111 if (renderer_ != NULL) | |
112 { | |
113 SDL_DestroyRenderer(renderer_); | |
114 } | |
115 | |
116 if (window_ != NULL) | |
117 { | |
118 SDL_DestroyWindow(window_); | |
119 } | |
120 } | |
121 | |
122 | |
123 unsigned int SdlWindow::GetWidth() const | |
124 { | |
125 int w = -1; | |
126 SDL_GetWindowSize(window_, &w, NULL); | |
127 | |
128 if (w < 0) | |
129 { | |
130 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
131 } | |
132 else | |
133 { | |
134 return static_cast<unsigned int>(w); | |
135 } | |
136 } | |
137 | |
138 | |
139 unsigned int SdlWindow::GetHeight() const | |
140 { | |
141 int h = -1; | |
142 SDL_GetWindowSize(window_, NULL, &h); | |
143 | |
144 if (h < 0) | |
145 { | |
146 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
147 } | |
148 else | |
149 { | |
150 return static_cast<unsigned int>(h); | |
151 } | |
152 } | |
153 | |
154 | |
155 void SdlWindow::Render(SDL_Surface* surface) | |
156 { | |
157 //SDL_RenderClear(renderer_); | |
158 | |
159 SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer_, surface); | |
160 if (texture != NULL) | |
161 { | |
162 SDL_RenderCopy(renderer_, texture, NULL, NULL); | |
163 SDL_DestroyTexture(texture); | |
164 } | |
165 | |
166 SDL_RenderPresent(renderer_); | |
167 } | |
168 | |
169 | |
170 void SdlWindow::ToggleMaximize() | |
171 { | |
172 if (maximized_) | |
173 { | |
174 SDL_RestoreWindow(window_); | |
175 maximized_ = false; | |
176 } | |
177 else | |
178 { | |
179 SDL_MaximizeWindow(window_); | |
180 maximized_ = true; | |
181 } | |
182 } | |
214 | 183 |
184 | |
185 void SdlWindow::GlobalInitialize() | |
186 { | |
187 if (SDL_Init(SDL_INIT_VIDEO) != 0) | |
188 { | |
189 LOG(ERROR) << "Cannot initialize SDL"; | |
190 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
191 } | |
192 } | |
193 | |
194 | |
195 void SdlWindow::GlobalFinalize() | |
196 { | |
197 SDL_Quit(); | |
198 } | |
0 | 199 } |
200 | |
201 #endif |