comparison Samples/Sdl/SingleFrameViewer/SdlSimpleViewer.cpp @ 1383:ab871499ed30

SingleFrameViewer: refactored file locations + names to share files for RtViewer
author Benjamin Golinvaux <bgo@osimis.io>
date Mon, 27 Apr 2020 10:01:03 +0200
parents Samples/Sdl/SimpleViewer/SdlSimpleViewer.cpp@9d138883be66
children 4ebf246f3919
comparison
equal deleted inserted replaced
1382:9d138883be66 1383:ab871499ed30
1
2 #include "SdlSimpleViewerApplication.h"
3
4 #include <string>
5
6 #include <boost/program_options.hpp>
7
8 #include <SDL.h>
9
10 #include <Core/OrthancException.h>
11
12 #include <Framework/Loaders/GenericLoadersContext.h>
13 #include <Framework/StoneException.h>
14 #include <Framework/StoneEnumerations.h>
15 #include <Framework/StoneInitialization.h>
16 #include <Framework/Viewport/SdlViewport.h>
17
18 #include "../SdlHelpers.h"
19 #include "../../Common/SampleHelpers.h"
20
21 std::string orthancUrl;
22 std::string instanceId;
23 int frameIndex = 0;
24
25 static void ProcessOptions(int argc, char* argv[])
26 {
27 namespace po = boost::program_options;
28 po::options_description desc("Usage:");
29
30 desc.add_options()
31 ("log_level", po::value<std::string>()->default_value("WARNING"),
32 "You can choose WARNING, INFO or TRACE for the logging level: Errors and warnings will always be displayed. (default: WARNING)")
33
34 ("orthanc", po::value<std::string>()->default_value("http://localhost:8042"),
35 "Base URL of the Orthanc instance")
36
37 ("instance", po::value<std::string>()->default_value("285dece8-e1956b38-cdc7d084-6ce3371e-536a9ffc"),
38 "Orthanc ID of the instance to display")
39
40 ("frame_index", po::value<int>()->default_value(0),
41 "The zero-based index of the frame (for multi-frame instances)")
42 ;
43
44 po::variables_map vm;
45 try
46 {
47 po::store(po::parse_command_line(argc, argv, desc), vm);
48 po::notify(vm);
49 }
50 catch (std::exception& e)
51 {
52 std::cerr << "Please check your command line options! (\"" << e.what() << "\")" << std::endl;
53 }
54
55 if (vm.count("log_level") > 0)
56 {
57 std::string logLevel = vm["log_level"].as<std::string>();
58 OrthancStoneHelpers::SetLogLevel(logLevel);
59 }
60
61 if (vm.count("orthanc") > 0)
62 {
63 // maybe check URL validity here
64 orthancUrl = vm["orthanc"].as<std::string>();
65 }
66
67 if (vm.count("instance") > 0)
68 {
69 instanceId = vm["instance"].as<std::string>();
70 }
71
72 if (vm.count("frame_index") > 0)
73 {
74 frameIndex = vm["frame_index"].as<int>();
75 }
76
77 }
78
79 extern void f()
80 {
81 std::cout << "f()" << std::endl;
82 }
83
84 /**
85 * IMPORTANT: The full arguments to "main()" are needed for SDL on
86 * Windows. Otherwise, one gets the linking error "undefined reference
87 * to `SDL_main'". https://wiki.libsdl.org/FAQWindows
88 **/
89 int main(int argc, char* argv[])
90 {
91 f();
92
93 try
94 {
95 OrthancStone::StoneInitialize();
96
97 ProcessOptions(argc, argv);
98
99 //Orthanc::Logging::EnableInfoLevel(true);
100 //Orthanc::Logging::EnableTraceLevel(true);
101
102 {
103
104 #if 1
105 boost::shared_ptr<OrthancStone::SdlViewport> viewport =
106 OrthancStone::SdlOpenGLViewport::Create("Stone of Orthanc", 800, 600);
107 #else
108 boost::shared_ptr<OrthancStone::SdlViewport> viewport =
109 OrthancStone::SdlCairoViewport::Create("Stone of Orthanc", 800, 600);
110 #endif
111
112 OrthancStone::GenericLoadersContext context(1, 4, 1);
113
114 context.StartOracle();
115
116 {
117
118 boost::shared_ptr<SdlSimpleViewerApplication> application(
119 SdlSimpleViewerApplication::Create(context, viewport));
120
121 OrthancStone::DicomSource source;
122
123 application->LoadOrthancFrame(source, instanceId, frameIndex);
124
125 OrthancStone::DefaultViewportInteractor interactor;
126
127 {
128 int scancodeCount = 0;
129 const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount);
130
131 bool stop = false;
132 while (!stop)
133 {
134 bool paint = false;
135 SDL_Event event;
136 while (SDL_PollEvent(&event))
137 {
138 if (event.type == SDL_QUIT)
139 {
140 stop = true;
141 break;
142 }
143 else if (viewport->IsRefreshEvent(event))
144 {
145 paint = true;
146 }
147 else if (event.type == SDL_WINDOWEVENT &&
148 (event.window.event == SDL_WINDOWEVENT_RESIZED ||
149 event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED))
150 {
151 viewport->UpdateSize(event.window.data1, event.window.data2);
152 }
153 else if (event.type == SDL_WINDOWEVENT &&
154 (event.window.event == SDL_WINDOWEVENT_SHOWN ||
155 event.window.event == SDL_WINDOWEVENT_EXPOSED))
156 {
157 paint = true;
158 }
159 else if (event.type == SDL_KEYDOWN &&
160 event.key.repeat == 0 /* Ignore key bounce */)
161 {
162 switch (event.key.keysym.sym)
163 {
164 case SDLK_f:
165 viewport->ToggleMaximize();
166 break;
167
168 case SDLK_s:
169 application->FitContent();
170 break;
171
172 case SDLK_q:
173 stop = true;
174 break;
175
176 default:
177 break;
178 }
179 }
180 else if (event.type == SDL_MOUSEBUTTONDOWN ||
181 event.type == SDL_MOUSEMOTION ||
182 event.type == SDL_MOUSEBUTTONUP)
183 {
184 std::auto_ptr<OrthancStone::IViewport::ILock> lock(viewport->Lock());
185 if (lock->HasCompositor())
186 {
187 OrthancStone::PointerEvent p;
188 OrthancStoneHelpers::GetPointerEvent(p, lock->GetCompositor(),
189 event, keyboardState, scancodeCount);
190
191 switch (event.type)
192 {
193 case SDL_MOUSEBUTTONDOWN:
194 lock->GetController().HandleMousePress(interactor, p,
195 lock->GetCompositor().GetCanvasWidth(),
196 lock->GetCompositor().GetCanvasHeight());
197 lock->Invalidate();
198 break;
199
200 case SDL_MOUSEMOTION:
201 if (lock->GetController().HandleMouseMove(p))
202 {
203 lock->Invalidate();
204 }
205 break;
206
207 case SDL_MOUSEBUTTONUP:
208 lock->GetController().HandleMouseRelease(p);
209 lock->Invalidate();
210 break;
211
212 default:
213 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
214 }
215 }
216 }
217 }
218
219 if (paint)
220 {
221 viewport->Paint();
222 }
223
224 // Small delay to avoid using 100% of CPU
225 SDL_Delay(1);
226 }
227 }
228
229 context.StopOracle();
230 }
231 }
232
233 OrthancStone::StoneFinalize();
234 return 0;
235 }
236 catch (Orthanc::OrthancException& e)
237 {
238 auto test = e.What();
239 fprintf(stdout, test);
240 LOG(ERROR) << "OrthancException: " << e.What();
241 return -1;
242 }
243 catch (OrthancStone::StoneException& e)
244 {
245 LOG(ERROR) << "StoneException: " << e.What();
246 return -1;
247 }
248 catch (std::runtime_error& e)
249 {
250 LOG(ERROR) << "Runtime error: " << e.what();
251 return -1;
252 }
253 catch (...)
254 {
255 LOG(ERROR) << "Native exception";
256 return -1;
257 }
258 }