Mercurial > hg > orthanc-stone
annotate Samples/Sdl/BasicScene.cpp @ 702:c0a5eb9a4290 refactor-viewport-controller
Merge from default
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Sun, 19 May 2019 16:29:50 +0200 |
parents | 059e1fd05fd6 |
children | ef07304d4423 |
rev | line source |
---|---|
600 | 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-2019 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
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. | |
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 | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 // From Stone | |
23 #include "../../Applications/Sdl/SdlOpenGLWindow.h" | |
24 #include "../../Framework/Scene2D/CairoCompositor.h" | |
610
0c4c0693b97c
OrthancStone::StoneInitialize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
607
diff
changeset
|
25 #include "../../Framework/Scene2D/ColorTextureSceneLayer.h" |
600 | 26 #include "../../Framework/Scene2D/OpenGLCompositor.h" |
27 #include "../../Framework/Scene2D/PanSceneTracker.h" | |
28 #include "../../Framework/Scene2D/RotateSceneTracker.h" | |
610
0c4c0693b97c
OrthancStone::StoneInitialize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
607
diff
changeset
|
29 #include "../../Framework/Scene2D/Scene2D.h" |
600 | 30 #include "../../Framework/Scene2D/ZoomSceneTracker.h" |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
31 #include "../../Framework/Scene2DViewport/ViewportController.h" |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
32 |
610
0c4c0693b97c
OrthancStone::StoneInitialize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
607
diff
changeset
|
33 #include "../../Framework/StoneInitialization.h" |
654
462a5074f914
Turned the scene into an observable to be able to dynamically react to
Benjamin Golinvaux <bgo@osimis.io>
parents:
617
diff
changeset
|
34 #include "../../Framework/Messages/MessageBroker.h" |
600 | 35 |
36 // From Orthanc framework | |
37 #include <Core/Logging.h> | |
38 #include <Core/OrthancException.h> | |
39 #include <Core/Images/Image.h> | |
40 #include <Core/Images/ImageProcessing.h> | |
41 #include <Core/Images/PngWriter.h> | |
42 | |
43 #include <SDL.h> | |
44 #include <stdio.h> | |
45 | |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
46 static const unsigned int FONT_SIZE = 32; |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
47 static const int LAYER_POSITION = 150; |
600 | 48 |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
49 using namespace OrthancStone; |
600 | 50 |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
51 void PrepareScene(ViewportControllerPtr controller) |
600 | 52 { |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
53 Scene2D& scene(*controller->GetScene()); |
600 | 54 // Texture of 2x2 size |
55 { | |
56 Orthanc::Image i(Orthanc::PixelFormat_RGB24, 2, 2, false); | |
57 | |
58 uint8_t *p = reinterpret_cast<uint8_t*>(i.GetRow(0)); | |
59 p[0] = 255; | |
60 p[1] = 0; | |
61 p[2] = 0; | |
62 | |
63 p[3] = 0; | |
64 p[4] = 255; | |
65 p[5] = 0; | |
66 | |
67 p = reinterpret_cast<uint8_t*>(i.GetRow(1)); | |
68 p[0] = 0; | |
69 p[1] = 0; | |
70 p[2] = 255; | |
71 | |
72 p[3] = 255; | |
73 p[4] = 0; | |
74 p[5] = 0; | |
75 | |
76 scene.SetLayer(12, new ColorTextureSceneLayer(i)); | |
77 | |
78 std::auto_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i)); | |
79 l->SetOrigin(-3, 2); | |
80 l->SetPixelSpacing(1.5, 1); | |
81 l->SetAngle(20.0 / 180.0 * M_PI); | |
82 scene.SetLayer(14, l.release()); | |
83 } | |
84 | |
85 // Texture of 1x1 size | |
86 { | |
87 Orthanc::Image i(Orthanc::PixelFormat_RGB24, 1, 1, false); | |
88 | |
89 uint8_t *p = reinterpret_cast<uint8_t*>(i.GetRow(0)); | |
90 p[0] = 255; | |
91 p[1] = 0; | |
92 p[2] = 0; | |
93 | |
94 std::auto_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i)); | |
95 l->SetOrigin(-2, 1); | |
96 l->SetAngle(20.0 / 180.0 * M_PI); | |
97 scene.SetLayer(13, l.release()); | |
98 } | |
99 | |
100 // Some lines | |
101 { | |
102 std::auto_ptr<PolylineSceneLayer> layer(new PolylineSceneLayer); | |
103 | |
104 layer->SetThickness(1); | |
105 | |
106 PolylineSceneLayer::Chain chain; | |
107 chain.push_back(ScenePoint2D(0 - 0.5, 0 - 0.5)); | |
108 chain.push_back(ScenePoint2D(0 - 0.5, 2 - 0.5)); | |
109 chain.push_back(ScenePoint2D(2 - 0.5, 2 - 0.5)); | |
110 chain.push_back(ScenePoint2D(2 - 0.5, 0 - 0.5)); | |
111 layer->AddChain(chain, true); | |
112 | |
113 chain.clear(); | |
114 chain.push_back(ScenePoint2D(-5, -5)); | |
115 chain.push_back(ScenePoint2D(5, -5)); | |
116 chain.push_back(ScenePoint2D(5, 5)); | |
117 chain.push_back(ScenePoint2D(-5, 5)); | |
118 layer->AddChain(chain, true); | |
119 | |
120 double dy = 1.01; | |
121 chain.clear(); | |
122 chain.push_back(ScenePoint2D(-4, -4)); | |
123 chain.push_back(ScenePoint2D(4, -4 + dy)); | |
124 chain.push_back(ScenePoint2D(-4, -4 + 2.0 * dy)); | |
125 chain.push_back(ScenePoint2D(4, 2)); | |
126 layer->AddChain(chain, false); | |
127 | |
128 layer->SetColor(0,255, 255); | |
129 scene.SetLayer(50, layer.release()); | |
130 } | |
131 | |
132 // Some text | |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
133 { |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
134 std::auto_ptr<TextSceneLayer> layer(new TextSceneLayer); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
135 layer->SetText("Hello"); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
136 scene.SetLayer(100, layer.release()); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
137 } |
600 | 138 } |
139 | |
140 | |
141 void TakeScreenshot(const std::string& target, | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
142 const Scene2D& scene, |
600 | 143 unsigned int canvasWidth, |
144 unsigned int canvasHeight) | |
145 { | |
146 // Take a screenshot, then save it as PNG file | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
147 CairoCompositor compositor(scene, canvasWidth, canvasHeight); |
600 | 148 compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, FONT_SIZE, Orthanc::Encoding_Latin1); |
149 compositor.Refresh(); | |
150 | |
151 Orthanc::ImageAccessor canvas; | |
152 compositor.GetCanvas().GetReadOnlyAccessor(canvas); | |
153 | |
154 Orthanc::Image png(Orthanc::PixelFormat_RGB24, canvas.GetWidth(), canvas.GetHeight(), false); | |
155 Orthanc::ImageProcessing::Convert(png, canvas); | |
156 | |
157 Orthanc::PngWriter writer; | |
158 writer.WriteToFile(target, png); | |
159 } | |
160 | |
161 | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
162 void HandleApplicationEvent(ViewportControllerPtr controller, |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
163 const OpenGLCompositor& compositor, |
600 | 164 const SDL_Event& event, |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
165 FlexiblePointerTrackerPtr& activeTracker) |
600 | 166 { |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
167 Scene2D& scene(*controller->GetScene()); |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
168 if (event.type == SDL_MOUSEMOTION) |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
169 { |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
170 int scancodeCount = 0; |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
171 const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
172 |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
173 if (activeTracker.get() == NULL && |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
174 SDL_SCANCODE_LCTRL < scancodeCount && |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
175 keyboardState[SDL_SCANCODE_LCTRL]) |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
176 { |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
177 // The "left-ctrl" key is down, while no tracker is present |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
178 |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
179 PointerEvent e; |
617 | 180 e.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y)); |
181 | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
182 ScenePoint2D p = e.GetMainPosition().Apply(scene.GetCanvasToSceneTransform()); |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
183 |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
184 char buf[64]; |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
185 sprintf(buf, "(%0.02f,%0.02f)", p.GetX(), p.GetY()); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
186 |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
187 if (scene.HasLayer(LAYER_POSITION)) |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
188 { |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
189 TextSceneLayer& layer = |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
190 dynamic_cast<TextSceneLayer&>(scene.GetLayer(LAYER_POSITION)); |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
191 layer.SetText(buf); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
192 layer.SetPosition(p.GetX(), p.GetY()); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
193 } |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
194 else |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
195 { |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
196 std::auto_ptr<TextSceneLayer> |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
197 layer(new TextSceneLayer); |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
198 layer->SetColor(0, 255, 0); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
199 layer->SetText(buf); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
200 layer->SetBorder(20); |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
201 layer->SetAnchor(BitmapAnchor_BottomCenter); |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
202 layer->SetPosition(p.GetX(), p.GetY()); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
203 scene.SetLayer(LAYER_POSITION, layer.release()); |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
204 } |
617 | 205 } |
206 else | |
207 { | |
208 scene.DeleteLayer(LAYER_POSITION); | |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
209 } |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
210 } |
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
211 else if (event.type == SDL_MOUSEBUTTONDOWN) |
600 | 212 { |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
213 PointerEvent e; |
617 | 214 e.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y)); |
600 | 215 |
216 switch (event.button.button) | |
217 { | |
218 case SDL_BUTTON_MIDDLE: | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
219 activeTracker.reset(new PanSceneTracker( |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
220 controller, e)); |
600 | 221 break; |
222 | |
223 case SDL_BUTTON_RIGHT: | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
224 activeTracker.reset(new ZoomSceneTracker( |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
225 controller, e, compositor.GetCanvasHeight())); |
600 | 226 break; |
227 | |
228 case SDL_BUTTON_LEFT: | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
229 activeTracker.reset(new RotateSceneTracker( |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
230 controller, e)); |
600 | 231 break; |
232 | |
233 default: | |
234 break; | |
235 } | |
236 } | |
237 else if (event.type == SDL_KEYDOWN && | |
238 event.key.repeat == 0 /* Ignore key bounce */) | |
239 { | |
240 switch (event.key.keysym.sym) | |
241 { | |
242 case SDLK_s: | |
617 | 243 scene.FitContent(compositor.GetCanvasWidth(), |
244 compositor.GetCanvasHeight()); | |
600 | 245 break; |
246 | |
247 case SDLK_c: | |
617 | 248 TakeScreenshot("screenshot.png", scene, |
249 compositor.GetCanvasWidth(), | |
250 compositor.GetCanvasHeight()); | |
600 | 251 break; |
252 | |
253 default: | |
254 break; | |
255 } | |
256 } | |
257 } | |
258 | |
259 | |
260 static void GLAPIENTRY | |
261 OpenGLMessageCallback(GLenum source, | |
262 GLenum type, | |
263 GLuint id, | |
264 GLenum severity, | |
265 GLsizei length, | |
266 const GLchar* message, | |
267 const void* userParam ) | |
268 { | |
269 if (severity != GL_DEBUG_SEVERITY_NOTIFICATION) | |
270 { | |
271 fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n", | |
272 ( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ), | |
273 type, severity, message ); | |
274 } | |
275 } | |
276 | |
277 | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
278 void Run(ViewportControllerPtr controller) |
600 | 279 { |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
280 SdlOpenGLWindow window("Hello", 1024, 768); |
611
e3f21a265be5
Added version directive to GLSL shader code + glew init function in sample code
Benjamin Golinvaux <bgo@osimis.io>
parents:
610
diff
changeset
|
281 |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
282 controller->GetScene()->FitContent( |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
283 window.GetCanvasWidth(), window.GetCanvasHeight()); |
611
e3f21a265be5
Added version directive to GLSL shader code + glew init function in sample code
Benjamin Golinvaux <bgo@osimis.io>
parents:
610
diff
changeset
|
284 |
600 | 285 glEnable(GL_DEBUG_OUTPUT); |
613
412a2d01a189
automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
611
diff
changeset
|
286 glDebugMessageCallback(OpenGLMessageCallback, 0); |
600 | 287 |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
288 OpenGLCompositor compositor(window, *controller->GetScene()); |
600 | 289 compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, |
290 FONT_SIZE, Orthanc::Encoding_Latin1); | |
291 | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
292 FlexiblePointerTrackerPtr tracker; |
600 | 293 |
294 bool stop = false; | |
295 while (!stop) | |
296 { | |
297 compositor.Refresh(); | |
298 | |
299 SDL_Event event; | |
300 while (!stop && | |
301 SDL_PollEvent(&event)) | |
302 { | |
303 if (event.type == SDL_QUIT) | |
304 { | |
305 stop = true; | |
306 break; | |
307 } | |
308 else if (event.type == SDL_MOUSEMOTION) | |
309 { | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
310 if (tracker) |
600 | 311 { |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
312 PointerEvent e; |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
313 e.AddPosition(compositor.GetPixelCenterCoordinates( |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
314 event.button.x, event.button.y)); |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
315 tracker->PointerMove(e); |
600 | 316 } |
317 } | |
318 else if (event.type == SDL_MOUSEBUTTONUP) | |
319 { | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
320 if (tracker) |
600 | 321 { |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
322 PointerEvent e; |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
323 e.AddPosition(compositor.GetPixelCenterCoordinates( |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
324 event.button.x, event.button.y)); |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
325 tracker->PointerUp(e); |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
326 if(!tracker->IsAlive()) |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
327 tracker = NULL; |
600 | 328 } |
329 } | |
330 else if (event.type == SDL_WINDOWEVENT && | |
331 event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) | |
332 { | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
333 tracker = NULL; |
600 | 334 compositor.UpdateSize(); |
335 } | |
336 else if (event.type == SDL_KEYDOWN && | |
337 event.key.repeat == 0 /* Ignore key bounce */) | |
338 { | |
339 switch (event.key.keysym.sym) | |
340 { | |
341 case SDLK_f: | |
342 window.GetWindow().ToggleMaximize(); | |
343 break; | |
344 | |
345 case SDLK_q: | |
346 stop = true; | |
347 break; | |
348 | |
349 default: | |
350 break; | |
351 } | |
352 } | |
602
03c4b998fcd0
display scene positions in the basic sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
600
diff
changeset
|
353 |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
354 HandleApplicationEvent(controller, compositor, event, tracker); |
600 | 355 } |
356 | |
357 SDL_Delay(1); | |
358 } | |
359 } | |
360 | |
361 | |
607
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
362 |
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
363 |
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
364 /** |
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
365 * IMPORTANT: The full arguments to "main()" are needed for SDL on |
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
366 * Windows. Otherwise, one gets the linking error "undefined reference |
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
367 * to `SDL_main'". https://wiki.libsdl.org/FAQWindows |
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
368 **/ |
f4b37a991dac
using glew to build on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
602
diff
changeset
|
369 int main(int argc, char* argv[]) |
600 | 370 { |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
371 StoneInitialize(); |
613
412a2d01a189
automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
611
diff
changeset
|
372 Orthanc::Logging::EnableInfoLevel(true); |
600 | 373 |
374 try | |
375 { | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
376 MessageBroker broker; |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
377 ViewportControllerPtr controller( |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
378 new ViewportController(broker)); |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
379 PrepareScene(controller); |
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
380 Run(controller); |
600 | 381 } |
382 catch (Orthanc::OrthancException& e) | |
383 { | |
384 LOG(ERROR) << "EXCEPTION: " << e.What(); | |
385 } | |
386 | |
700
059e1fd05fd6
Introduced the ViewportController that sits between the application and the
Benjamin Golinvaux <bgo@osimis.io>
parents:
654
diff
changeset
|
387 StoneFinalize(); |
600 | 388 |
389 return 0; | |
390 } |