]> git.mxchange.org Git - flightgear.git/blob - Main/options.cxx
Strip out \r when parsing config file in case we are on a windoze system.
[flightgear.git] / Main / options.cxx
1 //
2 // options.cxx -- class to handle command line options
3 //
4 // Written by Curtis Olson, started April 1998.
5 //
6 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
7 //
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.
12 //
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.
17 //
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.
21 //
22 // $Id$
23 // (Log is kept at end of this file)
24
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 #include <math.h>            // rint()
31 #include <stdio.h>
32 #include <stdlib.h>          // atof(), atoi()
33 #include <string.h>
34
35 #include <Debug/fg_debug.h>
36 #include <Include/fg_constants.h>
37 #include <Include/fg_zlib.h>
38
39 #include "options.hxx"
40
41
42 // Defined the shared options class here
43 fgOPTIONS current_options;
44
45
46 // Constructor
47 fgOPTIONS::fgOPTIONS( void ) {
48     // set initial values/defaults
49
50     if ( getenv("FG_ROOT") != NULL ) {
51         // fg_root could be anywhere, so default to environmental
52         // variable $FG_ROOT if it is set.
53
54         strcpy(fg_root, getenv("FG_ROOT"));
55     } else {
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.
59
60 #if defined(WIN32)
61         strcpy(fg_root, "\\FlightGear");
62 #else
63         strcpy(fg_root, "/usr/local/lib/FlightGear");
64 #endif
65     }
66
67     // default airport id
68     strcpy(airport_id, "");
69
70     // Miscellaneous
71     splash_screen = 1;
72     intro_music = 1;
73     mouse_pointer = 0;
74
75     // Features
76     hud_status = 1;
77     panel_status = 0;
78
79     // Rendering options
80     fog = 2;    // nicest
81     fov = 65.0;
82     fullscreen = 0;
83     shading = 1;
84     skyblend = 1;
85     textures = 1;
86     wireframe = 0;
87
88     // Scenery options
89     tile_diameter = 5;
90
91     // Time options
92     time_offset = 0;
93 }
94
95
96 // Parse an int out of a --foo-bar=n type option 
97 static int parse_int(char *arg) {
98     int result;
99
100     // advance past the '='
101     while ( (arg[0] != '=') && (arg[0] != '\0') ) {
102         arg++;
103     }
104
105     if ( arg[0] == '=' ) {
106         arg++;
107     }
108
109     // printf("parse_int(): arg = %s\n", arg);
110
111     result = atoi(arg);
112
113     // printf("parse_int(): result = %d\n", result);
114
115     return(result);
116 }
117
118
119 // Parse an int out of a --foo-bar=n type option 
120 static double parse_double(char *arg) {
121     double result;
122
123     // advance past the '='
124     while ( (arg[0] != '=') && (arg[0] != '\0') ) {
125         arg++;
126     }
127
128     if ( arg[0] == '=' ) {
129         arg++;
130     }
131
132     printf("parse_double(): arg = %s\n", arg);
133
134     result = atof(arg);
135
136     printf("parse_double(): result = %.4f\n", result);
137
138     return(result);
139 }
140
141
142 // parse time string in the form of [+-]hh:mm:ss, returns the value in seconds
143 static double parse_time(char *time_str) {
144     char num[256];
145     double hours, minutes, seconds;
146     double result = 0.0;
147     int sign = 1;
148     int i;
149
150     // printf("parse_time(): %s\n", time_str);
151
152     // check for sign
153     if ( strlen(time_str) ) {
154         if ( time_str[0] == '+' ) {
155             sign = 1;
156             time_str++;
157         } else if ( time_str[0] == '-' ) {
158             sign = -1;
159             time_str++;
160         }
161     }
162     // printf("sign = %d\n", sign);
163
164     // get hours
165     if ( strlen(time_str) ) {
166         i = 0;
167         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
168             num[i] = time_str[0];
169             time_str++;
170             i++;
171         }
172         if ( time_str[0] == ':' ) {
173             time_str++;
174         }
175         num[i] = '\0';
176         hours = atof(num);
177         // printf("hours = %.2lf\n", hours);
178
179         result += hours * 3600.0;
180     }
181
182     // get minutes
183     if ( strlen(time_str) ) {
184         i = 0;
185         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
186             num[i] = time_str[0];
187             time_str++;
188             i++;
189         }
190         if ( time_str[0] == ':' ) {
191             time_str++;
192         }
193         num[i] = '\0';
194         minutes = atof(num);
195         // printf("minutes = %.2lf\n", minutes);
196
197         result += minutes * 60.0;
198     }
199
200     // get seconds
201     if ( strlen(time_str) ) {
202         i = 0;
203         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
204             num[i] = time_str[0];
205             time_str++;
206             i++;
207         }
208         num[i] = '\0';
209         seconds = atof(num);
210         // printf("seconds = %.2lf\n", seconds);
211
212         result += seconds;
213     }
214
215     return(sign * result);
216 }
217
218
219 // parse time offset command line option
220 static int parse_time_offset(char *time_str) {
221     int result;
222
223     time_str += 14;
224
225     // printf("time offset = %s\n", time_str);
226
227 #ifdef HAVE_RINT
228     result = (int)rint(parse_time(time_str));
229 #else
230     result = (int)parse_time(time_str);
231 #endif
232
233     printf("parse_time_offset(): %d\n", result);
234
235     return( result );
236 }
237
238
239 // Parse --tile-diameter=n type option 
240
241 #define FG_RADIUS_MIN 1
242 #define FG_RADIUS_MAX 4
243
244 static int parse_tile_radius(char *arg) {
245     int radius, tmp;
246
247     radius = parse_int(arg);
248
249     if ( radius < FG_RADIUS_MIN ) { radius = FG_RADIUS_MIN; }
250     if ( radius > FG_RADIUS_MAX ) { radius = FG_RADIUS_MAX; }
251
252     // printf("parse_tile_radius(): radius = %d\n", radius);
253
254     return(radius);
255 }
256
257
258 // Parse --fov=x.xx type option 
259 static double parse_fov(char *arg) {
260     double fov;
261
262     fov = parse_double(arg);
263
264     if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
265     if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
266
267     printf("parse_fov(): result = %.4f\n", fov);
268
269     return(fov);
270 }
271
272
273 // Parse a single option
274 int fgOPTIONS::parse_option( char *arg ) {
275     // General Options
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 ) {
281         splash_screen = 0;
282     } else if ( strcmp(arg, "--enable-splash-screen") == 0 ) {
283         splash_screen = 1;
284     } else if ( strcmp(arg, "--disable-intro-music") == 0 ) {
285         intro_music = 0;
286     } else if ( strcmp(arg, "--enable-intro-music") == 0 ) {
287         intro_music = 1;
288     } else if ( strcmp(arg, "--disable-mouse-pointer") == 0 ) {
289         mouse_pointer = 1;
290     } else if ( strcmp(arg, "--enable-mouse-pointer") == 0 ) {
291         mouse_pointer = 2;
292     } else if ( strcmp(arg, "--disable-hud") == 0 ) {
293         hud_status = 0; 
294     } else if ( strcmp(arg, "--enable-hud") == 0 ) {
295         hud_status = 1; 
296     } else if ( strcmp(arg, "--disable-panel") == 0 ) {
297         panel_status = 0;
298     } else if ( strcmp(arg, "--enable-panel") == 0 ) {
299         panel_status = 1;
300     } else if ( strncmp(arg, "--airport-id=", 13) == 0 ) {
301         arg += 13;
302         strncpy(airport_id, arg, 4);
303     } else if ( strncmp(arg, "--fg-root=", 10) == 0 ) {
304         arg += 10;
305         strcpy(fg_root, arg);
306     } else if ( strcmp(arg, "--fog-disable") == 0 ) {
307         fog = 0;        
308     } else if ( strcmp(arg, "--fog-fastest") == 0 ) {
309         fog = 1;        
310     } else if ( strcmp(arg, "--fog-nicest") == 0 ) {
311         fog = 2;        
312     } else if ( strncmp(arg, "--fov=", 6) == 0 ) {
313         fov = parse_fov(arg);
314     } else if ( strcmp(arg, "--disable-fullscreen") == 0 ) {
315         fullscreen = 0; 
316     } else if ( strcmp(arg, "--enable-fullscreen") == 0 ) {
317         fullscreen = 1; 
318     } else if ( strcmp(arg, "--shading-flat") == 0 ) {
319         shading = 0;    
320     } else if ( strcmp(arg, "--shading-smooth") == 0 ) {
321         shading = 1;    
322     } else if ( strcmp(arg, "--disable-skyblend") == 0 ) {
323         skyblend = 0;   
324     } else if ( strcmp(arg, "--enable-skyblend") == 0 ) {
325         skyblend = 1;   
326     } else if ( strcmp(arg, "--disable-textures") == 0 ) {
327         textures = 0;   
328     } else if ( strcmp(arg, "--enable-textures") == 0 ) {
329         textures = 1;   
330     } else if ( strcmp(arg, "--disable-wireframe") == 0 ) {
331         wireframe = 0;  
332     } else if ( strcmp(arg, "--enable-wireframe") == 0 ) {
333         wireframe = 1;  
334     } else if ( strncmp(arg, "--tile-radius=", 14) == 0 ) {
335         tile_radius = parse_tile_radius(arg);
336         tile_diameter = tile_radius * 2 + 1;
337     } else if ( strncmp(arg, "--time-offset=", 14) == 0 ) {
338         time_offset = parse_time_offset(arg);
339     } else {
340         return(FG_OPTIONS_ERROR);
341     }
342     
343     return(FG_OPTIONS_OK);
344 }
345
346
347 // Parse the command line options
348 int fgOPTIONS::parse_command_line( int argc, char **argv ) {
349     int i = 1;
350     int result;
351
352     fgPrintf(FG_GENERAL, FG_INFO, "Processing command line arguments\n");
353
354     while ( i < argc ) {
355         fgPrintf(FG_GENERAL, FG_DEBUG, "argv[%d] = %s\n", i, argv[i]);
356
357         result = parse_option(argv[i]);
358         if ( (result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR) ) {
359             return(result);
360         }
361
362         i++;
363     }
364     
365     return(FG_OPTIONS_OK);
366 }
367
368
369 // Parse the command line options
370 int fgOPTIONS::parse_config_file( char *path ) {
371     char fgpath[256], line[256];
372     fgFile f;
373     int len, result;
374
375     strcpy(fgpath, path);
376     strcat(fgpath, ".gz");
377
378     // first try "path.gz"
379     if ( (f = fgopen(fgpath, "rb")) == NULL ) {
380         // next try "path"
381         if ( (f = fgopen(path, "rb")) == NULL ) {
382             return(FG_OPTIONS_ERROR);
383         }
384     }
385
386     fgPrintf(FG_GENERAL, FG_INFO, "Processing config file: %s\n", path);
387
388     while ( fggets(f, line, 250) != NULL ) {
389         // strip trailing newline if it exists
390         len = strlen(line);
391         if ( line[len-1] == '\n' ) {
392             line[len-1] = '\0';
393         }
394
395         // strip dos ^M if it exists
396         len = strlen(line);
397         if ( line[len-1] == '\r' ) {
398             line[len-1] = '\0';
399         }
400
401         result = parse_option(line);
402         if ( result == FG_OPTIONS_ERROR ) {
403             fgPrintf( FG_GENERAL, FG_EXIT, 
404                       "Config file parse error: %s '%s'\n", path, line );
405         }
406     }
407
408     fgclose(f);
409     return(FG_OPTIONS_OK);
410 }
411
412
413 // Print usage message
414 void fgOPTIONS::usage ( void ) {
415     printf("Usage: fg [ options ... ]\n");
416     printf("\n");
417
418     printf("General Options:\n");
419     printf("\t--help -h:  print usage\n");
420     printf("\t--fg-root=path:  specify the root path for all the data files\n");
421     printf("\t--disable-splash-screen:  disable splash screen\n");
422     printf("\t--enable-splash-screen:  enable splash screen\n");
423     printf("\t--disable-intro-music:  disable introduction music\n");
424     printf("\t--enable-intro-music:  enable introduction music\n");
425     printf("\t--disable-mouse-pointer:  disable extra mouse pointer\n");
426     printf("\t--enable-mouse-pointer:  enable extra mouse pointer (i.e. for\n");
427     printf("\t\tfull screen voodoo/voodoo-II based cards.\n");
428     printf("\n");
429
430     printf("Features:\n");
431     printf("\t--disable-hud:  disable heads up display\n");
432     printf("\t--enable-hud:  enable heads up display\n");
433     printf("\t--disable-panel:  disable instrument panel\n");
434     printf("\t--enable-panel:  enable instrumetn panel\n");
435     printf("\n");
436  
437     printf("Initial Position:\n");
438     printf("\t--airport-id=ABCD:  specify starting postion by airport id\n");
439     printf("\n");
440
441     printf("Rendering Options:\n");
442     printf("\t--fog-disable:  disable fog/haze\n");
443     printf("\t--fog-fastest:  enable fastest fog/haze\n");
444     printf("\t--fog-nicest:  enable nicest fog/haze\n");
445     printf("\t--fov=xx.x:  specify initial field of view angle in degrees\n");
446     printf("\t--disable-fullscreen:  disable fullscreen mode\n");
447     printf("\t--enable-fullscreen:  enable fullscreen mode\n");
448     printf("\t--shading-flat:  enable flat shading\n");
449     printf("\t--shading-smooth:  enable smooth shading\n");
450     printf("\t--disable-skyblend:  disable sky blending\n");
451     printf("\t--enable-skyblend:  enable sky blending\n");
452     printf("\t--disable-textures:  disable textures\n");
453     printf("\t--enable-textures:  enable textures\n");
454     printf("\t--disable-wireframe:  disable wireframe drawing mode\n");
455     printf("\t--enable-wireframe:  enable wireframe drawing mode\n");
456     printf("\n");
457
458     printf("Scenery Options:\n");
459     printf("\t--tile-radius=n:  specify tile radius, must be 1 - 4\n");
460     printf("\n");
461
462     printf("Time Options:\n");
463     printf("\t--time-offset=[+-]hh:mm:ss:  offset local time by this amount\n");
464 }
465
466
467 // Query functions
468 void fgOPTIONS::get_fg_root(char *root) { strcpy(root, fg_root); }
469 void fgOPTIONS::get_airport_id(char *id) { strcpy(id, airport_id); }
470 int fgOPTIONS::get_splash_screen( void ) { return(splash_screen); }
471 int fgOPTIONS::get_intro_music( void ) { return(intro_music); }
472 int fgOPTIONS::get_mouse_pointer( void ) { return(mouse_pointer); }
473 int fgOPTIONS::get_hud_status( void ) { return(hud_status); }
474 int fgOPTIONS::get_panel_status( void ) { return(panel_status); }
475 int fgOPTIONS::get_fog( void ) { return(fog); }
476 double fgOPTIONS::get_fov( void ) { return(fov); }
477 int fgOPTIONS::get_fullscreen( void ) { return(fullscreen); }
478 int fgOPTIONS::get_shading( void ) { return(shading); }
479 int fgOPTIONS::get_skyblend( void ) { return(skyblend); }
480 int fgOPTIONS::get_textures( void ) { return(textures); }
481 int fgOPTIONS::get_wireframe( void ) { return(wireframe); }
482 int fgOPTIONS::get_tile_radius( void ) { return(tile_radius); }
483 int fgOPTIONS::get_tile_diameter( void ) { return(tile_diameter); }
484 int fgOPTIONS::get_time_offset( void ) { return(time_offset); }
485
486
487 // Update functions
488 void fgOPTIONS::set_hud_status( int status ) { hud_status = status; }
489 void fgOPTIONS::set_fov( double amount ) { fov = amount; }
490
491 // Destructor
492 fgOPTIONS::~fgOPTIONS( void ) {
493 }
494
495
496 // $Log$
497 // Revision 1.18  1998/07/22 01:27:03  curt
498 // Strip out \r when parsing config file in case we are on a windoze system.
499 //
500 // Revision 1.17  1998/07/16 17:33:38  curt
501 // "H" / "h" now control hud brightness as well with off being one of the
502 //   states.
503 // Better checking for xmesa/fx 3dfx fullscreen/window support for deciding
504 //   whether or not to build in the feature.
505 // Translucent menu support.
506 // HAVE_AUDIO_SUPPORT -> ENABLE_AUDIO_SUPPORT
507 // Use fork() / wait() for playing mp3 init music in background under unix.
508 // Changed default tile diameter to 5.
509 //
510 // Revision 1.16  1998/07/13 21:01:39  curt
511 // Wrote access functions for current fgOPTIONS.
512 //
513 // Revision 1.15  1998/07/06 21:34:19  curt
514 // Added an enable/disable splash screen option.
515 // Added an enable/disable intro music option.
516 // Added an enable/disable instrument panel option.
517 // Added an enable/disable mouse pointer option.
518 // Added using namespace std for compilers that support this.
519 //
520 // Revision 1.14  1998/07/04 00:52:26  curt
521 // Add my own version of gluLookAt() (which is nearly identical to the
522 // Mesa/glu version.)  But, by calculating the Model View matrix our selves
523 // we can save this matrix without having to read it back in from the video
524 // card.  This hopefully allows us to save a few cpu cycles when rendering
525 // out the fragments because we can just use glLoadMatrixd() with the
526 // precalculated matrix for each tile rather than doing a push(), translate(),
527 // pop() for every fragment.
528 //
529 // Panel status defaults to off for now until it gets a bit more developed.
530 //
531 // Extract OpenGL driver info on initialization.
532 //
533 // Revision 1.13  1998/06/27 16:54:34  curt
534 // Replaced "extern displayInstruments" with a entry in fgOPTIONS.
535 // Don't change the view port when displaying the panel.
536 //
537 // Revision 1.12  1998/06/17 21:35:13  curt
538 // Refined conditional audio support compilation.
539 // Moved texture parameter setup calls to ../Scenery/materials.cxx
540 // #include <string.h> before various STL includes.
541 // Make HUD default state be enabled.
542 //
543 // Revision 1.11  1998/06/13 00:40:33  curt
544 // Tweaked fog command line options.
545 //
546 // Revision 1.10  1998/05/16 13:08:36  curt
547 // C++ - ified views.[ch]xx
548 // Shuffled some additional view parameters into the fgVIEW class.
549 // Changed tile-radius to tile-diameter because it is a much better
550 //   name.
551 // Added a WORLD_TO_EYE transformation to views.cxx.  This allows us
552 //  to transform world space to eye space for view frustum culling.
553 //
554 // Revision 1.9  1998/05/13 18:29:59  curt
555 // Added a keyboard binding to dynamically adjust field of view.
556 // Added a command line option to specify fov.
557 // Adjusted terrain color.
558 // Root path info moved to fgOPTIONS.
559 // Added ability to parse options out of a config file.
560 //
561 // Revision 1.8  1998/05/07 23:14:16  curt
562 // Added "D" key binding to set autopilot heading.
563 // Made frame rate calculation average out over last 10 frames.
564 // Borland C++ floating point exception workaround.
565 // Added a --tile-radius=n option.
566 //
567 // Revision 1.7  1998/05/06 03:16:25  curt
568 // Added an averaged global frame rate counter.
569 // Added an option to control tile radius.
570 //
571 // Revision 1.6  1998/05/03 00:47:32  curt
572 // Added an option to enable/disable full-screen mode.
573 //
574 // Revision 1.5  1998/04/30 12:34:19  curt
575 // Added command line rendering options:
576 //   enable/disable fog/haze
577 //   specify smooth/flat shading
578 //   disable sky blending and just use a solid color
579 //   enable wireframe drawing mode
580 //
581 // Revision 1.4  1998/04/28 01:20:22  curt
582 // Type-ified fgTIME and fgVIEW.
583 // Added a command line option to disable textures.
584 //
585 // Revision 1.3  1998/04/26 05:01:19  curt
586 // Added an rint() / HAVE_RINT check.
587 //
588 // Revision 1.2  1998/04/25 15:11:12  curt
589 // Added an command line option to set starting position based on airport ID.
590 //
591 // Revision 1.1  1998/04/24 00:49:21  curt
592 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
593 // Trying out some different option parsing code.
594 // Some code reorganization.
595 //
596 //