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