1 // main.cxx -- top level sim routines
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 - 2002 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <simgear/compiler.h>
33 #include <osg/GraphicsContext>
34 #include <osgDB/Registry>
37 #include <simgear/canvas/VGInitOperation.hxx>
38 #include <simgear/scene/model/modellib.hxx>
39 #include <simgear/scene/material/matlib.hxx>
40 #include <simgear/props/AtomicChangeListener.hxx>
41 #include <simgear/props/props.hxx>
42 #include <simgear/timing/sg_time.hxx>
43 #include <simgear/io/raw_socket.hxx>
44 #include <simgear/scene/tsync/terrasync.hxx>
45 #include <simgear/math/SGMath.hxx>
46 #include <simgear/math/sg_random.h>
48 #include <Model/panelnode.hxx>
49 #include <Scenery/scenery.hxx>
50 #include <Scenery/tilemgr.hxx>
51 #include <Sound/soundmanager.hxx>
52 #include <Time/TimeManager.hxx>
54 #include <Viewer/splash.hxx>
55 #include <Viewer/renderer.hxx>
56 #include <Viewer/WindowSystemAdapter.hxx>
57 #include <Navaids/NavDataCache.hxx>
59 #include "fg_commands.hxx"
63 #include "fg_init.hxx"
65 #include "fg_props.hxx"
66 #include "positioninit.hxx"
67 #include "subsystemFactory.hxx"
69 using namespace flightgear;
74 // The atexit() function handler should know when the graphical subsystem
76 extern int _bootstrap_OSInit;
78 static SGPropertyNode_ptr frame_signal;
79 static TimeManager* timeMgr;
81 // What should we do when we have nothing else to do? Let's get ready
82 // for the next move and update the display?
83 static void fgMainLoop( void )
85 frame_signal->fireValueChanged();
87 SG_LOG( SG_GENERAL, SG_DEBUG, "Running Main Loop");
88 SG_LOG( SG_GENERAL, SG_DEBUG, "======= ==== ====");
90 // compute simulated time (allowing for pause, warp, etc) and
92 double sim_dt, real_dt;
93 timeMgr->computeTimeDeltas(sim_dt, real_dt);
95 // update all subsystems
96 globals->get_subsystem_mgr()->update(sim_dt);
98 simgear::AtomicChangeListener::fireChangeListeners();
100 SG_LOG( SG_GENERAL, SG_DEBUG, "" );
104 static void registerMainLoop()
106 // stash current frame signal property
107 frame_signal = fgGetNode("/sim/signals/frame", true);
108 timeMgr = (TimeManager*) globals->get_subsystem("time");
109 fgRegisterIdleHandler( fgMainLoop );
112 // This is the top level master main function that is registered as
115 // The first few passes take care of initialization things (a couple
116 // per pass) and once everything has been initialized fgMainLoop from
119 static void fgIdleFunction ( void ) {
120 // Specify our current idle function state. This is used to run all
121 // our initializations out of the idle callback so that we can get a
122 // splash screen up and running right away.
123 static int idle_state = 0;
125 if ( idle_state == 0 ) {
129 fgSplashProgress("loading-aircraft-list");
132 } else if ( idle_state == 2 ) {
134 fgSplashProgress("loading-nav-dat");
136 } else if ( idle_state == 3 ) {
138 bool done = fgInitNav();
141 fgSplashProgress("init-scenery");
143 fgSplashProgress("loading-nav-dat");
146 } else if ( idle_state == 4 ) {
148 // based on the requested presets, calculate the true starting
150 flightgear::initPosition();
151 flightgear::initTowerLocationListener();
153 TimeManager* t = new TimeManager;
154 globals->add_subsystem("time", t, SGSubsystemMgr::INIT);
155 t->init(); // need to init now, not during initSubsystems
157 // Do some quick general initializations
158 if( !fgInitGeneral()) {
159 SG_LOG( SG_GENERAL, SG_ALERT,
160 "General initialization failed ..." );
164 ////////////////////////////////////////////////////////////////////
165 // Initialize the property-based built-in commands
166 ////////////////////////////////////////////////////////////////////
169 flightgear::registerSubsystemCommands(globals->get_commands());
171 ////////////////////////////////////////////////////////////////////
172 // Initialize the material manager
173 ////////////////////////////////////////////////////////////////////
174 globals->set_matlib( new SGMaterialLib );
175 simgear::SGModelLib::init(globals->get_fg_root(), globals->get_props());
176 simgear::SGModelLib::setPanelFunc(FGPanelNode::load);
178 ////////////////////////////////////////////////////////////////////
179 // Initialize the TG scenery subsystem.
180 ////////////////////////////////////////////////////////////////////
181 simgear::SGTerraSync* terra_sync = new simgear::SGTerraSync(globals->get_props());
182 globals->add_subsystem("terrasync", terra_sync);
183 globals->set_scenery( new FGScenery );
184 globals->get_scenery()->init();
185 globals->get_scenery()->bind();
186 globals->set_tile_mgr( new FGTileMgr );
188 fgSplashProgress("loading-aircraft");
190 } else if ( idle_state == 6 ) {
192 fgSplashProgress("creating-subsystems");
194 } else if ( idle_state == 7 ) {
198 fgCreateSubsystems();
199 SG_LOG(SG_GENERAL, SG_INFO, "Creating subsystems took:" << st.elapsedMSec());
200 fgSplashProgress("binding-subsystems");
202 } else if ( idle_state == 8 ) {
206 globals->get_subsystem_mgr()->bind();
207 SG_LOG(SG_GENERAL, SG_INFO, "Binding subsystems took:" << st.elapsedMSec());
209 fgSplashProgress("init-subsystems");
210 } else if ( idle_state == 9 ) {
211 SGSubsystem::InitStatus status = globals->get_subsystem_mgr()->incrementalInit();
212 if ( status == SGSubsystem::INIT_DONE) {
214 fgSplashProgress("finishing-subsystems");
216 fgSplashProgress("init-subsystems");
219 } else if ( idle_state == 10 ) {
221 fgPostInitSubsystems();
222 } else if ( idle_state == 800 ) {
223 if (flightgear::finalizePosition()) {
225 fgSplashProgress("init-graphics");
227 fgSplashProgress("finalize-position");
229 } else if ( idle_state == 900 ) {
232 // setup OpenGL view parameters
233 globals->get_renderer()->setupView();
235 globals->get_renderer()->resize( fgGetInt("/sim/startup/xsize"),
236 fgGetInt("/sim/startup/ysize") );
237 WindowSystemAdapter::getWSA()->windows[0]->gc->add(
238 new simgear::canvas::VGInitOperation()
241 int session = fgGetInt("/sim/session",0);
243 fgSetInt("/sim/session",session);
246 if ( idle_state == 1000 ) {
247 // We've finished all our initialization steps, from now on we
248 // run the main loop.
249 fgSetBool("sim/sceneryloaded", false);
254 static void upper_case_property(const char *name)
256 using namespace simgear;
257 SGPropertyNode *p = fgGetNode(name, false);
259 p = fgGetNode(name, true);
260 p->setStringValue("");
262 props::Type t = p->getType();
263 if (t == props::NONE || t == props::UNSPECIFIED)
264 p->setStringValue("");
266 assert(t == props::STRING);
268 p->addChangeListener(new FGMakeUpperCase);
272 // Main top level initialization
273 int fgMainInit( int argc, char **argv ) {
275 // set default log levels
276 sglog().setLogLevels( SG_ALL, SG_ALERT );
279 #ifdef FLIGHTGEAR_VERSION
280 version = FLIGHTGEAR_VERSION;
282 version = "unknown version";
284 SG_LOG( SG_GENERAL, SG_INFO, "FlightGear: Version "
286 SG_LOG( SG_GENERAL, SG_INFO, "Built with " << SG_COMPILER_STR << std::endl );
288 // Allocate global data structures. This needs to happen before
289 // we parse command line options
291 globals = new FGGlobals;
293 // seed the random number generator
296 string_list *col = new string_list;
297 globals->set_channel_options_list( col );
299 fgValidatePath("", false); // initialize static variables
300 upper_case_property("/sim/presets/airport-id");
301 upper_case_property("/sim/presets/runway");
302 upper_case_property("/sim/tower/airport-id");
303 upper_case_property("/autopilot/route-manager/input");
305 // Load the configuration parameters. (Command line options
306 // override config file options. Config file options override
308 if ( !fgInitConfig(argc, argv) ) {
309 SG_LOG( SG_GENERAL, SG_ALERT, "Config option parsing failed ..." );
313 // Initialize the Window/Graphics environment.
314 fgOSInit(&argc, argv);
317 fgRegisterIdleHandler( &fgIdleFunction );
319 // Initialize sockets (WinSock needs this)
320 simgear::Socket::initSockets();
322 // Clouds3D requires an alpha channel
323 fgOSOpenWindow(true /* request stencil buffer */);
325 // Initialize the splash screen right away
329 // pass control off to the master event handler
330 int result = fgOSMainLoop();
332 // clean up here; ensure we null globals to avoid
333 // confusing the atexit() handler
337 // delete the NavCache here. This will cause the destruction of many cached
338 // objects (eg, airports, navaids, runways).
339 delete flightgear::NavDataCache::instance();