Debian Codegraph @

utils/fgpanel/FGPanelApplication.cxx

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2011 Torsten Dreyer
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include <config.h>
5
6 #ifdef HAVE_WINDOWS_H
7 # include <windows.h>
8 # include <direct.h>
9 #else
10 # include <unistd.h>
11 #endif
12
13 #ifdef __APPLE__
14 # include <CoreFoundation/CoreFoundation.h>
15 #endif
16
17 #include <chrono>
18 #include <filesystem>
19 #include <iostream>
20 #include <thread>
21
22 #if defined (SG_MAC)
23 #include <OpenGL/gl.h>
24 #include <GLUT/glut.h>
25 #elif defined (_GLES2)
26 #include <GLES2/gl2.h>
27 #else
28 #include <GL/glew.h> // Must be included before <GL/gl.h>
29 #include <GL/gl.h>
30 #include <GL/glut.h>
31 #endif
32
33 #include "FGGLApplication.hxx"
34 #include "FGPanelApplication.hxx"
35
36 #include <simgear/math/SGMisc.hxx>
37 #include <simgear/misc/sg_path.hxx>
38 #include <simgear/props/props_io.hxx>
39 #include <simgear/structure/exception.hxx>
40 #include <simgear/misc/ResourceManager.hxx>
41
42 #include "panel_io.hxx"
43 #include "ApplicationProperties.hxx"
44
45
46 using namespace std;
47 using namespace std::string_literals;
48
49 inline static string
50 ParseArgs (int argc, char **argv, const string &token) {
51 for (int i = 0; i < argc; i++) {
52 const string arg (argv[i]);
53 if (arg.find (token) == 0) {
54 return arg.substr (token.length ());
55 }
56 }
57 return "";
58 }
59
60 // define default location of fgdata (use the same as for fgfs)
61 inline static SGPath
62 platformDefaultRoot () {
63 #if defined(__CYGWIN__)
64 return SGPath("../data"s);
65 #elif defined(_WIN32)
66 return SGPath("..\\data"s);
67 #elif defined(__APPLE__)
68 /*
69 The following code looks for the base package inside the application
70 bundle, in the standard Contents/Resources location.
71 */
72 CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL (CFBundleGetMainBundle ());
73
74 // look for a 'data' subdir
75 CFURLRef dataDir = CFURLCreateCopyAppendingPathComponent (NULL, resourcesUrl, CFSTR ("data"), true);
76
77 // now convert down to a path, and the a c-string
78 CFStringRef path = CFURLCopyFileSystemPath (dataDir, kCFURLPOSIXPathStyle);
79 string root = CFStringGetCStringPtr (path, CFStringGetSystemEncoding ());
80
81 CFRelease (resourcesUrl);
82 CFRelease (dataDir);
83 CFRelease (path);
84
85 return SGPath(root);
86 #else
87 return SGPath(std::string(PKGLIBDIR));
88 #endif
89 }
90
91 #include "FGPNGTextureLoader.hxx"
92 #include "FGRGBTextureLoader.hxx"
93
94 static FGPNGTextureLoader pngTextureLoader;
95 static FGRGBTextureLoader rgbTextureLoader;
96
97 FGPanelApplication::FGPanelApplication (int argc, char **argv) :
98 FGGLApplication ("FlightGear Panel", argc, argv) {
99 sglog().setLogLevels (SG_ALL, SG_WARN);
100 FGCroppedTexture::registerTextureLoader ("png", &pngTextureLoader);
101 FGCroppedTexture::registerTextureLoader ("rgb", &rgbTextureLoader);
102
103 ApplicationProperties::root = platformDefaultRoot ().local8BitStr ();
104
105 const string panelFilename (ParseArgs (argc, argv, "--panel="));
106 const string fgRoot (ParseArgs (argc, argv, "--fg-root="));
107
108 if (fgRoot.length () > 0) {
109 ApplicationProperties::root = fgRoot;
110 }
111 simgear::ResourceManager::instance ()->addBasePath (ApplicationProperties::root);
112
113 if (panelFilename.length () == 0 ) {
114 cerr << "Need a panel filename. Use --panel=path_to_filename" << endl;
115 throw exception ();
116 }
117
118 // see if we got a valid fgdata path
119 SGPath BaseCheck (ApplicationProperties::root);
120 BaseCheck.append ("version");
121 if (!BaseCheck.exists ()) {
122 cerr << "Missing base package. Use --fg-root=path_to_fgdata" << endl;
123 throw exception ();
124 }
125
126 try {
127 const SGPath tpath (ApplicationProperties::GetRootPath (panelFilename.c_str ()));
128 readProperties (tpath, ApplicationProperties::Properties);
129 }
130 catch (sg_io_exception & e) {
131 cerr << e.getFormattedMessage () << endl;
132 throw;
133 }
134
135 for (int i = 1; i < argc; i++) {
136 const string arg (argv[i]);
137 if (arg.find ("--prop:") == 0 ) {
138 const string s2 (arg.substr (7));
139 string::size_type p (s2.find ("="));
140 if (p != string::npos) {
141 const string propertyName (s2.substr (0, p));
142 const string propertyValue (s2.substr (p + 1));
143 ApplicationProperties::Properties->getNode (propertyName.c_str (), true )->setValue (propertyValue.c_str ());
144 }
145 }
146 }
147
148 const SGPropertyNode_ptr n (ApplicationProperties::Properties->getNode ("panel"));
149 if (n != NULL) {
150 panel = FGReadablePanel::read (n);
151 }
152 protocol = new FGPanelProtocol (ApplicationProperties::Properties->getNode ("communication", true));
153 protocol->init ();
154 }
155
156 FGPanelApplication::~FGPanelApplication () {
157 }
158
159 void
160 FGPanelApplication::Run () {
161 #ifdef _GLES2
162 const int mode (0);
163 #else
164 const int mode (GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
165 #endif
166 int w (panel == NULL ? 0 : panel->getWidth ());
167 int h (panel == NULL ? 0 : panel->getHeight ());
168 if (w == 0 && h == 0) {
169 w = 1024;
170 h = 768;
171 } else if (w == 0) {
172 w = h / 0.75;
173 } else if (h == 0) {
174 h = w * 0.75;
175 }
176
177 const bool gameMode (ApplicationProperties::Properties->getNode ( "game-mode", true)->getBoolValue ());
178 FGGLApplication::Run (mode, gameMode, w, h);
179 }
180
181 void
182 FGPanelApplication::Init () {
183 #ifndef _GLES2
184 glutSetCursor (GLUT_CURSOR_NONE);
185 #endif
186 if (panel != NULL) {
187 panel->init ();
188 }
189 }
190
191 void
192 FGPanelApplication::Reshape (const int width, const int height) {
193 this->width = width;
194 this->height = height;
195 glViewport (0, 0, GLsizei (width), GLsizei (height));
196 }
197
198 void
199 FGPanelApplication::Idle () {
200 #ifndef _GLES2
201 const double d (glutGet (GLUT_ELAPSED_TIME));
202 #endif
203 const double dt (Sleep ());
204 if (dt == 0) {
205 return;
206 }
207 if (panel != NULL) {
208 panel->update (dt);
209 }
210 #ifndef _GLES2
211 glutSwapBuffers ();
212 #endif
213 if (protocol != NULL) {
214 protocol->update (dt);
215 }
216 #ifndef _GLES2
217 static double dsum = 0.0;
218 static unsigned cnt = 0;
219 dsum += glutGet (GLUT_ELAPSED_TIME) - d;
220 cnt++;
221 if (dsum > 1000.0) {
222 ApplicationProperties::Properties->getNode ("/sim/frame-rate", true)->setDoubleValue (cnt * 1000.0 / dsum);
223 dsum = 0.0;
224 cnt = 0;
225 }
226 #endif
227 }
228
229 void
230 FGPanelApplication::Key (const unsigned char key, const int x, const int y) {
231 switch (key) {
232 case 0x1b:
233 exit(0);
234 break;
235 }
236 }
237
238 double
239 FGPanelApplication::Sleep () {
240 SGTimeStamp current_time_stamp;
241 static SGTimeStamp last_time_stamp;
242
243 if (last_time_stamp.get_seconds () == 0) {
244 last_time_stamp.stamp ();
245 }
246 const double model_hz (60);
247 const double throttle_hz (ApplicationProperties::getDouble ("/sim/frame-rate-throttle-hz", 0.0));
248 if (throttle_hz > 0.0) {
249 // optionally throttle the frame rate (to get consistent frame
250 // rates or reduce cpu usage.
251
252 double frame_us (1.0e6 / throttle_hz);
253
254 // sleep based timing loop.
255 //
256 // Calling sleep, even usleep() on linux is less accurate than
257 // we like, but it does free up the cpu for other tasks during
258 // the sleep so it is desirable. Because of the way sleep()
259 // is implemented in consumer operating systems like windows
260 // and linux, you almost always sleep a little longer than the
261 // requested amount.
262 //
263 // To combat the problem of sleeping too long, we calculate the
264 // desired wait time and shorten it by 2000us (2ms) to avoid
265 // [hopefully] over-sleep'ing. The 2ms value was arrived at
266 // via experimentation. We follow this up at the end with a
267 // simple busy-wait loop to get the final pause timing exactly
268 // right.
269 //
270 // Assuming we don't oversleep by more than 2000us, this
271 // should be a reasonable compromise between sleep based
272 // waiting, and busy waiting.
273
274 // sleep() will always overshoot by a bit so undersleep by
275 // 2000us in the hopes of never oversleeping.
276 frame_us -= 2000.0;
277 if (frame_us < 0.0) {
278 frame_us = 0.0;
279 }
280 current_time_stamp.stamp ();
281
282 /* Convert to ms */
283 const double elapsed_us ((current_time_stamp - last_time_stamp).toUSecs ());
284 if (elapsed_us < frame_us) {
285 const double requested_us (frame_us - elapsed_us);
286 std::this_thread::sleep_for(std::chrono::milliseconds((int) (requested_us * 1000.0)));
287 }
288
289 // busy wait timing loop.
290 //
291 // This yields the most accurate timing. If the previous
292 // usleep() call is omitted this will peg the cpu
293 // (which is just fine if FG is the only app you care about.)
294 current_time_stamp.stamp ();
295 const SGTimeStamp next_time_stamp (last_time_stamp + SGTimeStamp::fromSec (1e-6*frame_us));
296 while (current_time_stamp < next_time_stamp) {
297 current_time_stamp.stamp ();
298 }
299
300 } else {
301 current_time_stamp.stamp ();
302 }
303
304 static double reminder = 0.0;
305 static long global_multi_loop = 0;
306 const double real_delta_time_sec ((double (current_time_stamp.toUSecs () - last_time_stamp.toUSecs ()) / 1000000.0) + reminder);
307 last_time_stamp = current_time_stamp;
308 //fprintf(stdout,"\r%4.1lf ", 1/real_delta_time_sec );
309 //fflush(stdout);
310
311 // round the real time down to a multiple of 1/model-hz.
312 // this way all systems are updated the _same_ amount of dt.
313 global_multi_loop = long (floor (real_delta_time_sec * model_hz));
314 global_multi_loop = SGMisc<long>::max (0, global_multi_loop);
315 reminder = real_delta_time_sec - double (global_multi_loop) / double (model_hz);
316 return double (global_multi_loop) / double (model_hz);
317 }