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