]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_init.cxx
- adjusted for no-value constructor for FGPanel
[flightgear.git] / src / Main / fg_init.cxx
1 // fg_init.cxx -- Flight Gear top level initialization routines
2 //
3 // Written by Curtis Olson, started August 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 // For BC 5.01 this must be included before OpenGL includes.
30 #ifdef SG_MATH_EXCEPTION_CLASH
31 #  include <math.h>
32 #endif
33
34 #ifdef HAVE_WINDOWS_H
35 #  include <windows.h>
36 #endif
37
38 #include <GL/glut.h>
39
40 #include <stdio.h>
41 #include <stdlib.h>
42
43
44 #if defined( unix ) || defined( __CYGWIN__ )
45 #  include <unistd.h>           // for gethostname()
46 #endif
47
48 // work around a stdc++ lib bug in some versions of linux, but doesn't
49 // seem to hurt to have this here for all versions of Linux.
50 #ifdef linux
51 #  define _G_NO_EXTERN_TEMPLATES
52 #endif
53
54 #include <simgear/compiler.h>
55
56 #include STL_STRING
57
58 #include <simgear/constants.h>
59 #include <simgear/debug/logstream.hxx>
60 #include <simgear/math/point3d.hxx>
61 #include <simgear/math/polar3d.hxx>
62 #include <simgear/math/sg_geodesy.hxx>
63 #include <simgear/misc/sg_path.hxx>
64 #include <simgear/timing/sg_time.hxx>
65
66 #include <Aircraft/aircraft.hxx>
67 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
68 #include <Airports/runways.hxx>
69 #include <Airports/simple.hxx>
70 #include <Autopilot/auto_gui.hxx>
71 #include <Autopilot/newauto.hxx>
72 #include <Cockpit/cockpit.hxx>
73 #include <Cockpit/radiostack.hxx>
74 #include <Cockpit/panel.hxx>
75 #include <Cockpit/panel_io.hxx>
76 #include <FDM/ADA.hxx>
77 #include <FDM/Balloon.h>
78 #include <FDM/External.hxx>
79 #include <FDM/JSBSim.hxx>
80 #include <FDM/LaRCsim.hxx>
81 #include <FDM/MagicCarpet.hxx>
82 #include <Include/general.hxx>
83 #include <Input/input.hxx>
84 // #include <Joystick/joystick.hxx>
85 #include <Objects/matlib.hxx>
86 #include <Navaids/fixlist.hxx>
87 #include <Navaids/ilslist.hxx>
88 #include <Navaids/mkrbeacons.hxx>
89 #include <Navaids/navlist.hxx>
90 #include <Scenery/scenery.hxx>
91 #include <Scenery/tilemgr.hxx>
92 #include <Time/event.hxx>
93 #include <Time/light.hxx>
94 #include <Time/sunpos.hxx>
95 #include <Time/moonpos.hxx>
96 #include <Time/tmp.hxx>
97
98 #ifndef FG_OLD_WEATHER
99 #  include <WeatherCM/FGLocalWeatherDatabase.h>
100 #else
101 #  include <Weather/weather.hxx>
102 #endif
103
104 #include "fg_init.hxx"
105 #include "fg_io.hxx"
106 #include "fg_commands.hxx"
107 #include "fg_props.hxx"
108 #include "options.hxx"
109 #include "globals.hxx"
110
111 #if defined(FX) && defined(XMESA)
112 #include <GL/xmesa.h>
113 #endif
114
115 SG_USING_STD(string);
116
117 extern const char *default_root;
118
119
120 // Read in configuration (file and command line) and just set fg_root
121 bool fgInitFGRoot ( int argc, char **argv ) {
122     string root;
123     char* envp;
124
125     // First parse command line options looking for fg-root, this will
126     // override anything specified in a config file
127     root = fgScanForRoot(argc, argv);
128
129 #if defined( unix ) || defined( __CYGWIN__ )
130     // Next check home directory for .fgfsrc.hostname file
131     if ( root == "" ) {
132         envp = ::getenv( "HOME" );
133         if ( envp != NULL ) {
134             SGPath config( envp );
135             config.append( ".fgfsrc" );
136             char name[256];
137             gethostname( name, 256 );
138             config.concat( "." );
139             config.concat( name );
140             root = fgScanForRoot(config.str());
141         }
142     }
143 #endif
144
145     // Next check home directory for .fgfsrc file
146     if ( root == "" ) {
147         envp = ::getenv( "HOME" );
148         if ( envp != NULL ) {
149             SGPath config( envp );
150             config.append( ".fgfsrc" );
151             root = fgScanForRoot(config.str());
152         }
153     }
154     
155     // Next check if fg-root is set as an env variable
156     if ( root == "" ) {
157         envp = ::getenv( "FG_ROOT" );
158         if ( envp != NULL ) {
159             root = envp;
160         }
161     }
162
163     // Otherwise, default to a random compiled-in location if we can't
164     // find fg-root any other way.
165     if ( root == "" ) {
166 #if defined( __CYGWIN__ )
167         root = "/FlightGear";
168 #elif defined( WIN32 )
169         root = "\\FlightGear";
170 #elif defined( macintosh )
171         root = "";
172 #else
173         root = PKGLIBDIR;
174 #endif
175     }
176
177     SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
178     globals->set_fg_root(root);
179
180     return true;
181 }
182
183
184 // Read in configuration (file and command line)
185 bool fgInitConfig ( int argc, char **argv ) {
186
187                                 // First, set some sane default values
188     fgSetDefaults();
189
190     // Read global preferences from $FG_ROOT/preferences.xml
191     SGPath props_path(globals->get_fg_root());
192     props_path.append("preferences.xml");
193     SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
194     if (!readProperties(props_path.str(), globals->get_props())) {
195       SG_LOG(SG_INPUT, SG_ALERT, "Failed to read global preferences from "
196              << props_path.str());
197     } else {
198       SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
199     }
200
201     // Attempt to locate and parse the various config files in order
202     // from least precidence to greatest precidence
203
204     // Check for $fg_root/system.fgfsrc
205     SGPath config( globals->get_fg_root() );
206     config.append( "system.fgfsrc" );
207     fgParseOptions(config.str());
208
209     char name[256];
210 #if defined( unix ) || defined( __CYGWIN__ )
211     // Check for $fg_root/system.fgfsrc.hostname
212     gethostname( name, 256 );
213     config.concat( "." );
214     config.concat( name );
215     fgParseOptions(config.str());
216 #endif
217
218     // Check for ~/.fgfsrc
219     char* envp = ::getenv( "HOME" );
220     if ( envp != NULL ) {
221         config.set( envp );
222         config.append( ".fgfsrc" );
223         fgParseOptions(config.str());
224     }
225
226 #if defined( unix ) || defined( __CYGWIN__ )
227     // Check for ~/.fgfsrc.hostname
228     gethostname( name, 256 );
229     config.concat( "." );
230     config.concat( name );
231     fgParseOptions(config.str());
232 #endif
233
234     // Parse remaining command line options
235     // These will override anything specified in a config file
236     fgParseOptions(argc, argv);
237
238     return true;
239 }
240
241
242 // find basic airport location info from airport database
243 bool fgFindAirportID( const string& id, FGAirport *a ) {
244     if ( id.length() ) {
245         SGPath path( globals->get_fg_root() );
246         path.append( "Airports" );
247         path.append( "simple.mk4" );
248         FGAirports airports( path.c_str() );
249
250         SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
251
252         if ( ! airports.search( id, a ) ) {
253             SG_LOG( SG_GENERAL, SG_ALERT,
254                     "Failed to find " << id << " in " << path.str() );
255             return false;
256         }
257     } else {
258         return false;
259     }
260
261     SG_LOG( SG_GENERAL, SG_INFO,
262             "Position for " << id << " is ("
263             << a->longitude << ", "
264             << a->latitude << ")" );
265
266     return true;
267 }
268
269
270 // Set current_options lon/lat given an airport id
271 bool fgSetPosFromAirportID( const string& id ) {
272     FGAirport a;
273     // double lon, lat;
274
275     SG_LOG( SG_GENERAL, SG_INFO,
276             "Attempting to set starting position from airport code " << id );
277
278     if ( fgFindAirportID( id, &a ) ) {
279         fgSetDouble("/position/longitude",  a.longitude );
280         fgSetDouble("/position/latitude",  a.latitude );
281         SG_LOG( SG_GENERAL, SG_INFO,
282                 "Position for " << id << " is ("
283                 << a.longitude << ", "
284                 << a.latitude << ")" );
285
286         return true;
287     } else {
288         return false;
289     }
290
291 }
292
293
294 // Set current_options lon/lat given an airport id and heading (degrees)
295 bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
296     FGRunway r;
297     FGRunway found_r;
298     double found_dir = 0.0;
299
300     if ( id.length() ) {
301         // set initial position from runway and heading
302
303         SGPath path( globals->get_fg_root() );
304         path.append( "Airports" );
305         path.append( "runways.mk4" );
306         FGRunways runways( path.c_str() );
307
308         SG_LOG( SG_GENERAL, SG_INFO,
309                 "Attempting to set starting position from runway code "
310                 << id << " heading " << tgt_hdg );
311
312         // SGPath inpath( globals->get_fg_root() );
313         // inpath.append( "Airports" );
314         // inpath.append( "apt_simple" );
315         // airports.load( inpath.c_str() );
316
317         // SGPath outpath( globals->get_fg_root() );
318         // outpath.append( "Airports" );
319         // outpath.append( "simple.gdbm" );
320         // airports.dump_gdbm( outpath.c_str() );
321
322         if ( ! runways.search( id, &r ) ) {
323             SG_LOG( SG_GENERAL, SG_ALERT,
324                     "Failed to find " << id << " in database." );
325             return false;
326         }
327
328         double diff;
329         double min_diff = 360.0;
330
331         while ( r.id == id ) {
332             // forward direction
333             diff = tgt_hdg - r.heading;
334             while ( diff < -180.0 ) { diff += 360.0; }
335             while ( diff >  180.0 ) { diff -= 360.0; }
336             diff = fabs(diff);
337             SG_LOG( SG_GENERAL, SG_INFO,
338                     "Runway " << r.rwy_no << " heading = " << r.heading <<
339                     " diff = " << diff );
340             if ( diff < min_diff ) {
341                 min_diff = diff;
342                 found_r = r;
343                 found_dir = 0;
344             }
345
346             // reverse direction
347             diff = tgt_hdg - r.heading - 180.0;
348             while ( diff < -180.0 ) { diff += 360.0; }
349             while ( diff >  180.0 ) { diff -= 360.0; }
350             diff = fabs(diff);
351             SG_LOG( SG_GENERAL, SG_INFO,
352                     "Runway -" << r.rwy_no << " heading = " <<
353                     r.heading + 180.0 <<
354                     " diff = " << diff );
355             if ( diff < min_diff ) {
356                 min_diff = diff;
357                 found_r = r;
358                 found_dir = 180.0;
359             }
360
361             runways.next( &r );
362         }
363
364         SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << found_r.rwy_no
365                 << " + " << found_dir );
366
367     } else {
368         return false;
369     }
370
371     double heading = found_r.heading + found_dir;
372     while ( heading >= 360.0 ) { heading -= 360.0; }
373
374     double lat2, lon2, az2;
375     double azimuth = found_r.heading + found_dir + 180.0;
376     while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
377
378     SG_LOG( SG_GENERAL, SG_INFO,
379             "runway =  " << found_r.lon << ", " << found_r.lat
380             << " length = " << found_r.length * SG_FEET_TO_METER * 0.5 
381             << " heading = " << azimuth );
382     
383     geo_direct_wgs_84 ( 0, found_r.lat, found_r.lon, 
384                         azimuth, found_r.length * SG_FEET_TO_METER * 0.5 - 5.0,
385                         &lat2, &lon2, &az2 );
386
387     if ( fabs( fgGetDouble("/sim/startup/offset-distance") ) > SG_EPSILON ) {
388         double olat, olon;
389         double odist = fgGetDouble("/sim/startup/offset-distance");
390         odist *= SG_NM_TO_METER;
391         double oaz = azimuth;
392         if ( fabs(fgGetDouble("/sim/startup/offset-azimuth")) > SG_EPSILON ) {
393             oaz = fgGetDouble("/sim/startup/offset-azimuth") + 180;
394         }
395         while ( oaz >= 360.0 ) { oaz -= 360.0; }
396         geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
397         lat2=olat;
398         lon2=olon;
399     }
400     fgSetDouble("/position/longitude",  lon2 );
401     fgSetDouble("/position/latitude",  lat2 );
402     fgSetDouble("/orientation/heading", heading );
403
404     SG_LOG( SG_GENERAL, SG_INFO,
405             "Position for " << id << " is ("
406             << lon2 << ", "
407             << lat2 << ") new heading is "
408             << heading );
409
410     return true;
411 }
412
413
414 // General house keeping initializations
415 bool fgInitGeneral( void ) {
416     string root;
417
418 #if defined(FX) && defined(XMESA)
419     char *mesa_win_state;
420 #endif
421
422     SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
423     SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
424
425     root = globals->get_fg_root();
426     if ( ! root.length() ) {
427         // No root path set? Then bail ...
428         SG_LOG( SG_GENERAL, SG_ALERT,
429                 "Cannot continue without environment variable FG_ROOT"
430                 << "being defined." );
431         exit(-1);
432     }
433     SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
434
435 #if defined(FX) && defined(XMESA)
436     // initialize full screen flag
437     global_fullscreen = false;
438     if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
439         // Test for the MESA_GLX_FX env variable
440         if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
441             // test if we are fullscreen mesa/glide
442             if ( (mesa_win_state[0] == 'f') ||
443                  (mesa_win_state[0] == 'F') ) {
444                 global_fullscreen = true;
445             }
446         }
447     }
448 #endif
449
450     return true;
451 }
452
453
454 // This is the top level init routine which calls all the other
455 // initialization routines.  If you are adding a subsystem to flight
456 // gear, its initialization call should located in this routine.
457 // Returns non-zero if a problem encountered.
458 bool fgInitSubsystems( void ) {
459     fgLIGHT *l = &cur_light_params;
460
461     SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
462     SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
463
464
465     ////////////////////////////////////////////////////////////////////
466     // Initialize the material property subsystem.
467     ////////////////////////////////////////////////////////////////////
468
469     SGPath mpath( globals->get_fg_root() );
470     mpath.append( "materials" );
471     if ( material_lib.load( mpath.str() ) ) {
472     } else {
473         SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
474         exit(-1);
475     }
476
477
478     ////////////////////////////////////////////////////////////////////
479     // Initialize the scenery management subsystem.
480     ////////////////////////////////////////////////////////////////////
481
482     if ( fgSceneryInit() ) {
483         // Material lib initialized ok.
484     } else {
485         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Scenery initialization!" );
486         exit(-1);
487     }
488
489     if ( global_tile_mgr.init() ) {
490         // Load the local scenery data
491         global_tile_mgr.update( fgGetDouble("/position/longitude"),
492                                 fgGetDouble("/position/latitude") );
493     } else {
494         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
495         exit(-1);
496     }
497
498     SG_LOG( SG_GENERAL, SG_DEBUG,
499             "Current terrain elevation after tile mgr init " <<
500             scenery.cur_elev );
501
502
503     ////////////////////////////////////////////////////////////////////
504     // Initialize the flight model subsystem.
505     ////////////////////////////////////////////////////////////////////
506
507     double dt = 1.0 / fgGetInt("/sim/model-hz");
508     // cout << "dt = " << dt << endl;
509
510     aircraft_dir = fgGetString("/sim/aircraft-dir");
511     const string &model = fgGetString("/sim/flight-model");
512     if (model == "larcsim") {
513         cur_fdm_state = new FGLaRCsim( dt );
514     } else if (model == "jsb") {
515         cur_fdm_state = new FGJSBsim( dt );
516     } else if (model == "ada") {
517         cur_fdm_state = new FGADA( dt );
518     } else if (model == "balloon") {
519         cur_fdm_state = new FGBalloonSim( dt );
520     } else if (model == "magic") {
521         cur_fdm_state = new FGMagicCarpet( dt );
522     } else if (model == "external") {
523         cur_fdm_state = new FGExternal( dt );
524     } else {
525         SG_LOG(SG_GENERAL, SG_ALERT,
526                "Unrecognized flight model '" << model
527                << ", can't init aircraft");
528         exit(-1);
529     }
530     cur_fdm_state->init();
531     cur_fdm_state->bind();
532     
533     // allocates structures so must happen before any of the flight
534     // model or control parameters are set
535     fgAircraftInit();   // In the future this might not be the case.
536
537
538     ////////////////////////////////////////////////////////////////////
539     // Initialize the event manager subsystem.
540     ////////////////////////////////////////////////////////////////////
541
542     global_events.Init();
543
544     // Output event stats every 60 seconds
545     global_events.Register( "fgEVENT_MGR::PrintStats()",
546                             fgMethodCallback<fgEVENT_MGR>( &global_events,
547                                                    &fgEVENT_MGR::PrintStats),
548                             fgEVENT::FG_EVENT_READY, 60000 );
549
550
551     ////////////////////////////////////////////////////////////////////
552     // Initialize the view manager subsystem.
553     ////////////////////////////////////////////////////////////////////
554
555     // Initialize win_ratio parameters
556     for ( int i = 0; i < globals->get_viewmgr()->size(); ++i ) {
557         globals->get_viewmgr()->get_view(i)->
558             set_win_ratio( fgGetInt("/sim/startup/xsize") /
559                            fgGetInt("/sim/startup/ysize") );
560     }
561
562     // Initialize pilot view
563     FGViewerRPH *pilot_view =
564         (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
565
566     pilot_view->set_geod_view_pos( cur_fdm_state->get_Longitude(), 
567                                    cur_fdm_state->get_Lat_geocentric(), 
568                                    cur_fdm_state->get_Altitude() *
569                                    SG_FEET_TO_METER );
570     pilot_view->set_sea_level_radius( cur_fdm_state->get_Sea_level_radius() *
571                                       SG_FEET_TO_METER ); 
572     pilot_view->set_rph( cur_fdm_state->get_Phi(),
573                          cur_fdm_state->get_Theta(),
574                          cur_fdm_state->get_Psi() );
575
576     // set current view to 0 (first) which is our main pilot view
577     globals->set_current_view( pilot_view );
578
579     SG_LOG( SG_GENERAL, SG_DEBUG, "  abs_view_pos = "
580             << globals->get_current_view()->get_abs_view_pos());
581
582
583     ////////////////////////////////////////////////////////////////////
584     // Initialize the lighting subsystem.
585     ////////////////////////////////////////////////////////////////////
586
587     // fgUpdateSunPos() needs a few position and view parameters set
588     // so it can calculate local relative sun angle and a few other
589     // things for correctly orienting the sky.
590     fgUpdateSunPos();
591     fgUpdateMoonPos();
592     global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
593                             fgEVENT::FG_EVENT_READY, 60000);
594     global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
595                             fgEVENT::FG_EVENT_READY, 60000);
596
597     // Initialize Lighting interpolation tables
598     l->Init();
599
600     // update the lighting parameters (based on sun angle)
601     global_events.Register( "fgLight::Update()",
602                             fgMethodCallback<fgLIGHT>( &cur_light_params,
603                                                        &fgLIGHT::Update),
604                             fgEVENT::FG_EVENT_READY, 30000 );
605
606
607     ////////////////////////////////////////////////////////////////////
608     // Initialize the local time subsystem.
609     ////////////////////////////////////////////////////////////////////
610
611     // update the current timezone each 30 minutes
612     global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
613                             fgEVENT::FG_EVENT_READY, 1800000);
614
615
616     ////////////////////////////////////////////////////////////////////
617     // Initialize the weather subsystem.
618     ////////////////////////////////////////////////////////////////////
619
620     // Initialize the weather modeling subsystem
621 #ifndef FG_OLD_WEATHER
622     // Initialize the WeatherDatabase
623     SG_LOG(SG_GENERAL, SG_INFO, "Creating LocalWeatherDatabase");
624     sgVec3 position;
625     sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
626                current_aircraft.fdm_state->get_Longitude(),
627                current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER );
628     FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 
629         new FGLocalWeatherDatabase( position,
630                                     globals->get_fg_root() );
631     // cout << theFGLocalWeatherDatabase << endl;
632     // cout << "visibility = " 
633     //      << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
634
635     WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
636     
637     double init_vis = fgGetDouble("/environment/visibility");
638     if ( init_vis > 0 ) {
639         WeatherDatabase->setWeatherVisibility( init_vis );
640     }
641
642     // register the periodic update of the weather
643     global_events.Register( "weather update", fgUpdateWeatherDatabase,
644                             fgEVENT::FG_EVENT_READY, 30000);
645 #else
646     current_weather.Init();
647 #endif
648
649     ////////////////////////////////////////////////////////////////////
650     // Initialize vor/ndb/ils/fix list management and query systems
651     ////////////////////////////////////////////////////////////////////
652
653     SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaids");
654
655     SG_LOG(SG_GENERAL, SG_INFO, "  VOR/NDB");
656     current_navlist = new FGNavList;
657     SGPath p_nav( globals->get_fg_root() );
658     p_nav.append( "Navaids/default.nav" );
659     current_navlist->init( p_nav );
660
661     SG_LOG(SG_GENERAL, SG_INFO, "  ILS and Marker Beacons");
662     current_beacons = new FGMarkerBeacons;
663     current_beacons->init();
664     current_ilslist = new FGILSList;
665     SGPath p_ils( globals->get_fg_root() );
666     p_ils.append( "Navaids/default.ils" );
667     current_ilslist->init( p_ils );
668
669     SG_LOG(SG_GENERAL, SG_INFO, "  Fixes");
670     current_fixlist = new FGFixList;
671     SGPath p_fix( globals->get_fg_root() );
672     p_fix.append( "Navaids/default.fix" );
673     current_fixlist->init( p_fix );
674
675
676     ////////////////////////////////////////////////////////////////////
677     // Initialize the built-in commands.
678     ////////////////////////////////////////////////////////////////////
679     fgInitCommands();
680
681
682     ////////////////////////////////////////////////////////////////////
683     // Initialize the radio stack subsystem.
684     ////////////////////////////////////////////////////////////////////
685
686                                 // A textbook example of how FGSubsystem
687                                 // should work...
688     current_radiostack = new FGRadioStack;
689     current_radiostack->init();
690     current_radiostack->bind();
691
692
693     ////////////////////////////////////////////////////////////////////
694     // Initialize the cockpit subsystem
695     ////////////////////////////////////////////////////////////////////
696
697     if( fgCockpitInit( &current_aircraft )) {
698         // Cockpit initialized ok.
699     } else {
700         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Cockpit initialization!" );
701         exit(-1);
702     }
703
704
705     ////////////////////////////////////////////////////////////////////
706     // Initialize the joystick subsystem.
707     ////////////////////////////////////////////////////////////////////
708
709     // if ( ! fgJoystickInit() ) {
710     //   SG_LOG( SG_GENERAL, SG_ALERT, "Error in Joystick initialization!" );
711     // }
712
713
714     ////////////////////////////////////////////////////////////////////
715     // Initialize the autopilot subsystem.
716     ////////////////////////////////////////////////////////////////////
717
718     current_autopilot = new FGAutopilot;
719     current_autopilot->init();
720
721     // initialize the gui parts of the autopilot
722     NewTgtAirportInit();
723     fgAPAdjustInit() ;
724     NewHeadingInit();
725     NewAltitudeInit();
726
727     
728     ////////////////////////////////////////////////////////////////////
729     // Initialize I/O subsystem.
730     ////////////////////////////////////////////////////////////////////
731
732 #if ! defined( macintosh )
733     fgIOInit();
734 #endif
735
736     // Initialize the 2D panel.
737     string panel_path = fgGetString("/sim/panel/path",
738                                     "Panels/Default/default.xml");
739     current_panel = fgReadPanel(panel_path);
740     if (current_panel == 0) {
741         SG_LOG( SG_INPUT, SG_ALERT, 
742                 "Error reading new panel from " << panel_path );
743     } else {
744         SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
745         current_panel->init();
746         current_panel->bind();
747     }
748
749     
750     ////////////////////////////////////////////////////////////////////
751     // Initialize the default (kludged) properties.
752     ////////////////////////////////////////////////////////////////////
753
754     fgInitProps();
755
756
757     ////////////////////////////////////////////////////////////////////
758     // Initialize the controls subsystem.
759     ////////////////////////////////////////////////////////////////////
760
761     controls.init();
762     controls.bind();
763
764
765     ////////////////////////////////////////////////////////////////////
766     // Initialize the input subsystem.
767     ////////////////////////////////////////////////////////////////////
768
769     current_input.init();
770     current_input.bind();
771
772
773     ////////////////////////////////////////////////////////////////////////
774     // End of subsystem initialization.
775     ////////////////////////////////////////////////////////////////////
776
777     SG_LOG( SG_GENERAL, SG_INFO, endl);
778
779                                 // Save the initial state for future
780                                 // reference.
781     globals->saveInitialState();
782
783     return true;
784 }
785
786
787 void fgReInitSubsystems( void )
788 {
789     SG_LOG( SG_GENERAL, SG_INFO,
790             "/position/altitude = " << fgGetDouble("/position/altitude") );
791
792     bool freeze = globals->get_freeze();
793     if( !freeze )
794         globals->set_freeze( true );
795     
796     // Initialize the Scenery Management subsystem
797     if ( ! fgSceneryInit() ) {
798         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Scenery initialization!" );
799         exit(-1);
800     }
801
802     if( global_tile_mgr.init() ) {
803         // Load the local scenery data
804         global_tile_mgr.update( fgGetDouble("/position/longitude"),
805                                 fgGetDouble("/position/latitude") );
806     } else {
807         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
808                 exit(-1);
809     }
810
811     // Initialize view parameters
812     FGViewerRPH *pilot_view =
813         (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
814
815     pilot_view->set_view_offset( 0.0 );
816     pilot_view->set_goal_view_offset( 0.0 );
817
818     pilot_view->set_geod_view_pos( cur_fdm_state->get_Longitude(), 
819                                    cur_fdm_state->get_Lat_geocentric(), 
820                                    cur_fdm_state->get_Altitude() *
821                                    SG_FEET_TO_METER );
822     pilot_view->set_sea_level_radius( cur_fdm_state->get_Sea_level_radius() *
823                                       SG_FEET_TO_METER ); 
824     pilot_view->set_rph( cur_fdm_state->get_Phi(),
825                          cur_fdm_state->get_Theta(),
826                          cur_fdm_state->get_Psi() );
827
828     // set current view to 0 (first) which is our main pilot view
829     globals->set_current_view( pilot_view );
830
831     SG_LOG( SG_GENERAL, SG_DEBUG, "  abs_view_pos = "
832             << globals->get_current_view()->get_abs_view_pos());
833
834     cur_fdm_state->init();
835
836     controls.reset_all();
837     current_autopilot->reset();
838
839     fgUpdateSunPos();
840     fgUpdateMoonPos();
841     cur_light_params.Update();
842     fgUpdateLocalTime();
843
844     if( !freeze )
845         globals->set_freeze( false );
846 }