1 // main.cxx -- top level sim routines
3 // Written by Curtis Olson for OpenGL, started May 1997.
5 // Copyright (C) 1997 - 1999 Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define MICHAEL_JOHNSON_EXPERIMENTAL_ENGINE_AUDIO
31 #ifdef FG_MATH_EXCEPTION_CLASH
50 #ifdef HAVE_SYS_STAT_H
51 # include <sys/stat.h> /* for stat() */
55 # include <unistd.h> /* for stat() */
58 #include <pu.h> // plib include
59 #include <ssg.h> // plib include
61 #ifdef ENABLE_AUDIO_SUPPORT
62 # include <sl.h> // plib include
63 # include <sm.h> // plib include
66 #include <Include/fg_constants.h> // for VERSION
67 #include <Include/general.hxx>
69 #include <Debug/logstream.hxx>
70 #include <Aircraft/aircraft.hxx>
71 #include <Astro/sky.hxx>
72 #include <Astro/stars.hxx>
73 #include <Astro/solarsystem.hxx>
75 #include <Autopilot/autopilot.hxx>
76 #include <Cockpit/cockpit.hxx>
78 #include <Joystick/joystick.hxx>
79 #include <Math/fg_geodesy.hxx>
80 #include <Math/mat3.h>
81 #include <Math/polar3d.hxx>
82 #include <Math/fg_random.h>
83 #include <Misc/fgpath.hxx>
84 #include <Scenery/scenery.hxx>
85 #include <Scenery/tilemgr.hxx>
86 #include <Time/event.hxx>
87 #include <Time/fg_time.hxx>
88 #include <Time/fg_timer.hxx>
89 #include <Time/sunpos.hxx>
90 #include <Weather/weather.hxx>
92 #include "fg_init.hxx"
93 #include "keyboard.hxx"
94 #include "options.hxx"
97 #include "fg_serial.hxx"
100 // -dw- use custom sioux settings so I can see output window
103 # include <sioux.h> // settings for output window
105 # include <console.h>
109 // This is a record containing a bit of global housekeeping information
112 // Specify our current idle function state. This is used to run all
113 // our initializations out of the glutIdleLoop() so that we can get a
114 // splash screen up and running right away.
115 static int idle_state = 0;
120 // Global structures for the Audio library
121 #ifdef ENABLE_AUDIO_SUPPORT
122 slEnvelope pitch_envelope ( 1, SL_SAMPLE_ONE_SHOT ) ;
123 slEnvelope volume_envelope ( 1, SL_SAMPLE_ONE_SHOT ) ;
124 slScheduler *audio_sched;
125 smMixer *audio_mixer;
132 ssgRoot *scene = NULL;
133 ssgTransform *penguin = NULL;
136 // The following defines flight gear options. Because glutlib will also
137 // want to parse its own options, those options must not be included here
138 // or they will get parsed by the main program option parser. Hence case
139 // is significant for any option added that might be in conflict with
142 // glutlib parses for:
144 // -direct (invalid in Win32)
148 // -indirect (invalid in Win32)
151 // Note that glutlib depends upon strings while this program's
152 // option parser wants only initial characters followed by numbers
157 // fgInitVisuals() -- Initialize various GL/view parameters
158 static void fgInitVisuals( void ) {
161 l = &cur_light_params;
163 #ifndef GLUT_WRONG_VERSION
164 // Go full screen if requested ...
165 if ( current_options.get_fullscreen() ) {
170 // If enabled, normal vectors specified with glNormal are scaled
171 // to unit length after transformation. See glNormal.
172 // xglEnable( GL_NORMALIZE );
174 xglEnable( GL_LIGHTING );
175 xglEnable( GL_LIGHT0 );
176 xglLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
179 sgSetVec3( sunpos, l->sun_vec[0], l->sun_vec[1], l->sun_vec[2] );
180 ssgGetLight( 0 ) -> setPosition( sunpos );
182 // xglFogi (GL_FOG_MODE, GL_LINEAR);
183 xglFogi (GL_FOG_MODE, GL_EXP2);
184 // Fog density is now set when the weather system is initialized
185 // xglFogf (GL_FOG_DENSITY, w->fog_density);
186 if ( (current_options.get_fog() == 1) ||
187 (current_options.get_shading() == 0) ) {
188 // if fastest fog requested, or if flat shading force fastest
189 xglHint ( GL_FOG_HINT, GL_FASTEST );
190 } else if ( current_options.get_fog() == 2 ) {
191 xglHint ( GL_FOG_HINT, GL_NICEST );
193 if ( current_options.get_wireframe() ) {
195 xglPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
198 // This is the default anyways, but it can't hurt
199 xglFrontFace ( GL_CCW );
202 // xglEnable(GL_POINT_SMOOTH);
203 // xglEnable(GL_LINE_SMOOTH);
204 // xglEnable(GL_POLYGON_SMOOTH);
209 // Draw a basic instrument panel
210 static void fgUpdateInstrViewParams( void ) {
214 fgVIEW *v = ¤t_view;
216 xglViewport(0, 0 , (GLint)(v->winWidth), (GLint)(v->winHeight) / 2);
218 xglMatrixMode(GL_PROJECTION);
222 gluOrtho2D(0, 640, 0, 480);
223 xglMatrixMode(GL_MODELVIEW);
227 xglColor3f(1.0, 1.0, 1.0);
230 xglDisable(GL_DEPTH_TEST);
231 xglDisable(GL_LIGHTING);
234 xglColor3f (0.5, 0.5, 0.5);
237 xglVertex2f(0.0, 0.00);
238 xglVertex2f(0.0, 480.0);
239 xglVertex2f(640.0,480.0);
240 xglVertex2f(640.0, 0.0);
243 xglRectf(0.0,0.0, 640, 480);
244 xglEnable(GL_DEPTH_TEST);
245 xglEnable(GL_LIGHTING);
246 xglMatrixMode(GL_PROJECTION);
248 xglMatrixMode(GL_MODELVIEW);
254 // Update all Visuals (redraws anything graphics related)
255 static void fgRenderFrame( void ) {
256 fgLIGHT *l = &cur_light_params;
257 FGTime *t = FGTime::cur_time_params;
258 FGView *v = ¤t_view;
261 // GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
262 GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
263 GLfloat terrain_color[4] = { 0.54, 0.44, 0.29, 1.0 };
264 // GLfloat mat_shininess[] = { 10.0 };
265 GLbitfield clear_mask;
267 if ( idle_state != 1000 ) {
268 // still initializing, draw the splash screen
269 if ( current_options.get_splash_screen() == 1 ) {
273 // idle_state is now 1000 meaning we've finished all our
274 // initializations and are running the main loop, so this will
275 // now work without seg faulting the system.
277 // printf("Ground = %.2f Altitude = %.2f\n", scenery.cur_elev,
278 // FG_Altitude * FEET_TO_METER);
280 // this is just a temporary hack, to make me understand Pui
281 // timerText -> setLabel (ctime (&t->cur_time));
284 // update view volume parameters
285 v->UpdateViewParams();
287 // set the sun position
288 xglLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
291 sgSetVec3( sunpos, l->sun_vec[0], l->sun_vec[1], l->sun_vec[2] );
292 ssgGetLight( 0 ) -> setPosition( sunpos );
294 clear_mask = GL_DEPTH_BUFFER_BIT;
295 if ( current_options.get_wireframe() ) {
296 clear_mask |= GL_COLOR_BUFFER_BIT;
298 if ( current_options.get_panel_status() ) {
299 // we can't clear the screen when the panel is active
300 } else if ( current_options.get_skyblend() ) {
301 if ( current_options.get_textures() ) {
302 // glClearColor(black[0], black[1], black[2], black[3]);
303 glClearColor(l->adj_fog_color[0], l->adj_fog_color[1],
304 l->adj_fog_color[2], l->adj_fog_color[3]);
305 clear_mask |= GL_COLOR_BUFFER_BIT;
308 glClearColor(l->sky_color[0], l->sky_color[1],
309 l->sky_color[2], l->sky_color[3]);
310 clear_mask |= GL_COLOR_BUFFER_BIT;
312 xglClear( clear_mask );
314 // Tell GL we are switching to model view parameters
315 xglMatrixMode(GL_MODELVIEW);
316 // xglLoadIdentity();
319 xglDisable( GL_DEPTH_TEST );
320 xglDisable( GL_LIGHTING );
321 xglDisable( GL_CULL_FACE );
322 xglDisable( GL_FOG );
323 xglShadeModel( GL_SMOOTH );
324 if ( current_options.get_skyblend() ) {
328 // setup transformation for drawing astronomical objects
330 // Translate to view position
331 Point3D view_pos = v->get_view_pos();
332 xglTranslatef( view_pos.x(), view_pos.y(), view_pos.z() );
333 // Rotate based on gst (sidereal time)
334 // note: constant should be 15.041085, Curt thought it was 15
335 angle = t->getGst() * 15.041085;
336 // printf("Rotating astro objects by %.2f degrees\n",angle);
337 xglRotatef( angle, 0.0, 0.0, -1.0 );
339 // draw stars and planets
341 xglEnable( GL_CULL_FACE ); // for moon
342 //xglEnable(GL_DEPTH_TEST);
343 SolarSystem::theSolarSystem->draw();
348 if ( current_options.get_shading() ) {
349 xglShadeModel( GL_SMOOTH );
351 xglShadeModel( GL_FLAT );
353 xglEnable( GL_DEPTH_TEST );
354 if ( current_options.get_fog() > 0 ) {
356 xglFogfv (GL_FOG_COLOR, l->adj_fog_color);
358 // set lighting parameters
359 xglLightfv(GL_LIGHT0, GL_AMBIENT, l->scene_ambient );
360 xglLightfv(GL_LIGHT0, GL_DIFFUSE, l->scene_diffuse );
361 // xglLightfv(GL_LIGHT0, GL_SPECULAR, white );
363 if ( current_options.get_textures() ) {
364 // texture parameters
365 xglEnable( GL_TEXTURE_2D );
366 xglTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
367 xglHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ) ;
368 // set base color (I don't think this is doing anything here)
369 xglMaterialfv (GL_FRONT, GL_AMBIENT, white);
370 xglMaterialfv (GL_FRONT, GL_DIFFUSE, white);
371 // xglMaterialfv (GL_FRONT, GL_SPECULAR, white);
372 // xglMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess);
374 xglDisable( GL_TEXTURE_2D );
375 xglMaterialfv (GL_FRONT, GL_AMBIENT, terrain_color);
376 xglMaterialfv (GL_FRONT, GL_DIFFUSE, terrain_color);
377 // xglMaterialfv (GL_FRONT, GL_AMBIENT, white);
378 // xglMaterialfv (GL_FRONT, GL_DIFFUSE, white);
381 global_tile_mgr.render();
383 xglDisable( GL_TEXTURE_2D );
384 xglDisable( GL_FOG );
386 // display HUD && Panel
387 xglDisable( GL_CULL_FACE );
390 // We can do translucent menus, so why not. :-)
391 xglEnable ( GL_BLEND ) ;
392 xglBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
394 xglDisable ( GL_BLEND ) ;
398 cout << "trying to draw ssg scene" << endl;
400 xglMatrixMode(GL_PROJECTION);
402 ssgSetFOV(60.0f, 0.0f);
403 ssgSetNearFar(1.0f, 700.0f);
406 current_view.view_pos.x() + current_view.view_forward[0]
408 current_view.view_pos.y() + current_view.view_forward[1]
410 current_view.view_pos.z() + current_view.view_forward[2]
413 penguin->setTransform( &tuxpos );
417 current_view.view_pos.x(),
418 current_view.view_pos.y(),
419 current_view.view_pos.z(),
421 ssgSetCamera( &campos );
422 ssgCullAndDraw( scene );
430 // Update internal time dependent calculations (i.e. flight model)
431 void fgUpdateTimeDepCalcs(int multi_loop, int remainder) {
432 FGInterface *f = current_aircraft.fdm_state;
433 fgLIGHT *l = &cur_light_params;
434 FGTime *t = FGTime::cur_time_params;
435 FGView *v = ¤t_view;
438 // update the flight model
439 if ( multi_loop < 0 ) {
440 multi_loop = DEFAULT_MULTILOOP;
443 if ( !t->getPause() ) {
444 // run Autopilot system
447 // printf("updating flight model x %d\n", multi_loop);
448 fgFDMUpdate( current_options.get_flight_model(),
449 cur_fdm_state, multi_loop, remainder );
451 fgFDMUpdate( current_options.get_flight_model(),
452 cur_fdm_state, 0, remainder );
455 // update the view angle
456 for ( i = 0; i < multi_loop; i++ ) {
457 if ( fabs(v->get_goal_view_offset() - v->get_view_offset()) < 0.05 ) {
458 v->set_view_offset( v->get_goal_view_offset() );
461 // move v->view_offset towards v->goal_view_offset
462 if ( v->get_goal_view_offset() > v->get_view_offset() ) {
463 if ( v->get_goal_view_offset() - v->get_view_offset() < FG_PI ){
464 v->inc_view_offset( 0.01 );
466 v->inc_view_offset( -0.01 );
469 if ( v->get_view_offset() - v->get_goal_view_offset() < FG_PI ){
470 v->inc_view_offset( -0.01 );
472 v->inc_view_offset( 0.01 );
475 if ( v->get_view_offset() > FG_2PI ) {
476 v->inc_view_offset( -FG_2PI );
477 } else if ( v->get_view_offset() < 0 ) {
478 v->inc_view_offset( FG_2PI );
483 double tmp = -(l->sun_rotation + FG_PI)
484 - (f->get_Psi() - v->get_view_offset() );
485 while ( tmp < 0.0 ) {
488 while ( tmp > FG_2PI ) {
491 /* printf("Psi = %.2f, viewoffset = %.2f sunrot = %.2f rottosun = %.2f\n",
492 FG_Psi * RAD_TO_DEG, v->view_offset * RAD_TO_DEG,
493 -(l->sun_rotation+FG_PI) * RAD_TO_DEG, tmp * RAD_TO_DEG); */
498 void fgInitTimeDepCalcs( void ) {
501 // #ifdef HAVE_SETITIMER
502 // fgTimerInit( 1.0 / DEFAULT_TIMER_HZ, fgUpdateTimeDepCalcs );
503 // #endif HAVE_SETITIMER
506 static const double alt_adjust_ft = 3.758099;
507 static const double alt_adjust_m = alt_adjust_ft * FEET_TO_METER;
509 // What should we do when we have nothing else to do? Let's get ready
510 // for the next move and update the display?
511 static void fgMainLoop( void ) {
514 static long remainder = 0;
515 long elapsed, multi_loop;
516 #ifdef FANCY_FRAME_COUNTER
520 static time_t last_time = 0;
521 static int frames = 0;
522 #endif // FANCY_FRAME_COUNTER
524 f = current_aircraft.fdm_state;
525 t = FGTime::cur_time_params;
527 FG_LOG( FG_ALL, FG_DEBUG, "Running Main Loop");
528 FG_LOG( FG_ALL, FG_DEBUG, "======= ==== ====");
530 #if defined( ENABLE_PLIB_JOYSTICK )
531 // Read joystick and update control settings
533 #elif defined( ENABLE_GLUT_JOYSTICK )
534 // Glut joystick support works by feeding a joystick handler
535 // function to glut. This is taken care of once in the joystick
536 // init routine and we don't have to worry about it again.
539 current_weather.Update();
541 // Fix elevation. I'm just sticking this here for now, it should
542 // probably move eventually
544 /* printf("Before - ground = %.2f runway = %.2f alt = %.2f\n",
546 f->get_Runway_altitude() * FEET_TO_METER,
547 f->get_Altitude() * FEET_TO_METER); */
549 if ( scenery.cur_elev > -9990 ) {
550 if ( f->get_Altitude() * FEET_TO_METER <
551 (scenery.cur_elev + alt_adjust_m - 3.0) ) {
552 // now set aircraft altitude above ground
553 printf("Current Altitude = %.2f < %.2f forcing to %.2f\n",
554 f->get_Altitude() * FEET_TO_METER,
555 scenery.cur_elev + alt_adjust_m - 3.0,
556 scenery.cur_elev + alt_adjust_m );
557 fgFDMForceAltitude( current_options.get_flight_model(),
558 scenery.cur_elev + alt_adjust_m );
560 FG_LOG( FG_ALL, FG_DEBUG,
561 "<*> resetting altitude to "
562 << f->get_Altitude() * FEET_TO_METER << " meters" );
564 fgFDMSetGroundElevation( current_options.get_flight_model(),
565 scenery.cur_elev ); // meters
568 /* printf("Adjustment - ground = %.2f runway = %.2f alt = %.2f\n",
570 f->get_Runway_altitude() * FEET_TO_METER,
571 f->get_Altitude() * FEET_TO_METER); */
576 // Get elapsed time (in usec) for this past frame
577 elapsed = fgGetTimeInterval();
578 FG_LOG( FG_ALL, FG_DEBUG,
579 "Elapsed time interval is = " << elapsed
580 << ", previous remainder is = " << remainder );
582 // Calculate frame rate average
583 #ifdef FANCY_FRAME_COUNTER
584 /* old fps calculation */
588 for ( i = FG_FRAME_RATE_HISTORY - 2; i >= 0; i-- ) {
589 tmp = general.get_frame(i);
591 // printf("frame[%d] = %.2f\n", i, g->frames[i]);
592 general.set_frame(i+1,tmp);
594 tmp = 1000000.0 / (float)elapsed;
595 general.set_frame(0,tmp);
596 // printf("frame[0] = %.2f\n", general.frames[0]);
598 general.set_frame_rate(accum / (float)FG_FRAME_RATE_HISTORY);
599 // printf("ave = %.2f\n", general.frame_rate);
602 if ( (t->get_cur_time() != last_time) && (last_time > 0) ) {
603 general.set_frame_rate( frames );
604 FG_LOG( FG_ALL, FG_DEBUG,
605 "--> Frame rate is = " << general.get_frame_rate() );
608 last_time = t->get_cur_time();
613 if ( ! use_signals ) {
614 // Calculate model iterations needed for next frame
615 elapsed += remainder;
617 multi_loop = (int)(((double)elapsed * 0.000001) * DEFAULT_MODEL_HZ);
618 remainder = elapsed - ((multi_loop*1000000) / DEFAULT_MODEL_HZ);
619 FG_LOG( FG_ALL, FG_DEBUG,
620 "Model iterations needed = " << multi_loop
621 << ", new remainder = " << remainder );
624 if ( multi_loop > 0 ) {
625 fgUpdateTimeDepCalcs(multi_loop, remainder);
627 FG_LOG( FG_ALL, FG_INFO, "Elapsed time is zero ... we're zinging" );
631 #if ! defined( MACOS )
632 // Do any serial port work that might need to be done
636 // see if we need to load any new scenery tiles
637 global_tile_mgr.update();
639 // Process/manage pending events
640 global_events.Process();
642 // Run audio scheduler
643 #ifdef ENABLE_AUDIO_SUPPORT
644 if ( current_options.get_sound() && !audio_sched->not_working() ) {
646 # ifdef MICHAEL_JOHNSON_EXPERIMENTAL_ENGINE_AUDIO
648 // note: all these factors are relative to the sample. our
649 // sample format should really contain a conversion factor so
650 // that we can get prop speed right for arbitrary samples.
651 // Note that for normal-size props, there is a point at which
652 // the prop tips approach the speed of sound; that is a pretty
653 // strong limit to how fast the prop can go.
655 // multiplication factor is prime pitch control; add some log
656 // component for verisimilitude
658 double pitch = log((controls.get_throttle(0) * 14.0) + 1.0);
659 //fprintf(stderr, "pitch1: %f ", pitch);
660 if (controls.get_throttle(0) > 0.0 || f->v_rel_wind > 40.0) {
661 //fprintf(stderr, "rel_wind: %f ", f->v_rel_wind);
662 // only add relative wind and AoA if prop is moving
663 // or we're really flying at idle throttle
664 if (pitch < 5.4) { // this needs tuning
665 // prop tips not breaking sound barrier
666 pitch += log(f->v_rel_wind + 0.8)/2;
668 // prop tips breaking sound barrier
669 pitch += log(f->v_rel_wind + 0.8)/10;
671 //fprintf(stderr, "pitch2: %f ", pitch);
672 //fprintf(stderr, "AoA: %f ", FG_Gamma_vert_rad);
674 // Angle of Attack next... -x^3(e^x) is my best guess Just
675 // need to calculate some reasonable scaling factor and
676 // then clamp it on the positive aoa (neg adj) side
677 double aoa = f->get_Gamma_vert_rad() * 2.2;
679 double aoa_adj = pow(-aoa, tmp) * pow(M_E, aoa);
680 if (aoa_adj < -0.8) aoa_adj = -0.8;
682 //fprintf(stderr, "pitch3: %f ", pitch);
684 // don't run at absurdly slow rates -- not realistic
685 // and sounds bad to boot. :-)
686 if (pitch < 0.8) pitch = 0.8;
688 //fprintf(stderr, "pitch4: %f\n", pitch);
690 double volume = controls.get_throttle(0) * 1.15 + 0.3 +
691 log(f->v_rel_wind + 1.0)/14.0;
692 // fprintf(stderr, "volume: %f\n", volume);
694 pitch_envelope.setStep ( 0, 0.01, pitch );
695 volume_envelope.setStep ( 0, 0.01, volume );
699 double param = controls.get_throttle( 0 ) * 2.0 + 1.0;
700 pitch_envelope.setStep ( 0, 0.01, param );
701 volume_envelope.setStep ( 0, 0.01, param );
703 # endif // experimental throttle patch
705 audio_sched -> update();
712 FG_LOG( FG_ALL, FG_DEBUG, "" );
716 // This is the top level master main function that is registered as
720 // The first few passes take care of initialization things (a couple
721 // per pass) and once everything has been initialized fgMainLoop from
724 static void fgIdleFunction ( void ) {
725 // printf("idle state == %d\n", idle_state);
727 if ( idle_state == 0 ) {
728 // Initialize the splash screen right away
729 if ( current_options.get_splash_screen() ) {
734 } else if ( idle_state == 1 ) {
735 // Start the intro music
737 if ( current_options.get_intro_music() ) {
738 string lockfile = "/tmp/mpg123.running";
739 FGPath mp3file( current_options.get_fg_root() );
740 mp3file.append( "Sounds/intro.mp3" );
742 string command = "(touch " + lockfile + "; mpg123 "
743 + mp3file.str() + "> /dev/null 2>&1; /bin/rm "
745 FG_LOG( FG_GENERAL, FG_INFO,
746 "Starting intro music: " << mp3file.str() );
747 system ( command.c_str() );
752 } else if ( idle_state == 2 ) {
753 // These are a few miscellaneous things that aren't really
754 // "subsystems" but still need to be initialized.
757 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
758 grTexLodBiasValue ( GR_TMU0, 1.0 ) ;
763 } else if ( idle_state == 3 ) {
764 // This is the top level init routine which calls all the
765 // other subsystem initialization routines. If you are adding
766 // a subsystem to flight gear, its initialization call should
767 // located in this routine.
768 if( !fgInitSubsystems()) {
769 FG_LOG( FG_GENERAL, FG_ALERT,
770 "Subsystem initializations failed ..." );
775 } else if ( idle_state == 4 ) {
776 // setup OpenGL view parameters
780 // init timer routines, signals, etc. Arrange for an alarm
781 // signal to be generated, etc.
782 fgInitTimeDepCalcs();
786 } else if ( idle_state == 5 ) {
789 } else if ( idle_state == 6 ) {
790 // Initialize audio support
791 #ifdef ENABLE_AUDIO_SUPPORT
794 if ( current_options.get_intro_music() ) {
795 // Let's wait for mpg123 to finish
796 string lockfile = "/tmp/mpg123.running";
797 struct stat stat_buf;
799 FG_LOG( FG_GENERAL, FG_INFO,
800 "Waiting for mpg123 player to finish ..." );
801 while ( stat(lockfile.c_str(), &stat_buf) == 0 ) {
802 // file exist, wait ...
804 FG_LOG( FG_GENERAL, FG_INFO, ".");
806 FG_LOG( FG_GENERAL, FG_INFO, "");
810 audio_sched = new slScheduler ( 8000 );
811 audio_mixer = new smMixer;
812 audio_mixer -> setMasterVolume ( 80 ) ; /* 80% of max volume. */
813 audio_sched -> setSafetyMargin ( 1.0 ) ;
815 FGPath slfile( current_options.get_fg_root() );
816 slfile.append( "Sounds/wasp.wav" );
818 s1 = new slSample ( (char *)slfile.c_str() );
819 FG_LOG( FG_GENERAL, FG_INFO,
820 "Rate = " << s1 -> getRate()
821 << " Bps = " << s1 -> getBps()
822 << " Stereo = " << s1 -> getStereo() );
823 audio_sched -> loopSample ( s1 );
825 if ( audio_sched->not_working() ) {
828 pitch_envelope.setStep ( 0, 0.01, 0.6 );
829 volume_envelope.setStep ( 0, 0.01, 0.6 );
831 audio_sched -> addSampleEnvelope( s1, 0, 0, &
834 audio_sched -> addSampleEnvelope( s1, 0, 1,
836 SL_VOLUME_ENVELOPE );
839 // strcpy(slfile, path);
840 // strcat(slfile, "thunder.wav");
841 // s2 -> loadFile ( slfile );
842 // s2 -> adjustVolume(0.5);
843 // audio_sched -> playSample ( s2 );
850 if ( idle_state == 1000 ) {
851 // We've finished all our initialization steps, from now on we
852 // run the main loop.
856 if ( current_options.get_splash_screen() == 1 ) {
862 // options.cxx needs to see this for toggle_panel()
863 // Handle new window size or exposure
864 void fgReshape( int width, int height ) {
865 if ( ! current_options.get_panel_status() ) {
866 current_view.set_win_ratio( (GLfloat) width / (GLfloat) height );
867 xglViewport(0, 0 , (GLint)(width), (GLint)(height) );
869 current_view.set_win_ratio( (GLfloat) width /
870 ((GLfloat) (height)*0.4232) );
871 xglViewport(0, (GLint)((height)*0.5768), (GLint)(width),
872 (GLint)((height)*0.4232) );
875 current_view.set_winWidth( width );
876 current_view.set_winHeight( height );
877 current_view.force_update_fov_math();
879 if ( idle_state == 1000 ) {
880 // yes we've finished all our initializations and are running
881 // the main loop, so this will now work without seg faulting
883 current_view.UpdateViewParams();
884 if ( current_options.get_panel_status() ) {
885 FGPanel::OurPanel->ReInit(0, 0, 1024, 768);
891 // Initialize GLUT and define a main window
892 int fgGlutInit( int *argc, char **argv ) {
894 #if !defined( MACOS )
895 // GLUT will extract all glut specific options so later on we only
896 // need wory about our own.
897 xglutInit(argc, argv);
900 // Define Display Parameters
901 xglutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
903 FG_LOG( FG_GENERAL, FG_INFO, "Opening a window: " <<
904 current_options.get_xsize() << "x" << current_options.get_ysize() );
906 // Define initial window size
907 xglutInitWindowSize( current_options.get_xsize(),
908 current_options.get_ysize() );
910 // Initialize windows
911 if ( current_options.get_game_mode() == 0 ) {
912 // Open the regular window
913 xglutCreateWindow("Flight Gear");
914 #ifndef GLUT_WRONG_VERSION
916 // Open the cool new 'game mode' window
917 char game_mode_str[256];
918 sprintf( game_mode_str, "width=%d height=%d bpp=32",
919 current_options.get_xsize(),
920 current_options.get_ysize() );
922 FG_LOG( FG_GENERAL, FG_INFO,
923 "game mode params = " << game_mode_str );
924 glutGameModeString( game_mode_str );
929 // This seems to be the absolute earliest in the init sequence
930 // that these calls will return valid info. Too bad it's after
931 // we've already created and sized out window. :-(
932 general.set_glVendor( (char *)glGetString ( GL_VENDOR ) );
933 general.set_glRenderer( (char *)glGetString ( GL_RENDERER ) );
934 general.set_glVersion( (char *)glGetString ( GL_VERSION ) );
936 FG_LOG ( FG_GENERAL, FG_INFO, general.get_glRenderer() );
939 // try to determine if we should adjust the initial default
940 // display resolution. The options class defaults (is
941 // initialized) to 640x480.
942 string renderer = general.glRenderer;
944 // currently we only know how to deal with Mesa/Glide/Voodoo cards
945 if ( renderer.find( "Glide" ) != string::npos ) {
946 FG_LOG( FG_GENERAL, FG_INFO, "Detected a Glide driver" );
947 if ( renderer.find( "FB/8" ) != string::npos ) {
948 // probably a voodoo-2
949 if ( renderer.find( "TMU/SLI" ) != string::npos ) {
950 // probably two SLI'd Voodoo-2's
951 current_options.set_xsize( 1024 );
952 current_options.set_ysize( 768 );
953 FG_LOG( FG_GENERAL, FG_INFO,
954 "It looks like you have two sli'd voodoo-2's." << endl
955 << "upgrading your win resolution to 1024 x 768" );
956 glutReshapeWindow(1024, 768);
958 // probably a single non-SLI'd Voodoo-2
959 current_options.set_xsize( 800 );
960 current_options.set_ysize( 600 );
961 FG_LOG( FG_GENERAL, FG_INFO,
962 "It looks like you have a voodoo-2." << endl
963 << "upgrading your win resolution to 800 x 600" );
964 glutReshapeWindow(800, 600);
966 } else if ( renderer.find( "FB/2" ) != string::npos ) {
967 // probably a voodoo-1, stick with the default
970 // we have no special knowledge of this card, stick with the default
978 // Initialize GLUT event handlers
979 int fgGlutInitEvents( void ) {
980 // call fgReshape() on window resizes
981 xglutReshapeFunc( fgReshape );
983 // call GLUTkey() on keyboard event
984 xglutKeyboardFunc( GLUTkey );
985 glutSpecialFunc( GLUTspecialkey );
987 // call guiMouseFunc() whenever our little rodent is used
988 glutMouseFunc ( guiMouseFunc );
989 glutMotionFunc (guiMotionFunc );
990 glutPassiveMotionFunc (guiMotionFunc );
992 // call fgMainLoop() whenever there is
993 // nothing else to do
994 xglutIdleFunc( fgIdleFunction );
997 xglutDisplayFunc( fgRenderFrame );
1004 int main( int argc, char **argv ) {
1006 #if defined( MACOS )
1007 argc = ccommand( &argv );
1011 _control87(MCW_EM, MCW_EM); /* defined in float.h */
1014 // set default log levels
1015 fglog().setLogLevels( FG_ALL, FG_INFO );
1017 FG_LOG( FG_GENERAL, FG_INFO, "Flight Gear: Version " << VERSION << endl );
1019 // seed the random number generater
1022 // Load the configuration parameters
1023 if ( !fgInitConfig(argc, argv) ) {
1024 FG_LOG( FG_GENERAL, FG_ALERT, "Config option parsing failed ..." );
1028 // Initialize the Window/Graphics environment.
1029 if( !fgGlutInit(&argc, argv) ) {
1030 FG_LOG( FG_GENERAL, FG_ALERT, "GLUT initialization failed ..." );
1034 // Initialize the various GLUT Event Handlers.
1035 if( !fgGlutInitEvents() ) {
1036 FG_LOG( FG_GENERAL, FG_ALERT,
1037 "GLUT event handler initialization failed ..." );
1041 // Initialize ssg (from plib)
1044 // Initialize the user interface (we need to do this before
1045 // passing off control to glut and before fgInitGeneral to get our
1049 // Do some quick general initializations
1050 if( !fgInitGeneral()) {
1051 FG_LOG( FG_GENERAL, FG_ALERT,
1052 "General initializations failed ..." );
1057 // some ssg test stuff (requires data from the plib source
1058 // distribution) specifically from the ssg tux example
1061 ssgModelPath( "/stage/pinky01/src/Libs/plib-1.0.12/examples/ssg/tux/data/" );
1062 ssgTexturePath( "/stage/pinky01/src/Libs/plib-1.0.12/examples/ssg/tux/data/" );
1064 scene = new ssgRoot;
1065 penguin = new ssgTransform;
1067 ssgEntity *tux_obj = ssgLoadAC( "tuxedo.ac" );
1068 penguin->addKid( tux_obj );
1069 ssgFlatten( tux_obj );
1070 ssgStripify( penguin );
1072 scene->addKid( penguin );
1074 // pass control off to the master GLUT event handler
1077 // we never actually get here ... but to avoid compiler warnings,