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()
35 #include <Debug/fg_debug.h>
36 #include <Flight/flight.h>
37 #include <Include/fg_constants.h>
38 #include <Include/fg_zlib.h>
40 #include "options.hxx"
43 // Defined the shared options class here
44 fgOPTIONS current_options;
48 fgOPTIONS::fgOPTIONS( void ) {
49 // set initial values/defaults
51 if ( getenv("FG_ROOT") != NULL ) {
52 // fg_root could be anywhere, so default to environmental
53 // variable $FG_ROOT if it is set.
55 strcpy(fg_root, getenv("FG_ROOT"));
57 // Otherwise, default to a random compiled in location if
58 // $FG_ROOT is not set. This can still be overridden from the
59 // command line or a config file.
62 strcpy(fg_root, "\\FlightGear");
64 strcpy(fg_root, "/usr/local/lib/FlightGear");
68 // Starting posistion and orientation
69 strcpy(airport_id, ""); // default airport id
70 lon = 0.0; // starting longitude in degrees (west = -)
71 lat = 0.0; // starting latitude in degrees (south = -)
73 // If nothing else is specified, default initial position is
78 // North of the city of Globe
82 // North of the city of Globe
86 // Near where I used to live in Globe, AZ
90 // 10125 Jewell St. NE
94 // Near KHSP (Hot Springs, VA)
95 // lon = -79.8338964 + 0.01;
96 // lat = 37.9514564 + 0.008;
98 // (SEZ) SEDONA airport
99 // lon = -111.7884614 + 0.01;
100 // lat = 34.8486289 - 0.015;
102 // Somewhere near the Grand Canyon
106 // Jim Brennon's Kingmont Observatory
107 // lon = -121.1131667;
110 // Huaras, Peru (S09d 31.871' W077d 31.498')
111 // lon = -77.5249667;
114 // Eclipse Watching w73.5 n10 (approx) 18:00 UT
123 // lon = -90.1953055556;
124 // lat = 45.4511388889;
126 altitude = -9999.0; // starting altitude in meters (this will be
127 // reset to ground level if it is lower
130 // Initial Orientation
131 heading = 270.0; // heading (yaw) angle in degress (Psi)
132 roll = 0.0; // roll angle in degrees (Phi)
133 pitch = 0.424; // pitch angle in degrees (Theta)
147 // Flight Model options
148 flight_model = FG_LARCSIM;
167 // Parse an int out of a --foo-bar=n type option
168 static int parse_int(char *arg) {
171 // advance past the '='
172 while ( (arg[0] != '=') && (arg[0] != '\0') ) {
176 if ( arg[0] == '=' ) {
180 // printf("parse_int(): arg = %s\n", arg);
184 // printf("parse_int(): result = %d\n", result);
190 // Parse an int out of a --foo-bar=n type option
191 static double parse_double(char *arg) {
194 // advance past the '='
195 while ( (arg[0] != '=') && (arg[0] != '\0') ) {
199 if ( arg[0] == '=' ) {
203 // printf("parse_double(): arg = %s\n", arg);
207 // printf("parse_double(): result = %.4f\n", result);
213 static double parse_time(char *time_str) {
215 double hours, minutes, seconds;
220 // printf("parse_time(): %s\n", time_str);
223 if ( strlen(time_str) ) {
224 if ( time_str[0] == '+' ) {
227 } else if ( time_str[0] == '-' ) {
232 // printf("sign = %d\n", sign);
235 if ( strlen(time_str) ) {
237 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
238 num[i] = time_str[0];
242 if ( time_str[0] == ':' ) {
247 // printf("hours = %.2lf\n", hours);
253 if ( strlen(time_str) ) {
255 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
256 num[i] = time_str[0];
260 if ( time_str[0] == ':' ) {
265 // printf("minutes = %.2lf\n", minutes);
267 result += minutes / 60.0;
271 if ( strlen(time_str) ) {
273 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
274 num[i] = time_str[0];
280 // printf("seconds = %.2lf\n", seconds);
282 result += seconds / 3600.0;
285 return(sign * result);
289 // parse degree in the form of [+/-]hhh:mm:ss
290 static double parse_degree(char *degree_str) {
293 // advance past the '='
294 while ( (degree_str[0] != '=') && (degree_str[0] != '\0') ) {
298 if ( degree_str[0] == '=' ) {
302 result = parse_time(degree_str);
304 // printf("Degree = %.4f\n", result);
310 // parse time offset command line option
311 static int parse_time_offset(char *time_str) {
314 // advance past the '='
315 while ( (time_str[0] != '=') && (time_str[0] != '\0') ) {
319 if ( time_str[0] == '=' ) {
323 // printf("time offset = %s\n", time_str);
326 result = (int)rint(parse_time(time_str) * 3600.0);
328 result = (int)(parse_time(time_str) * 3600.0);
331 // printf("parse_time_offset(): %d\n", result);
337 // Parse --tile-diameter=n type option
339 #define FG_RADIUS_MIN 1
340 #define FG_RADIUS_MAX 4
342 static int parse_tile_radius(char *arg) {
345 radius = parse_int(arg);
347 if ( radius < FG_RADIUS_MIN ) { radius = FG_RADIUS_MIN; }
348 if ( radius > FG_RADIUS_MAX ) { radius = FG_RADIUS_MAX; }
350 // printf("parse_tile_radius(): radius = %d\n", radius);
356 // Parse --flightmode=abcdefg type option
357 static int parse_flight_model(char *fm) {
360 // printf("flight model = %s\n", fm);
362 if ( strcmp(fm, "slew") == 0 ) {
364 } else if ( strcmp(fm, "larcsim") == 0 ) {
366 } else if ( strcmp(fm, "LaRCsim") == 0 ) {
369 fgPrintf( FG_GENERAL, FG_EXIT, "Unknown flight model = %s\n", fm);
372 // we'll never get here, but it makes the compiler happy.
377 // Parse --fov=x.xx type option
378 static double parse_fov(char *arg) {
381 fov = parse_double(arg);
383 if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
384 if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
386 // printf("parse_fov(): result = %.4f\n", fov);
392 // Parse a single option
393 int fgOPTIONS::parse_option( char *arg ) {
395 if ( (strcmp(arg, "--help") == 0) ||
396 (strcmp(arg, "-h") == 0) ) {
397 // help/usage request
398 return(FG_OPTIONS_HELP);
399 } else if ( strcmp(arg, "--disable-game-mode") == 0 ) {
401 } else if ( strcmp(arg, "--enable-game-mode") == 0 ) {
403 } else if ( strcmp(arg, "--disable-splash-screen") == 0 ) {
405 } else if ( strcmp(arg, "--enable-splash-screen") == 0 ) {
407 } else if ( strcmp(arg, "--disable-intro-music") == 0 ) {
409 } else if ( strcmp(arg, "--enable-intro-music") == 0 ) {
411 } else if ( strcmp(arg, "--disable-mouse-pointer") == 0 ) {
413 } else if ( strcmp(arg, "--enable-mouse-pointer") == 0 ) {
415 } else if ( strcmp(arg, "--disable-pause") == 0 ) {
417 } else if ( strcmp(arg, "--enable-pause") == 0 ) {
419 } else if ( strcmp(arg, "--disable-hud") == 0 ) {
421 } else if ( strcmp(arg, "--enable-hud") == 0 ) {
423 } else if ( strcmp(arg, "--disable-panel") == 0 ) {
425 } else if ( strcmp(arg, "--enable-panel") == 0 ) {
427 } else if ( strcmp(arg, "--disable-sound") == 0 ) {
429 } else if ( strcmp(arg, "--enable-sound") == 0 ) {
431 } else if ( strncmp(arg, "--airport-id=", 13) == 0 ) {
433 strncpy(airport_id, arg, 4);
434 } else if ( strncmp(arg, "--lon=", 6) == 0 ) {
435 lon = parse_degree(arg);
436 } else if ( strncmp(arg, "--lat=", 6) == 0 ) {
437 lat = parse_degree(arg);
438 } else if ( strncmp(arg, "--altitude=", 11) == 0 ) {
439 altitude = parse_double(arg);
440 } else if ( strncmp(arg, "--heading=", 6) == 0 ) {
441 heading = parse_double(arg);
442 } else if ( strncmp(arg, "--roll=", 7) == 0 ) {
443 roll = parse_double(arg);
444 } else if ( strncmp(arg, "--pitch=", 8) == 0 ) {
445 pitch = parse_double(arg);
446 } else if ( strncmp(arg, "--fg-root=", 10) == 0 ) {
448 strcpy(fg_root, arg);
449 } else if ( strncmp(arg, "--flight-model=", 15) == 0 ) {
450 flight_model = parse_flight_model(arg);
451 } else if ( strcmp(arg, "--fog-disable") == 0 ) {
453 } else if ( strcmp(arg, "--fog-fastest") == 0 ) {
455 } else if ( strcmp(arg, "--fog-nicest") == 0 ) {
457 } else if ( strncmp(arg, "--fov=", 6) == 0 ) {
458 fov = parse_fov(arg);
459 } else if ( strcmp(arg, "--disable-fullscreen") == 0 ) {
461 } else if ( strcmp(arg, "--enable-fullscreen") == 0 ) {
463 } else if ( strcmp(arg, "--shading-flat") == 0 ) {
465 } else if ( strcmp(arg, "--shading-smooth") == 0 ) {
467 } else if ( strcmp(arg, "--disable-skyblend") == 0 ) {
469 } else if ( strcmp(arg, "--enable-skyblend") == 0 ) {
471 } else if ( strcmp(arg, "--disable-textures") == 0 ) {
473 } else if ( strcmp(arg, "--enable-textures") == 0 ) {
475 } else if ( strcmp(arg, "--disable-wireframe") == 0 ) {
477 } else if ( strcmp(arg, "--enable-wireframe") == 0 ) {
479 } else if ( strncmp(arg, "--tile-radius=", 14) == 0 ) {
480 tile_radius = parse_tile_radius(arg);
481 tile_diameter = tile_radius * 2 + 1;
482 } else if ( strncmp(arg, "--time-offset=", 14) == 0 ) {
483 time_offset = parse_time_offset(arg);
485 fgPrintf( FG_GENERAL, FG_EXIT, "Unknown option '%s'\n", arg);
486 return(FG_OPTIONS_ERROR);
489 return(FG_OPTIONS_OK);
493 // Parse the command line options
494 int fgOPTIONS::parse_command_line( int argc, char **argv ) {
498 fgPrintf(FG_GENERAL, FG_INFO, "Processing command line arguments\n");
501 fgPrintf(FG_GENERAL, FG_DEBUG, "argv[%d] = %s\n", i, argv[i]);
503 result = parse_option(argv[i]);
504 if ( (result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR) ) {
511 return(FG_OPTIONS_OK);
515 // Parse the command line options
516 int fgOPTIONS::parse_config_file( char *path ) {
517 char fgpath[256], line[256];
521 strcpy(fgpath, path);
522 strcat(fgpath, ".gz");
524 // first try "path.gz"
525 if ( (f = fgopen(fgpath, "rb")) == NULL ) {
527 if ( (f = fgopen(path, "rb")) == NULL ) {
528 return(FG_OPTIONS_ERROR);
532 fgPrintf(FG_GENERAL, FG_INFO, "Processing config file: %s\n", path);
534 while ( fggets(f, line, 250) != NULL ) {
535 // strip trailing newline if it exists
537 if ( line[len-1] == '\n' ) {
541 // strip dos ^M if it exists
543 if ( line[len-1] == '\r' ) {
547 result = parse_option(line);
548 if ( result == FG_OPTIONS_ERROR ) {
549 fgPrintf( FG_GENERAL, FG_EXIT,
550 "Config file parse error: %s '%s'\n", path, line );
555 return(FG_OPTIONS_OK);
559 // Print usage message
560 void fgOPTIONS::usage ( void ) {
561 printf("Usage: fg [ options ... ]\n");
564 printf("General Options:\n");
565 printf("\t--help -h: print usage\n");
566 printf("\t--fg-root=path: specify the root path for all the data files\n");
567 printf("\t--disable-gamemode: disable full-screen game mode\n");
568 printf("\t--enable-gamemode: enable full-screen game mode\n");
569 printf("\t--disable-splash-screen: disable splash screen\n");
570 printf("\t--enable-splash-screen: enable splash screen\n");
571 printf("\t--disable-intro-music: disable introduction music\n");
572 printf("\t--enable-intro-music: enable introduction music\n");
573 printf("\t--disable-mouse-pointer: disable extra mouse pointer\n");
574 printf("\t--enable-mouse-pointer: enable extra mouse pointer (i.e. for\n");
575 printf("\t\tfull screen voodoo/voodoo-II based cards.)\n");
576 printf("\t--disable-pause: start out in an active state\n");
577 printf("\t--enable-pause: start out in a paused state\n");
580 printf("Features:\n");
581 printf("\t--disable-hud: disable heads up display\n");
582 printf("\t--enable-hud: enable heads up display\n");
583 printf("\t--disable-panel: disable instrument panel\n");
584 printf("\t--enable-panel: enable instrumetn panel\n");
585 printf("\t--disable-sound: disable sound effects\n");
586 printf("\t--enable-sound: enable sound effects\n");
589 printf("Initial Position and Orientation:\n");
590 printf("\t--airport-id=ABCD: specify starting postion by airport id\n");
591 printf("\t--lon=degrees: starting longitude in degrees (west = -)\n");
592 printf("\t--lat=degrees: starting latitude in degrees (south = -)\n");
593 printf("\t--altitude=meters: starting altitude in meters\n");
594 printf("\t--heading=degrees: heading (yaw) angle in degress (Psi)\n");
595 printf("\t--roll=degrees: roll angle in degrees (Phi)\n");
596 printf("\t--pitch=degrees: pitch angle in degrees (Theta)\n");
599 printf("Rendering Options:\n");
600 printf("\t--fog-disable: disable fog/haze\n");
601 printf("\t--fog-fastest: enable fastest fog/haze\n");
602 printf("\t--fog-nicest: enable nicest fog/haze\n");
603 printf("\t--fov=xx.x: specify initial field of view angle in degrees\n");
604 printf("\t--disable-fullscreen: disable fullscreen mode\n");
605 printf("\t--enable-fullscreen: enable fullscreen mode\n");
606 printf("\t--shading-flat: enable flat shading\n");
607 printf("\t--shading-smooth: enable smooth shading\n");
608 printf("\t--disable-skyblend: disable sky blending\n");
609 printf("\t--enable-skyblend: enable sky blending\n");
610 printf("\t--disable-textures: disable textures\n");
611 printf("\t--enable-textures: enable textures\n");
612 printf("\t--disable-wireframe: disable wireframe drawing mode\n");
613 printf("\t--enable-wireframe: enable wireframe drawing mode\n");
616 printf("Scenery Options:\n");
617 printf("\t--tile-radius=n: specify tile radius, must be 1 - 4\n");
620 printf("Time Options:\n");
621 printf("\t--time-offset=[+-]hh:mm:ss: offset local time by this amount\n");
626 void fgOPTIONS::get_fg_root(char *root) { strcpy(root, fg_root); }
627 void fgOPTIONS::get_airport_id(char *id) { strcpy(id, airport_id); }
628 double fgOPTIONS::get_lon( void ) { return(lon); }
629 double fgOPTIONS::get_lat( void ) { return(lat); }
630 double fgOPTIONS::get_altitude( void ) { return(altitude); }
631 double fgOPTIONS::get_heading( void ) { return(heading); }
632 double fgOPTIONS::get_roll( void ) { return(roll); }
633 double fgOPTIONS::get_pitch( void ) { return(pitch); }
634 int fgOPTIONS::get_game_mode( void ) { return(game_mode); }
635 int fgOPTIONS::get_splash_screen( void ) { return(splash_screen); }
636 int fgOPTIONS::get_intro_music( void ) { return(intro_music); }
637 int fgOPTIONS::get_mouse_pointer( void ) { return(mouse_pointer); }
638 int fgOPTIONS::get_pause( void ) { return(pause); }
639 int fgOPTIONS::get_hud_status( void ) { return(hud_status); }
640 int fgOPTIONS::get_panel_status( void ) { return(panel_status); }
641 int fgOPTIONS::get_sound( void ) { return(sound); }
642 int fgOPTIONS::get_flight_model( void ) { return(flight_model); }
643 int fgOPTIONS::get_fog( void ) { return(fog); }
644 double fgOPTIONS::get_fov( void ) { return(fov); }
645 int fgOPTIONS::get_fullscreen( void ) { return(fullscreen); }
646 int fgOPTIONS::get_shading( void ) { return(shading); }
647 int fgOPTIONS::get_skyblend( void ) { return(skyblend); }
648 int fgOPTIONS::get_textures( void ) { return(textures); }
649 int fgOPTIONS::get_wireframe( void ) { return(wireframe); }
650 int fgOPTIONS::get_tile_radius( void ) { return(tile_radius); }
651 int fgOPTIONS::get_tile_diameter( void ) { return(tile_diameter); }
652 int fgOPTIONS::get_time_offset( void ) { return(time_offset); }
656 void fgOPTIONS::set_hud_status( int status ) { hud_status = status; }
657 void fgOPTIONS::set_fov( double amount ) { fov = amount; }
660 fgOPTIONS::~fgOPTIONS( void ) {
665 // Revision 1.21 1998/08/20 15:10:34 curt
666 // Added GameGLUT support.
668 // Revision 1.20 1998/07/30 23:48:28 curt
669 // Output position & orientation when pausing.
670 // Eliminated libtool use.
671 // Added options to specify initial position and orientation.
672 // Changed default fov to 55 degrees.
673 // Added command line option to start in paused or unpaused state.
675 // Revision 1.19 1998/07/27 18:41:25 curt
676 // Added a pause command "p"
677 // Fixed some initialization order problems between pui and glut.
678 // Added an --enable/disable-sound option.
680 // Revision 1.18 1998/07/22 01:27:03 curt
681 // Strip out \r when parsing config file in case we are on a windoze system.
683 // Revision 1.17 1998/07/16 17:33:38 curt
684 // "H" / "h" now control hud brightness as well with off being one of the
686 // Better checking for xmesa/fx 3dfx fullscreen/window support for deciding
687 // whether or not to build in the feature.
688 // Translucent menu support.
689 // HAVE_AUDIO_SUPPORT -> ENABLE_AUDIO_SUPPORT
690 // Use fork() / wait() for playing mp3 init music in background under unix.
691 // Changed default tile diameter to 5.
693 // Revision 1.16 1998/07/13 21:01:39 curt
694 // Wrote access functions for current fgOPTIONS.
696 // Revision 1.15 1998/07/06 21:34:19 curt
697 // Added an enable/disable splash screen option.
698 // Added an enable/disable intro music option.
699 // Added an enable/disable instrument panel option.
700 // Added an enable/disable mouse pointer option.
701 // Added using namespace std for compilers that support this.
703 // Revision 1.14 1998/07/04 00:52:26 curt
704 // Add my own version of gluLookAt() (which is nearly identical to the
705 // Mesa/glu version.) But, by calculating the Model View matrix our selves
706 // we can save this matrix without having to read it back in from the video
707 // card. This hopefully allows us to save a few cpu cycles when rendering
708 // out the fragments because we can just use glLoadMatrixd() with the
709 // precalculated matrix for each tile rather than doing a push(), translate(),
710 // pop() for every fragment.
712 // Panel status defaults to off for now until it gets a bit more developed.
714 // Extract OpenGL driver info on initialization.
716 // Revision 1.13 1998/06/27 16:54:34 curt
717 // Replaced "extern displayInstruments" with a entry in fgOPTIONS.
718 // Don't change the view port when displaying the panel.
720 // Revision 1.12 1998/06/17 21:35:13 curt
721 // Refined conditional audio support compilation.
722 // Moved texture parameter setup calls to ../Scenery/materials.cxx
723 // #include <string.h> before various STL includes.
724 // Make HUD default state be enabled.
726 // Revision 1.11 1998/06/13 00:40:33 curt
727 // Tweaked fog command line options.
729 // Revision 1.10 1998/05/16 13:08:36 curt
730 // C++ - ified views.[ch]xx
731 // Shuffled some additional view parameters into the fgVIEW class.
732 // Changed tile-radius to tile-diameter because it is a much better
734 // Added a WORLD_TO_EYE transformation to views.cxx. This allows us
735 // to transform world space to eye space for view frustum culling.
737 // Revision 1.9 1998/05/13 18:29:59 curt
738 // Added a keyboard binding to dynamically adjust field of view.
739 // Added a command line option to specify fov.
740 // Adjusted terrain color.
741 // Root path info moved to fgOPTIONS.
742 // Added ability to parse options out of a config file.
744 // Revision 1.8 1998/05/07 23:14:16 curt
745 // Added "D" key binding to set autopilot heading.
746 // Made frame rate calculation average out over last 10 frames.
747 // Borland C++ floating point exception workaround.
748 // Added a --tile-radius=n option.
750 // Revision 1.7 1998/05/06 03:16:25 curt
751 // Added an averaged global frame rate counter.
752 // Added an option to control tile radius.
754 // Revision 1.6 1998/05/03 00:47:32 curt
755 // Added an option to enable/disable full-screen mode.
757 // Revision 1.5 1998/04/30 12:34:19 curt
758 // Added command line rendering options:
759 // enable/disable fog/haze
760 // specify smooth/flat shading
761 // disable sky blending and just use a solid color
762 // enable wireframe drawing mode
764 // Revision 1.4 1998/04/28 01:20:22 curt
765 // Type-ified fgTIME and fgVIEW.
766 // Added a command line option to disable textures.
768 // Revision 1.3 1998/04/26 05:01:19 curt
769 // Added an rint() / HAVE_RINT check.
771 // Revision 1.2 1998/04/25 15:11:12 curt
772 // Added an command line option to set starting position based on airport ID.
774 // Revision 1.1 1998/04/24 00:49:21 curt
775 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
776 // Trying out some different option parsing code.
777 // Some code reorganization.