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