Mercurial > hg > orthanc-stone
annotate Applications/Sdl/SdlStoneApplicationRunner.cpp @ 984:35ed6812d639 toa2019090602
Build fix
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Fri, 06 Sep 2019 11:46:51 +0200 |
parents | 4fe4b221a31f |
children | b537002f83a9 2d8ab34c8c91 |
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 | 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 | 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 | 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 | 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 | 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 | 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_); |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
294
diff
changeset
|
106 SdlEngine sdl(window, context, broker_); |
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 | 109 NativeStoneApplicationContext::GlobalMutexLocker locker(context); |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
294
diff
changeset
|
110 |
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
294
diff
changeset
|
111 locker.GetCentralViewport().RegisterObserverCallback( |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
112 new Callable<SdlEngine, Deprecated::IViewport::ViewportChangedMessage> |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
294
diff
changeset
|
113 (sdl, &SdlEngine::OnViewportChanged)); |
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
294
diff
changeset
|
114 |
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
294
diff
changeset
|
115 //context.GetCentralViewport().Register(sdl); // (*) |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
116 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
117 |
274
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
118 context.Start(); |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
119 sdl.Run(); |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
120 |
274
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
121 LOG(WARNING) << "Stopping the application"; |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
122 |
274
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
123 // 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
|
124 // otherwise the application might crash, because the |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
125 // "SdlEngine" is an observer of the viewport (*) and the |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
126 // update thread started by "context.Start()" would call a |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
127 // destructed object (the "SdlEngine" is deleted with the |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
128 // lexical scope). |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
294
diff
changeset
|
129 |
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
294
diff
changeset
|
130 // TODO Is this still true with message broker? |
274
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
131 context.Stop(); |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
132 } |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
133 |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
294
diff
changeset
|
134 |
293 | 135 void SdlStoneApplicationRunner::Finalize() |
274
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
136 { |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
137 SdlWindow::GlobalFinalize(); |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
138 } |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
139 } |