Mercurial > hg > orthanc-stone
annotate Applications/Platforms/Sdl/SdlWindow.cpp @ 1836:5a6adbc20ace
cleanup cmake
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 12 Jun 2021 13:43:42 +0200 |
parents | 5a872e69c74f |
children | 3889ae96d2e9 |
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 | |
1739
9ac2a65d4172
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
5 * Copyright (C) 2017-2021 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. | |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1591
diff
changeset
|
16 * |
47 | 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 | |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1279
diff
changeset
|
26 #include <Logging.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1279
diff
changeset
|
27 #include <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 |
1198
4cc997207d8a
fix compatibility between SDL and DCMTK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1047
diff
changeset
|
34 #include <SDL_render.h> |
4cc997207d8a
fix compatibility between SDL and DCMTK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1047
diff
changeset
|
35 #include <SDL_video.h> |
214 | 36 #include <SDL.h> |
37 | |
0 | 38 namespace OrthancStone |
39 { | |
1804 | 40 SdlWindow::SdlWindow(const std::string& title, |
0 | 41 unsigned int width, |
42 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
|
43 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
|
44 bool allowDpiScaling) : |
0 | 45 maximized_(false) |
46 { | |
47 // TODO Understand why, with SDL_WINDOW_OPENGL + MinGW32 + Release | |
48 // build mode, the application crashes whenever the SDL window is | |
49 // resized or maximized | |
50 | |
51 uint32_t windowFlags, rendererFlags; | |
52 if (enableOpenGl) | |
53 { | |
54 windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; | |
55 rendererFlags = SDL_RENDERER_ACCELERATED; | |
56 } | |
57 else | |
58 { | |
59 windowFlags = SDL_WINDOW_RESIZABLE; | |
60 rendererFlags = SDL_RENDERER_SOFTWARE; | |
61 } | |
656
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
62 |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
63 // TODO: probably required on MacOS X, too |
662 | 64 #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
|
65 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
|
66 { |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
67 // 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
|
68 // 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
|
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 // 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
|
71 |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
72 // 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
|
73 // 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
|
74 // ADD THIS AT THE TOP TO ENABLE IT: |
662 | 75 // |
76 //#pragma comment(lib, "Shcore.lib") THIS IS ONLY REQUIRED FOR SetProcessDpiAwareness | |
77 //#include <windows.h> | |
78 //#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
|
79 //#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
|
80 // 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
|
81 |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
82 // 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
|
83 SetProcessDPIAware(); |
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 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
|
86 } |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
87 #endif |
002d9562c8f5
Added support to DISABLE legacy scaling in SDL Windows (only in WIN32... this
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
88 // WIN32 |
0 | 89 |
1804 | 90 window_ = SDL_CreateWindow(title.c_str(), |
0 | 91 SDL_WINDOWPOS_UNDEFINED, |
92 SDL_WINDOWPOS_UNDEFINED, | |
93 width, height, windowFlags); | |
94 | |
95 if (window_ == NULL) | |
96 { | |
97 LOG(ERROR) << "Cannot create the SDL window: " << SDL_GetError(); | |
98 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
99 } | |
100 | |
101 renderer_ = SDL_CreateRenderer(window_, -1, rendererFlags); | |
102 if (!renderer_) | |
103 { | |
104 LOG(ERROR) << "Cannot create the SDL renderer: " << SDL_GetError(); | |
105 SDL_DestroyWindow(window_); | |
106 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
107 } | |
108 } | |
109 | |
110 | |
111 SdlWindow::~SdlWindow() | |
112 { | |
113 if (renderer_ != NULL) | |
114 { | |
115 SDL_DestroyRenderer(renderer_); | |
116 } | |
117 | |
118 if (window_ != NULL) | |
119 { | |
120 SDL_DestroyWindow(window_); | |
121 } | |
122 } | |
123 | |
124 | |
125 unsigned int SdlWindow::GetWidth() const | |
126 { | |
127 int w = -1; | |
128 SDL_GetWindowSize(window_, &w, NULL); | |
129 | |
130 if (w < 0) | |
131 { | |
132 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
133 } | |
134 else | |
135 { | |
136 return static_cast<unsigned int>(w); | |
137 } | |
138 } | |
139 | |
140 | |
141 unsigned int SdlWindow::GetHeight() const | |
142 { | |
143 int h = -1; | |
144 SDL_GetWindowSize(window_, NULL, &h); | |
145 | |
146 if (h < 0) | |
147 { | |
148 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
149 } | |
150 else | |
151 { | |
152 return static_cast<unsigned int>(h); | |
153 } | |
154 } | |
155 | |
156 | |
157 void SdlWindow::Render(SDL_Surface* surface) | |
158 { | |
1205 | 159 /** |
160 * "You are strongly encouraged to call SDL_RenderClear() to | |
161 * initialize the backbuffer before starting each new frame's | |
162 * drawing, even if you plan to overwrite every pixel." | |
163 * https://wiki.libsdl.org/SDL_RenderPresent | |
164 **/ | |
165 SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255); | |
166 SDL_RenderClear(renderer_); // Clear the entire screen to our selected color | |
0 | 167 |
168 SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer_, surface); | |
169 if (texture != NULL) | |
170 { | |
171 SDL_RenderCopy(renderer_, texture, NULL, NULL); | |
172 SDL_DestroyTexture(texture); | |
173 } | |
174 | |
175 SDL_RenderPresent(renderer_); | |
176 } | |
177 | |
178 | |
179 void SdlWindow::ToggleMaximize() | |
180 { | |
181 if (maximized_) | |
182 { | |
183 SDL_RestoreWindow(window_); | |
184 maximized_ = false; | |
185 } | |
186 else | |
187 { | |
188 SDL_MaximizeWindow(window_); | |
189 maximized_ = true; | |
190 } | |
191 } | |
214 | 192 |
193 | |
194 void SdlWindow::GlobalInitialize() | |
195 { | |
196 if (SDL_Init(SDL_INIT_VIDEO) != 0) | |
197 { | |
198 LOG(ERROR) << "Cannot initialize SDL"; | |
199 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
200 } | |
201 } | |
202 | |
203 | |
204 void SdlWindow::GlobalFinalize() | |
205 { | |
206 SDL_Quit(); | |
207 } | |
0 | 208 } |
209 | |
210 #endif |