comparison Framework/Applications/Sdl/SdlEngine.cpp @ 0:351ab0da0150

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 14 Oct 2016 15:34:11 +0200
parents
children ff1e935768e7
comparison
equal deleted inserted replaced
-1:000000000000 0:351ab0da0150
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #include "SdlEngine.h"
34
35 #if ORTHANC_ENABLE_SDL == 1
36
37 #include "../../Orthanc/Core/Logging.h"
38
39 #include <SDL.h>
40
41 namespace OrthancStone
42 {
43 void SdlEngine::RenderFrame()
44 {
45 if (!viewportChanged_)
46 {
47 return;
48 }
49
50 viewportChanged_ = false;
51
52 if (buffering_.RenderOffscreen(viewport_))
53 {
54 // Do not notify twice when a new frame was rendered, to avoid
55 // spoiling the SDL event queue
56 SDL_Event event;
57 SDL_memset(&event, 0, sizeof(event));
58 event.type = refreshEvent_;
59 event.user.code = 0;
60 event.user.data1 = 0;
61 event.user.data2 = 0;
62 SDL_PushEvent(&event);
63 }
64 }
65
66
67 void SdlEngine::RenderThread(SdlEngine* that)
68 {
69 for (;;)
70 {
71 that->renderFrame_.Wait();
72
73 if (that->continue_)
74 {
75 that->RenderFrame();
76 }
77 else
78 {
79 return;
80 }
81 }
82 }
83
84
85 KeyboardModifiers SdlEngine::GetKeyboardModifiers(const uint8_t* keyboardState,
86 const int scancodeCount)
87 {
88 int result = KeyboardModifiers_None;
89
90 if (keyboardState != NULL)
91 {
92 if (SDL_SCANCODE_LSHIFT < scancodeCount &&
93 keyboardState[SDL_SCANCODE_LSHIFT])
94 {
95 result |= KeyboardModifiers_Shift;
96 }
97
98 if (SDL_SCANCODE_RSHIFT < scancodeCount &&
99 keyboardState[SDL_SCANCODE_RSHIFT])
100 {
101 result |= KeyboardModifiers_Shift;
102 }
103
104 if (SDL_SCANCODE_LCTRL < scancodeCount &&
105 keyboardState[SDL_SCANCODE_LCTRL])
106 {
107 result |= KeyboardModifiers_Control;
108 }
109
110 if (SDL_SCANCODE_RCTRL < scancodeCount &&
111 keyboardState[SDL_SCANCODE_RCTRL])
112 {
113 result |= KeyboardModifiers_Control;
114 }
115
116 if (SDL_SCANCODE_LALT < scancodeCount &&
117 keyboardState[SDL_SCANCODE_LALT])
118 {
119 result |= KeyboardModifiers_Alt;
120 }
121
122 if (SDL_SCANCODE_RALT < scancodeCount &&
123 keyboardState[SDL_SCANCODE_RALT])
124 {
125 result |= KeyboardModifiers_Alt;
126 }
127 }
128
129 return static_cast<KeyboardModifiers>(result);
130 }
131
132
133 void SdlEngine::SetSize(unsigned int width,
134 unsigned int height)
135 {
136 buffering_.SetSize(width, height, viewport_);
137 viewportChanged_ = true;
138 Refresh();
139 }
140
141
142 void SdlEngine::Stop()
143 {
144 if (continue_)
145 {
146 continue_ = false;
147 renderFrame_.Signal(); // Unlock the render thread
148 renderThread_.join();
149 }
150 }
151
152
153 void SdlEngine::Refresh()
154 {
155 renderFrame_.Signal();
156 }
157
158
159 SdlEngine::SdlEngine(SdlWindow& window,
160 IViewport& viewport) :
161 window_(window),
162 viewport_(viewport),
163 continue_(true)
164 {
165 refreshEvent_ = SDL_RegisterEvents(1);
166
167 SetSize(window_.GetWidth(), window_.GetHeight());
168
169 viewport_.Register(*this);
170
171 renderThread_ = boost::thread(RenderThread, this);
172 }
173
174
175 SdlEngine::~SdlEngine()
176 {
177 Stop();
178
179 viewport_.Unregister(*this);
180 }
181
182
183 void SdlEngine::Run()
184 {
185 int scancodeCount = 0;
186 const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount);
187
188 bool stop = false;
189 while (!stop)
190 {
191 Refresh();
192
193 SDL_Event event;
194
195 while (SDL_PollEvent(&event))
196 {
197 if (event.type == SDL_QUIT)
198 {
199 stop = true;
200 break;
201 }
202 else if (event.type == refreshEvent_)
203 {
204 buffering_.SwapToScreen(window_);
205 }
206 else if (event.type == SDL_MOUSEBUTTONDOWN)
207 {
208 KeyboardModifiers modifiers = GetKeyboardModifiers(keyboardState, scancodeCount);
209
210 switch (event.button.button)
211 {
212 case SDL_BUTTON_LEFT:
213 viewport_.MouseDown(MouseButton_Left, event.button.x, event.button.y, modifiers);
214 break;
215
216 case SDL_BUTTON_RIGHT:
217 viewport_.MouseDown(MouseButton_Right, event.button.x, event.button.y, modifiers);
218 break;
219
220 case SDL_BUTTON_MIDDLE:
221 viewport_.MouseDown(MouseButton_Middle, event.button.x, event.button.y, modifiers);
222 break;
223
224 default:
225 break;
226 }
227 }
228 else if (event.type == SDL_MOUSEMOTION)
229 {
230 viewport_.MouseMove(event.button.x, event.button.y);
231 }
232 else if (event.type == SDL_MOUSEBUTTONUP)
233 {
234 viewport_.MouseUp();
235 }
236 else if (event.type == SDL_WINDOWEVENT)
237 {
238 switch (event.window.event)
239 {
240 case SDL_WINDOWEVENT_LEAVE:
241 viewport_.MouseLeave();
242 break;
243
244 case SDL_WINDOWEVENT_ENTER:
245 viewport_.MouseEnter();
246 break;
247
248 case SDL_WINDOWEVENT_SIZE_CHANGED:
249 SetSize(event.window.data1, event.window.data2);
250 break;
251
252 default:
253 break;
254 }
255 }
256 else if (event.type == SDL_MOUSEWHEEL)
257 {
258 KeyboardModifiers modifiers = GetKeyboardModifiers(keyboardState, scancodeCount);
259
260 int x, y;
261 SDL_GetMouseState(&x, &y);
262
263 if (event.wheel.y > 0)
264 {
265 viewport_.MouseWheel(MouseWheelDirection_Up, x, y, modifiers);
266 }
267 else if (event.wheel.y < 0)
268 {
269 viewport_.MouseWheel(MouseWheelDirection_Down, x, y, modifiers);
270 }
271 }
272 else if (event.type == SDL_KEYDOWN)
273 {
274 KeyboardModifiers modifiers = GetKeyboardModifiers(keyboardState, scancodeCount);
275
276 switch (event.key.keysym.sym)
277 {
278 case SDLK_a: viewport_.KeyPressed('a', modifiers); break;
279 case SDLK_b: viewport_.KeyPressed('b', modifiers); break;
280 case SDLK_c: viewport_.KeyPressed('c', modifiers); break;
281 case SDLK_d: viewport_.KeyPressed('d', modifiers); break;
282 case SDLK_e: viewport_.KeyPressed('e', modifiers); break;
283 case SDLK_f: window_.ToggleMaximize(); break;
284 case SDLK_g: viewport_.KeyPressed('g', modifiers); break;
285 case SDLK_h: viewport_.KeyPressed('h', modifiers); break;
286 case SDLK_i: viewport_.KeyPressed('i', modifiers); break;
287 case SDLK_j: viewport_.KeyPressed('j', modifiers); break;
288 case SDLK_k: viewport_.KeyPressed('k', modifiers); break;
289 case SDLK_l: viewport_.KeyPressed('l', modifiers); break;
290 case SDLK_m: viewport_.KeyPressed('m', modifiers); break;
291 case SDLK_n: viewport_.KeyPressed('n', modifiers); break;
292 case SDLK_o: viewport_.KeyPressed('o', modifiers); break;
293 case SDLK_p: viewport_.KeyPressed('p', modifiers); break;
294 case SDLK_q: stop = true; break;
295 case SDLK_r: viewport_.KeyPressed('r', modifiers); break;
296 case SDLK_s: viewport_.KeyPressed('s', modifiers); break;
297 case SDLK_t: viewport_.KeyPressed('t', modifiers); break;
298 case SDLK_u: viewport_.KeyPressed('u', modifiers); break;
299 case SDLK_v: viewport_.KeyPressed('v', modifiers); break;
300 case SDLK_w: viewport_.KeyPressed('w', modifiers); break;
301 case SDLK_x: viewport_.KeyPressed('x', modifiers); break;
302 case SDLK_y: viewport_.KeyPressed('y', modifiers); break;
303 case SDLK_z: viewport_.KeyPressed('z', modifiers); break;
304
305 default:
306 break;
307 }
308 }
309 }
310
311 SDL_Delay(10); // Necessary for mouse wheel events to work
312 }
313
314 Stop();
315 }
316
317
318 void SdlEngine::GlobalInitialize()
319 {
320 SDL_Init(SDL_INIT_VIDEO);
321 }
322
323
324 void SdlEngine::GlobalFinalize()
325 {
326 SDL_Quit();
327 }
328 }
329
330 #endif