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