Mercurial > hg > orthanc-stone
annotate Applications/Sdl/SdlEngine.cpp @ 378:814fa32e2fcc
refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 06 Nov 2018 14:21:02 +0100 |
parents | 7a364e44fbb4 |
children | 6cc3ce74dc05 |
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 | |
134
4cff7b1ed31d
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
5 * Copyright (C) 2017-2018 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. | |
327 | 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 "SdlEngine.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> |
201
e9c7a78a3e77
removed absolute paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
196
diff
changeset
|
27 |
0 | 28 #include <SDL.h> |
29 | |
30 namespace OrthancStone | |
31 { | |
271
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
222
diff
changeset
|
32 void SdlEngine::SetSize(unsigned int width, |
56
9e3c2e75b870
extremely simplified SDL engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
33 unsigned int height) |
9e3c2e75b870
extremely simplified SDL engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
34 { |
271
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
222
diff
changeset
|
35 context_.GetCentralViewport().SetSize(width, height); |
56
9e3c2e75b870
extremely simplified SDL engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
36 surface_.SetSize(width, height); |
9e3c2e75b870
extremely simplified SDL engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
37 } |
327 | 38 |
56
9e3c2e75b870
extremely simplified SDL engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
39 |
0 | 40 void SdlEngine::RenderFrame() |
41 { | |
56
9e3c2e75b870
extremely simplified SDL engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
42 if (viewportChanged_) |
0 | 43 { |
293 | 44 NativeStoneApplicationContext::GlobalMutexLocker locker(context_); |
271
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
222
diff
changeset
|
45 surface_.Render(context_.GetCentralViewport()); |
53
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
51
diff
changeset
|
46 |
56
9e3c2e75b870
extremely simplified SDL engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
47 viewportChanged_ = false; |
0 | 48 } |
49 } | |
50 | |
51 | |
52 KeyboardModifiers SdlEngine::GetKeyboardModifiers(const uint8_t* keyboardState, | |
53 const int scancodeCount) | |
54 { | |
55 int result = KeyboardModifiers_None; | |
56 | |
57 if (keyboardState != NULL) | |
58 { | |
59 if (SDL_SCANCODE_LSHIFT < scancodeCount && | |
60 keyboardState[SDL_SCANCODE_LSHIFT]) | |
61 { | |
62 result |= KeyboardModifiers_Shift; | |
63 } | |
64 | |
65 if (SDL_SCANCODE_RSHIFT < scancodeCount && | |
66 keyboardState[SDL_SCANCODE_RSHIFT]) | |
67 { | |
68 result |= KeyboardModifiers_Shift; | |
69 } | |
70 | |
71 if (SDL_SCANCODE_LCTRL < scancodeCount && | |
72 keyboardState[SDL_SCANCODE_LCTRL]) | |
73 { | |
74 result |= KeyboardModifiers_Control; | |
75 } | |
76 | |
77 if (SDL_SCANCODE_RCTRL < scancodeCount && | |
78 keyboardState[SDL_SCANCODE_RCTRL]) | |
79 { | |
80 result |= KeyboardModifiers_Control; | |
81 } | |
82 | |
83 if (SDL_SCANCODE_LALT < scancodeCount && | |
84 keyboardState[SDL_SCANCODE_LALT]) | |
85 { | |
86 result |= KeyboardModifiers_Alt; | |
87 } | |
88 | |
89 if (SDL_SCANCODE_RALT < scancodeCount && | |
90 keyboardState[SDL_SCANCODE_RALT]) | |
91 { | |
92 result |= KeyboardModifiers_Alt; | |
93 } | |
94 } | |
95 | |
96 return static_cast<KeyboardModifiers>(result); | |
97 } | |
98 | |
99 | |
100 SdlEngine::SdlEngine(SdlWindow& window, | |
293 | 101 NativeStoneApplicationContext& context) : |
0 | 102 window_(window), |
53
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
51
diff
changeset
|
103 context_(context), |
56
9e3c2e75b870
extremely simplified SDL engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
104 surface_(window), |
9e3c2e75b870
extremely simplified SDL engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
105 viewportChanged_(true) |
0 | 106 { |
107 } | |
108 | |
109 | |
110 SdlEngine::~SdlEngine() | |
111 { | |
112 } | |
113 | |
114 | |
115 void SdlEngine::Run() | |
116 { | |
117 int scancodeCount = 0; | |
118 const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount); | |
119 | |
61
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
120 { |
293 | 121 NativeStoneApplicationContext::GlobalMutexLocker locker(context_); |
271
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
222
diff
changeset
|
122 SetSize(window_.GetWidth(), window_.GetHeight()); |
330 | 123 context_.GetCentralViewport().FitContent(); |
61
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
124 } |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
125 |
0 | 126 bool stop = false; |
127 while (!stop) | |
128 { | |
56
9e3c2e75b870
extremely simplified SDL engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
129 RenderFrame(); |
0 | 130 |
131 SDL_Event event; | |
132 | |
56
9e3c2e75b870
extremely simplified SDL engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
133 while (!stop && |
9e3c2e75b870
extremely simplified SDL engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
134 SDL_PollEvent(&event)) |
0 | 135 { |
293 | 136 NativeStoneApplicationContext::GlobalMutexLocker locker(context_); |
53
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
51
diff
changeset
|
137 |
327 | 138 if (event.type == SDL_QUIT) |
0 | 139 { |
140 stop = true; | |
141 break; | |
142 } | |
143 else if (event.type == SDL_MOUSEBUTTONDOWN) | |
144 { | |
145 KeyboardModifiers modifiers = GetKeyboardModifiers(keyboardState, scancodeCount); | |
146 | |
147 switch (event.button.button) | |
148 { | |
327 | 149 case SDL_BUTTON_LEFT: |
150 context_.GetCentralViewport().MouseDown(MouseButton_Left, event.button.x, event.button.y, modifiers); | |
151 break; | |
0 | 152 |
327 | 153 case SDL_BUTTON_RIGHT: |
154 context_.GetCentralViewport().MouseDown(MouseButton_Right, event.button.x, event.button.y, modifiers); | |
155 break; | |
0 | 156 |
327 | 157 case SDL_BUTTON_MIDDLE: |
158 context_.GetCentralViewport().MouseDown(MouseButton_Middle, event.button.x, event.button.y, modifiers); | |
159 break; | |
0 | 160 |
327 | 161 default: |
162 break; | |
0 | 163 } |
164 } | |
165 else if (event.type == SDL_MOUSEMOTION) | |
166 { | |
271
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
222
diff
changeset
|
167 context_.GetCentralViewport().MouseMove(event.button.x, event.button.y); |
0 | 168 } |
169 else if (event.type == SDL_MOUSEBUTTONUP) | |
170 { | |
271
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
222
diff
changeset
|
171 context_.GetCentralViewport().MouseUp(); |
0 | 172 } |
173 else if (event.type == SDL_WINDOWEVENT) | |
174 { | |
175 switch (event.window.event) | |
176 { | |
327 | 177 case SDL_WINDOWEVENT_LEAVE: |
178 context_.GetCentralViewport().MouseLeave(); | |
179 break; | |
0 | 180 |
327 | 181 case SDL_WINDOWEVENT_ENTER: |
182 context_.GetCentralViewport().MouseEnter(); | |
183 break; | |
0 | 184 |
327 | 185 case SDL_WINDOWEVENT_SIZE_CHANGED: |
186 SetSize(event.window.data1, event.window.data2); | |
187 break; | |
0 | 188 |
327 | 189 default: |
190 break; | |
0 | 191 } |
192 } | |
193 else if (event.type == SDL_MOUSEWHEEL) | |
194 { | |
195 KeyboardModifiers modifiers = GetKeyboardModifiers(keyboardState, scancodeCount); | |
196 | |
197 int x, y; | |
198 SDL_GetMouseState(&x, &y); | |
199 | |
200 if (event.wheel.y > 0) | |
201 { | |
271
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
222
diff
changeset
|
202 context_.GetCentralViewport().MouseWheel(MouseWheelDirection_Up, x, y, modifiers); |
0 | 203 } |
204 else if (event.wheel.y < 0) | |
205 { | |
271
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
222
diff
changeset
|
206 context_.GetCentralViewport().MouseWheel(MouseWheelDirection_Down, x, y, modifiers); |
0 | 207 } |
208 } | |
56
9e3c2e75b870
extremely simplified SDL engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
209 else if (event.type == SDL_KEYDOWN && |
9e3c2e75b870
extremely simplified SDL engine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
210 event.key.repeat == 0 /* Ignore key bounce */) |
0 | 211 { |
212 KeyboardModifiers modifiers = GetKeyboardModifiers(keyboardState, scancodeCount); | |
213 | |
214 switch (event.key.keysym.sym) | |
215 { | |
327 | 216 case SDLK_a: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'a', modifiers); break; |
217 case SDLK_b: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'b', modifiers); break; | |
218 case SDLK_c: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'c', modifiers); break; | |
219 case SDLK_d: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'd', modifiers); break; | |
220 case SDLK_e: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'e', modifiers); break; | |
221 case SDLK_f: window_.ToggleMaximize(); break; | |
222 case SDLK_g: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'g', modifiers); break; | |
223 case SDLK_h: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'h', modifiers); break; | |
224 case SDLK_i: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'i', modifiers); break; | |
225 case SDLK_j: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'j', modifiers); break; | |
226 case SDLK_k: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'k', modifiers); break; | |
227 case SDLK_l: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'l', modifiers); break; | |
228 case SDLK_m: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'm', modifiers); break; | |
229 case SDLK_n: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'n', modifiers); break; | |
230 case SDLK_o: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'o', modifiers); break; | |
231 case SDLK_p: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'p', modifiers); break; | |
232 case SDLK_q: stop = true; break; | |
233 case SDLK_r: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'r', modifiers); break; | |
234 case SDLK_s: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 's', modifiers); break; | |
235 case SDLK_t: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 't', modifiers); break; | |
236 case SDLK_u: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'u', modifiers); break; | |
237 case SDLK_v: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'v', modifiers); break; | |
238 case SDLK_w: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'w', modifiers); break; | |
239 case SDLK_x: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'x', modifiers); break; | |
240 case SDLK_y: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'y', modifiers); break; | |
241 case SDLK_z: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, 'z', modifiers); break; | |
242 case SDLK_KP_0: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, '0', modifiers); break; | |
243 case SDLK_KP_1: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, '1', modifiers); break; | |
244 case SDLK_KP_2: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, '2', modifiers); break; | |
245 case SDLK_KP_3: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, '3', modifiers); break; | |
246 case SDLK_KP_4: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, '4', modifiers); break; | |
247 case SDLK_KP_5: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, '5', modifiers); break; | |
248 case SDLK_KP_6: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, '6', modifiers); break; | |
249 case SDLK_KP_7: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, '7', modifiers); break; | |
250 case SDLK_KP_8: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, '8', modifiers); break; | |
251 case SDLK_KP_9: context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, '9', modifiers); break; | |
0 | 252 |
327 | 253 case SDLK_PLUS: |
254 case SDLK_KP_PLUS: | |
255 context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, '+', modifiers); break; | |
256 | |
257 case SDLK_MINUS: | |
258 case SDLK_KP_MINUS: | |
259 context_.GetCentralViewport().KeyPressed(KeyboardKeys_Generic, '-', modifiers); break; | |
170
0261909fa6f0
add support for plus/minus in SDL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
167
diff
changeset
|
260 |
327 | 261 case SDLK_RIGHT: |
262 context_.GetCentralViewport().KeyPressed(KeyboardKeys_Right, 0, modifiers); break; | |
263 case SDLK_LEFT: | |
264 context_.GetCentralViewport().KeyPressed(KeyboardKeys_Left, 0, modifiers); break; | |
265 case SDLK_UP: | |
266 context_.GetCentralViewport().KeyPressed(KeyboardKeys_Up, 0, modifiers); break; | |
267 case SDLK_DOWN: | |
268 context_.GetCentralViewport().KeyPressed(KeyboardKeys_Down, 0, modifiers); break; | |
269 default: | |
270 break; | |
0 | 271 } |
272 } | |
273 } | |
274 | |
98
a33abae66344
scrolling over series
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
275 // Small delay to avoid using 100% of CPU |
a33abae66344
scrolling over series
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
276 SDL_Delay(1); |
0 | 277 } |
278 } | |
279 } | |
280 | |
281 #endif |