Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Platforms/Sdl/SdlWindow.cpp @ 1899:917500c46fe0
moved the Platform folder from the Applications folder to the Stone library itself
author | Alain Mazy <am@osimis.io> |
---|---|
date | Sat, 29 Jan 2022 12:47:32 +0100 |
parents | |
children | 07964689cb0b |
comparison
equal
deleted
inserted
replaced
1898:a5e54bd87b25 | 1899:917500c46fe0 |
---|---|
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-2022 Osimis S.A., Belgium | |
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium | |
7 * | |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Lesser General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
19 * License along with this program. If not, see | |
20 * <http://www.gnu.org/licenses/>. | |
21 **/ | |
22 | |
23 | |
24 #include "SdlWindow.h" | |
25 | |
26 #if ORTHANC_ENABLE_SDL == 1 | |
27 | |
28 #include <Logging.h> | |
29 #include <OrthancException.h> | |
30 | |
31 #ifdef WIN32 | |
32 #include <windows.h> // for SetProcessDpiAware | |
33 #endif | |
34 // WIN32 | |
35 | |
36 #include <SDL_render.h> | |
37 #include <SDL_video.h> | |
38 #include <SDL.h> | |
39 | |
40 namespace OrthancStone | |
41 { | |
42 SdlWindow::SdlWindow(const std::string& title, | |
43 unsigned int width, | |
44 unsigned int height, | |
45 bool enableOpenGl, | |
46 bool allowDpiScaling) : | |
47 maximized_(false) | |
48 { | |
49 // TODO Understand why, with SDL_WINDOW_OPENGL + MinGW32 + Release | |
50 // build mode, the application crashes whenever the SDL window is | |
51 // resized or maximized | |
52 | |
53 uint32_t windowFlags, rendererFlags; | |
54 if (enableOpenGl) | |
55 { | |
56 windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; | |
57 rendererFlags = SDL_RENDERER_ACCELERATED; | |
58 } | |
59 else | |
60 { | |
61 windowFlags = SDL_WINDOW_RESIZABLE; | |
62 rendererFlags = SDL_RENDERER_SOFTWARE; | |
63 } | |
64 | |
65 // TODO: probably required on MacOS X, too | |
66 #if defined(WIN32) && (_WIN32_WINNT >= 0x0600) | |
67 if (!allowDpiScaling) | |
68 { | |
69 // if we do NOT allow DPI scaling, it means an SDL pixel will be a real | |
70 // monitor pixel. This is needed for high-DPI applications | |
71 | |
72 // Enable high-DPI support on Windows | |
73 | |
74 // THE FOLLOWING HAS BEEN COMMENTED OUT BECAUSE IT WILL CRASH UNDER | |
75 // OLD WINDOWS VERSIONS | |
76 // ADD THIS AT THE TOP TO ENABLE IT: | |
77 // | |
78 //#pragma comment(lib, "Shcore.lib") THIS IS ONLY REQUIRED FOR SetProcessDpiAwareness | |
79 //#include <windows.h> | |
80 //#include <ShellScalingAPI.h> THIS IS ONLY REQUIRED FOR SetProcessDpiAwareness | |
81 //#include <comdef.h> THIS IS ONLY REQUIRED FOR SetProcessDpiAwareness | |
82 // SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE); | |
83 | |
84 // This is supported on Vista+ | |
85 SetProcessDPIAware(); | |
86 | |
87 windowFlags |= SDL_WINDOW_ALLOW_HIGHDPI; | |
88 } | |
89 #endif | |
90 // WIN32 | |
91 | |
92 window_ = SDL_CreateWindow(title.c_str(), | |
93 SDL_WINDOWPOS_UNDEFINED, | |
94 SDL_WINDOWPOS_UNDEFINED, | |
95 width, height, windowFlags); | |
96 | |
97 if (window_ == NULL) | |
98 { | |
99 LOG(ERROR) << "Cannot create the SDL window: " << SDL_GetError(); | |
100 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
101 } | |
102 | |
103 renderer_ = SDL_CreateRenderer(window_, -1, rendererFlags); | |
104 if (!renderer_) | |
105 { | |
106 LOG(ERROR) << "Cannot create the SDL renderer: " << SDL_GetError(); | |
107 SDL_DestroyWindow(window_); | |
108 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
109 } | |
110 } | |
111 | |
112 | |
113 SdlWindow::~SdlWindow() | |
114 { | |
115 if (renderer_ != NULL) | |
116 { | |
117 SDL_DestroyRenderer(renderer_); | |
118 } | |
119 | |
120 if (window_ != NULL) | |
121 { | |
122 SDL_DestroyWindow(window_); | |
123 } | |
124 } | |
125 | |
126 | |
127 unsigned int SdlWindow::GetWidth() const | |
128 { | |
129 int w = -1; | |
130 SDL_GetWindowSize(window_, &w, NULL); | |
131 | |
132 if (w < 0) | |
133 { | |
134 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
135 } | |
136 else | |
137 { | |
138 return static_cast<unsigned int>(w); | |
139 } | |
140 } | |
141 | |
142 | |
143 unsigned int SdlWindow::GetHeight() const | |
144 { | |
145 int h = -1; | |
146 SDL_GetWindowSize(window_, NULL, &h); | |
147 | |
148 if (h < 0) | |
149 { | |
150 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
151 } | |
152 else | |
153 { | |
154 return static_cast<unsigned int>(h); | |
155 } | |
156 } | |
157 | |
158 | |
159 void SdlWindow::Render(SDL_Surface* surface) | |
160 { | |
161 /** | |
162 * "You are strongly encouraged to call SDL_RenderClear() to | |
163 * initialize the backbuffer before starting each new frame's | |
164 * drawing, even if you plan to overwrite every pixel." | |
165 * https://wiki.libsdl.org/SDL_RenderPresent | |
166 **/ | |
167 SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255); | |
168 SDL_RenderClear(renderer_); // Clear the entire screen to our selected color | |
169 | |
170 SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer_, surface); | |
171 if (texture != NULL) | |
172 { | |
173 SDL_RenderCopy(renderer_, texture, NULL, NULL); | |
174 SDL_DestroyTexture(texture); | |
175 } | |
176 | |
177 SDL_RenderPresent(renderer_); | |
178 } | |
179 | |
180 | |
181 void SdlWindow::ToggleMaximize() | |
182 { | |
183 if (maximized_) | |
184 { | |
185 SDL_RestoreWindow(window_); | |
186 maximized_ = false; | |
187 } | |
188 else | |
189 { | |
190 SDL_MaximizeWindow(window_); | |
191 maximized_ = true; | |
192 } | |
193 } | |
194 | |
195 | |
196 void SdlWindow::GlobalInitialize() | |
197 { | |
198 if (SDL_Init(SDL_INIT_VIDEO) != 0) | |
199 { | |
200 LOG(ERROR) << "Cannot initialize SDL"; | |
201 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
202 } | |
203 } | |
204 | |
205 | |
206 void SdlWindow::GlobalFinalize() | |
207 { | |
208 SDL_Quit(); | |
209 } | |
210 } | |
211 | |
212 #endif |