annotate Applications/Sdl/SdlStoneApplicationRunner.cpp @ 1186:3284c3fd96ad broker

merge
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 22 Nov 2019 09:51:47 +0100
parents 8d3936f06541
children 0ca50d275b9a
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
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
28 #include "../../Platforms/Generic/OracleWebService.h"
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
29 #include "SdlEngine.h"
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
30
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
31 #include <Core/Logging.h>
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
32 #include <Core/HttpClient.h>
260
am@osimis.io
parents: 251
diff changeset
33 #include <Core/Toolbox.h>
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
34 #include <Core/OrthancException.h>
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
35 #include <Plugins/Samples/Common/OrthancHttpConnection.h>
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
36
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
37 #include <boost/program_options.hpp>
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
38
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
39 namespace OrthancStone
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
40 {
293
017044be141b renaming
am@osimis.io
parents: 279
diff changeset
41 void SdlStoneApplicationRunner::Initialize()
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
42 {
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
43 SdlWindow::GlobalInitialize();
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
44 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
45
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
46
293
017044be141b renaming
am@osimis.io
parents: 279
diff changeset
47 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
48 {
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
49 boost::program_options::options_description sdl("SDL options");
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
50 sdl.add_options()
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
51 ("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
52 ("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
53 ("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
54 ;
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
55
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
56 options.add(sdl);
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
57 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
58
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
59
293
017044be141b renaming
am@osimis.io
parents: 279
diff changeset
60 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
61 {
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
62 if (!parameters.count("width") ||
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
63 !parameters.count("height") ||
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
64 !parameters.count("opengl"))
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
65 {
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
66 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
67 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
276
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
68 }
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 int w = parameters["width"].as<int>();
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
71 int h = parameters["height"].as<int>();
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
72 if (w <= 0 || h <= 0)
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
73 {
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
74 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
75 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
276
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
76 }
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 width_ = static_cast<unsigned int>(w);
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
79 height_ = static_cast<unsigned int>(h);
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
80 LOG(WARNING) << "Initial display size: " << width_ << "x" << height_;
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
81
279
8f5d7495076d SimpleViewer sample working in SDL, Qt and WASM
am@osimis.io
parents: 276
diff changeset
82 enableOpenGl_ = parameters["opengl"].as<bool>();
8f5d7495076d SimpleViewer sample working in SDL, Qt and WASM
am@osimis.io
parents: 276
diff changeset
83 if (enableOpenGl_)
276
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
84 {
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
85 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
86 }
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
87 else
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
88 {
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
89 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
90 }
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
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
93
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
94 void SdlStoneApplicationRunner::Run(NativeStoneApplicationContext& context,
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
95 const std::string& title,
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
96 int argc,
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
97 char* argv[])
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
98 {
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
99 /**************************************************************
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
100 * Run the application inside a SDL window
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
101 **************************************************************/
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
102
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
103 LOG(WARNING) << "Starting the application";
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
104
276
5de5699ad570 first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents: 274
diff changeset
105 SdlWindow window(title.c_str(), width_, height_, enableOpenGl_);
1073
8d3936f06541 fix for older gcc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1068
diff changeset
106 boost::shared_ptr<SdlEngine> sdl(new SdlEngine(window, context));
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
107
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
108 {
293
017044be141b renaming
am@osimis.io
parents: 279
diff changeset
109 NativeStoneApplicationContext::GlobalMutexLocker locker(context);
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
110
1068
04a95ee91327 recovery of OrthancStoneSingleFrame
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
111 sdl->Register<Deprecated::IViewport::ViewportChangedMessage>
1066
b537002f83a9 removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 792
diff changeset
112 (locker.GetCentralViewport(), &SdlEngine::OnViewportChanged);
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
113
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
114 //context.GetCentralViewport().Register(sdl); // (*)
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
115 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
116
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
117 context.Start();
1068
04a95ee91327 recovery of OrthancStoneSingleFrame
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1066
diff changeset
118 sdl->Run();
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
119
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
120 LOG(WARNING) << "Stopping the application";
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 // 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
123 // otherwise the application might crash, because the
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
124 // "SdlEngine" is an observer of the viewport (*) and the
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
125 // update thread started by "context.Start()" would call a
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
126 // destructed object (the "SdlEngine" is deleted with the
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
127 // lexical scope).
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
128
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
129 // TODO Is this still true with message broker?
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
130 context.Stop();
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
131 }
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
132
385
6cc3ce74dc05 using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 294
diff changeset
133
293
017044be141b renaming
am@osimis.io
parents: 279
diff changeset
134 void SdlStoneApplicationRunner::Finalize()
274
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
135 {
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
136 SdlWindow::GlobalFinalize();
dc1beee33134 split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents: 271
diff changeset
137 }
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
138 }