annotate Applications/Sdl/SdlStoneApplicationRunner.cpp @ 622:8a3a25f2d42c

uncoupling oracle from context
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 May 2019 12:32:21 +0200
parents b70e9be013e4
children 4f2416d519b4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
1 /**
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
2 * Stone of Orthanc
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
439
b70e9be013e4 preparing for 2019
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 385
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
6 *
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
10 * the License, or (at your option) any later version.
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
11 *
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
15 * Affero General Public License for more details.
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
16 *
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
19 **/
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
20
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
21
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
22 #if ORTHANC_ENABLE_SDL != 1
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
23 #error this file shall be included only with the ORTHANC_ENABLE_SDL set to 1
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
24 #endif
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
25
294
faccc4b07b92 renamings
am@osimis.io
parents: 293
diff changeset
26 #include "SdlStoneApplicationRunner.h"
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
27
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
28 #include "../../Framework/Toolbox/MessagingToolbox.h"
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
29 #include "../../Platforms/Generic/OracleWebService.h"
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
30 #include "SdlEngine.h"
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
31
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
32 #include <Core/Logging.h>
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
33 #include <Core/HttpClient.h>
260
am@osimis.io
parents: 251
diff changeset
34 #include <Core/Toolbox.h>
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
35 #include <Core/OrthancException.h>
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
36 #include <Plugins/Samples/Common/OrthancHttpConnection.h>
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
37
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
38 #include <boost/program_options.hpp>
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
39
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
40 namespace OrthancStone
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
41 {
293
017044be141b renaming
am@osimis.io
parents: 279
diff changeset
42 void SdlStoneApplicationRunner::Initialize()
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
43 {
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
44 SdlWindow::GlobalInitialize();
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
45 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
46
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
47
293
017044be141b renaming
am@osimis.io
parents: 279
diff changeset
48 void SdlStoneApplicationRunner::DeclareCommandLineOptions(boost::program_options::options_description& options)
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
49 {
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
50 boost::program_options::options_description sdl("SDL options");
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
51 sdl.add_options()
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
52 ("width", boost::program_options::value<int>()->default_value(1024), "Initial width of the SDL window")
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
53 ("height", boost::program_options::value<int>()->default_value(768), "Initial height of the SDL window")
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
54 ("opengl", boost::program_options::value<bool>()->default_value(true), "Enable OpenGL in SDL")
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
55 ;
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
56
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
57 options.add(sdl);
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
58 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
59
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
60
293
017044be141b renaming
am@osimis.io
parents: 279
diff changeset
61 void SdlStoneApplicationRunner::ParseCommandLineOptions(const boost::program_options::variables_map& parameters)
276
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
62 {
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
63 if (!parameters.count("width") ||
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
64 !parameters.count("height") ||
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
65 !parameters.count("opengl"))
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
66 {
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
67 LOG(ERROR) << "Parameter \"width\", \"height\" or \"opengl\" is missing";
279
8f5d7495076d SimpleViewer sample working in SDL, Qt and WASM
am@osimis.io
parents: 276
diff changeset
68 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
276
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
69 }
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
70
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
71 int w = parameters["width"].as<int>();
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
72 int h = parameters["height"].as<int>();
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
73 if (w <= 0 || h <= 0)
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
74 {
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
75 LOG(ERROR) << "Parameters \"width\" and \"height\" must be positive";
279
8f5d7495076d SimpleViewer sample working in SDL, Qt and WASM
am@osimis.io
parents: 276
diff changeset
76 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
276
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
77 }
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
78
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
79 width_ = static_cast<unsigned int>(w);
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
80 height_ = static_cast<unsigned int>(h);
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
81 LOG(WARNING) << "Initial display size: " << width_ << "x" << height_;
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
82
279
8f5d7495076d SimpleViewer sample working in SDL, Qt and WASM
am@osimis.io
parents: 276
diff changeset
83 enableOpenGl_ = parameters["opengl"].as<bool>();
8f5d7495076d SimpleViewer sample working in SDL, Qt and WASM
am@osimis.io
parents: 276
diff changeset
84 if (enableOpenGl_)
276
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
85 {
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
86 LOG(WARNING) << "OpenGL is enabled, disable it with option \"--opengl=off\" if the application crashes";
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
87 }
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
88 else
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
89 {
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
90 LOG(WARNING) << "OpenGL is disabled, enable it with option \"--opengl=on\" for best performance";
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
91 }
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
92 }
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
93
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
94
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
95 void SdlStoneApplicationRunner::Run(NativeStoneApplicationContext& context,
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
96 const std::string& title,
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
97 int argc,
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
98 char* argv[])
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
99 {
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
100 /**************************************************************
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
101 * Run the application inside a SDL window
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
102 **************************************************************/
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
103
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
104 LOG(WARNING) << "Starting the application";
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
105
276
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
106 SdlWindow window(title.c_str(), width_, height_, enableOpenGl_);
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
107 SdlEngine sdl(window, context, broker_);
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
108
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
109 {
293
017044be141b renaming
am@osimis.io
parents: 279
diff changeset
110 NativeStoneApplicationContext::GlobalMutexLocker locker(context);
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
111
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
112 locker.GetCentralViewport().RegisterObserverCallback(
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
113 new Callable<SdlEngine, IViewport::ViewportChangedMessage>
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
114 (sdl, &SdlEngine::OnViewportChanged));
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
115
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
116 //context.GetCentralViewport().Register(sdl); // (*)
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
117 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
118
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
119 context.Start();
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
120 sdl.Run();
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
121
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
122 LOG(WARNING) << "Stopping the application";
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
123
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
124 // Don't move the "Stop()" command below out of the block,
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
125 // otherwise the application might crash, because the
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
126 // "SdlEngine" is an observer of the viewport (*) and the
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
127 // update thread started by "context.Start()" would call a
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
128 // destructed object (the "SdlEngine" is deleted with the
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
129 // lexical scope).
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
130
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
131 // TODO Is this still true with message broker?
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
132 context.Stop();
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
133 }
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
134
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
135
293
017044be141b renaming
am@osimis.io
parents: 279
diff changeset
136 void SdlStoneApplicationRunner::Finalize()
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
137 {
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
138 SdlWindow::GlobalFinalize();
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
139 }
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
140 }