2 // Written and (c) Torsten Dreyer - Torsten(at)t3r_dot_de
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include "FGGLApplication.hxx"
29 #include "FGPanelApplication.hxx"
31 #include <OpenGL/gl.h>
32 #include <GLUT/glut.h>
38 #include <simgear/math/SGMisc.hxx>
39 #include <simgear/misc/sg_path.hxx>
40 #include <simgear/props/props_io.hxx>
41 #include <simgear/structure/exception.hxx>
42 #include <simgear/misc/ResourceManager.hxx>
46 #include "panel_io.hxx"
47 #include "ApplicationProperties.hxx"
51 inline static string ParseArgs( int argc, char ** argv, const string & token )
53 for( int i = 0; i < argc; i++ ) {
55 if( arg.find( token ) == 0 )
56 return arg.substr( token.length() );
61 inline static string ParseArgs( int argc, char ** argv, const char * token )
64 return ParseArgs( argc, argv, s );
67 #include "FGPNGTextureLoader.hxx"
68 #include "FGRGBTextureLoader.hxx"
69 static FGPNGTextureLoader pngTextureLoader;
70 static FGRGBTextureLoader rgbTextureLoader;
72 FGPanelApplication::FGPanelApplication( int argc, char ** argv ) :
73 FGGLApplication( "FlightGear Panel", argc, argv )
75 sglog().setLogLevels( SG_ALL, SG_WARN );
76 FGCroppedTexture::registerTextureLoader( "png", &pngTextureLoader );
77 FGCroppedTexture::registerTextureLoader( "rgb", &rgbTextureLoader );
82 for( int i = 1; i < argc; i++ ) {
83 panelFilename = ParseArgs( argc, argv, "--panel=" );
84 fgRoot = ParseArgs( argc, argv, "--fg-root=" );
87 if( fgRoot.length() > 0 )
88 ApplicationProperties::root = fgRoot;
90 simgear::ResourceManager::instance()->addBasePath(ApplicationProperties::root);
92 if( panelFilename.length() == 0 ) {
93 cerr << "Need a panel filename. Use --panel=path_to_filename" << endl;
97 // see if we got a valid fgdata path
98 SGPath BaseCheck(ApplicationProperties::root);
99 BaseCheck.append("version");
100 if (!BaseCheck.exists())
102 cerr << "Missing base package. Use --fg-root=path_to_fgdata" << endl;
107 SGPath tpath = ApplicationProperties::GetRootPath( panelFilename.c_str() );
108 readProperties( tpath.str(), ApplicationProperties::Properties );
110 catch( sg_io_exception & e ) {
111 cerr << e.getFormattedMessage() << endl;
115 for( int i = 1; i < argc; i++ ) {
116 string arg = argv[i];
117 if( arg.find( "--prop:" ) == 0 ) {
118 string s2 = arg.substr( 7 );
119 string::size_type p = s2.find( "=" );
120 if( p != string::npos ) {
121 string propertyName = s2.substr( 0, p );
122 string propertyValue = s2.substr( p+1 );
123 ApplicationProperties::Properties->getNode( propertyName.c_str(), true )->setValue( propertyValue.c_str() );
128 SGPropertyNode_ptr n;
129 if( (n = ApplicationProperties::Properties->getNode( "panel" )) != NULL )
130 panel = FGReadablePanel::read( n );
132 protocol = new FGPanelProtocol( ApplicationProperties::Properties->getNode( "communication", true ) );
136 FGPanelApplication::~FGPanelApplication()
140 void FGPanelApplication::Run()
142 int mode = GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE;
143 int w = panel == NULL ? 0 : panel->getWidth();
144 int h = panel == NULL ? 0 : panel->getHeight();
145 if( w == 0 && h == 0 ) {
148 } else if( w == 0 ) {
150 } else if( h == 0 ) {
154 bool gameMode = ApplicationProperties::Properties->getNode( "game-mode", true )->getBoolValue();
155 FGGLApplication::Run( mode, gameMode, w, h );
158 void FGPanelApplication::Init()
160 glAlphaFunc(GL_GREATER, 0.1);
161 glutSetCursor( GLUT_CURSOR_NONE );
162 ApplicationProperties::fontCache.initializeFonts();
165 void FGPanelApplication::Reshape( int width, int height )
168 this->height = height;
169 glViewport(0, 0, (GLsizei) width, (GLsizei) height);
172 void FGPanelApplication::Idle()
174 double d = glutGet(GLUT_ELAPSED_TIME);
185 if( protocol != NULL )
186 protocol->update( dt );
188 static double dsum = 0.0;
189 static unsigned cnt = 0;
190 dsum += glutGet(GLUT_ELAPSED_TIME)-d;
192 if( dsum > 1000.0 ) {
193 ApplicationProperties::Properties->getNode( "/sim/frame-rate", true )->setDoubleValue(cnt*1000.0/dsum );
199 void FGPanelApplication::Key( unsigned char key, int x, int y )
208 double FGPanelApplication::Sleep()
210 SGTimeStamp current_time_stamp;
211 static SGTimeStamp last_time_stamp;
213 if ( last_time_stamp.get_seconds() == 0 )
214 last_time_stamp.stamp();
216 double model_hz = 60;
217 double throttle_hz = ApplicationProperties::getDouble("/sim/frame-rate-throttle-hz", 0.0);
218 if ( throttle_hz > 0.0 ) {
219 // optionally throttle the frame rate (to get consistent frame
220 // rates or reduce cpu usage.
222 double frame_us = 1.0e6 / throttle_hz;
224 // sleep based timing loop.
226 // Calling sleep, even usleep() on linux is less accurate than
227 // we like, but it does free up the cpu for other tasks during
228 // the sleep so it is desirable. Because of the way sleep()
229 // is implemented in consumer operating systems like windows
230 // and linux, you almost always sleep a little longer than the
233 // To combat the problem of sleeping too long, we calculate the
234 // desired wait time and shorten it by 2000us (2ms) to avoid
235 // [hopefully] over-sleep'ing. The 2ms value was arrived at
236 // via experimentation. We follow this up at the end with a
237 // simple busy-wait loop to get the final pause timing exactly
240 // Assuming we don't oversleep by more than 2000us, this
241 // should be a reasonable compromise between sleep based
242 // waiting, and busy waiting.
244 // sleep() will always overshoot by a bit so undersleep by
245 // 2000us in the hopes of never oversleeping.
247 if ( frame_us < 0.0 ) {
250 current_time_stamp.stamp();
253 double elapsed_us = (current_time_stamp - last_time_stamp).toUSecs();
254 if ( elapsed_us < frame_us ) {
255 double requested_us = frame_us - elapsed_us;
257 ::Sleep ((int)(requested_us / 1000.0)) ;
259 usleep ( (useconds_t)(requested_us ) ) ;
262 // busy wait timing loop.
264 // This yields the most accurate timing. If the previous
265 // usleep() call is omitted this will peg the cpu
266 // (which is just fine if FG is the only app you care about.)
267 current_time_stamp.stamp();
268 SGTimeStamp next_time_stamp = last_time_stamp;
269 next_time_stamp += SGTimeStamp::fromSec(1e-6*frame_us);
270 while ( current_time_stamp < next_time_stamp ) {
271 current_time_stamp.stamp();
275 current_time_stamp.stamp();
278 double real_delta_time_sec = double(current_time_stamp.toUSecs() - last_time_stamp.toUSecs()) / 1000000.0;
279 last_time_stamp = current_time_stamp;
280 //fprintf(stdout,"\r%4.1lf ", 1/real_delta_time_sec );
283 // round the real time down to a multiple of 1/model-hz.
284 // this way all systems are updated the _same_ amount of dt.
285 static double reminder = 0.0;
286 static long global_multi_loop = 0;
287 real_delta_time_sec += reminder;
288 global_multi_loop = long(floor(real_delta_time_sec*model_hz));
289 global_multi_loop = SGMisc<long>::max(0, global_multi_loop);
290 reminder = real_delta_time_sec - double(global_multi_loop)/double(model_hz);
291 return double(global_multi_loop)/double(model_hz);
294 double ApplicationProperties::getDouble( const char * name, double def )
296 SGPropertyNode_ptr n = ApplicationProperties::Properties->getNode( name, false );
297 if( n == NULL ) return def;
298 return n->getDoubleValue();
301 SGPath ApplicationProperties::GetCwd()
304 char buf[512], *cwd = getcwd(buf, 511);
313 SGPath ApplicationProperties::GetRootPath( const char * sub )
317 SGPath subpath( sub );
319 // relative path to current working dir?
320 if (subpath.isRelative())
322 SGPath path = GetCwd();
328 if ( subpath.exists() )
335 // default: relative path to FGROOT
336 SGPath path( ApplicationProperties::root );
343 std::string ApplicationProperties::root = ".";
344 SGPropertyNode_ptr ApplicationProperties::Properties = new SGPropertyNode;
345 FGFontCache ApplicationProperties::fontCache;