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 <Include/fg_constants.h>
37 #include <Include/fg_zlib.h>
39 #include "options.hxx"
42 // Defined the shared options class here
43 fgOPTIONS current_options;
47 fgOPTIONS::fgOPTIONS( void ) {
48 // set initial values/defaults
50 if ( getenv("FG_ROOT") != NULL ) {
51 // fg_root could be anywhere, so default to environmental
52 // variable $FG_ROOT if it is set.
54 strcpy(fg_root, getenv("FG_ROOT"));
56 // Otherwise, default to a random compiled in location if
57 // $FG_ROOT is not set. This can still be overridden from the
58 // command line or a config file.
61 strcpy(fg_root, "\\FlightGear");
63 strcpy(fg_root, "/usr/local/lib/FlightGear");
68 strcpy(airport_id, "");
96 // Parse an int out of a --foo-bar=n type option
97 static int parse_int(char *arg) {
100 // advance past the '='
101 while ( (arg[0] != '=') && (arg[0] != '\0') ) {
105 if ( arg[0] == '=' ) {
109 // printf("parse_int(): arg = %s\n", arg);
113 // printf("parse_int(): result = %d\n", result);
119 // Parse an int out of a --foo-bar=n type option
120 static double parse_double(char *arg) {
123 // advance past the '='
124 while ( (arg[0] != '=') && (arg[0] != '\0') ) {
128 if ( arg[0] == '=' ) {
132 printf("parse_double(): arg = %s\n", arg);
136 printf("parse_double(): result = %.4f\n", result);
142 // parse time string in the form of [+-]hh:mm:ss, returns the value in seconds
143 static double parse_time(char *time_str) {
145 double hours, minutes, seconds;
150 // printf("parse_time(): %s\n", time_str);
153 if ( strlen(time_str) ) {
154 if ( time_str[0] == '+' ) {
157 } else if ( time_str[0] == '-' ) {
162 // printf("sign = %d\n", sign);
165 if ( strlen(time_str) ) {
167 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
168 num[i] = time_str[0];
172 if ( time_str[0] == ':' ) {
177 // printf("hours = %.2lf\n", hours);
179 result += hours * 3600.0;
183 if ( strlen(time_str) ) {
185 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
186 num[i] = time_str[0];
190 if ( time_str[0] == ':' ) {
195 // printf("minutes = %.2lf\n", minutes);
197 result += minutes * 60.0;
201 if ( strlen(time_str) ) {
203 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
204 num[i] = time_str[0];
210 // printf("seconds = %.2lf\n", seconds);
215 return(sign * result);
219 // parse time offset command line option
220 static int parse_time_offset(char *time_str) {
225 // printf("time offset = %s\n", time_str);
228 result = (int)rint(parse_time(time_str));
230 result = (int)parse_time(time_str);
233 printf("parse_time_offset(): %d\n", result);
239 // Parse --tile-diameter=n type option
241 #define FG_RADIUS_MIN 1
242 #define FG_RADIUS_MAX 4
244 static int parse_tile_radius(char *arg) {
247 radius = parse_int(arg);
249 if ( radius < FG_RADIUS_MIN ) { radius = FG_RADIUS_MIN; }
250 if ( radius > FG_RADIUS_MAX ) { radius = FG_RADIUS_MAX; }
252 // printf("parse_tile_radius(): radius = %d\n", radius);
258 // Parse --fov=x.xx type option
259 static double parse_fov(char *arg) {
262 fov = parse_double(arg);
264 if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
265 if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
267 printf("parse_fov(): result = %.4f\n", fov);
273 // Parse a single option
274 int fgOPTIONS::parse_option( char *arg ) {
276 if ( (strcmp(arg, "--help") == 0) ||
277 (strcmp(arg, "-h") == 0) ) {
278 // help/usage request
279 return(FG_OPTIONS_HELP);
280 } else if ( strcmp(arg, "--disable-splash-screen") == 0 ) {
282 } else if ( strcmp(arg, "--enable-splash-screen") == 0 ) {
284 } else if ( strcmp(arg, "--disable-intro-music") == 0 ) {
286 } else if ( strcmp(arg, "--enable-intro-music") == 0 ) {
288 } else if ( strcmp(arg, "--disable-mouse-pointer") == 0 ) {
290 } else if ( strcmp(arg, "--enable-mouse-pointer") == 0 ) {
292 } else if ( strcmp(arg, "--disable-hud") == 0 ) {
294 } else if ( strcmp(arg, "--enable-hud") == 0 ) {
296 } else if ( strcmp(arg, "--disable-panel") == 0 ) {
298 } else if ( strcmp(arg, "--enable-panel") == 0 ) {
300 } else if ( strcmp(arg, "--disable-sound") == 0 ) {
302 } else if ( strcmp(arg, "--enable-sound") == 0 ) {
304 } else if ( strncmp(arg, "--airport-id=", 13) == 0 ) {
306 strncpy(airport_id, arg, 4);
307 } else if ( strncmp(arg, "--fg-root=", 10) == 0 ) {
309 strcpy(fg_root, arg);
310 } else if ( strcmp(arg, "--fog-disable") == 0 ) {
312 } else if ( strcmp(arg, "--fog-fastest") == 0 ) {
314 } else if ( strcmp(arg, "--fog-nicest") == 0 ) {
316 } else if ( strncmp(arg, "--fov=", 6) == 0 ) {
317 fov = parse_fov(arg);
318 } else if ( strcmp(arg, "--disable-fullscreen") == 0 ) {
320 } else if ( strcmp(arg, "--enable-fullscreen") == 0 ) {
322 } else if ( strcmp(arg, "--shading-flat") == 0 ) {
324 } else if ( strcmp(arg, "--shading-smooth") == 0 ) {
326 } else if ( strcmp(arg, "--disable-skyblend") == 0 ) {
328 } else if ( strcmp(arg, "--enable-skyblend") == 0 ) {
330 } else if ( strcmp(arg, "--disable-textures") == 0 ) {
332 } else if ( strcmp(arg, "--enable-textures") == 0 ) {
334 } else if ( strcmp(arg, "--disable-wireframe") == 0 ) {
336 } else if ( strcmp(arg, "--enable-wireframe") == 0 ) {
338 } else if ( strncmp(arg, "--tile-radius=", 14) == 0 ) {
339 tile_radius = parse_tile_radius(arg);
340 tile_diameter = tile_radius * 2 + 1;
341 } else if ( strncmp(arg, "--time-offset=", 14) == 0 ) {
342 time_offset = parse_time_offset(arg);
344 return(FG_OPTIONS_ERROR);
347 return(FG_OPTIONS_OK);
351 // Parse the command line options
352 int fgOPTIONS::parse_command_line( int argc, char **argv ) {
356 fgPrintf(FG_GENERAL, FG_INFO, "Processing command line arguments\n");
359 fgPrintf(FG_GENERAL, FG_DEBUG, "argv[%d] = %s\n", i, argv[i]);
361 result = parse_option(argv[i]);
362 if ( (result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR) ) {
369 return(FG_OPTIONS_OK);
373 // Parse the command line options
374 int fgOPTIONS::parse_config_file( char *path ) {
375 char fgpath[256], line[256];
379 strcpy(fgpath, path);
380 strcat(fgpath, ".gz");
382 // first try "path.gz"
383 if ( (f = fgopen(fgpath, "rb")) == NULL ) {
385 if ( (f = fgopen(path, "rb")) == NULL ) {
386 return(FG_OPTIONS_ERROR);
390 fgPrintf(FG_GENERAL, FG_INFO, "Processing config file: %s\n", path);
392 while ( fggets(f, line, 250) != NULL ) {
393 // strip trailing newline if it exists
395 if ( line[len-1] == '\n' ) {
399 // strip dos ^M if it exists
401 if ( line[len-1] == '\r' ) {
405 result = parse_option(line);
406 if ( result == FG_OPTIONS_ERROR ) {
407 fgPrintf( FG_GENERAL, FG_EXIT,
408 "Config file parse error: %s '%s'\n", path, line );
413 return(FG_OPTIONS_OK);
417 // Print usage message
418 void fgOPTIONS::usage ( void ) {
419 printf("Usage: fg [ options ... ]\n");
422 printf("General Options:\n");
423 printf("\t--help -h: print usage\n");
424 printf("\t--fg-root=path: specify the root path for all the data files\n");
425 printf("\t--disable-splash-screen: disable splash screen\n");
426 printf("\t--enable-splash-screen: enable splash screen\n");
427 printf("\t--disable-intro-music: disable introduction music\n");
428 printf("\t--enable-intro-music: enable introduction music\n");
429 printf("\t--disable-mouse-pointer: disable extra mouse pointer\n");
430 printf("\t--enable-mouse-pointer: enable extra mouse pointer (i.e. for\n");
431 printf("\t\tfull screen voodoo/voodoo-II based cards.\n");
434 printf("Features:\n");
435 printf("\t--disable-hud: disable heads up display\n");
436 printf("\t--enable-hud: enable heads up display\n");
437 printf("\t--disable-panel: disable instrument panel\n");
438 printf("\t--enable-panel: enable instrumetn panel\n");
439 printf("\t--disable-sound: disable sound effects\n");
440 printf("\t--enable-sound: enable sound effects\n");
443 printf("Initial Position:\n");
444 printf("\t--airport-id=ABCD: specify starting postion by airport id\n");
447 printf("Rendering Options:\n");
448 printf("\t--fog-disable: disable fog/haze\n");
449 printf("\t--fog-fastest: enable fastest fog/haze\n");
450 printf("\t--fog-nicest: enable nicest fog/haze\n");
451 printf("\t--fov=xx.x: specify initial field of view angle in degrees\n");
452 printf("\t--disable-fullscreen: disable fullscreen mode\n");
453 printf("\t--enable-fullscreen: enable fullscreen mode\n");
454 printf("\t--shading-flat: enable flat shading\n");
455 printf("\t--shading-smooth: enable smooth shading\n");
456 printf("\t--disable-skyblend: disable sky blending\n");
457 printf("\t--enable-skyblend: enable sky blending\n");
458 printf("\t--disable-textures: disable textures\n");
459 printf("\t--enable-textures: enable textures\n");
460 printf("\t--disable-wireframe: disable wireframe drawing mode\n");
461 printf("\t--enable-wireframe: enable wireframe drawing mode\n");
464 printf("Scenery Options:\n");
465 printf("\t--tile-radius=n: specify tile radius, must be 1 - 4\n");
468 printf("Time Options:\n");
469 printf("\t--time-offset=[+-]hh:mm:ss: offset local time by this amount\n");
474 void fgOPTIONS::get_fg_root(char *root) { strcpy(root, fg_root); }
475 void fgOPTIONS::get_airport_id(char *id) { strcpy(id, airport_id); }
476 int fgOPTIONS::get_splash_screen( void ) { return(splash_screen); }
477 int fgOPTIONS::get_intro_music( void ) { return(intro_music); }
478 int fgOPTIONS::get_mouse_pointer( void ) { return(mouse_pointer); }
479 int fgOPTIONS::get_hud_status( void ) { return(hud_status); }
480 int fgOPTIONS::get_panel_status( void ) { return(panel_status); }
481 int fgOPTIONS::get_sound( void ) { return(sound); }
482 int fgOPTIONS::get_fog( void ) { return(fog); }
483 double fgOPTIONS::get_fov( void ) { return(fov); }
484 int fgOPTIONS::get_fullscreen( void ) { return(fullscreen); }
485 int fgOPTIONS::get_shading( void ) { return(shading); }
486 int fgOPTIONS::get_skyblend( void ) { return(skyblend); }
487 int fgOPTIONS::get_textures( void ) { return(textures); }
488 int fgOPTIONS::get_wireframe( void ) { return(wireframe); }
489 int fgOPTIONS::get_tile_radius( void ) { return(tile_radius); }
490 int fgOPTIONS::get_tile_diameter( void ) { return(tile_diameter); }
491 int fgOPTIONS::get_time_offset( void ) { return(time_offset); }
495 void fgOPTIONS::set_hud_status( int status ) { hud_status = status; }
496 void fgOPTIONS::set_fov( double amount ) { fov = amount; }
499 fgOPTIONS::~fgOPTIONS( void ) {
504 // Revision 1.19 1998/07/27 18:41:25 curt
505 // Added a pause command "p"
506 // Fixed some initialization order problems between pui and glut.
507 // Added an --enable/disable-sound option.
509 // Revision 1.18 1998/07/22 01:27:03 curt
510 // Strip out \r when parsing config file in case we are on a windoze system.
512 // Revision 1.17 1998/07/16 17:33:38 curt
513 // "H" / "h" now control hud brightness as well with off being one of the
515 // Better checking for xmesa/fx 3dfx fullscreen/window support for deciding
516 // whether or not to build in the feature.
517 // Translucent menu support.
518 // HAVE_AUDIO_SUPPORT -> ENABLE_AUDIO_SUPPORT
519 // Use fork() / wait() for playing mp3 init music in background under unix.
520 // Changed default tile diameter to 5.
522 // Revision 1.16 1998/07/13 21:01:39 curt
523 // Wrote access functions for current fgOPTIONS.
525 // Revision 1.15 1998/07/06 21:34:19 curt
526 // Added an enable/disable splash screen option.
527 // Added an enable/disable intro music option.
528 // Added an enable/disable instrument panel option.
529 // Added an enable/disable mouse pointer option.
530 // Added using namespace std for compilers that support this.
532 // Revision 1.14 1998/07/04 00:52:26 curt
533 // Add my own version of gluLookAt() (which is nearly identical to the
534 // Mesa/glu version.) But, by calculating the Model View matrix our selves
535 // we can save this matrix without having to read it back in from the video
536 // card. This hopefully allows us to save a few cpu cycles when rendering
537 // out the fragments because we can just use glLoadMatrixd() with the
538 // precalculated matrix for each tile rather than doing a push(), translate(),
539 // pop() for every fragment.
541 // Panel status defaults to off for now until it gets a bit more developed.
543 // Extract OpenGL driver info on initialization.
545 // Revision 1.13 1998/06/27 16:54:34 curt
546 // Replaced "extern displayInstruments" with a entry in fgOPTIONS.
547 // Don't change the view port when displaying the panel.
549 // Revision 1.12 1998/06/17 21:35:13 curt
550 // Refined conditional audio support compilation.
551 // Moved texture parameter setup calls to ../Scenery/materials.cxx
552 // #include <string.h> before various STL includes.
553 // Make HUD default state be enabled.
555 // Revision 1.11 1998/06/13 00:40:33 curt
556 // Tweaked fog command line options.
558 // Revision 1.10 1998/05/16 13:08:36 curt
559 // C++ - ified views.[ch]xx
560 // Shuffled some additional view parameters into the fgVIEW class.
561 // Changed tile-radius to tile-diameter because it is a much better
563 // Added a WORLD_TO_EYE transformation to views.cxx. This allows us
564 // to transform world space to eye space for view frustum culling.
566 // Revision 1.9 1998/05/13 18:29:59 curt
567 // Added a keyboard binding to dynamically adjust field of view.
568 // Added a command line option to specify fov.
569 // Adjusted terrain color.
570 // Root path info moved to fgOPTIONS.
571 // Added ability to parse options out of a config file.
573 // Revision 1.8 1998/05/07 23:14:16 curt
574 // Added "D" key binding to set autopilot heading.
575 // Made frame rate calculation average out over last 10 frames.
576 // Borland C++ floating point exception workaround.
577 // Added a --tile-radius=n option.
579 // Revision 1.7 1998/05/06 03:16:25 curt
580 // Added an averaged global frame rate counter.
581 // Added an option to control tile radius.
583 // Revision 1.6 1998/05/03 00:47:32 curt
584 // Added an option to enable/disable full-screen mode.
586 // Revision 1.5 1998/04/30 12:34:19 curt
587 // Added command line rendering options:
588 // enable/disable fog/haze
589 // specify smooth/flat shading
590 // disable sky blending and just use a solid color
591 // enable wireframe drawing mode
593 // Revision 1.4 1998/04/28 01:20:22 curt
594 // Type-ified fgTIME and fgVIEW.
595 // Added a command line option to disable textures.
597 // Revision 1.3 1998/04/26 05:01:19 curt
598 // Added an rint() / HAVE_RINT check.
600 // Revision 1.2 1998/04/25 15:11:12 curt
601 // Added an command line option to set starting position based on airport ID.
603 // Revision 1.1 1998/04/24 00:49:21 curt
604 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
605 // Trying out some different option parsing code.
606 // Some code reorganization.