Mercurial > hg > orthanc-stone
annotate Applications/Sdl/SdlStoneApplicationRunner.cpp @ 384:d20d75f20c5d
better following of the MVC design pattern
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 07 Nov 2018 16:17:02 +0100 |
parents | faccc4b07b92 |
children | 6cc3ce74dc05 |
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 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium |
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" |
243 | 27 #include <boost/program_options.hpp> |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
28 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
29 #include "../../Framework/Toolbox/MessagingToolbox.h" |
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 | 34 #include <Core/Toolbox.h> |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
35 #include <Plugins/Samples/Common/OrthancHttpConnection.h> |
271
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
260
diff
changeset
|
36 #include "../../Platforms/Generic/OracleWebService.h" |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
37 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
38 namespace OrthancStone |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
39 { |
293 | 40 void SdlStoneApplicationRunner::Initialize() |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
41 { |
274
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
42 SdlWindow::GlobalInitialize(); |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
43 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
44 |
293 | 45 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
|
46 { |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
47 boost::program_options::options_description sdl("SDL options"); |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
48 sdl.add_options() |
274
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
49 ("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
|
50 ("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
|
51 ("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
|
52 ; |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
53 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
54 options.add(sdl); |
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 |
293 | 57 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
|
58 { |
5de5699ad570
first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents:
274
diff
changeset
|
59 if (!parameters.count("width") || |
5de5699ad570
first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents:
274
diff
changeset
|
60 !parameters.count("height") || |
5de5699ad570
first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents:
274
diff
changeset
|
61 !parameters.count("opengl")) |
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 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
|
64 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
276
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 |
5de5699ad570
first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents:
274
diff
changeset
|
67 int w = parameters["width"].as<int>(); |
5de5699ad570
first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents:
274
diff
changeset
|
68 int h = parameters["height"].as<int>(); |
5de5699ad570
first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents:
274
diff
changeset
|
69 if (w <= 0 || h <= 0) |
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 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
|
72 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
276
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 |
5de5699ad570
first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents:
274
diff
changeset
|
75 width_ = static_cast<unsigned int>(w); |
5de5699ad570
first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents:
274
diff
changeset
|
76 height_ = static_cast<unsigned int>(h); |
5de5699ad570
first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents:
274
diff
changeset
|
77 LOG(WARNING) << "Initial display size: " << width_ << "x" << height_; |
5de5699ad570
first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents:
274
diff
changeset
|
78 |
279
8f5d7495076d
SimpleViewer sample working in SDL, Qt and WASM
am@osimis.io
parents:
276
diff
changeset
|
79 enableOpenGl_ = parameters["opengl"].as<bool>(); |
8f5d7495076d
SimpleViewer sample working in SDL, Qt and WASM
am@osimis.io
parents:
276
diff
changeset
|
80 if (enableOpenGl_) |
276
5de5699ad570
first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents:
274
diff
changeset
|
81 { |
5de5699ad570
first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents:
274
diff
changeset
|
82 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
|
83 } |
5de5699ad570
first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents:
274
diff
changeset
|
84 else |
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 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
|
87 } |
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 } |
5de5699ad570
first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents:
274
diff
changeset
|
90 |
293 | 91 void SdlStoneApplicationRunner::Run(NativeStoneApplicationContext& context, const std::string& title, int argc, char* argv[]) |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
92 { |
274
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
93 /************************************************************** |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
94 * Run the application inside a SDL window |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
95 **************************************************************/ |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
96 |
274
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
97 LOG(WARNING) << "Starting the application"; |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
98 |
276
5de5699ad570
first display in QCairoWidget; no mouse interaction yet
am@osimis.io
parents:
274
diff
changeset
|
99 SdlWindow window(title.c_str(), width_, height_, enableOpenGl_); |
274
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
100 SdlEngine sdl(window, context); |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
101 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
102 { |
293 | 103 NativeStoneApplicationContext::GlobalMutexLocker locker(context); |
274
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
104 context.GetCentralViewport().Register(sdl); // (*) |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
105 } |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
106 |
274
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
107 context.Start(); |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
108 sdl.Run(); |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
109 |
274
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
110 LOG(WARNING) << "Stopping the application"; |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
111 |
274
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
112 // 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
|
113 // otherwise the application might crash, because the |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
114 // "SdlEngine" is an observer of the viewport (*) and the |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
115 // update thread started by "context.Start()" would call a |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
116 // destructed object (the "SdlEngine" is deleted with the |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
117 // lexical scope). |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
118 context.Stop(); |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
119 } |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
120 |
293 | 121 void SdlStoneApplicationRunner::Finalize() |
274
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
122 { |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
123 SdlWindow::GlobalFinalize(); |
dc1beee33134
split SdlApplication into NativeApplication and SdlApplication
am@osimis.io
parents:
271
diff
changeset
|
124 } |
221
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
125 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
126 |
d7b2590744f8
wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff
changeset
|
127 } |