]> git.mxchange.org Git - flightgear.git/blob - Main/options.cxx
Tweaked fog command line options.
[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     // Features
71     hud_status = 0;
72
73     // Rendering options
74     fog = 2;    // nicest
75     fov = 65.0;
76     fullscreen = 0;
77     shading = 1;
78     skyblend = 1;
79     textures = 1;
80     wireframe = 0;
81
82     // Scenery options
83     tile_diameter = 7;
84
85     // Time options
86     time_offset = 0;
87 }
88
89
90 // Parse an int out of a --foo-bar=n type option 
91 static int parse_int(char *arg) {
92     int result;
93
94     // advance past the '='
95     while ( (arg[0] != '=') && (arg[0] != '\0') ) {
96         arg++;
97     }
98
99     if ( arg[0] == '=' ) {
100         arg++;
101     }
102
103     printf("parse_int(): arg = %s\n", arg);
104
105     result = atoi(arg);
106
107     printf("parse_int(): result = %d\n", result);
108
109     return(result);
110 }
111
112
113 // Parse an int out of a --foo-bar=n type option 
114 static double parse_double(char *arg) {
115     double result;
116
117     // advance past the '='
118     while ( (arg[0] != '=') && (arg[0] != '\0') ) {
119         arg++;
120     }
121
122     if ( arg[0] == '=' ) {
123         arg++;
124     }
125
126     printf("parse_double(): arg = %s\n", arg);
127
128     result = atof(arg);
129
130     printf("parse_double(): result = %.4f\n", result);
131
132     return(result);
133 }
134
135
136 // parse time string in the form of [+-]hh:mm:ss, returns the value in seconds
137 static double parse_time(char *time_str) {
138     char num[256];
139     double hours, minutes, seconds;
140     double result = 0.0;
141     int sign = 1;
142     int i;
143
144     // printf("parse_time(): %s\n", time_str);
145
146     // check for sign
147     if ( strlen(time_str) ) {
148         if ( time_str[0] == '+' ) {
149             sign = 1;
150             time_str++;
151         } else if ( time_str[0] == '-' ) {
152             sign = -1;
153             time_str++;
154         }
155     }
156     // printf("sign = %d\n", sign);
157
158     // get hours
159     if ( strlen(time_str) ) {
160         i = 0;
161         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
162             num[i] = time_str[0];
163             time_str++;
164             i++;
165         }
166         if ( time_str[0] == ':' ) {
167             time_str++;
168         }
169         num[i] = '\0';
170         hours = atof(num);
171         // printf("hours = %.2lf\n", hours);
172
173         result += hours * 3600.0;
174     }
175
176     // get minutes
177     if ( strlen(time_str) ) {
178         i = 0;
179         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
180             num[i] = time_str[0];
181             time_str++;
182             i++;
183         }
184         if ( time_str[0] == ':' ) {
185             time_str++;
186         }
187         num[i] = '\0';
188         minutes = atof(num);
189         // printf("minutes = %.2lf\n", minutes);
190
191         result += minutes * 60.0;
192     }
193
194     // get seconds
195     if ( strlen(time_str) ) {
196         i = 0;
197         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
198             num[i] = time_str[0];
199             time_str++;
200             i++;
201         }
202         num[i] = '\0';
203         seconds = atof(num);
204         // printf("seconds = %.2lf\n", seconds);
205
206         result += seconds;
207     }
208
209     return(sign * result);
210 }
211
212
213 // parse time offset command line option
214 static int parse_time_offset(char *time_str) {
215     int result;
216
217     time_str += 14;
218
219     // printf("time offset = %s\n", time_str);
220
221 #ifdef HAVE_RINT
222     result = (int)rint(parse_time(time_str));
223 #else
224     result = (int)parse_time(time_str);
225 #endif
226
227     printf("parse_time_offset(): %d\n", result);
228
229     return( result );
230 }
231
232
233 // Parse --tile-diameter=n type option 
234
235 #define FG_RADIUS_MIN 1
236 #define FG_RADIUS_MAX 4
237
238 static int parse_tile_radius(char *arg) {
239     int radius, tmp;
240
241     radius = parse_int(arg);
242
243     if ( radius < FG_RADIUS_MIN ) { radius = FG_RADIUS_MIN; }
244     if ( radius > FG_RADIUS_MAX ) { radius = FG_RADIUS_MAX; }
245
246     printf("parse_tile_radius(): radius = %d\n", radius);
247
248     return(radius);
249 }
250
251
252 // Parse --fov=x.xx type option 
253 static double parse_fov(char *arg) {
254     double fov;
255
256     fov = parse_double(arg);
257
258     if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
259     if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
260
261     printf("parse_fov(): result = %.4f\n", fov);
262
263     return(fov);
264 }
265
266
267 // Parse a single option
268 int fgOPTIONS::parse_option( char *arg ) {
269     // General Options
270     if ( (strcmp(arg, "--help") == 0) ||
271          (strcmp(arg, "-h") == 0) ) {
272         // help/usage request
273         return(FG_OPTIONS_HELP);
274     } else if ( strcmp(arg, "--disable-hud") == 0 ) {
275         hud_status = 0; 
276     } else if ( strcmp(arg, "--enable-hud") == 0 ) {
277         hud_status = 1; 
278     } else if ( strncmp(arg, "--airport-id=", 13) == 0 ) {
279         arg += 13;
280         strncpy(airport_id, arg, 4);
281     } else if ( strncmp(arg, "--fg-root=", 10) == 0 ) {
282         arg += 10;
283         strcpy(fg_root, arg);
284     } else if ( strcmp(arg, "--fog-disable") == 0 ) {
285         fog = 0;        
286     } else if ( strcmp(arg, "--fog-fastest") == 0 ) {
287         fog = 1;        
288     } else if ( strcmp(arg, "--fog-nicest") == 0 ) {
289         fog = 2;        
290     } else if ( strncmp(arg, "--fov=", 6) == 0 ) {
291         fov = parse_fov(arg);
292     } else if ( strcmp(arg, "--disable-fullscreen") == 0 ) {
293         fullscreen = 0; 
294     } else if ( strcmp(arg, "--enable-fullscreen") == 0 ) {
295         fullscreen = 1; 
296     } else if ( strcmp(arg, "--shading-flat") == 0 ) {
297         shading = 0;    
298     } else if ( strcmp(arg, "--shading-smooth") == 0 ) {
299         shading = 1;    
300     } else if ( strcmp(arg, "--disable-skyblend") == 0 ) {
301         skyblend = 0;   
302     } else if ( strcmp(arg, "--enable-skyblend") == 0 ) {
303         skyblend = 1;   
304     } else if ( strcmp(arg, "--disable-textures") == 0 ) {
305         textures = 0;   
306     } else if ( strcmp(arg, "--enable-textures") == 0 ) {
307         textures = 1;   
308     } else if ( strcmp(arg, "--disable-wireframe") == 0 ) {
309         wireframe = 0;  
310     } else if ( strcmp(arg, "--enable-wireframe") == 0 ) {
311         wireframe = 1;  
312     } else if ( strncmp(arg, "--tile-radius=", 14) == 0 ) {
313         tile_radius = parse_tile_radius(arg);
314         tile_diameter = tile_radius * 2 + 1;
315     } else if ( strncmp(arg, "--time-offset=", 14) == 0 ) {
316         time_offset = parse_time_offset(arg);
317     } else {
318         return(FG_OPTIONS_ERROR);
319     }
320     
321     return(FG_OPTIONS_OK);
322 }
323
324
325 // Parse the command line options
326 int fgOPTIONS::parse_command_line( int argc, char **argv ) {
327     int i = 1;
328     int result;
329
330     fgPrintf(FG_GENERAL, FG_INFO, "Processing command line arguments\n");
331
332     while ( i < argc ) {
333         fgPrintf(FG_GENERAL, FG_INFO, "argv[%d] = %s\n", i, argv[i]);
334
335         result = parse_option(argv[i]);
336         if ( (result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR) ) {
337             return(result);
338         }
339
340         i++;
341     }
342     
343     return(FG_OPTIONS_OK);
344 }
345
346
347 // Parse the command line options
348 int fgOPTIONS::parse_config_file( char *path ) {
349     char fgpath[256], line[256];
350     fgFile f;
351     int len, result;
352
353     strcpy(fgpath, path);
354     strcat(fgpath, ".gz");
355
356     // first try "path.gz"
357     if ( (f = fgopen(fgpath, "rb")) == NULL ) {
358         // next try "path"
359         if ( (f = fgopen(path, "rb")) == NULL ) {
360             return(FG_OPTIONS_ERROR);
361         }
362     }
363
364     fgPrintf(FG_GENERAL, FG_INFO, "Processing config file: %s\n", path);
365
366     while ( fggets(f, line, 250) != NULL ) {
367         // strip trailing newline if it exists
368         len = strlen(line);
369         if ( line[len-1] == '\n' ) {
370             line[len-1] = '\0';
371         }
372
373         result = parse_option(line);
374         if ( result == FG_OPTIONS_ERROR ) {
375             fgPrintf( FG_GENERAL, FG_EXIT, 
376                       "Config file parse error: %s '%s'\n", path, line );
377         }
378     }
379
380     fgclose(f);
381     return(FG_OPTIONS_OK);
382 }
383
384
385 // Print usage message
386 void fgOPTIONS::usage ( void ) {
387     printf("Usage: fg [ options ... ]\n");
388     printf("\n");
389
390     printf("General Options:\n");
391     printf("\t--help -h:  print usage\n");
392     printf("\t--fg-root=path:  specify the root path for all the data files\n");
393     printf("\n");
394
395     printf("Features:\n");
396     printf("\t--disable-hud:  disable heads up display\n");
397     printf("\t--enable-hud:  enable heads up display\n");
398     printf("\n");
399  
400     printf("Initial Position:\n");
401     printf("\t--airport-id=ABCD:  specify starting postion by airport id\n");
402     printf("\n");
403
404     printf("Rendering Options:\n");
405     printf("\t--fog-disable:  disable fog/haze\n");
406     printf("\t--fog-fastest:  enable fastest fog/haze\n");
407     printf("\t--fog-nicest:  enable nicest fog/haze\n");
408     printf("\t--fov=xx.x:  specify initial field of view angle in degrees\n");
409     printf("\t--disable-fullscreen:  disable fullscreen mode\n");
410     printf("\t--enable-fullscreen:  enable fullscreen mode\n");
411     printf("\t--shading-flat:  enable flat shading\n");
412     printf("\t--shading-smooth:  enable smooth shading\n");
413     printf("\t--disable-skyblend:  disable sky blending\n");
414     printf("\t--enable-skyblend:  enable sky blending\n");
415     printf("\t--disable-textures:  disable textures\n");
416     printf("\t--enable-textures:  enable textures\n");
417     printf("\t--disable-wireframe:  disable wireframe drawing mode\n");
418     printf("\t--enable-wireframe:  enable wireframe drawing mode\n");
419     printf("\n");
420
421     printf("Scenery Options:\n");
422     printf("\t--tile-radius=n:  specify tile radius, must be 1 - 4\n");
423     printf("\n");
424
425     printf("Time Options:\n");
426     printf("\t--time-offset=[+-]hh:mm:ss:  offset local time by this amount\n");
427 }
428
429
430 // Destructor
431 fgOPTIONS::~fgOPTIONS( void ) {
432 }
433
434
435 // $Log$
436 // Revision 1.11  1998/06/13 00:40:33  curt
437 // Tweaked fog command line options.
438 //
439 // Revision 1.10  1998/05/16 13:08:36  curt
440 // C++ - ified views.[ch]xx
441 // Shuffled some additional view parameters into the fgVIEW class.
442 // Changed tile-radius to tile-diameter because it is a much better
443 //   name.
444 // Added a WORLD_TO_EYE transformation to views.cxx.  This allows us
445 //  to transform world space to eye space for view frustum culling.
446 //
447 // Revision 1.9  1998/05/13 18:29:59  curt
448 // Added a keyboard binding to dynamically adjust field of view.
449 // Added a command line option to specify fov.
450 // Adjusted terrain color.
451 // Root path info moved to fgOPTIONS.
452 // Added ability to parse options out of a config file.
453 //
454 // Revision 1.8  1998/05/07 23:14:16  curt
455 // Added "D" key binding to set autopilot heading.
456 // Made frame rate calculation average out over last 10 frames.
457 // Borland C++ floating point exception workaround.
458 // Added a --tile-radius=n option.
459 //
460 // Revision 1.7  1998/05/06 03:16:25  curt
461 // Added an averaged global frame rate counter.
462 // Added an option to control tile radius.
463 //
464 // Revision 1.6  1998/05/03 00:47:32  curt
465 // Added an option to enable/disable full-screen mode.
466 //
467 // Revision 1.5  1998/04/30 12:34:19  curt
468 // Added command line rendering options:
469 //   enable/disable fog/haze
470 //   specify smooth/flat shading
471 //   disable sky blending and just use a solid color
472 //   enable wireframe drawing mode
473 //
474 // Revision 1.4  1998/04/28 01:20:22  curt
475 // Type-ified fgTIME and fgVIEW.
476 // Added a command line option to disable textures.
477 //
478 // Revision 1.3  1998/04/26 05:01:19  curt
479 // Added an rint() / HAVE_RINT check.
480 //
481 // Revision 1.2  1998/04/25 15:11:12  curt
482 // Added an command line option to set starting position based on airport ID.
483 //
484 // Revision 1.1  1998/04/24 00:49:21  curt
485 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
486 // Trying out some different option parsing code.
487 // Some code reorganization.
488 //
489 //