comparison Deprecated/Applications/Sdl/SdlStoneApplicationRunner.cpp @ 1399:ff8d2e46ac63

moved Applications into Deprecated
author Alain Mazy <alain@mazy.be>
date Wed, 29 Apr 2020 20:44:31 +0200
parents Applications/Sdl/SdlStoneApplicationRunner.cpp@7ec8fea061b9
children
comparison
equal deleted inserted replaced
1398:c5403d52078c 1399:ff8d2e46ac63
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-2020 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 #if ORTHANC_ENABLE_SDL != 1
23 #error this file shall be included only with the ORTHANC_ENABLE_SDL set to 1
24 #endif
25
26 #include "SdlStoneApplicationRunner.h"
27
28 #include "../../Platforms/Generic/OracleWebService.h"
29 #include "SdlEngine.h"
30
31 #include <Core/Logging.h>
32 #include <Core/HttpClient.h>
33 #include <Core/Toolbox.h>
34 #include <Core/OrthancException.h>
35 #include <Plugins/Samples/Common/OrthancHttpConnection.h>
36
37 #include <boost/program_options.hpp>
38
39 namespace OrthancStone
40 {
41 void SdlStoneApplicationRunner::Initialize()
42 {
43 SdlWindow::GlobalInitialize();
44 }
45
46
47 void SdlStoneApplicationRunner::DeclareCommandLineOptions(boost::program_options::options_description& options)
48 {
49 boost::program_options::options_description sdl("SDL options");
50 sdl.add_options()
51 ("width", boost::program_options::value<int>()->default_value(1024), "Initial width of the SDL window")
52 ("height", boost::program_options::value<int>()->default_value(768), "Initial height of the SDL window")
53 ("opengl", boost::program_options::value<bool>()->default_value(true), "Enable OpenGL in SDL")
54 ;
55
56 options.add(sdl);
57 }
58
59
60 void SdlStoneApplicationRunner::ParseCommandLineOptions(const boost::program_options::variables_map& parameters)
61 {
62 if (!parameters.count("width") ||
63 !parameters.count("height") ||
64 !parameters.count("opengl"))
65 {
66 LOG(ERROR) << "Parameter \"width\", \"height\" or \"opengl\" is missing";
67 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
68 }
69
70 int w = parameters["width"].as<int>();
71 int h = parameters["height"].as<int>();
72 if (w <= 0 || h <= 0)
73 {
74 LOG(ERROR) << "Parameters \"width\" and \"height\" must be positive";
75 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
76 }
77
78 width_ = static_cast<unsigned int>(w);
79 height_ = static_cast<unsigned int>(h);
80 LOG(WARNING) << "Initial display size: " << width_ << "x" << height_;
81
82 enableOpenGl_ = parameters["opengl"].as<bool>();
83 if (enableOpenGl_)
84 {
85 LOG(WARNING) << "OpenGL is enabled, disable it with option \"--opengl=off\" if the application crashes";
86 }
87 else
88 {
89 LOG(WARNING) << "OpenGL is disabled, enable it with option \"--opengl=on\" for best performance";
90 }
91 }
92
93
94 void SdlStoneApplicationRunner::Run(NativeStoneApplicationContext& context,
95 const std::string& title,
96 int argc,
97 char* argv[])
98 {
99 /**************************************************************
100 * Run the application inside a SDL window
101 **************************************************************/
102
103 LOG(WARNING) << "Starting the application";
104
105 SdlWindow window(title.c_str(), width_, height_, enableOpenGl_);
106 boost::shared_ptr<SdlEngine> sdl(new SdlEngine(window, context));
107
108 {
109 NativeStoneApplicationContext::GlobalMutexLocker locker(context);
110
111 sdl->Register<Deprecated::IViewport::ViewportChangedMessage>
112 (locker.GetCentralViewport(), &SdlEngine::OnViewportChanged);
113
114 //context.GetCentralViewport().Register(sdl); // (*)
115 }
116
117 context.Start();
118 sdl->Run();
119
120 LOG(WARNING) << "Stopping the application";
121
122 // Don't move the "Stop()" command below out of the block,
123 // otherwise the application might crash, because the
124 // "SdlEngine" is an observer of the viewport (*) and the
125 // update thread started by "context.Start()" would call a
126 // destructed object (the "SdlEngine" is deleted with the
127 // lexical scope).
128
129 // TODO Is this still true with message broker?
130 context.Stop();
131 }
132
133
134 void SdlStoneApplicationRunner::Finalize()
135 {
136 SdlWindow::GlobalFinalize();
137 }
138 }