]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/cockpit.cxx
6f47431978271a824348cda6e728274f4b168480
[flightgear.git] / src / Cockpit / cockpit.cxx
1 // cockpit.cxx -- routines to draw a cockpit (initial draft)
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  - nomimarketing@mail.telepac.pt
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
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H          
29 #  include <windows.h>
30 #endif
31
32 #include <GL/glut.h>
33 #include <simgear/xgl/xgl.h>
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38
39 #include <simgear/constants.h>
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/math/fg_random.h>
42 #include <simgear/math/polar3d.hxx>
43
44 #include <Aircraft/aircraft.hxx>
45 #include <Include/general.hxx>
46 #include <Main/options.hxx>
47 #include <Main/views.hxx>
48 #include <Scenery/scenery.hxx>
49 #include <Time/fg_timer.hxx>
50 #include <Time/fg_time.hxx>
51 #include <GUI/gui.h>
52
53 #include "cockpit.hxx"
54
55
56 // This is a structure that contains all data related to
57 // cockpit/panel/hud system
58
59 static pCockpit ac_cockpit;
60 // The following routines obtain information concerntin the aircraft's
61 // current state and return it to calling instrument display routines.
62 // They should eventually be member functions of the aircraft.
63 //
64
65 float get_latitude( void )
66 {
67     double lat;
68
69     lat = current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
70
71     float flat = lat;
72     return(flat);
73
74 }
75
76 float get_lat_min( void )
77 {
78     double      a, d;
79
80     a = current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;    
81     if (a < 0.0) {
82         a = -a;
83     }
84     d = (double) ( (int) a);
85     float lat_min = (a - d) * 60.0;
86     return(lat_min );
87 }
88
89
90 float get_longitude( void )
91 {
92     double lon;
93
94     lon = current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
95
96     float flon = lon;
97     return(flon);
98 }
99
100
101 char*
102 get_formated_gmt_time( void )
103 {
104     static char buf[32];
105     FGTime *t = FGTime::cur_time_params;
106     const struct tm *p = t->getGmt();
107     sprintf( buf, "%d/%d/%4d %d:%02d:%02d", 
108          p->tm_mon+1, p->tm_mday, 1900 + p->tm_year,
109          p->tm_hour, p->tm_min, p->tm_sec);
110     return buf;
111 }
112
113
114 float get_long_min( void )
115 {
116     double  a, d;
117     a = current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;   
118     if (a < 0.0) {
119         a = -a;
120     }
121     d = (double) ( (int) a);
122     float lon_min = (a - d) * 60.0; 
123     return(lon_min);
124 }
125
126 float get_throttleval( void )
127 {
128     float throttle = controls.get_throttle( 0 );
129     return (throttle);     // Hack limiting to one engine
130 }
131
132 float get_aileronval( void )
133 {
134     float aileronval = controls.get_aileron();
135     return (aileronval);
136 }
137
138 float get_elevatorval( void )
139 {
140     float elevator_val = (float)controls.get_elevator();
141     return elevator_val;
142 }
143
144 float get_elev_trimval( void )
145 {
146     float elevatorval = controls.get_elevator_trim();
147     return (elevatorval);
148 }
149
150 float get_rudderval( void )
151 {
152     float rudderval = controls.get_rudder();
153     return (rudderval);
154 }
155
156 float get_speed( void )
157 {
158     // Make an explicit function call.
159     float speed = current_aircraft.fdm_state->get_V_calibrated_kts()
160         * current_options.get_speed_up();
161     return( speed );
162 }
163
164 float get_mach(void)
165 {
166         float mach=current_aircraft.fdm_state->get_Mach_number();
167         return mach;
168 }       
169
170 float get_aoa( void )
171 {
172     float aoa = current_aircraft.fdm_state->get_Alpha() * RAD_TO_DEG;
173     return( aoa );
174 }
175
176 float get_roll( void )
177 {
178     float roll = current_aircraft.fdm_state->get_Phi();
179     return( roll );
180 }
181
182 float get_pitch( void )
183 {
184     float pitch = current_aircraft.fdm_state->get_Theta();
185     return( pitch );
186 }
187
188 float get_heading( void )
189 {
190     float heading = (current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG);
191     return( heading );
192 }
193
194 float get_altitude( void )
195 {
196 //  FGState *f;
197     // double rough_elev;
198
199 //  current_aircraft.fdm_state
200     // rough_elev = mesh_altitude(f->get_Longitude() * RAD_TO_ARCSEC,
201     //                         f->get_Latitude()  * RAD_TO_ARCSEC);
202     float altitude;
203
204     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
205         altitude = current_aircraft.fdm_state->get_Altitude();
206     } else {
207         altitude = (current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER);
208     }
209     return altitude;
210 }
211
212 float get_agl( void )
213 {
214     float agl;
215
216     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
217         agl = (current_aircraft.fdm_state->get_Altitude()
218                - scenery.cur_elev * METER_TO_FEET);
219     } else {
220         agl = (current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER
221                - scenery.cur_elev);
222     }
223     return agl;
224 }
225
226 float get_sideslip( void )
227 {
228     float sideslip = current_aircraft.fdm_state->get_Beta();
229     return( sideslip );
230 }
231
232 float get_frame_rate( void )
233 {
234     float frame_rate = general.get_frame_rate();
235     return (frame_rate); 
236 }
237
238 float get_fov( void )
239 {
240     float fov = current_options.get_fov(); 
241     return (fov);
242 }
243
244 float get_vfc_ratio( void )
245 {
246     // float vfc = current_view.get_vfc_ratio();
247     // return (vfc);
248     return 0.0;
249 }
250
251 float get_vfc_tris_drawn   ( void )
252 {
253     // float rendered = current_view.get_tris_rendered();
254     // return (rendered);
255     return 0.0;
256 }
257
258 float get_vfc_tris_culled   ( void )
259 {
260     // float culled = current_view.get_tris_culled();
261     // return (culled);
262     return 0.0;
263 }
264
265 float get_climb_rate( void )
266 {
267     float climb_rate;
268     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
269         climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
270     } else {
271         climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * FEET_TO_METER * 60.0;
272     }
273     return (climb_rate);
274 }
275
276
277 float get_view_direction( void )
278 {
279     double view;
280  
281     view = FG_2PI - current_view.get_view_offset();
282     view = ( current_aircraft.fdm_state->get_Psi() + view) * RAD_TO_DEG;
283     
284     if(view > 360.)
285         view -= 360.;
286     else if(view<0.)
287         view += 360.;
288     
289     float fview = view;
290     return( fview );
291 }
292
293 #ifdef NOT_USED
294 /****************************************************************************/
295 /* Convert degrees to dd mm'ss.s" (DMS-Format)                              */
296 /****************************************************************************/
297 char *dmshh_format(double degrees)
298 {
299     static char buf[16];    
300     int deg_part;
301     int min_part;
302     double sec_part;
303     
304     if (degrees < 0)
305       degrees = -degrees;
306
307     deg_part = degrees;
308     min_part = 60.0 * (degrees - deg_part);
309     sec_part = 3600.0 * (degrees - deg_part - min_part / 60.0);
310
311     /* Round off hundredths */
312     if (sec_part + 0.005 >= 60.0)
313       sec_part -= 60.0, min_part += 1;
314     if (min_part >= 60)
315       min_part -= 60, deg_part += 1;
316
317     sprintf(buf,"%02d*%02d %05.2f",deg_part,min_part,sec_part);
318
319     return buf;
320 }
321 #endif // 0
322
323 /****************************************************************************/
324 /* Convert degrees to dd mm'ss.s'' (DMS-Format)                              */
325 /****************************************************************************/
326 static char *toDMS(float a)
327 {
328   int        neg = 0;
329   float       d, m, s;
330   static char  dms[16];
331
332   if (a < 0.0f) {
333     a   = -a;
334     neg = 1;
335   }
336   d = (float) ((int) a); 
337   a = (a - d) * 60.0f;
338   m = (float) ((int) a);
339   s = (a - m) * 60.0f;
340   
341   if (s > 59.5f) {
342     s  = 0.0f;
343     m += 1.0f;
344   }
345   if (m > 59.5f) {
346     m  = 0.0f;
347     d += 1.0f;
348   }
349   if (neg)
350     d = -d;
351   
352   sprintf(dms, "%.0f*%02.0f %04.1f", d, m, s);
353   return dms;
354 }
355
356
357 /****************************************************************************/
358 /* Convert degrees to dd mm.mmm' (DMM-Format)                               */
359 /****************************************************************************/
360 static char *toDM(float a)
361 {
362   int        neg = 0;
363   float       d, m;
364   static char  dm[16];
365   
366   if (a < 0.0f) {
367     a = -a;
368     neg = 1;
369   }
370
371   d = (float) ( (int) a);
372   m = (a - d) * 60.0f;
373   
374   if (m > 59.5f) {
375     m  = 0.0f;
376     d += 1.0f;
377   }
378   if (neg) d = -d;
379   
380   sprintf(dm, "%.0f*%06.3f", d, m);
381   return dm;
382 }
383
384 // Have to set the LatLon display type
385 //static char *(*fgLatLonFormat)(float) = toDM;
386 static char *(*fgLatLonFormat)(float);
387
388 char *coord_format_lat(float latitude)
389 {
390     static char buf[16];
391
392     sprintf(buf,"%s%c",
393 //      dmshh_format(latitude),
394 //      toDMS(latitude),
395 //      toDM(latitude),
396         fgLatLonFormat(latitude),           
397         latitude > 0 ? 'N' : 'S');
398     return buf;
399 }
400
401 char *coord_format_lon(float longitude)
402 {
403     static char buf[80];
404
405     sprintf(buf,"%s%c",
406 //      dmshh_format(longitude),
407 //      toDMS(longitude),
408 //      toDM(longitude),
409         fgLatLonFormat(longitude),
410         longitude > 0 ? 'E' : 'W');
411     return buf;
412 }
413
414 void fgLatLonFormatToggle( puObject *)
415 {
416     static int toggle = 0;
417
418     if ( toggle ) 
419         fgLatLonFormat = toDM;
420     else
421         fgLatLonFormat = toDMS;
422     
423     toggle = ~toggle;
424 }
425
426 #ifdef NOT_USED
427 char *coord_format_latlon(double latitude, double longitude)
428 {
429     static char buf[1024];
430
431     sprintf(buf,"%s%c %s%c",
432         dmshh_format(latitude),
433         latitude > 0 ? 'N' : 'S',
434         dmshh_format(longitude),
435         longitude > 0 ? 'E' : 'W');
436     return buf;
437 }
438 #endif
439
440
441 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
442 {
443     FG_LOG( FG_COCKPIT, FG_INFO, "Initializing cockpit subsystem" );
444
445     //  cockpit->code = 1;  /* It will be aircraft dependent */
446     //  cockpit->status = 0;
447
448     // If aircraft has HUD specified we will get the specs from its def
449     // file. For now we will depend upon hard coding in hud?
450     
451     // We must insure that the existing instrument link is purged.
452     // This is done by deleting the links in the list.
453     
454     // HI_Head is now a null pointer so we can generate a new list from the
455     // current aircraft.
456
457     fgHUDInit( cur_aircraft );
458     ac_cockpit = new fg_Cockpit();
459     
460     // Have to set the LatLon display type
461     fgLatLonFormat = toDM;
462     
463     FG_LOG( FG_COCKPIT, FG_INFO,
464         "  Code " << ac_cockpit->code() << " Status " 
465         << ac_cockpit->status() );
466
467         return true;
468 }
469
470 void fgCockpitUpdate( void ) {
471
472     FG_LOG( FG_COCKPIT, FG_DEBUG,
473         "Cockpit: code " << ac_cockpit->code() << " status " 
474         << ac_cockpit->status() );
475
476         int iwidth   = current_view.get_winWidth();
477         int iheight  = current_view.get_winHeight();
478         float width  = iwidth;
479         float height = iheight;
480
481     if ( current_options.get_hud_status() ) {
482         // This will check the global hud linked list pointer.
483         // If these is anything to draw it will.
484         fgUpdateHUD();
485     }
486 #define DISPLAY_COUNTER
487 #ifdef DISPLAY_COUNTER
488     else
489     {
490         char buf[64];
491         float fps    =       get_frame_rate();
492 //      float tris   = fps * get_vfc_tris_drawn();
493 //      float culled = fps * get_vfc_tris_culled();
494 //      sprintf(buf,"%-4.1f  %7.0f  %7.0f", fps, tris, culled);
495         sprintf(buf,"%-5.1f", fps);
496
497         glMatrixMode(GL_PROJECTION);
498         glPushMatrix();
499         glLoadIdentity();
500         gluOrtho2D(0, width, 0, height);
501         glMatrixMode(GL_MODELVIEW);
502         glPushMatrix();
503         glLoadIdentity();
504
505         glDisable(GL_DEPTH_TEST);
506         glDisable(GL_LIGHTING);
507         
508         glColor3f (0.9, 0.4, 0.2);
509
510         guiFnt.drawString( buf,
511                            // width/2 - guiFnt.getStringWidth(buf)/2,
512                            int(width - guiFnt.getStringWidth(buf) - 10),
513                            10 );
514         glEnable(GL_DEPTH_TEST);
515         glEnable(GL_LIGHTING);
516         glMatrixMode(GL_PROJECTION);
517         glPopMatrix();
518         glMatrixMode(GL_MODELVIEW);
519         glPopMatrix();
520     }
521 #endif // #ifdef DISPLAY_COUNTER
522     
523     xglViewport( 0, 0, iwidth, iheight );
524
525     current_panel->update();
526 }