]> git.mxchange.org Git - flightgear.git/blob - utils/fgpanel/panel.cxx
Fix standalone terrasync build
[flightgear.git] / utils / fgpanel / panel.cxx
1 //  panel.cxx - default, 2D single-engine prop instrument panel
2 //
3 //  Written by David Megginson, started January 2000.
4 //
5 //  This program is free software; you can redistribute it and/or
6 //  modify it under the terms of the GNU General Public License as
7 //  published by the Free Software Foundation; either version 2 of the
8 //  License, or (at your option) any later version.
9 // 
10 //  This program is distributed in the hope that it will be useful, but
11 //  WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 //  General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 //
19 //  $Id: panel.cxx,v 1.44 2006/09/05 20:28:48 curt Exp $
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #ifdef HAVE_WINDOWS_H          
26 #  include <windows.h>
27 #endif
28
29 #include <stdio.h>      // sprintf
30 #include <string.h>
31
32 #include <simgear/compiler.h>
33
34 #if defined (SG_MAC)
35 #include <GLUT/glut.h>
36 #else
37 #include <GL/glut.h>
38 #endif
39
40 #include <plib/fnt.h>
41
42 #include <simgear/debug/logstream.hxx>
43 #include <simgear/misc/sg_path.hxx>
44
45 #include "panel.hxx"
46 #include "ApplicationProperties.hxx"
47 ////////////////////////////////////////////////////////////////////////
48 // Local functions.
49 ////////////////////////////////////////////////////////////////////////
50
51 class FGDummyTextureLoader : public FGTextureLoaderInterface {
52 public:
53   virtual GLuint loadTexture( const string & filename );
54 };
55
56 GLuint FGDummyTextureLoader::loadTexture( const string & filename )
57 {
58   GLuint _texture = 0;
59   glGenTextures( 1, &_texture );
60   glBindTexture( GL_TEXTURE_2D, _texture );
61
62 //  glTexEnvi       ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
63 //  glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ) ;
64 //  glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ) ;
65 //  glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ) ;
66 //  glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) ;
67
68   GLubyte image[ 2 * 2 * 3 ] ;
69
70   /* Red and white chequerboard */
71   image [ 0 ] = 255 ; image [ 1 ] =  0  ; image [ 2 ] =  0  ;
72   image [ 3 ] = 255 ; image [ 4 ] = 255 ; image [ 5 ] = 255 ;
73   image [ 6 ] = 255 ; image [ 7 ] = 255 ; image [ 8 ] = 255 ;
74   image [ 9 ] = 255 ; image [ 10] =  0  ; image [ 11] =  0  ;
75
76   glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, 2, 2, 0,
77         GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*) image);
78
79   return _texture;
80 }
81
82 ////////////////////////////////////////////////////////////////////////
83 // Implementation of FGCropped Texture.
84 ////////////////////////////////////////////////////////////////////////
85
86 GLuint FGCroppedTexture::current_bound_texture = 0;
87 map<string,GLuint> FGCroppedTexture::cache;
88 map<string,FGTextureLoaderInterface*> FGCroppedTexture::textureLoader;
89 static FGDummyTextureLoader dummyTextureLoader;
90
91 FGCroppedTexture::FGCroppedTexture (const string &path,
92                                     float minX, float minY,
93                                     float maxX, float maxY)
94   : _path(path),
95     _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY), _texture(0)
96 {
97 }
98
99 FGCroppedTexture::~FGCroppedTexture ()
100 {
101 }
102
103 void FGCroppedTexture::bind( bool doGLBind )
104 {
105   if( _texture == 0 ) {
106     SG_LOG( SG_COCKPIT, SG_DEBUG, "First bind of texture " << _path );
107     if( cache.count(_path) > 0 ) {
108       _texture = cache[_path];
109       SG_LOG( SG_COCKPIT, SG_DEBUG, "Using texture " << _path << " from cache (#" << _texture << ")" );
110     } else {
111       SGPath tpath = ApplicationProperties::GetRootPath(_path.c_str());
112       string extension = tpath.extension();
113       FGTextureLoaderInterface * loader = &dummyTextureLoader;
114       if( textureLoader.count( extension ) == 0 ) {
115         SG_LOG( SG_COCKPIT, SG_ALERT, "Can't handle textures of type " << extension );
116       } else {
117         loader = textureLoader[extension];
118       }
119
120       _texture = loader->loadTexture( tpath.c_str() );
121       SG_LOG( SG_COCKPIT, SG_DEBUG, "Texture " << tpath.c_str() << " loaded from file as #" << _texture );
122       
123       cache[_path] = _texture;
124     }
125   }
126
127   if( !doGLBind || current_bound_texture == _texture )
128     return;
129
130   glBindTexture( GL_TEXTURE_2D, _texture );
131   current_bound_texture = _texture;
132 }
133
134 \f
135 ////////////////////////////////////////////////////////////////////////
136 // Implementation of FGPanel.
137 ////////////////////////////////////////////////////////////////////////
138
139 /**
140  * Constructor.
141  */
142 FGPanel::FGPanel ( SGPropertyNode_ptr root)
143   : _root(root),
144     _flipx(root->getNode("/sim/panel/flip-x", true)),
145     _rotate(root->getNode("/sim/panel/rotate-deg", true)),
146     _bg_width(1.0), _bg_height(1.0),
147     initDisplayList(0)
148 {
149 }
150
151
152 /**
153  * Destructor.
154  */
155 FGPanel::~FGPanel ()
156 {
157   for (instrument_list_type::iterator it = _instruments.begin();
158        it != _instruments.end();
159        it++) {
160     delete *it;
161     *it = 0;
162   }
163 }
164
165
166 /**
167  * Add an instrument to the panel.
168  */
169 void
170 FGPanel::addInstrument (FGPanelInstrument * instrument)
171 {
172   _instruments.push_back(instrument);
173 }
174
175
176 /**
177  * Initialize the panel.
178  */
179 void
180 FGPanel::init ()
181 {
182 }
183
184
185 /**
186  * Bind panel properties.
187  */
188 void
189 FGPanel::bind ()
190 {
191 }
192
193
194 /**
195  * Unbind panel properties.
196  */
197 void
198 FGPanel::unbind ()
199 {
200 }
201
202 GLuint FGPanel::getInitDisplayList()
203 {
204   if( initDisplayList != 0 ) return initDisplayList;
205   glMatrixMode(GL_PROJECTION);
206   glLoadIdentity();
207   if ( _flipx->getBoolValue() ) {
208     gluOrtho2D( _width, 0, _height, 0 ); /* up side down */
209   } else {
210     gluOrtho2D( 0, _width, 0, _height ); /* right side up */
211   }
212
213   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
214   
215   glMatrixMode(GL_MODELVIEW);
216   glLoadIdentity();
217
218   glClear( GL_COLOR_BUFFER_BIT);
219
220   // save some state
221   glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT
222                 | GL_TEXTURE_BIT | GL_PIXEL_MODE_BIT | GL_CULL_FACE 
223                 | GL_DEPTH_BUFFER_BIT );
224
225   // Draw the background
226   glEnable(GL_TEXTURE_2D);
227
228   glDisable(GL_LIGHTING);
229   glEnable(GL_BLEND);
230   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
231   glEnable(GL_ALPHA_TEST);
232   glEnable(GL_COLOR_MATERIAL);
233   glEnable(GL_CULL_FACE);
234   glCullFace(GL_BACK);
235   glDisable(GL_DEPTH_TEST);
236
237   if (_bg != NULL) {
238     _bg->bind();
239 //    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
240     glBegin(GL_QUADS);
241     glTexCoord2f(0.0, 0.0); glVertex2f(0, 0);
242     glTexCoord2f(_bg_width, 0.0); glVertex2f(_width, 0);
243     glTexCoord2f(_bg_width, _bg_height); glVertex2f(_width, _height);
244     glTexCoord2f(0.0, _bg_height); glVertex2f(0, _height);
245     glEnd();
246   } else if( _mbg[0] != NULL ) {
247     for (int i = 0; i < 4; i ++) {
248       // top row of textures...(1,3,5,7)
249       _mbg[i*2]->bind();
250 //      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
251       glBegin(GL_QUADS);
252       glTexCoord2f(0.0, 0.0); glVertex2f(i*_width/4,     _height/2);
253       glTexCoord2f(1.0, 0.0); glVertex2f((i+1)*_width/4, _height/2);
254       glTexCoord2f(1.0, 1.0); glVertex2f((i+1)*_width/4, _height);
255       glTexCoord2f(0.0, 1.0); glVertex2f(i*_width/4,     _height);
256       glEnd();
257       // bottom row of textures...(2,4,6,8)
258       _mbg[i*2+1]->bind();
259 //      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
260       glBegin(GL_QUADS);
261       glTexCoord2f(0.0, 0.0); glVertex2f( i*_width/4,     0);
262       glTexCoord2f(1.0, 0.0); glVertex2f( (i+1)*_width/4, 0);
263       glTexCoord2f(1.0, 1.0); glVertex2f( (i+1)*_width/4, _height/2);
264       glTexCoord2f(0.0, 1.0); glVertex2f( i*_width/4,     _height/2);
265       glEnd();
266     }
267   } else {
268     float c[4];
269     glGetFloatv( GL_CURRENT_COLOR, c );
270     glColor4f( 0.0, 0.0, 0.0, 1.0 );
271     glBegin(GL_QUADS);
272     glVertex2f(0, 0);
273     glVertex2f(_width, 0);
274     glVertex2f(_width, _height);
275     glVertex2f(0, _height);
276     glEnd();
277     glColor4fv( c );
278   }
279
280
281   return initDisplayList;  
282 }
283
284 void
285 FGPanel::update (double dt)
286 {
287   /*glCallList*/(getInitDisplayList());
288
289   // Draw the instruments.
290   // Syd Adams: added instrument clipping
291   instrument_list_type::const_iterator current = _instruments.begin();
292   instrument_list_type::const_iterator end = _instruments.end();
293
294   GLdouble blx[4]={1.0,0.0,0.0,0.0};
295   GLdouble bly[4]={0.0,1.0,0.0,0.0};
296   GLdouble urx[4]={-1.0,0.0,0.0,0.0};
297   GLdouble ury[4]={0.0,-1.0,0.0,0.0};
298
299   for ( ; current != end; current++) {
300     FGPanelInstrument * instr = *current;
301     glPushMatrix();
302     glTranslated(instr->getXPos(), instr->getYPos(), 0);
303
304     int ix= instr->getWidth();
305     int iy= instr->getHeight();
306     glPushMatrix();
307     glTranslated(-ix/2,-iy/2,0);
308     glClipPlane(GL_CLIP_PLANE0,blx);
309     glClipPlane(GL_CLIP_PLANE1,bly);
310     glEnable(GL_CLIP_PLANE0);
311     glEnable(GL_CLIP_PLANE1);
312
313     glTranslated(ix,iy,0);
314     glClipPlane(GL_CLIP_PLANE2,urx);
315     glClipPlane(GL_CLIP_PLANE3,ury);
316     glEnable(GL_CLIP_PLANE2);
317     glEnable(GL_CLIP_PLANE3);
318     glPopMatrix();
319     instr->draw();
320
321     glPopMatrix();
322   }
323
324   glDisable(GL_CLIP_PLANE0);
325   glDisable(GL_CLIP_PLANE1);
326   glDisable(GL_CLIP_PLANE2);
327   glDisable(GL_CLIP_PLANE3);
328
329   // restore some original state
330   glPopAttrib();
331 }
332
333 #if 0
334 /**
335  * Update the panel.
336  */
337 void
338 FGPanel::update (double dt)
339 {
340   glMatrixMode(GL_PROJECTION);
341   glLoadIdentity();
342   if ( _flipx->getBoolValue() ) {
343     gluOrtho2D( _width, 0, _height, 0 ); /* up side down */
344   } else {
345     gluOrtho2D( 0, _width, 0, _height ); /* right side up */
346   }
347
348   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
349   
350   glMatrixMode(GL_MODELVIEW);
351   glLoadIdentity();
352   
353   draw();
354 }
355
356 void FGPanel::draw()
357 {
358   glClear( GL_COLOR_BUFFER_BIT);
359
360   // save some state
361   glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT
362                 | GL_TEXTURE_BIT | GL_PIXEL_MODE_BIT | GL_CULL_FACE 
363                 | GL_DEPTH_BUFFER_BIT );
364
365   // Draw the background
366   glEnable(GL_TEXTURE_2D);
367
368   glDisable(GL_LIGHTING);
369   glEnable(GL_BLEND);
370   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
371   glEnable(GL_ALPHA_TEST);
372   glEnable(GL_COLOR_MATERIAL);
373   glEnable(GL_CULL_FACE);
374   glCullFace(GL_BACK);
375   glDisable(GL_DEPTH_TEST);
376
377   if (_bg != NULL) {
378     _bg->bind();
379 //    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
380     glBegin(GL_QUADS);
381     glTexCoord2f(0.0, 0.0); glVertex2f(0, 0);
382     glTexCoord2f(_bg_width, 0.0); glVertex2f(_width, 0);
383     glTexCoord2f(_bg_width, _bg_height); glVertex2f(_width, _height);
384     glTexCoord2f(0.0, _bg_height); glVertex2f(0, _height);
385     glEnd();
386   } else if( _mbg[0] != NULL ) {
387     for (int i = 0; i < 4; i ++) {
388       // top row of textures...(1,3,5,7)
389       _mbg[i*2]->bind();
390 //      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
391       glBegin(GL_QUADS);
392       glTexCoord2f(0.0, 0.0); glVertex2f(i*_width/4,     _height/2);
393       glTexCoord2f(1.0, 0.0); glVertex2f((i+1)*_width/4, _height/2);
394       glTexCoord2f(1.0, 1.0); glVertex2f((i+1)*_width/4, _height);
395       glTexCoord2f(0.0, 1.0); glVertex2f(i*_width/4,     _height);
396       glEnd();
397       // bottom row of textures...(2,4,6,8)
398       _mbg[i*2+1]->bind();
399 //      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
400       glBegin(GL_QUADS);
401       glTexCoord2f(0.0, 0.0); glVertex2f( i*_width/4,     0);
402       glTexCoord2f(1.0, 0.0); glVertex2f( (i+1)*_width/4, 0);
403       glTexCoord2f(1.0, 1.0); glVertex2f( (i+1)*_width/4, _height/2);
404       glTexCoord2f(0.0, 1.0); glVertex2f( i*_width/4,     _height/2);
405       glEnd();
406     }
407   } else {
408     float c[4];
409     glGetFloatv( GL_CURRENT_COLOR, c );
410     glColor4f( 0.0, 0.0, 0.0, 1.0 );
411     glBegin(GL_QUADS);
412     glVertex2f(0, 0);
413     glVertex2f(_width, 0);
414     glVertex2f(_width, _height);
415     glVertex2f(0, _height);
416     glEnd();
417     glColor4fv( c );
418   }
419
420   // Draw the instruments.
421   // Syd Adams: added instrument clipping
422   instrument_list_type::const_iterator current = _instruments.begin();
423   instrument_list_type::const_iterator end = _instruments.end();
424
425   GLdouble blx[4]={1.0,0.0,0.0,0.0};
426   GLdouble bly[4]={0.0,1.0,0.0,0.0};
427   GLdouble urx[4]={-1.0,0.0,0.0,0.0};
428   GLdouble ury[4]={0.0,-1.0,0.0,0.0};
429
430   for ( ; current != end; current++) {
431     FGPanelInstrument * instr = *current;
432     glPushMatrix();
433     glTranslated(instr->getXPos(), instr->getYPos(), 0);
434
435     int ix= instr->getWidth();
436     int iy= instr->getHeight();
437     glPushMatrix();
438     glTranslated(-ix/2,-iy/2,0);
439     glClipPlane(GL_CLIP_PLANE0,blx);
440     glClipPlane(GL_CLIP_PLANE1,bly);
441     glEnable(GL_CLIP_PLANE0);
442     glEnable(GL_CLIP_PLANE1);
443
444     glTranslated(ix,iy,0);
445     glClipPlane(GL_CLIP_PLANE2,urx);
446     glClipPlane(GL_CLIP_PLANE3,ury);
447     glEnable(GL_CLIP_PLANE2);
448     glEnable(GL_CLIP_PLANE3);
449     glPopMatrix();
450     instr->draw();
451
452     glPopMatrix();
453   }
454
455   glDisable(GL_CLIP_PLANE0);
456   glDisable(GL_CLIP_PLANE1);
457   glDisable(GL_CLIP_PLANE2);
458   glDisable(GL_CLIP_PLANE3);
459
460   // restore some original state
461   glPopAttrib();
462 }
463 #endif
464
465 /**
466  * Set the panel's background texture.
467  */
468 void
469 FGPanel::setBackground (FGCroppedTexture_ptr texture)
470 {
471   _bg = texture;
472 }
473
474 /**
475  * Set the panel's multiple background textures.
476  */
477 void
478 FGPanel::setMultiBackground (FGCroppedTexture_ptr texture, int idx)
479 {
480   _bg = 0;
481   _mbg[idx] = texture;
482 }
483
484 ////////////////////////////////////////////////////////////////////////
485 // Implementation of FGPanelTransformation.
486 ////////////////////////////////////////////////////////////////////////
487
488 FGPanelTransformation::FGPanelTransformation ()
489   : table(0)
490 {
491 }
492
493 FGPanelTransformation::~FGPanelTransformation ()
494 {
495   delete table;
496 }
497
498
499 \f
500 ////////////////////////////////////////////////////////////////////////
501 // Implementation of FGPanelInstrument.
502 ////////////////////////////////////////////////////////////////////////
503
504
505 FGPanelInstrument::FGPanelInstrument ()
506 {
507   setPosition(0, 0);
508   setSize(0, 0);
509 }
510
511 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
512 {
513   setPosition(x, y);
514   setSize(w, h);
515 }
516
517 FGPanelInstrument::~FGPanelInstrument ()
518 {
519 }
520
521 void
522 FGPanelInstrument::setPosition (int x, int y)
523 {
524   _x = x;
525   _y = y;
526 }
527
528 void
529 FGPanelInstrument::setSize (int w, int h)
530 {
531   _w = w;
532   _h = h;
533 }
534
535 int
536 FGPanelInstrument::getXPos () const
537 {
538   return _x;
539 }
540
541 int
542 FGPanelInstrument::getYPos () const
543 {
544   return _y;
545 }
546
547 int
548 FGPanelInstrument::getWidth () const
549 {
550   return _w;
551 }
552
553 int
554 FGPanelInstrument::getHeight () const
555 {
556   return _h;
557 }
558
559 \f
560 ////////////////////////////////////////////////////////////////////////
561 // Implementation of FGLayeredInstrument.
562 ////////////////////////////////////////////////////////////////////////
563
564 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
565   : FGPanelInstrument(x, y, w, h)
566 {
567 }
568
569 FGLayeredInstrument::~FGLayeredInstrument ()
570 {
571   for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
572     delete *it;
573     *it = 0;
574   }
575 }
576
577 void
578 FGLayeredInstrument::draw ()
579 {
580   if (!test())
581     return;
582   
583   for (int i = 0; i < (int)_layers.size(); i++) {
584     glPushMatrix();
585     _layers[i]->draw();
586     glPopMatrix();
587   }
588 }
589
590 int
591 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
592 {
593   int n = _layers.size();
594   if (layer->getWidth() == -1) {
595     layer->setWidth(getWidth());
596   }
597   if (layer->getHeight() == -1) {
598     layer->setHeight(getHeight());
599   }
600   _layers.push_back(layer);
601   return n;
602 }
603
604 int
605 FGLayeredInstrument::addLayer (FGCroppedTexture_ptr texture, int w, int h)
606 {
607   return addLayer(new FGTexturedLayer(texture, w, h));
608 }
609
610 void
611 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
612 {
613   int layer = _layers.size() - 1;
614   _layers[layer]->addTransformation(transformation);
615 }
616
617
618 \f
619 ////////////////////////////////////////////////////////////////////////
620 // Implementation of FGInstrumentLayer.
621 ////////////////////////////////////////////////////////////////////////
622
623 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
624   : _w(w),
625     _h(h)
626 {
627 }
628
629 FGInstrumentLayer::~FGInstrumentLayer ()
630 {
631   for (transformation_list::iterator it = _transformations.begin();
632        it != _transformations.end();
633        it++) {
634     delete *it;
635     *it = 0;
636   }
637 }
638
639 void
640 FGInstrumentLayer::transform () const
641 {
642   transformation_list::const_iterator it = _transformations.begin();
643   transformation_list::const_iterator last = _transformations.end();
644   while (it != last) {
645     FGPanelTransformation *t = *it;
646     if (t->test()) {
647       float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
648
649       if (t->has_mod)
650           val = fmod(val, t->mod);
651       if (val < t->min) {
652         val = t->min;
653       } else if (val > t->max) {
654         val = t->max;
655       }
656
657       if (t->table==0) {
658         val = val * t->factor + t->offset;
659       } else {
660         val = t->table->interpolate(val) * t->factor + t->offset;
661      }
662       
663       switch (t->type) {
664       case FGPanelTransformation::XSHIFT:
665         glTranslatef(val, 0.0, 0.0);
666         break;
667       case FGPanelTransformation::YSHIFT:
668         glTranslatef(0.0, val, 0.0);
669         break;
670       case FGPanelTransformation::ROTATION:
671         glRotatef(-val, 0.0, 0.0, 1.0);
672         break;
673       }
674     }
675     it++;
676   }
677 }
678
679 void
680 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
681 {
682   _transformations.push_back(transformation);
683 }
684
685
686 \f
687 ////////////////////////////////////////////////////////////////////////
688 // Implementation of FGGroupLayer.
689 ////////////////////////////////////////////////////////////////////////
690
691 FGGroupLayer::FGGroupLayer ()
692 {
693 }
694
695 FGGroupLayer::~FGGroupLayer ()
696 {
697   for (unsigned int i = 0; i < _layers.size(); i++)
698     delete _layers[i];
699 }
700
701 void
702 FGGroupLayer::draw ()
703 {
704   if (test()) {
705     transform();
706     int nLayers = _layers.size();
707     for (int i = 0; i < nLayers; i++)
708       _layers[i]->draw( );
709   }
710 }
711
712 void
713 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
714 {
715   _layers.push_back(layer);
716 }
717
718
719 \f
720 ////////////////////////////////////////////////////////////////////////
721 // Implementation of FGTexturedLayer.
722 ////////////////////////////////////////////////////////////////////////
723
724
725 FGTexturedLayer::FGTexturedLayer (FGCroppedTexture_ptr texture, int w, int h)
726   : FGInstrumentLayer(w, h),
727     _emissive(false),
728     displayList(0)
729 {
730   setTexture(texture);
731 }
732
733
734 FGTexturedLayer::~FGTexturedLayer ()
735 {
736 }
737
738 GLuint
739 FGTexturedLayer::getDisplayList()
740 {
741   if( displayList != 0 )
742     return displayList;
743
744   int w2 = _w / 2;
745   int h2 = _h / 2;
746
747   _texture->bind( false );
748   displayList = glGenLists(1);
749   glNewList(displayList,GL_COMPILE_AND_EXECUTE);
750     glBindTexture( GL_TEXTURE_2D, _texture->getTexture() );
751     glBegin(GL_QUADS);
752       glTexCoord2f(_texture->getMinX(), _texture->getMinY()); glVertex2f(-w2, -h2);
753       glTexCoord2f(_texture->getMaxX(), _texture->getMinY()); glVertex2f(w2, -h2);
754       glTexCoord2f(_texture->getMaxX(), _texture->getMaxY()); glVertex2f(w2, h2);
755       glTexCoord2f(_texture->getMinX(), _texture->getMaxY()); glVertex2f(-w2, h2);
756     glEnd();
757   glEndList();
758
759   return displayList;
760 }
761
762 void
763 FGTexturedLayer::draw ( )
764 {
765   if (test()) {
766     transform();
767     glCallList(getDisplayList());
768   }
769 }
770
771
772 \f
773 ////////////////////////////////////////////////////////////////////////
774 // Implementation of FGTextLayer.
775 ////////////////////////////////////////////////////////////////////////
776
777 fntRenderer FGTextLayer::text_renderer;
778
779 FGTextLayer::FGTextLayer (int w, int h)
780   : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("Helvetica.txf")
781 {
782   _then.stamp();
783   _color[0] = _color[1] = _color[2] = 0.0;
784   _color[3] = 1.0;
785 }
786
787 FGTextLayer::~FGTextLayer ()
788 {
789   chunk_list::iterator it = _chunks.begin();
790   chunk_list::iterator last = _chunks.end();
791   for ( ; it != last; it++) {
792     delete *it;
793   }
794 }
795
796 void
797 FGTextLayer::draw ()
798 {
799   if (test()) {
800     float c[4];
801     glGetFloatv( GL_CURRENT_COLOR, c );
802     glColor4fv(_color);
803     transform();
804
805     text_renderer.setFont(ApplicationProperties::fontCache.getTexFont(_font_name.c_str()));
806     if (!text_renderer.getFont())
807     {
808         SG_LOG( SG_COCKPIT, SG_ALERT, "Missing font file: " << _font_name );
809         return;
810     }
811
812     text_renderer.setPointSize(_pointSize);
813     text_renderer.begin();
814     text_renderer.start3f(0, 0, 0);
815
816     _now.stamp();
817     long diff = (_now - _then).toUSecs();
818
819     if (diff > 100000 || diff < 0 ) {
820       // ( diff < 0 ) is a sanity check and indicates our time stamp
821       // difference math probably overflowed.  We can handle a max
822       // difference of 35.8 minutes since the returned value is in
823       // usec.  So if the panel is left off longer than that we can
824       // over flow the math with it is turned back on.  This (diff <
825       // 0) catches that situation, get's us out of trouble, and
826       // back on track.
827       recalc_value();
828       _then = _now;
829     }
830
831     // Something is goofy.  The code in this file renders only CCW
832     // polygons, and I have verified that the font code in plib
833     // renders only CCW trianbles.  Yet they come out backwards.
834     // Something around here or in plib is either changing the winding
835     // order or (more likely) pushing a left-handed matrix onto the
836     // stack.  But I can't find it; get out the chainsaw...
837     glFrontFace(GL_CW);
838     text_renderer.puts((char *)(_value.c_str()));
839     glFrontFace(GL_CCW);
840
841     text_renderer.end();
842     glColor4fv( c );
843   }
844 }
845
846 void
847 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
848 {
849   _chunks.push_back(chunk);
850 }
851
852 void
853 FGTextLayer::setColor (float r, float g, float b)
854 {
855   _color[0] = r;
856   _color[1] = g;
857   _color[2] = b;
858   _color[3] = 1.0;
859 }
860
861 void
862 FGTextLayer::setPointSize (float size)
863 {
864   _pointSize = size;
865 }
866
867 void
868 FGTextLayer::setFontName(const string &name)
869 {
870   _font_name = name + ".txf";
871 }
872
873
874 void
875 FGTextLayer::setFont(fntFont * font)
876 {
877   FGTextLayer::text_renderer.setFont(font);
878 }
879
880
881 void
882 FGTextLayer::recalc_value () const
883 {
884   _value = "";
885   chunk_list::const_iterator it = _chunks.begin();
886   chunk_list::const_iterator last = _chunks.end();
887   for ( ; it != last; it++) {
888     _value += (*it)->getValue();
889   }
890 }
891
892
893 \f
894 ////////////////////////////////////////////////////////////////////////
895 // Implementation of FGTextLayer::Chunk.
896 ////////////////////////////////////////////////////////////////////////
897
898 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
899   : _type(FGTextLayer::TEXT), _fmt(fmt)
900 {
901   _text = text;
902   if (_fmt.empty()) 
903     _fmt = "%s";
904 }
905
906 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
907                            const string &fmt, float mult, float offs,
908                            bool truncation)
909   : _type(type), _fmt(fmt), _mult(mult), _offs(offs), _trunc(truncation)
910 {
911   if (_fmt.empty()) {
912     if (type == TEXT_VALUE)
913       _fmt = "%s";
914     else
915       _fmt = "%.2f";
916   }
917   _node = node;
918 }
919
920 const char *
921 FGTextLayer::Chunk::getValue () const
922 {
923   if (test()) {
924     _buf[0] = '\0';
925     switch (_type) {
926     case TEXT:
927       sprintf(_buf, _fmt.c_str(), _text.c_str());
928       return _buf;
929     case TEXT_VALUE:
930       sprintf(_buf, _fmt.c_str(), _node->getStringValue());
931       break;
932     case DOUBLE_VALUE:
933       double d = _offs + _node->getFloatValue() * _mult;
934       if (_trunc)  d = (d < 0) ? -floor(-d) : floor(d);
935       sprintf(_buf, _fmt.c_str(), d);
936       break;
937     }
938     return _buf;
939   } else {
940     return "";
941   }
942 }
943
944
945 \f
946 ////////////////////////////////////////////////////////////////////////
947 // Implementation of FGSwitchLayer.
948 ////////////////////////////////////////////////////////////////////////
949
950 FGSwitchLayer::FGSwitchLayer ()
951   : FGGroupLayer()
952 {
953 }
954
955 void
956 FGSwitchLayer::draw ()
957 {
958   if (test()) {
959     transform();
960     int nLayers = _layers.size();
961     for (int i = 0; i < nLayers; i++) {
962       if (_layers[i]->test()) {
963           _layers[i]->draw();
964           return;
965       }
966     }
967   }
968 }
969
970 \f
971 // end of panel.cxx