2 // options.cxx -- class to handle command line options
4 // Written by Curtis Olson, started April 1998.
6 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 // (Log is kept at end of this file)
30 #include <math.h> // rint()
32 #include <stdlib.h> // atof(), atoi()
36 #include <Debug/fg_debug.h>
37 #include <Flight/flight.h>
38 #include <Include/fg_constants.h>
39 #include <Misc/fgstream.hxx>
41 #include "options.hxx"
43 const int fgOPTIONS::FG_RADIUS_MIN;
44 const int fgOPTIONS::FG_RADIUS_MAX;
47 atof( const string& str )
49 return ::atof( str.c_str() );
53 atoi( const string& str )
55 return ::atoi( str.c_str() );
58 // Defined the shared options class here
59 fgOPTIONS current_options;
63 fgOPTIONS::fgOPTIONS() :
64 // starting longitude in degrees (west = -)
65 // starting latitude in degrees (south = -)
67 // Default initial position is Globe, AZ (P13)
71 // North of the city of Globe
75 // North of the city of Globe
79 // Near where I used to live in Globe, AZ
83 // 10125 Jewell St. NE
87 // Near KHSP (Hot Springs, VA)
88 // lon(-79.8338964 + 0.01),
89 // lat( 37.9514564 + 0.008),
91 // (SEZ) SEDONA airport
92 // lon(-111.7884614 + 0.01),
93 // lat( 34.8486289 - 0.015),
95 // Somewhere near the Grand Canyon
99 // Jim Brennon's Kingmont Observatory
100 // lon(-121.1131667),
103 // Huaras, Peru (S09d 31.871' W077d 31.498')
107 // Eclipse Watching w73.5 n10 (approx) 18:00 UT
116 // lon(-90.1953055556),
117 // lat( 45.4511388889),
119 // starting altitude in meters (this will be reset to ground level
120 // if it is lower than the terrain
123 // Initial Orientation
124 heading(270.0), // heading (yaw) angle in degress (Psi)
125 roll(0.0), // roll angle in degrees (Phi)
126 pitch(0.424), // pitch angle in degrees (Theta)
140 // Flight Model options
141 flight_model(FG_LARCSIM),
144 fog(FG_FOG_NICEST), // nicest
161 // set initial values/defaults
162 char* envp = ::getenv( "FG_ROOT" );
164 if ( envp != NULL ) {
165 // fg_root could be anywhere, so default to environmental
166 // variable $FG_ROOT if it is set.
169 // Otherwise, default to a random compiled in location if
170 // $FG_ROOT is not set. This can still be overridden from the
171 // command line or a config file.
174 fg_root = "\\FlightGear";
176 fg_root = "/usr/local/lib/FlightGear";
180 airport_id = ""; // default airport id
185 fgOPTIONS::parse_time(const string& time_in) {
186 char *time_str, num[256];
187 double hours, minutes, seconds;
192 time_str = (char *)time_in.c_str();
194 // printf("parse_time(): %s\n", time_str);
197 if ( strlen(time_str) ) {
198 if ( time_str[0] == '+' ) {
201 } else if ( time_str[0] == '-' ) {
206 // printf("sign = %d\n", sign);
209 if ( strlen(time_str) ) {
211 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
212 num[i] = time_str[0];
216 if ( time_str[0] == ':' ) {
221 // printf("hours = %.2lf\n", hours);
227 if ( strlen(time_str) ) {
229 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
230 num[i] = time_str[0];
234 if ( time_str[0] == ':' ) {
239 // printf("minutes = %.2lf\n", minutes);
241 result += minutes / 60.0;
245 if ( strlen(time_str) ) {
247 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
248 num[i] = time_str[0];
254 // printf("seconds = %.2lf\n", seconds);
256 result += seconds / 3600.0;
259 return(sign * result);
263 // parse degree in the form of [+/-]hhh:mm:ss
265 fgOPTIONS::parse_degree( const string& degree_str) {
266 double result = parse_time( degree_str );
268 // printf("Degree = %.4f\n", result);
274 // parse time offset command line option
276 fgOPTIONS::parse_time_offset( const string& time_str) {
279 // printf("time offset = %s\n", time_str);
282 result = (int)rint(parse_time(time_str) * 3600.0);
284 result = (int)(parse_time(time_str) * 3600.0);
287 // printf("parse_time_offset(): %d\n", result);
293 // Parse --tile-diameter=n type option
296 fgOPTIONS::parse_tile_radius( const string& arg ) {
297 int radius = atoi( arg );
299 if ( radius < FG_RADIUS_MIN ) { radius = FG_RADIUS_MIN; }
300 if ( radius > FG_RADIUS_MAX ) { radius = FG_RADIUS_MAX; }
302 // printf("parse_tile_radius(): radius = %d\n", radius);
308 // Parse --flightmode=abcdefg type option
310 fgOPTIONS::parse_flight_model( const string& fm ) {
311 // printf("flight model = %s\n", fm);
313 if ( fm == "slew" ) {
315 } else if ( (fm == "larcsim") || (fm == "LaRCsim") ) {
318 fgPrintf( FG_GENERAL, FG_EXIT, "Unknown flight model = %s\n",
322 // we'll never get here, but it makes the compiler happy.
327 // Parse --fov=x.xx type option
329 fgOPTIONS::parse_fov( const string& arg ) {
330 double fov = atof(arg);
332 if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
333 if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
335 // printf("parse_fov(): result = %.4f\n", fov);
341 // Parse a single option
342 int fgOPTIONS::parse_option( const string& arg ) {
344 if ( (arg == "--help") || (arg == "-h") ) {
345 // help/usage request
346 return(FG_OPTIONS_HELP);
347 } else if ( arg == "--disable-game-mode") {
349 } else if ( arg == "--enable-game-mode" ) {
351 } else if ( arg == "--disable-splash-screen" ) {
352 splash_screen = false;
353 } else if ( arg == "--enable-splash-screen" ) {
354 splash_screen = true;
355 } else if ( arg == "--disable-intro-music" ) {
357 } else if ( arg == "--enable-intro-music" ) {
359 } else if ( arg == "--disable-mouse-pointer" ) {
361 } else if ( arg == "--enable-mouse-pointer" ) {
363 } else if ( arg == "--disable-pause" ) {
365 } else if ( arg == "--enable-pause" ) {
367 } else if ( arg == "--disable-hud" ) {
369 } else if ( arg == "--enable-hud" ) {
371 } else if ( arg == "--disable-panel" ) {
372 panel_status = false;
373 } else if ( arg == "--enable-panel" ) {
375 } else if ( arg == "--disable-sound" ) {
377 } else if ( arg == "--enable-sound" ) {
379 } else if ( arg.find( "--airport-id=") != string::npos ) {
380 airport_id = arg.substr( 13 );
381 } else if ( arg.find( "--lon=" ) != string::npos ) {
382 lon = parse_degree( arg.substr(6) );
383 } else if ( arg.find( "--lat=" ) != string::npos ) {
384 lat = parse_degree( arg.substr(6) );
385 } else if ( arg.find( "--altitude=" ) != string::npos ) {
386 altitude = atof( arg.substr(11) );
387 } else if ( arg.find( "--heading=" ) != string::npos ) {
388 heading = atof( arg.substr(10) );
389 } else if ( arg.find( "--roll=" ) != string::npos ) {
390 roll = atof( arg.substr(7) );
391 } else if ( arg.find( "--pitch=" ) != string::npos ) {
392 pitch = atof( arg.substr(8) );
393 } else if ( arg.find( "--fg-root=" ) != string::npos ) {
394 fg_root = arg.substr( 10 );
395 } else if ( arg.find( "--flight-model=" ) != string::npos ) {
396 flight_model = parse_flight_model( arg.substr(15) );
397 } else if ( arg == "--fog-disable" ) {
398 fog = FG_FOG_DISABLED;
399 } else if ( arg == "--fog-fastest" ) {
400 fog = FG_FOG_FASTEST;
401 } else if ( arg == "--fog-nicest" ) {
403 } else if ( arg.find( "--fov=" ) != string::npos ) {
404 fov = parse_fov( arg.substr(6) );
405 } else if ( arg == "--disable-fullscreen" ) {
407 } else if ( arg== "--enable-fullscreen") {
409 } else if ( arg == "--shading-flat") {
411 } else if ( arg == "--shading-smooth") {
413 } else if ( arg == "--disable-skyblend") {
415 } else if ( arg== "--enable-skyblend" ) {
417 } else if ( arg == "--disable-textures" ) {
419 } else if ( arg == "--enable-textures" ) {
421 } else if ( arg == "--disable-wireframe" ) {
423 } else if ( arg == "--enable-wireframe" ) {
425 } else if ( arg.find( "--tile-radius=" ) != string::npos ) {
426 tile_radius = parse_tile_radius( arg.substr(14) );
427 tile_diameter = tile_radius * 2 + 1;
428 } else if ( arg.find( "--time-offset=" ) != string::npos ) {
429 time_offset = parse_time_offset( (arg.substr(14)) );
430 } else if ( arg == "--hud-tris" ) {
432 } else if ( arg == "--hud-culled" ) {
435 fgPrintf( FG_GENERAL, FG_EXIT, "Unknown option '%s'\n",
437 return(FG_OPTIONS_ERROR);
440 return(FG_OPTIONS_OK);
444 // Parse the command line options
445 int fgOPTIONS::parse_command_line( int argc, char **argv ) {
449 fgPrintf(FG_GENERAL, FG_INFO, "Processing command line arguments\n");
452 fgPrintf(FG_GENERAL, FG_DEBUG, "argv[%d] = %s\n", i, argv[i]);
454 result = parse_option(argv[i]);
455 if ( (result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR) ) {
462 return(FG_OPTIONS_OK);
466 // Parse config file options
467 int fgOPTIONS::parse_config_file( const string& path ) {
468 fg_gzifstream in( path );
470 return(FG_OPTIONS_ERROR);
472 fgPrintf( FG_GENERAL, FG_INFO, "Processing config file: %s\n",
479 getline( in.stream(), line );
481 if ( parse_option( line ) == FG_OPTIONS_ERROR ) {
482 fgPrintf( FG_GENERAL, FG_EXIT,
483 "Config file parse error: %s '%s'\n",
484 path.c_str(), line.c_str() );
489 return FG_OPTIONS_OK;
493 // Print usage message
494 void fgOPTIONS::usage ( void ) {
495 printf("Usage: fg [ options ... ]\n");
498 printf("General Options:\n");
499 printf("\t--help -h: print usage\n");
500 printf("\t--fg-root=path: specify the root path for all the data files\n");
501 printf("\t--disable-game-mode: disable full-screen game mode\n");
502 printf("\t--enable-game-mode: enable full-screen game mode\n");
503 printf("\t--disable-splash-screen: disable splash screen\n");
504 printf("\t--enable-splash-screen: enable splash screen\n");
505 printf("\t--disable-intro-music: disable introduction music\n");
506 printf("\t--enable-intro-music: enable introduction music\n");
507 printf("\t--disable-mouse-pointer: disable extra mouse pointer\n");
508 printf("\t--enable-mouse-pointer: enable extra mouse pointer (i.e. for\n");
509 printf("\t\tfull screen voodoo/voodoo-II based cards.)\n");
510 printf("\t--disable-pause: start out in an active state\n");
511 printf("\t--enable-pause: start out in a paused state\n");
514 printf("Features:\n");
515 printf("\t--disable-hud: disable heads up display\n");
516 printf("\t--enable-hud: enable heads up display\n");
517 printf("\t--disable-panel: disable instrument panel\n");
518 printf("\t--enable-panel: enable instrumetn panel\n");
519 printf("\t--disable-sound: disable sound effects\n");
520 printf("\t--enable-sound: enable sound effects\n");
523 printf("Initial Position and Orientation:\n");
524 printf("\t--airport-id=ABCD: specify starting postion by airport id\n");
525 printf("\t--lon=degrees: starting longitude in degrees (west = -)\n");
526 printf("\t--lat=degrees: starting latitude in degrees (south = -)\n");
527 printf("\t--altitude=meters: starting altitude in meters\n");
528 printf("\t--heading=degrees: heading (yaw) angle in degress (Psi)\n");
529 printf("\t--roll=degrees: roll angle in degrees (Phi)\n");
530 printf("\t--pitch=degrees: pitch angle in degrees (Theta)\n");
533 printf("Rendering Options:\n");
534 printf("\t--fog-disable: disable fog/haze\n");
535 printf("\t--fog-fastest: enable fastest fog/haze\n");
536 printf("\t--fog-nicest: enable nicest fog/haze\n");
537 printf("\t--fov=xx.x: specify initial field of view angle in degrees\n");
538 printf("\t--disable-fullscreen: disable fullscreen mode\n");
539 printf("\t--enable-fullscreen: enable fullscreen mode\n");
540 printf("\t--shading-flat: enable flat shading\n");
541 printf("\t--shading-smooth: enable smooth shading\n");
542 printf("\t--disable-skyblend: disable sky blending\n");
543 printf("\t--enable-skyblend: enable sky blending\n");
544 printf("\t--disable-textures: disable textures\n");
545 printf("\t--enable-textures: enable textures\n");
546 printf("\t--disable-wireframe: disable wireframe drawing mode\n");
547 printf("\t--enable-wireframe: enable wireframe drawing mode\n");
550 printf("Scenery Options:\n");
551 printf("\t--tile-radius=n: specify tile radius, must be 1 - 4\n");
554 printf("Hud Options:\n");
555 printf("\t--hud-tris: Hud displays number of triangles rendered\n");
556 printf("\t--hud-culled: Hud displays percentage of triangles culled\n");
558 printf("Time Options:\n");
559 printf("\t--time-offset=[+-]hh:mm:ss: offset local time by this amount\n");
564 fgOPTIONS::~fgOPTIONS( void ) {
569 // Revision 1.25 1998/09/15 02:09:27 curt
570 // Include/fg_callback.hxx
571 // Moved code inline to stop g++ 2.7 from complaining.
573 // Simulator/Time/event.[ch]xx
574 // Changed return type of fgEVENT::printStat(). void caused g++ 2.7 to
575 // complain bitterly.
577 // Minor bugfix and changes.
579 // Simulator/Main/GLUTmain.cxx
580 // Added missing type to idle_state definition - eliminates a warning.
582 // Simulator/Main/fg_init.cxx
583 // Changes to airport lookup.
585 // Simulator/Main/options.cxx
586 // Uses fg_gzifstream when loading config file.
588 // Revision 1.24 1998/09/08 15:04:33 curt
589 // Optimizations by Norman Vine.
591 // Revision 1.23 1998/08/27 17:02:07 curt
592 // Contributions from Bernie Bright <bbright@c031.aone.net.au>
593 // - use strings for fg_root and airport_id and added methods to return
595 // - inlined all access methods,
596 // - made the parsing functions private methods,
597 // - deleted some unused functions.
598 // - propogated some of these changes out a bit further.
600 // Revision 1.22 1998/08/24 20:11:13 curt
601 // Added i/I to toggle full vs. minimal HUD.
602 // Added a --hud-tris vs --hud-culled option.
603 // Moved options accessor funtions to options.hxx.
605 // Revision 1.21 1998/08/20 15:10:34 curt
606 // Added GameGLUT support.
608 // Revision 1.20 1998/07/30 23:48:28 curt
609 // Output position & orientation when pausing.
610 // Eliminated libtool use.
611 // Added options to specify initial position and orientation.
612 // Changed default fov to 55 degrees.
613 // Added command line option to start in paused or unpaused state.
615 // Revision 1.19 1998/07/27 18:41:25 curt
616 // Added a pause command "p"
617 // Fixed some initialization order problems between pui and glut.
618 // Added an --enable/disable-sound option.
620 // Revision 1.18 1998/07/22 01:27:03 curt
621 // Strip out \r when parsing config file in case we are on a windoze system.
623 // Revision 1.17 1998/07/16 17:33:38 curt
624 // "H" / "h" now control hud brightness as well with off being one of the
626 // Better checking for xmesa/fx 3dfx fullscreen/window support for deciding
627 // whether or not to build in the feature.
628 // Translucent menu support.
629 // HAVE_AUDIO_SUPPORT -> ENABLE_AUDIO_SUPPORT
630 // Use fork() / wait() for playing mp3 init music in background under unix.
631 // Changed default tile diameter to 5.
633 // Revision 1.16 1998/07/13 21:01:39 curt
634 // Wrote access functions for current fgOPTIONS.
636 // Revision 1.15 1998/07/06 21:34:19 curt
637 // Added an enable/disable splash screen option.
638 // Added an enable/disable intro music option.
639 // Added an enable/disable instrument panel option.
640 // Added an enable/disable mouse pointer option.
641 // Added using namespace std for compilers that support this.
643 // Revision 1.14 1998/07/04 00:52:26 curt
644 // Add my own version of gluLookAt() (which is nearly identical to the
645 // Mesa/glu version.) But, by calculating the Model View matrix our selves
646 // we can save this matrix without having to read it back in from the video
647 // card. This hopefully allows us to save a few cpu cycles when rendering
648 // out the fragments because we can just use glLoadMatrixd() with the
649 // precalculated matrix for each tile rather than doing a push(), translate(),
650 // pop() for every fragment.
652 // Panel status defaults to off for now until it gets a bit more developed.
654 // Extract OpenGL driver info on initialization.
656 // Revision 1.13 1998/06/27 16:54:34 curt
657 // Replaced "extern displayInstruments" with a entry in fgOPTIONS.
658 // Don't change the view port when displaying the panel.
660 // Revision 1.12 1998/06/17 21:35:13 curt
661 // Refined conditional audio support compilation.
662 // Moved texture parameter setup calls to ../Scenery/materials.cxx
663 // #include <string.h> before various STL includes.
664 // Make HUD default state be enabled.
666 // Revision 1.11 1998/06/13 00:40:33 curt
667 // Tweaked fog command line options.
669 // Revision 1.10 1998/05/16 13:08:36 curt
670 // C++ - ified views.[ch]xx
671 // Shuffled some additional view parameters into the fgVIEW class.
672 // Changed tile-radius to tile-diameter because it is a much better
674 // Added a WORLD_TO_EYE transformation to views.cxx. This allows us
675 // to transform world space to eye space for view frustum culling.
677 // Revision 1.9 1998/05/13 18:29:59 curt
678 // Added a keyboard binding to dynamically adjust field of view.
679 // Added a command line option to specify fov.
680 // Adjusted terrain color.
681 // Root path info moved to fgOPTIONS.
682 // Added ability to parse options out of a config file.
684 // Revision 1.8 1998/05/07 23:14:16 curt
685 // Added "D" key binding to set autopilot heading.
686 // Made frame rate calculation average out over last 10 frames.
687 // Borland C++ floating point exception workaround.
688 // Added a --tile-radius=n option.
690 // Revision 1.7 1998/05/06 03:16:25 curt
691 // Added an averaged global frame rate counter.
692 // Added an option to control tile radius.
694 // Revision 1.6 1998/05/03 00:47:32 curt
695 // Added an option to enable/disable full-screen mode.
697 // Revision 1.5 1998/04/30 12:34:19 curt
698 // Added command line rendering options:
699 // enable/disable fog/haze
700 // specify smooth/flat shading
701 // disable sky blending and just use a solid color
702 // enable wireframe drawing mode
704 // Revision 1.4 1998/04/28 01:20:22 curt
705 // Type-ified fgTIME and fgVIEW.
706 // Added a command line option to disable textures.
708 // Revision 1.3 1998/04/26 05:01:19 curt
709 // Added an rint() / HAVE_RINT check.
711 // Revision 1.2 1998/04/25 15:11:12 curt
712 // Added an command line option to set starting position based on airport ID.
714 // Revision 1.1 1998/04/24 00:49:21 curt
715 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
716 // Trying out some different option parsing code.
717 // Some code reorganization.