]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/main.cxx
Use a pointer to a structure instead of a large number of variables when calling...
[flightgear.git] / src / Main / main.cxx
index f0ae06daae0c74a208ac976105a90a07aedaea14..c1ad6e9b399e7c65aa2400ca7537c22d766f8d86 100644 (file)
@@ -120,6 +120,7 @@ SG_USING_STD(endl);
 #include <MultiPlayer/multiplayrxmgr.hxx>
 #endif
 
+#include <Replay/replay.hxx>
 #include <Scenery/scenery.hxx>
 #include <Scenery/tilemgr.hxx>
 #ifdef ENABLE_AUDIO_SUPPORT
@@ -165,7 +166,7 @@ sgVec3 rway_ols;
 float scene_nearplane = 0.5f;
 float scene_farplane = 120000.0f;
 
-static double delta_time_sec = 0;
+static double delta_time_sec = 0.0;
 
 
 #ifdef FG_WEATHERCM
@@ -422,6 +423,10 @@ void trRenderFrame( void ) {
 
 // Update all Visuals (redraws anything graphics related)
 void fgRenderFrame() {
+    bool draw_otw = fgGetBool("/sim/rendering/draw-otw");
+    bool skyblend = fgGetBool("/sim/rendering/skyblend");
+    bool enhanced_lighting = fgGetBool("/sim/rendering/enhanced-lighting");
+    bool distance_attenuation = fgGetBool("/sim/rendering/distance-attenuation");
 
     GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
     GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
@@ -520,7 +525,7 @@ void fgRenderFrame() {
             clear_mask |= GL_COLOR_BUFFER_BIT;
         }
 
-        if ( fgGetBool("/sim/rendering/skyblend") ) {
+        if ( skyblend ) {
             if ( fgGetBool("/sim/rendering/textures") ) {
             // glClearColor(black[0], black[1], black[2], black[3]);
             glClearColor(l->adj_fog_color[0], l->adj_fog_color[1],
@@ -552,14 +557,14 @@ void fgRenderFrame() {
         thesky->set_visibility(visibility_meters);
 
         thesky->modify_vis( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
-                        ( global_multi_loop * fgGetInt("/sim/speed-up") )
+                            ( global_multi_loop * fgGetInt("/sim/speed-up") )
                             / (double)fgGetInt("/sim/model-hz") );
 
         // Set correct opengl fog density
         glFogf (GL_FOG_DENSITY, fog_exp2_density);
 
         // update the sky dome
-        if ( fgGetBool("/sim/rendering/skyblend") ) {
+        if ( skyblend ) {
             /*
              SG_LOG( SG_GENERAL, SG_BULK, "thesky->repaint() sky_color = "
              << cur_light_params.sky_color[0] << " "
@@ -575,15 +580,20 @@ void fgRenderFrame() {
                     "    sun_angle = " << cur_light_params.sun_angle
              << "    moon_angle = " << cur_light_params.moon_angle );
             */
-            thesky->repaint( cur_light_params.sky_color,
-                             cur_light_params.adj_fog_color,
-                             cur_light_params.cloud_color,
-                             cur_light_params.sun_angle,
-                             cur_light_params.moon_angle,
-                             globals->get_ephem()->getNumPlanets(),
-                             globals->get_ephem()->getPlanets(),
-                             globals->get_ephem()->getNumStars(),
-                             globals->get_ephem()->getStars() );
+
+            static SGSkyColor scolor;
+
+            scolor.sky_color   = cur_light_params.sky_color;
+            scolor.fog_color   = cur_light_params.adj_fog_color;
+            scolor.cloud_color = cur_light_params.cloud_color;
+            scolor.sun_angle   = cur_light_params.sun_angle;
+            scolor.moon_angle  = cur_light_params.moon_angle;
+            scolor.nplanets    = globals->get_ephem()->getNumPlanets();
+            scolor.nstars      = globals->get_ephem()->getNumStars();
+            scolor.planet_data = globals->get_ephem()->getPlanets();
+            scolor.star_data   = globals->get_ephem()->getStars();
+
+            thesky->repaint( scolor );
 
             /*
             SG_LOG( SG_GENERAL, SG_BULK,
@@ -616,23 +626,28 @@ void fgRenderFrame() {
             } else {
                sun_horiz_eff = moon_horiz_eff = 1.0;
             }
-            thesky->reposition( current__view->get_view_pos(),
-                                current__view->get_zero_elev(),
-                                current__view->get_world_up(),
-                                current__view->getLongitude_deg()
-                                  * SGD_DEGREES_TO_RADIANS,
-                                current__view->getLatitude_deg()
-                                  * SGD_DEGREES_TO_RADIANS,
-                                current__view->getAltitudeASL_ft()
-                                  * SG_FEET_TO_METER,
-                                cur_light_params.sun_rotation,
-                                globals->get_time_params()->getGst(),
-                                globals->get_ephem()->getSunRightAscension(),
-                                globals->get_ephem()->getSunDeclination(),
-                                50000.0 * sun_horiz_eff,
-                                globals->get_ephem()->getMoonRightAscension(),
-                                globals->get_ephem()->getMoonDeclination(),
-                                40000.0 * moon_horiz_eff );
+
+            static SGSkyState sstate;
+
+            sstate.view_pos  = current__view->get_view_pos();
+            sstate.zero_elev = current__view->get_zero_elev();
+            sstate.view_up   = current__view->get_world_up();
+            sstate.lon       = current__view->getLongitude_deg()
+                                * SGD_DEGREES_TO_RADIANS;
+            sstate.lat       = current__view->getLatitude_deg()
+                                * SGD_DEGREES_TO_RADIANS;
+            sstate.alt       = current__view->getAltitudeASL_ft()
+                                * SG_FEET_TO_METER;
+            sstate.spin      = cur_light_params.sun_rotation;
+            sstate.gst       = globals->get_time_params()->getGst();
+            sstate.sun_ra    = globals->get_ephem()->getSunRightAscension();
+            sstate.sun_dec   = globals->get_ephem()->getSunDeclination();
+            sstate.sun_dist  = 50000.0 * sun_horiz_eff;
+            sstate.moon_ra   = globals->get_ephem()->getMoonRightAscension();
+            sstate.moon_dec  = globals->get_ephem()->getMoonDeclination();
+            sstate.moon_dist = 40000.0 * moon_horiz_eff;
+
+            thesky->reposition( sstate );
         }
 
         glEnable( GL_DEPTH_TEST );
@@ -710,8 +725,7 @@ void fgRenderFrame() {
         globals->get_multiplayer_rx_mgr()->Update();
 #endif
 
-        if ( fgGetBool("/sim/rendering/skyblend") &&
-             fgGetBool("/sim/rendering/draw-otw") )
+        if ( draw_otw && skyblend )
         {
             // draw the sky backdrop
 
@@ -733,7 +747,7 @@ void fgRenderFrame() {
             // draw wire frame
             glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
         }
-        if ( fgGetBool("/sim/rendering/draw-otw") ) {
+        if ( draw_otw ) {
             ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
         }
 
@@ -763,13 +777,12 @@ void fgRenderFrame() {
         glFogf (GL_FOG_DENSITY, rwy_exp2_punch_through);
         ssgSetNearFar( scene_nearplane, scene_farplane );
 
-        if (fgGetBool("/sim/rendering/enhanced-lighting")) {
+        if ( enhanced_lighting ) {
 
             // Enable states for drawing points with GL_extension
             glEnable(GL_POINT_SMOOTH);
 
-            if ( fgGetBool("/sim/rendering/distance-attenuation") &&
-                 glPointParameterIsSupported )
+            if ( distance_attenuation && glPointParameterIsSupported )
             {
                 // Enable states for drawing points with GL_extension
                 glEnable(GL_POINT_SMOOTH);
@@ -793,7 +806,7 @@ void fgRenderFrame() {
         glPolygonMode(GL_FRONT, GL_POINT);
 
         // draw runway lighting
-        if ( fgGetBool("/sim/rendering/draw-otw") ) {
+        if ( draw_otw ) {
             ssgCullAndDraw( globals->get_scenery()->get_rwy_lights_root() );
         }
 
@@ -802,7 +815,7 @@ void fgRenderFrame() {
         // sgVec3 taxi_fog;
         // sgSetVec3( taxi_fog, 0.0, 0.0, 0.0 );
         // glFogfv ( GL_FOG_COLOR, taxi_fog );
-        if ( fgGetBool("/sim/rendering/draw-otw") ) {
+        if ( draw_otw ) {
             ssgCullAndDraw( globals->get_scenery()->get_taxi_lights_root() );
         }
 
@@ -821,9 +834,8 @@ void fgRenderFrame() {
         //_frame_count++;
 
 
-        if (fgGetBool("/sim/rendering/enhanced-lighting")) {
-            if ( fgGetBool("/sim/rendering/distance-attenuation") &&
-                 glPointParameterIsSupported )
+        if ( enhanced_lighting ) {
+            if ( distance_attenuation && glPointParameterIsSupported )
             {
                 glPointParameterfvPtr(GL_DISTANCE_ATTENUATION_EXT,
                                       default_attenuation);
@@ -835,22 +847,20 @@ void fgRenderFrame() {
 
         // draw ground lighting
         glFogf (GL_FOG_DENSITY, ground_exp2_punch_through);
-        if ( fgGetBool("/sim/rendering/draw-otw") ) {
+        if ( draw_otw ) {
             ssgCullAndDraw( globals->get_scenery()->get_gnd_lights_root() );
         }
 
-        if ( fgGetBool("/sim/rendering/skyblend") ) {
+        if ( skyblend ) {
             // draw the sky cloud layers
-            if ( fgGetBool("/environment/clouds/status") &&
-                 fgGetBool("/sim/rendering/draw-otw") )
+            if ( draw_otw && fgGetBool("/environment/clouds/status") )
             {
                 thesky->postDraw( cur_fdm_state->get_Altitude()
                                   * SG_FEET_TO_METER );
             }
         }
 
-        if ( fgGetBool("/sim/rendering/clouds3d") &&
-             fgGetBool("/sim/rendering/draw-otw") )
+        if ( draw_otw && fgGetBool("/sim/rendering/clouds3d") )
         {
             glDisable( GL_FOG );
             glDisable( GL_LIGHTING );
@@ -877,7 +887,7 @@ void fgRenderFrame() {
             glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
         }
 
-        if ( fgGetBool("/sim/rendering/draw-otw") ) {
+        if ( draw_otw ) {
             globals->get_model_mgr()->draw();
             globals->get_aircraft_model()->draw();
         }
@@ -933,6 +943,13 @@ void fgRenderFrame() {
 void fgUpdateTimeDepCalcs() {
     static bool inited = false;
 
+    static const SGPropertyNode *replay_master
+        = fgGetNode( "/sim/freeze/replay", true );
+    static SGPropertyNode *replay_time
+        = fgGetNode( "/sim/replay/time", true );
+    static const SGPropertyNode *replay_end_time
+        = fgGetNode( "/sim/replay/end-time", true );
+
     //SG_LOG(SG_FLIGHT,SG_INFO, "Updating time dep calcs()");
 
     fgLIGHT *l = &cur_light_params;
@@ -956,17 +973,25 @@ void fgUpdateTimeDepCalcs() {
 
     // conceptually, the following block could be done for each fdm
     // instance ...
-    if ( !cur_fdm_state->get_inited() ) {
-        // do nothing, fdm isn't inited yet
-    } else {
+    if ( cur_fdm_state->get_inited() ) {
         // we have been inited, and  we are good to go ...
 
         if ( !inited ) {
             inited = true;
         }
 
-        globals->get_autopilot()->update(delta_time_sec);
-        cur_fdm_state->update(delta_time_sec);
+        if ( ! replay_master->getBoolValue() ) {
+            globals->get_autopilot()->update( delta_time_sec );
+            cur_fdm_state->update( delta_time_sec );
+        } else {
+            FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
+            r->replay( replay_time->getDoubleValue() );
+            replay_time->setDoubleValue( replay_time->getDoubleValue()
+                                         + ( delta_time_sec
+                                             * fgGetInt("/sim/speed-up") ) );
+        }
+    } else {
+        // do nothing, fdm isn't inited yet
     }
 
     globals->get_model_mgr()->update(delta_time_sec);
@@ -999,6 +1024,7 @@ static const double alt_adjust_m = alt_adjust_ft * SG_FEET_TO_METER;
 // What should we do when we have nothing else to do?  Let's get ready
 // for the next move and update the display?
 static void fgMainLoop( void ) {
+    int model_hz = fgGetInt("/sim/model-hz");
 
     static const SGPropertyNode *longitude
         = fgGetNode("/position/longitude-deg");
@@ -1010,6 +1036,8 @@ static void fgMainLoop( void ) {
         = fgGetNode("/sim/freeze/clock", true);
     static const SGPropertyNode *cur_time_override
         = fgGetNode("/sim/time/cur-time-override", true);
+    static const SGPropertyNode *replay_master
+        = fgGetNode("/sim/freeze/replay", true);
 
     // Update the elapsed time.
     static bool first_time = true;
@@ -1034,7 +1062,7 @@ static void fgMainLoop( void ) {
     delta_time_sec = double(current_time_stamp - last_time_stamp) / 1000000.0;
     if ( clock_freeze->getBoolValue() ) {
         delta_time_sec = 0;
-    }
+    } 
     last_time_stamp = current_time_stamp;
     globals->inc_sim_time_sec( delta_time_sec );
     SGAnimation::set_sim_time_sec( globals->get_sim_time_sec() );
@@ -1092,15 +1120,20 @@ static void fgMainLoop( void ) {
            cur_fdm_state->get_Runway_altitude() * SG_FEET_TO_METER,
            cur_fdm_state->get_Altitude() * SG_FEET_TO_METER); */
 
-// Curt is this code used?  I don't see any problems when I comment it out.
+#if 0
+// This code is used by LaRCsim/UIUC to position the aircraft at a proper
+// place. This code should eventually go into the LaRCsim directory.
+// Commented out at Jul 27 2003 because tests show it is not needed.
+// Can probbaly be safely removed after Spet. 2003 - EMH
+
     if ( acmodel_location != 0 ) {
       if ( acmodel_location->get_cur_elev_m() > -9990 && cur_fdm_state->get_inited() ) {
         if ( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER < 
-             (acmodel_location->get_cur_elev_m() + alt_adjust_m - 3.0) ) {
+             (acmodel_location->get_cur_elev_m() + alt_adjust_m - 130.0) ) {
             // now set aircraft altitude above ground
             printf("(*) Current Altitude = %.2f < %.2f forcing to %.2f\n", 
                cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
-               acmodel_location->get_cur_elev_m() + alt_adjust_m - 3.0,
+               acmodel_location->get_cur_elev_m() + alt_adjust_m - 130.0,
                acmodel_location->get_cur_elev_m() + alt_adjust_m );
             cur_fdm_state->set_Altitude( (acmodel_location->get_cur_elev_m() 
                                                 + alt_adjust_m) * SG_METER_TO_FEET );
@@ -1112,6 +1145,7 @@ static void fgMainLoop( void ) {
       }
     }
 // End of code in question. (see Curt is this code used? above)
+#endif
 
     /* printf("Adjustment - ground = %.2f  runway = %.2f  alt = %.2f\n",
            scenery.get_cur_elev(),
@@ -1213,18 +1247,16 @@ static void fgMainLoop( void ) {
     // Calculate model iterations needed for next frame
     elapsed += remainder;
 
-    global_multi_loop = (long)(((double)elapsed * 0.000001) * 
-                          fgGetInt("/sim/model-hz"));
-    remainder = elapsed - ( (global_multi_loop*1000000) / 
-                        fgGetInt("/sim/model-hz") );
+    global_multi_loop = (long)(((double)elapsed * 0.000001) * model_hz );
+    remainder = elapsed - ( (global_multi_loop*1000000) / model_hz );
     SG_LOG( SG_ALL, SG_DEBUG, 
             "Model iterations needed = " << global_multi_loop
             << ", new remainder = " << remainder );
         
     // chop max interations to something reasonable if the sim was
     // delayed for an excesive amount of time
-    if ( global_multi_loop > 2.0 * fgGetInt("/sim/model-hz") ) {
-        global_multi_loop = (int)(2.0 * fgGetInt("/sim/model-hz") );
+    if ( global_multi_loop > 2.0 * model_hz ) {
+        global_multi_loop = (int)(2.0 * model_hz );
         remainder = 0;
     }
 
@@ -1628,7 +1660,7 @@ static bool fgMainInit( int argc, char **argv ) {
 
     // Initialize the various GLUT Event Handlers.
     if( !fgGlutInitEvents() ) {
- SG_LOG( SG_GENERAL, SG_ALERT, 
       SG_LOG( SG_GENERAL, SG_ALERT, 
                "GLUT event handler initialization failed ..." );
         exit(-1);
     }
@@ -1728,7 +1760,6 @@ static bool fgMainInit( int argc, char **argv ) {
     ////////////////////////////////////////////////////////////////////
     // Initialize the view manager subsystem.
     ////////////////////////////////////////////////////////////////////
-
     FGViewMgr *viewmgr = new FGViewMgr;
     globals->set_viewmgr( viewmgr );
     viewmgr->init();
@@ -1776,6 +1807,21 @@ static bool fgMainInit( int argc, char **argv ) {
     SGMagVar *magvar = new SGMagVar();
     globals->set_mag( magvar );
 
+
+                                // kludge to initialize mag compass
+                                // (should only be done for in-flight
+                                // startup)
+    // update magvar model
+    globals->get_mag()->update( fgGetDouble("/position/longitude-deg")
+                                * SGD_DEGREES_TO_RADIANS,
+                                fgGetDouble("/position/latitude-deg")
+                                * SGD_DEGREES_TO_RADIANS,
+                                fgGetDouble("/position/altitude-ft")
+                                * SG_FEET_TO_METER,
+                                globals->get_time_params()->getJD() );
+    double var = globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
+    fgSetDouble("/instrumentation/heading-indicator/offset-deg", -var);
+
     // airport = new ssgBranch;
     // airport->setName( "Airport Lighting" );
     // lighting->addKid( airport );