Mercurial > hg > orthanc-stone
annotate Applications/Samples/Sdl/SdlHelpers.h @ 2004:6a96a1920fd8
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 02 Nov 2022 15:29:01 +0100 |
parents | 184b0aeae1af |
children | 07964689cb0b |
rev | line source |
---|---|
1457 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
1871
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1457 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Affero 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 * Affero General Public License for more details. | |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1591
diff
changeset
|
17 * |
1457 | 18 * You should have received a copy of the GNU Affero General Public License |
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 **/ | |
21 | |
22 | |
1382
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
23 #pragma once |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
24 |
1386
dfb48f0794b1
Ongoing splitting SDL vs WASM (preparing RtViewer WASM)
Benjamin Golinvaux <bgo@osimis.io>
parents:
1382
diff
changeset
|
25 #if ORTHANC_ENABLE_SDL != 1 |
dfb48f0794b1
Ongoing splitting SDL vs WASM (preparing RtViewer WASM)
Benjamin Golinvaux <bgo@osimis.io>
parents:
1382
diff
changeset
|
26 # error This file cannot be used if ORTHANC_ENABLE_SDL != 1 |
dfb48f0794b1
Ongoing splitting SDL vs WASM (preparing RtViewer WASM)
Benjamin Golinvaux <bgo@osimis.io>
parents:
1382
diff
changeset
|
27 #endif |
dfb48f0794b1
Ongoing splitting SDL vs WASM (preparing RtViewer WASM)
Benjamin Golinvaux <bgo@osimis.io>
parents:
1382
diff
changeset
|
28 |
1901 | 29 #include "../../../OrthancStone/Sources/Platforms/Sdl/SdlViewport.h" |
1392
ffdb82850e98
Sdl run loop in /Common (might revert if need to customize)
Benjamin Golinvaux <bgo@osimis.io>
parents:
1388
diff
changeset
|
30 |
ffdb82850e98
Sdl run loop in /Common (might revert if need to customize)
Benjamin Golinvaux <bgo@osimis.io>
parents:
1388
diff
changeset
|
31 #include <boost/shared_ptr.hpp> |
ffdb82850e98
Sdl run loop in /Common (might revert if need to customize)
Benjamin Golinvaux <bgo@osimis.io>
parents:
1388
diff
changeset
|
32 |
1386
dfb48f0794b1
Ongoing splitting SDL vs WASM (preparing RtViewer WASM)
Benjamin Golinvaux <bgo@osimis.io>
parents:
1382
diff
changeset
|
33 #include <SDL.h> |
dfb48f0794b1
Ongoing splitting SDL vs WASM (preparing RtViewer WASM)
Benjamin Golinvaux <bgo@osimis.io>
parents:
1382
diff
changeset
|
34 |
1382
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
35 #include <map> |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
36 #include <string> |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
37 |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
38 namespace OrthancStoneHelpers |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
39 { |
1392
ffdb82850e98
Sdl run loop in /Common (might revert if need to customize)
Benjamin Golinvaux <bgo@osimis.io>
parents:
1388
diff
changeset
|
40 |
1382
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
41 inline OrthancStone::KeyboardModifiers GetKeyboardModifiers(const uint8_t* keyboardState, |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
42 const int scancodeCount) |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
43 { |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
44 using namespace OrthancStone; |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
45 int result = KeyboardModifiers_None; |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
46 |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
47 if (keyboardState != NULL) |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
48 { |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
49 if (SDL_SCANCODE_LSHIFT < scancodeCount && |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
50 keyboardState[SDL_SCANCODE_LSHIFT]) |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
51 { |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
52 result |= KeyboardModifiers_Shift; |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
53 } |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
54 |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
55 if (SDL_SCANCODE_RSHIFT < scancodeCount && |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
56 keyboardState[SDL_SCANCODE_RSHIFT]) |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
57 { |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
58 result |= KeyboardModifiers_Shift; |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
59 } |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
60 |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
61 if (SDL_SCANCODE_LCTRL < scancodeCount && |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
62 keyboardState[SDL_SCANCODE_LCTRL]) |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
63 { |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
64 result |= KeyboardModifiers_Control; |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
65 } |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
66 |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
67 if (SDL_SCANCODE_RCTRL < scancodeCount && |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
68 keyboardState[SDL_SCANCODE_RCTRL]) |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
69 { |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
70 result |= KeyboardModifiers_Control; |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
71 } |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
72 |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
73 if (SDL_SCANCODE_LALT < scancodeCount && |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
74 keyboardState[SDL_SCANCODE_LALT]) |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
75 { |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
76 result |= KeyboardModifiers_Alt; |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
77 } |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
78 |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
79 if (SDL_SCANCODE_RALT < scancodeCount && |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
80 keyboardState[SDL_SCANCODE_RALT]) |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
81 { |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
82 result |= KeyboardModifiers_Alt; |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
83 } |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
84 } |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
85 |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
86 return static_cast<KeyboardModifiers>(result); |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
87 } |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
88 |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
89 |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
90 inline void GetPointerEvent(OrthancStone::PointerEvent& p, |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
91 const OrthancStone::ICompositor& compositor, |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
92 SDL_Event event, |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
93 const uint8_t* keyboardState, |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
94 const int scancodeCount) |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
95 { |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
96 using namespace OrthancStone; |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
97 KeyboardModifiers modifiers = GetKeyboardModifiers(keyboardState, scancodeCount); |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
98 |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
99 switch (event.button.button) |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
100 { |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
101 case SDL_BUTTON_LEFT: |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
102 p.SetMouseButton(OrthancStone::MouseButton_Left); |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
103 break; |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
104 |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
105 case SDL_BUTTON_RIGHT: |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
106 p.SetMouseButton(OrthancStone::MouseButton_Right); |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
107 break; |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
108 |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
109 case SDL_BUTTON_MIDDLE: |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
110 p.SetMouseButton(OrthancStone::MouseButton_Middle); |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
111 break; |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
112 |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
113 default: |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
114 p.SetMouseButton(OrthancStone::MouseButton_None); |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
115 break; |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
116 } |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
117 |
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
118 p.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y)); |
1449 | 119 p.SetAltModifier( (modifiers & KeyboardModifiers_Alt) != 0); |
120 p.SetControlModifier( (modifiers & KeyboardModifiers_Control) != 0); | |
121 p.SetShiftModifier( (modifiers & KeyboardModifiers_Shift) != 0); | |
1382
9d138883be66
Proper command-line parameter management for SDL sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
diff
changeset
|
122 } |
1392
ffdb82850e98
Sdl run loop in /Common (might revert if need to customize)
Benjamin Golinvaux <bgo@osimis.io>
parents:
1388
diff
changeset
|
123 |
1501 | 124 |
125 inline boost::shared_ptr<OrthancStone::SdlViewport> GetSdlViewportFromWindowId( | |
1404
3e644f6fadd4
Three-viewport is now OK in SDL and Wasm
Benjamin Golinvaux <bgo@osimis.io>
parents:
1392
diff
changeset
|
126 const std::vector<boost::shared_ptr<OrthancStone::SdlViewport> >& viewports, |
3e644f6fadd4
Three-viewport is now OK in SDL and Wasm
Benjamin Golinvaux <bgo@osimis.io>
parents:
1392
diff
changeset
|
127 Uint32 windowID) |
3e644f6fadd4
Three-viewport is now OK in SDL and Wasm
Benjamin Golinvaux <bgo@osimis.io>
parents:
1392
diff
changeset
|
128 { |
3e644f6fadd4
Three-viewport is now OK in SDL and Wasm
Benjamin Golinvaux <bgo@osimis.io>
parents:
1392
diff
changeset
|
129 using namespace OrthancStone; |
3e644f6fadd4
Three-viewport is now OK in SDL and Wasm
Benjamin Golinvaux <bgo@osimis.io>
parents:
1392
diff
changeset
|
130 for (size_t i = 0; i < viewports.size(); ++i) |
3e644f6fadd4
Three-viewport is now OK in SDL and Wasm
Benjamin Golinvaux <bgo@osimis.io>
parents:
1392
diff
changeset
|
131 { |
3e644f6fadd4
Three-viewport is now OK in SDL and Wasm
Benjamin Golinvaux <bgo@osimis.io>
parents:
1392
diff
changeset
|
132 boost::shared_ptr<IViewport> viewport = viewports[i]; |
3e644f6fadd4
Three-viewport is now OK in SDL and Wasm
Benjamin Golinvaux <bgo@osimis.io>
parents:
1392
diff
changeset
|
133 boost::shared_ptr<SdlViewport> sdlViewport = boost::dynamic_pointer_cast<SdlViewport>(viewport); |
3e644f6fadd4
Three-viewport is now OK in SDL and Wasm
Benjamin Golinvaux <bgo@osimis.io>
parents:
1392
diff
changeset
|
134 Uint32 curWindowID = sdlViewport->GetSdlWindowId(); |
3e644f6fadd4
Three-viewport is now OK in SDL and Wasm
Benjamin Golinvaux <bgo@osimis.io>
parents:
1392
diff
changeset
|
135 if (windowID == curWindowID) |
3e644f6fadd4
Three-viewport is now OK in SDL and Wasm
Benjamin Golinvaux <bgo@osimis.io>
parents:
1392
diff
changeset
|
136 return sdlViewport; |
3e644f6fadd4
Three-viewport is now OK in SDL and Wasm
Benjamin Golinvaux <bgo@osimis.io>
parents:
1392
diff
changeset
|
137 } |
1424
b424028392cf
fixing some build errors
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1406
diff
changeset
|
138 |
b424028392cf
fixing some build errors
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1406
diff
changeset
|
139 return boost::shared_ptr<OrthancStone::SdlViewport>(); |
1404
3e644f6fadd4
Three-viewport is now OK in SDL and Wasm
Benjamin Golinvaux <bgo@osimis.io>
parents:
1392
diff
changeset
|
140 } |
3e644f6fadd4
Three-viewport is now OK in SDL and Wasm
Benjamin Golinvaux <bgo@osimis.io>
parents:
1392
diff
changeset
|
141 } |
1392
ffdb82850e98
Sdl run loop in /Common (might revert if need to customize)
Benjamin Golinvaux <bgo@osimis.io>
parents:
1388
diff
changeset
|
142 |
ffdb82850e98
Sdl run loop in /Common (might revert if need to customize)
Benjamin Golinvaux <bgo@osimis.io>
parents:
1388
diff
changeset
|
143 |