]> git.mxchange.org Git - flightgear.git/blob - Main/GLUTkey.cxx
More altitude hold tweaks.
[flightgear.git] / Main / GLUTkey.cxx
1 // GLUTkey.cxx -- handle GLUT keyboard events
2 //
3 // Written by Curtis Olson, started May 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@me.umn.edu
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 // $Id$
22 // (Log is kept at end of this file)
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #ifdef HAVE_WINDOWS_H
30 #  include <windows.h>                     
31 #endif
32
33 #include <GL/glut.h>
34 #include <XGL/xgl.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include <Aircraft/aircraft.h>
39 #include <Astro/solarsystem.hxx>
40 #include <Astro/sky.hxx>
41 #include <Autopilot/autopilot.hxx>
42 #include <Cockpit/hud.hxx>
43 #include <Debug/fg_debug.h>
44 #include <GUI/gui.h>
45 #include <Include/fg_constants.h>
46 #include <Objects/material.hxx>
47 #include <PUI/pu.h>
48 #include <Time/light.hxx>
49 #include <Weather/weather.h>
50
51 #include "GLUTkey.hxx"
52 #include "options.hxx"
53 #include "views.hxx"
54
55 #if defined(FX) && defined(XMESA)
56 #  include <GL/xmesa.h>
57    static int fullscreen = 1;
58 #endif
59
60
61 // Force an update of the sky and lighting parameters
62 static void local_update_sky_and_lighting_params( void ) {
63     // fgSunInit();
64     SolarSystem::theSolarSystem->rebuild();
65     cur_light_params.Update();
66     fgSkyColorsInit();
67 }
68
69
70 // Handle keyboard events
71 void GLUTkey(unsigned char k, int x, int y) {
72     fgCONTROLS *c;
73     fgFLIGHT *f;
74     fgTIME *t;
75     fgVIEW *v;
76     struct fgWEATHER *w;
77     float fov, tmp;
78
79     c = current_aircraft.controls;
80     f = current_aircraft.flight;
81     t = &cur_time_params;
82     v = &current_view;
83     w = &current_weather;
84
85     fgPrintf( FG_INPUT, FG_DEBUG, "Key hit = %d", k);
86     puKeyboard(k, PU_DOWN );
87
88     if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
89         fgPrintf( FG_INPUT, FG_DEBUG, " SHIFTED\n");
90         switch (k) {
91         case 1: // Ctrl-A key
92             fgAPToggleAltitude();
93             return;
94         case 8: // Ctrl-H key
95             fgAPToggleHeading();
96             return;
97         case 20: // Ctrl-T key
98             fgAPToggleTerrainFollow();
99             return;
100         case 49: // numeric keypad 1
101             v->goal_view_offset = FG_PI * 0.75;
102             return;
103         case 50: // numeric keypad 2
104             v->goal_view_offset = FG_PI;
105             return;
106         case 51: // numeric keypad 3
107             v->goal_view_offset = FG_PI * 1.25;
108             return;
109         case 52: // numeric keypad 4
110             v->goal_view_offset = FG_PI * 0.50;
111             return;
112         case 54: // numeric keypad 6
113             v->goal_view_offset = FG_PI * 1.50;
114             return;
115         case 55: // numeric keypad 7
116             v->goal_view_offset = FG_PI * 0.25;
117             return;
118         case 56: // numeric keypad 8
119             v->goal_view_offset = 0.00;
120             return;
121         case 57: // numeric keypad 9
122             v->goal_view_offset = FG_PI * 1.75;
123             return;
124         case 72: // H key
125             // status = current_options.get_hud_status();
126             // current_options.set_hud_status(!status);
127             HUD_brightkey( true );
128             return;
129         case 73: // i key
130             // Minimal Hud
131             fgHUDInit2(&current_aircraft);
132             return;
133         case 77: // M key
134             t->warp -= 60;
135             local_update_sky_and_lighting_params();
136             return;
137         case 84: // T key
138             t->warp_delta -= 30;
139             local_update_sky_and_lighting_params();
140             return;
141         case 87: // W key
142 #if defined(FX) && !defined(WIN32)
143             fullscreen = ( !fullscreen );
144 #if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
145             XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
146 #endif
147 #endif
148             return;
149         case 88: // X key
150             fov = current_options.get_fov();
151             fov *= 1.05;
152             if ( fov > FG_FOV_MAX ) {
153                 fov = FG_FOV_MAX;
154             }
155             current_options.set_fov(fov);
156             v->update_fov = TRUE;
157             return;
158         case 90: // Z key
159             tmp = fgWeatherGetVisibility();   // in meters
160             tmp /= 1.10;
161             fgWeatherSetVisibility( tmp );
162             return;
163         }
164     } else {
165         fgPrintf( FG_INPUT, FG_DEBUG, "\n");
166         switch (k) {
167         case 50: // numeric keypad 2
168             fgElevMove(-0.05);
169             return;
170         case 56: // numeric keypad 8
171             fgElevMove(0.05);
172             return;
173         case 49: // numeric keypad 1
174             fgElevTrimMove(-0.001);
175             return;
176         case 55: // numeric keypad 7
177             fgElevTrimMove(0.001);
178             return;
179         case 52: // numeric keypad 4
180             fgAileronMove(-0.05);
181             return;
182         case 54: // numeric keypad 6
183             fgAileronMove(0.05);
184             return;
185         case 48: // numeric keypad Ins
186             fgRudderMove(-0.05);
187             return;
188         case 13: // numeric keypad Enter
189             fgRudderMove(0.05);
190             return;
191         case 53: // numeric keypad 5
192             fgAileronSet(0.0);
193             fgElevSet(0.0);
194             fgRudderSet(0.0);
195             return;
196         case 57: // numeric keypad 9 (Pg Up)
197             fgThrottleMove(0, 0.01);
198             return;
199         case 51: // numeric keypad 3 (Pg Dn)
200             fgThrottleMove(0, -0.01);
201             return;
202         case 98: // b key
203             int b_ret;
204             double b_set;
205             b_ret = int( fgBrakeGet() );
206             b_set = double(!b_ret);
207             fgBrakeSet(b_set);
208             return;
209         case 104: // h key
210             HUD_brightkey( false );
211             return;
212         case 105: // i key
213             fgHUDInit(&current_aircraft);  // normal HUD
214             return;
215         case 109: // m key
216             t->warp += 60;
217             local_update_sky_and_lighting_params();
218             return;
219         case 112: // p key
220             t->pause = !t->pause;
221             // printf position and attitude information
222             fgPrintf( FG_INPUT, FG_INFO,
223                       "Lon = %.4f  Lat = %.4f  Altitude = %.1f\n", 
224                       FG_Longitude * RAD_TO_DEG,
225                       FG_Latitude * RAD_TO_DEG,
226                       FG_Altitude * FEET_TO_METER);
227             fgPrintf( FG_INPUT, FG_INFO,
228                       "Heading = %.2f  Roll = %.2f  Pitch = %.2f\n", 
229                       FG_Psi * RAD_TO_DEG,
230                       FG_Phi * RAD_TO_DEG,
231                       FG_Theta * RAD_TO_DEG);
232             return;
233         case 116: // t key
234             t->warp_delta += 30;
235             local_update_sky_and_lighting_params();
236             return;
237         case 120: // X key
238             fov = current_options.get_fov();
239             fov /= 1.05;
240             if ( fov < FG_FOV_MIN ) {
241                 fov = FG_FOV_MIN;
242             }
243             current_options.set_fov(fov);
244             v->update_fov = TRUE;
245             return;
246         case 122: // z key
247             tmp = fgWeatherGetVisibility();   // in meters
248             tmp *= 1.10;
249             fgWeatherSetVisibility( tmp );
250             return;
251         case 27: // ESC
252             // if( fg_DebugOutput ) {
253             //   fclose( fg_DebugOutput );
254             // }
255             fgPrintf( FG_INPUT, FG_EXIT, 
256                       "Program exiting normally at user request.\n");
257         }
258     }
259 }
260
261
262 // Handle "special" keyboard events
263 void GLUTspecialkey(int k, int x, int y) {
264     fgCONTROLS *c;
265     fgVIEW *v;
266
267     c = current_aircraft.controls;
268     v = &current_view;
269
270     fgPrintf( FG_INPUT, FG_DEBUG, "Special key hit = %d", k);
271     puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN);
272
273     if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
274         fgPrintf( FG_INPUT, FG_DEBUG, " SHIFTED\n");
275         switch (k) {
276         case GLUT_KEY_END: // numeric keypad 1
277             v->goal_view_offset = FG_PI * 0.75;
278             return;
279         case GLUT_KEY_DOWN: // numeric keypad 2
280             v->goal_view_offset = FG_PI;
281             return;
282         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
283             v->goal_view_offset = FG_PI * 1.25;
284             return;
285         case GLUT_KEY_LEFT: // numeric keypad 4
286             v->goal_view_offset = FG_PI * 0.50;
287             return;
288         case GLUT_KEY_RIGHT: // numeric keypad 6
289             v->goal_view_offset = FG_PI * 1.50;
290             return;
291         case GLUT_KEY_HOME: // numeric keypad 7
292             v->goal_view_offset = FG_PI * 0.25;
293             return;
294         case GLUT_KEY_UP: // numeric keypad 8
295             v->goal_view_offset = 0.00;
296             return;
297         case GLUT_KEY_PAGE_UP: // numeric keypad 9
298             v->goal_view_offset = FG_PI * 1.75;
299             return;
300         }
301     } else {
302         fgPrintf( FG_INPUT, FG_DEBUG, "\n");
303         switch (k) {
304         case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
305             current_options.cycle_fog();
306         
307             if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
308                 fgPrintf( FG_INPUT, FG_INFO, "Fog disabled\n" );
309             } else if ( current_options.get_fog() == 
310                         fgOPTIONS::FG_FOG_FASTEST )
311             {
312                 fgPrintf( FG_INPUT, FG_INFO, 
313                           "Fog enabled, hint set to fastest\n" );
314             } else if ( current_options.get_fog() ==
315                         fgOPTIONS::FG_FOG_NICEST )
316             {
317                 fgPrintf( FG_INPUT, FG_INFO,
318                           "Fog enabled, hint set to nicest\n" );
319             }
320
321             return;
322         case GLUT_KEY_F9: // F9 toggles textures on and off...
323             if ( material_mgr.get_textures_loaded() ) {
324                 current_options.get_textures() ?
325                     current_options.set_textures(false) :
326                     current_options.set_textures(true);
327                 fgPrintf( FG_INPUT, FG_INFO, "Toggling texture\n" );
328             } else {
329                 fgPrintf( FG_INPUT, FG_INFO, 
330                           "No textures loaded, cannot toggle\n" );
331             }
332             return;
333         case GLUT_KEY_F10: // F10 toggles menu on and off...
334             fgPrintf(FG_INPUT, FG_INFO, "Invoking call back function");
335             hideMenuButton -> 
336                 setValue ((int) !(hideMenuButton -> getValue() ) );
337             hideMenuButton -> invokeCallback();
338             //exit(1);
339             return;
340         case GLUT_KEY_UP:
341             fgElevMove(0.05);
342             return;
343         case GLUT_KEY_DOWN:
344             fgElevMove(-0.05);
345             return;
346         case GLUT_KEY_LEFT:
347             fgAileronMove(-0.05);
348             return;
349         case GLUT_KEY_RIGHT:
350             fgAileronMove(0.05);
351             return;
352         case GLUT_KEY_HOME: // numeric keypad 1
353             fgElevTrimMove(0.001);
354             return;
355         case GLUT_KEY_END: // numeric keypad 7
356             fgElevTrimMove(-0.001);
357             return;
358         case GLUT_KEY_INSERT: // numeric keypad Ins
359             fgRudderMove(-0.05);
360             return;
361         case 13: // numeric keypad Enter
362             fgRudderMove(0.05);
363             return;
364         case 53: // numeric keypad 5
365             fgAileronSet(0.0);
366             fgElevSet(0.0);
367             fgRudderSet(0.0);
368             return;
369         case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
370             fgThrottleMove(0, 0.01);
371             return;
372         case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
373             fgThrottleMove(0, -0.01);
374             return;
375         }
376     }
377 }
378
379
380 // $Log$
381 // Revision 1.26  1998/10/01 00:38:04  curt
382 // More altitude hold tweaks.
383 //
384 // Revision 1.25  1998/09/29 02:03:36  curt
385 // Autopilot mods.
386 //
387 // Revision 1.24  1998/09/26 13:16:44  curt
388 // C++-ified the comments.
389 //
390 // Revision 1.23  1998/09/17 18:35:30  curt
391 // Added F8 to toggle fog and F9 to toggle texturing.
392 //
393 // Revision 1.22  1998/09/15 04:27:27  curt
394 // Changes for new Astro code.
395 //
396 // Revision 1.21  1998/08/29 13:09:25  curt
397 // Changes to event manager from Bernie Bright.
398 //
399 // Revision 1.20  1998/08/24 20:11:12  curt
400 // Added i/I to toggle full vs. minimal HUD.
401 // Added a --hud-tris vs --hud-culled option.
402 // Moved options accessor funtions to options.hxx.
403 //
404 // Revision 1.19  1998/08/05 00:19:33  curt
405 // Added a local routine to update lighting params every frame when time is
406 // accelerated.
407 //
408 // Revision 1.18  1998/07/30 23:48:24  curt
409 // Output position & orientation when pausing.
410 // Eliminated libtool use.
411 // Added options to specify initial position and orientation.
412 // Changed default fov to 55 degrees.
413 // Added command line option to start in paused or unpaused state.
414 //
415 // Revision 1.17  1998/07/27 18:41:23  curt
416 // Added a pause command "p"
417 // Fixed some initialization order problems between pui and glut.
418 // Added an --enable/disable-sound option.
419 //
420 // Revision 1.16  1998/07/16 17:33:34  curt
421 // "H" / "h" now control hud brightness as well with off being one of the
422 //   states.
423 // Better checking for xmesa/fx 3dfx fullscreen/window support for deciding
424 //   whether or not to build in the feature.
425 // Translucent menu support.
426 // HAVE_AUDIO_SUPPORT -> ENABLE_AUDIO_SUPPORT
427 // Use fork() / wait() for playing mp3 init music in background under unix.
428 // Changed default tile diameter to 5.
429 //
430 // Revision 1.15  1998/07/13 21:01:34  curt
431 // Wrote access functions for current fgOPTIONS.
432 //
433 // Revision 1.14  1998/07/06 02:42:02  curt
434 // Added support for switching between fullscreen and window mode for
435 // Mesa/3dfx/glide.
436 //
437 // Added a basic splash screen.  Restructured the main loop and top level
438 // initialization routines to do this.
439 //
440 // Hacked in some support for playing a startup mp3 sound file while rest
441 // of sim initializes.  Currently only works in Unix using the mpg123 player.
442 // Waits for the mpg123 player to finish before initializing internal
443 // sound drivers.
444 //
445 // Revision 1.13  1998/06/27 16:54:32  curt
446 // Replaced "extern displayInstruments" with a entry in fgOPTIONS.
447 // Don't change the view port when displaying the panel.
448 //
449 // Revision 1.12  1998/06/12 14:27:26  curt
450 // Pui -> PUI, Gui -> GUI.
451 //
452 // Revision 1.11  1998/06/12 00:57:38  curt
453 // Added support for Pui/Gui.
454 // Converted fog to GL_FOG_EXP2.
455 // Link to static simulator parts.
456 // Update runfg.bat to try to be a little smarter.
457 //
458 // Revision 1.10  1998/05/27 02:24:05  curt
459 // View optimizations by Norman Vine.
460 //
461 // Revision 1.9  1998/05/16 13:05:21  curt
462 // Added limits to fov.
463 //
464 // Revision 1.8  1998/05/13 18:29:56  curt
465 // Added a keyboard binding to dynamically adjust field of view.
466 // Added a command line option to specify fov.
467 // Adjusted terrain color.
468 // Root path info moved to fgOPTIONS.
469 // Added ability to parse options out of a config file.
470 //
471 // Revision 1.7  1998/05/07 23:14:14  curt
472 // Added "D" key binding to set autopilot heading.
473 // Made frame rate calculation average out over last 10 frames.
474 // Borland C++ floating point exception workaround.
475 // Added a --tile-radius=n option.
476 //
477 // Revision 1.6  1998/04/28 01:20:20  curt
478 // Type-ified fgTIME and fgVIEW.
479 // Added a command line option to disable textures.
480 //
481 // Revision 1.5  1998/04/25 22:06:29  curt
482 // Edited cvs log messages in source files ... bad bad bad!
483 //
484 // Revision 1.4  1998/04/25 20:24:00  curt
485 // Cleaned up initialization sequence to eliminate interdependencies
486 // between sun position, lighting, and view position.  This creates a
487 // valid single pass initialization path.
488 //
489 // Revision 1.3  1998/04/24 14:19:29  curt
490 // Fog tweaks.
491 //
492 // Revision 1.2  1998/04/24 00:49:17  curt
493 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
494 // Trying out some different option parsing code.
495 // Some code reorganization.
496 //
497 // Revision 1.1  1998/04/22 13:25:40  curt
498 // C++ - ifing the code.
499 // Starting a bit of reorganization of lighting code.
500 //
501 // Revision 1.33  1998/04/18 04:11:25  curt
502 // Moved fg_debug to it's own library, added zlib support.
503 //
504 // Revision 1.32  1998/04/14 02:21:01  curt
505 // Incorporated autopilot heading hold contributed by:  Jeff Goeke-Smith
506 // <jgoeke@voyager.net>
507 //
508 // Revision 1.31  1998/04/08 23:34:05  curt
509 // Patch from Durk to fix trim reversal with numlock key active.
510 //
511 // Revision 1.30  1998/04/03 22:09:02  curt
512 // Converting to Gnu autoconf system.
513 //
514 // Revision 1.29  1998/02/07 15:29:40  curt
515 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
516 // <chotchkiss@namg.us.anritsu.com>
517 //
518 // Revision 1.28  1998/02/03 23:20:23  curt
519 // Lots of little tweaks to fix various consistency problems discovered by
520 // Solaris' CC.  Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
521 // passed arguments along to the real printf().  Also incorporated HUD changes
522 // by Michele America.
523 //
524 // Revision 1.27  1998/01/27 00:47:55  curt
525 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
526 // system and commandline/config file processing code.
527 //
528 // Revision 1.26  1998/01/19 19:27:07  curt
529 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
530 // This should simplify things tremendously.
531 //
532 // Revision 1.25  1998/01/05 18:44:34  curt
533 // Add an option to advance/decrease time from keyboard.
534 //
535 // Revision 1.24  1997/12/30 16:36:46  curt
536 // Merged in Durk's changes ...
537 //
538 // Revision 1.23  1997/12/15 23:54:44  curt
539 // Add xgl wrappers for debugging.
540 // Generate terrain normals on the fly.
541 //
542 // Revision 1.22  1997/12/10 22:37:45  curt
543 // Prepended "fg" on the name of all global structures that didn't have it yet.
544 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
545 //
546 // Revision 1.21  1997/08/27 21:32:23  curt
547 // Restructured view calculation code.  Added stars.
548 //
549 // Revision 1.20  1997/08/27 03:30:13  curt
550 // Changed naming scheme of basic shared structures.
551 //
552 // Revision 1.19  1997/08/25 20:27:21  curt
553 // Merged in initial HUD and Joystick code.
554 //
555 // Revision 1.18  1997/08/22 21:34:38  curt
556 // Doing a bit of reorganizing and house cleaning.
557 //
558 // Revision 1.17  1997/07/19 22:34:02  curt
559 // Moved PI definitions to ../constants.h
560 // Moved random() stuff to ../Utils/ and renamed fg_random()
561 //
562 // Revision 1.16  1997/07/18 23:41:24  curt
563 // Tweaks for building with Cygnus Win32 compiler.
564 //
565 // Revision 1.15  1997/07/16 20:04:47  curt
566 // Minor tweaks to aid Win32 port.
567 //
568 // Revision 1.14  1997/07/12 03:50:20  curt
569 // Added an #include <Windows32/Base.h> to help compiling for Win32
570 //
571 // Revision 1.13  1997/06/25 15:39:46  curt
572 // Minor changes to compile with rsxnt/win32.
573 //
574 // Revision 1.12  1997/06/21 17:12:52  curt
575 // Capitalized subdirectory names.
576 //
577 // Revision 1.11  1997/06/18 04:10:31  curt
578 // A couple more runway tweaks ...
579 //
580 // Revision 1.10  1997/06/18 02:21:23  curt
581 // Hacked in a runway
582 //
583 // Revision 1.9  1997/06/02 03:40:06  curt
584 // A tiny bit more view tweaking.
585 //
586 // Revision 1.8  1997/06/02 03:01:38  curt
587 // Working on views (side, front, back, transitions, etc.)
588 //
589 // Revision 1.7  1997/05/31 19:16:25  curt
590 // Elevator trim added.
591 //
592 // Revision 1.6  1997/05/31 04:13:52  curt
593 // WE CAN NOW FLY!!!
594 //
595 // Continuing work on the LaRCsim flight model integration.
596 // Added some MSFS-like keyboard input handling.
597 //
598 // Revision 1.5  1997/05/30 23:26:19  curt
599 // Added elevator/aileron controls.
600 //
601 // Revision 1.4  1997/05/27 17:44:31  curt
602 // Renamed & rearranged variables and routines.   Added some initial simple
603 // timer/alarm routines so the flight model can be updated on a regular 
604 // interval.
605 //
606 // Revision 1.3  1997/05/23 15:40:25  curt
607 // Added GNU copyright headers.
608 // Fog now works!
609 //
610 // Revision 1.2  1997/05/23 00:35:12  curt
611 // Trying to get fog to work ...
612 //
613 // Revision 1.1  1997/05/21 15:57:50  curt
614 // Renamed due to added GLUT support.
615 //
616 // Revision 1.2  1997/05/19 18:22:41  curt
617 // Parameter tweaking ... starting to stub in fog support.
618 //
619 // Revision 1.1  1997/05/16 16:05:51  curt
620 // Initial revision.
621