1 // HUD.hxx -- Head Up Display
3 // Written by Michele America, started September 1997.
5 // Copyright (C) 1997 Michele F. America [micheleamerica#geocities:com]
6 // Copyright (C) 2006 Melchior FRANZ [mfranz#aon:at]
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <simgear/compiler.h>
37 SG_USING_NAMESPACE(std);
41 #include <simgear/math/SGLimits.hxx>
42 #include <simgear/constants.h>
43 #include <simgear/structure/subsystem_mgr.hxx>
45 #include <Airports/runways.hxx> // FGRunway
46 #include <GUI/gui.h> // fntRenderer ? guiErrorMessage()
47 #include <GUI/new_gui.hxx> // FGFontCache, FGColor
48 #include <Include/fg_typedefs.h>
49 #include <Main/fg_props.hxx>
59 LineSegment(GLfloat x0, GLfloat y0, GLfloat x1, GLfloat y1)
60 : _x0(x0), _y0(y0), _x1(x1), _y1(y1) {}
68 GLfloat _x0, _y0, _x1, _y1;
75 void add(const LineSegment& seg) { _list.push_back(seg); }
76 void erase() { _list.erase(_list.begin(), _list.end()); }
77 inline unsigned int size() const { return _list.size(); }
80 vector<LineSegment>::const_iterator it, end = _list.end();
81 for (it = _list.begin(); it != end; ++it)
87 vector<LineSegment> _list;
94 HUDText(float x, float y, char *s, int d = 0) : _x(x), _y(y), _digits(d) {
95 strncpy(_msg, s, BUFSIZE);
98 // this code is changed to display Numbers with big/small digits
99 // according to MIL Standards for example Altitude above 10000 ft
100 // is shown as 10ooo.
102 void draw(fntRenderer *fnt) {
103 if (!_digits) { // show all digits in same size
104 fnt->start2f(_x, _y);
114 //if negative value then increase the c and p values
116 c++; // was moved to the comment. Unintentionally? TODO
120 while (tmp[i] != '\0') {
121 if ((tmp[i] >= '0') && (tmp[i] <= '9'))
126 float orig_size = fnt->getPointSize();
128 fnt->setPointSize(orig_size * 0.8);
130 char *tmp1 = _msg + p1;
133 fnt->start2f(_x + p2, _y);
136 fnt->setPointSize(orig_size * 1.2);
138 strncpy(tmp2, _msg, p1);
141 fnt->start2f(_x, _y);
144 fnt->setPointSize(orig_size * 1.2);
145 fnt->start2f(_x, _y);
148 fnt->setPointSize(orig_size);
154 static const int BUFSIZE = 64;
162 TextList() { _font = 0; }
164 void setFont(fntRenderer *Renderer) { _font = Renderer; }
165 void add(const HUDText& String) { _list.push_back(String); }
166 void erase() { _list.erase(_list.begin(), _list.end()); }
171 glPushAttrib(GL_COLOR_BUFFER_BIT);
175 vector<HUDText>::iterator it, end = _list.end();
176 for (it = _list.begin(); it != end; ++it)
180 glDisable(GL_TEXTURE_2D);
186 vector<HUDText> _list;
194 class HUD : public SGSubsystem, public SGPropertyChangeListener {
201 // called from Main/renderer.cxx to draw 2D and 3D HUD
204 // listener callback to read various HUD related properties
205 void valueChanged(SGPropertyNode *);
206 // set current glColor
207 void setColor() const;
208 inline bool isVisible() const { return _visible; }
209 inline bool isAntialiased() const { return _antialiased; }
210 inline bool isTransparent() const { return _transparent; }
211 inline bool is3D() const { return _3Denabled; }
212 inline float alphaClamp() const { return _cl; }
213 inline double timer() const { return _timer; }
215 enum Units { FEET, METER };
216 Units getUnits() const { return _units; }
219 HORZ = 0x0000, // keep that at zero?
241 int load(const char *, float x = 320.0f, float y = 240.0f,
242 int level = 0, const string& indent = "");
246 void draw2D(GLfloat, GLfloat, GLfloat, GLfloat);
255 class TurnBankIndicator;
260 deque<Item *> _items;
262 SGPropertyNode_ptr _current;
263 SGPropertyNode_ptr _visibility;
264 SGPropertyNode_ptr _3DenabledN;
265 SGPropertyNode_ptr _antialiasing;
266 SGPropertyNode_ptr _transparency;
267 SGPropertyNode_ptr _red, _green, _blue, _alpha;
268 SGPropertyNode_ptr _alpha_clamp;
269 SGPropertyNode_ptr _brightness;
274 float _r, _g, _b, _a, _cl;
276 SGPropertyNode_ptr _scr_widthN, _scr_heightN;
277 int _scr_width, _scr_height;
278 SGPropertyNode_ptr _unitsN;
282 fntRenderer *_font_renderer;
283 FGFontCache *_font_cache;
290 LineList _stipple_line_list;
297 Input(const SGPropertyNode *n, float factor = 1.0, float offset = 0.0,
298 float min = -SGLimitsf::max(), float max = SGLimitsf::max()) :
301 _damped(SGLimitsf::max())
305 _factor = n->getFloatValue("factor", factor);
306 _offset = n->getFloatValue("offset", offset);
307 _min = n->getFloatValue("min", min);
308 _max = n->getFloatValue("max", max);
309 _coeff = 1.0 - 1.0 / powf(10, fabsf(n->getFloatValue("damp", 0.0)));
310 SGPropertyNode *p = ((SGPropertyNode *)n)->getNode("property", false);
312 const char *path = p->getStringValue();
313 if (path && path[0]) {
314 _property = fgGetNode(path, true);
320 bool getBoolValue() const {
322 return _property->getBoolValue();
325 const char *getStringValue() const {
327 return _property->getStringValue();
330 float getFloatValue() {
332 float f = _property->getFloatValue() * _factor + _offset;
333 if (_damped == SGLimitsf::max())
336 f = _damped = f * (1.0f - _coeff) + _damped * _coeff;
340 inline float isValid() const { return _valid; }
341 inline float min() const { return _min; }
342 inline float max() const { return _max; }
343 inline float factor() const { return _factor; }
344 float clamp(float v) const { return v < _min ? _min : v > _max ? _max : v; }
346 void set_min(float m, bool force = true) {
347 if (force || _min == -SGLimitsf::max())
350 void set_max(float m, bool force = true) {
351 if (force || _max == SGLimitsf::max())
357 SGConstPropertyNode_ptr _property;
370 Item(HUD *parent, const SGPropertyNode *, float x = 0.0f, float y = 0.0f);
372 virtual void draw() = 0;
373 virtual bool isEnabled();
376 inline float get_span() const { return _scr_span; }
377 inline int get_digits() const { return _digits; }
379 inline bool option_vert() const { return (_options & VERT) == VERT; }
380 inline bool option_left() const { return (_options & LEFT) == LEFT; }
381 inline bool option_right() const { return (_options & RIGHT) == RIGHT; }
382 inline bool option_both() const { return (_options & BOTH) == BOTH; }
383 inline bool option_noticks() const { return (_options & NOTICKS) == NOTICKS; }
384 inline bool option_notext() const { return (_options & NOTEXT) == NOTEXT; }
385 inline bool option_top() const { return (_options & TOP) == TOP; }
386 inline bool option_bottom() const { return (_options & BOTTOM) == BOTTOM; }
388 void draw_line( float x1, float y1, float x2, float y2);
389 void draw_stipple_line( float x1, float y1, float x2, float y2);
390 void draw_text( float x, float y, char *msg, int digit);
391 float text_width(char *str) const;
392 void draw_circle(float x1, float y1, float r) const;
393 void draw_bullet(float, float, float);
398 float _x, _y, _w, _h;
399 float _center_x, _center_y;
402 SGCondition *_condition;
403 float _disp_factor; // Multiply by to get numbers shown on scale.
404 float _scr_span; // Working values for draw;
410 class HUD::Label : public Item {
412 Label(HUD *parent, const SGPropertyNode *, float x, float y);
426 Format check_format(const char *) const;
436 float _pointer_width;
437 float _pointer_length;
439 SGCondition *_blink_condition;
440 double _blink_interval;
441 double _blink_target; // time for next blink state change
447 // abstract base class for both moving scale and moving needle (fixed scale)
450 class HUD::Scale : public Item {
452 Scale(HUD *parent, const SGPropertyNode *, float x, float y);
453 virtual void draw ( void ) {} // No-op here. Defined in derived classes.
456 inline float factor() const { return _display_factor; }
457 inline float range_to_show() const { return _range_shown; }
460 float _major_divs; // major division marker units
461 float _minor_divs; // minor division marker units
462 unsigned int _modulo; // Roll over point
465 float _range_shown; // Width Units.
466 float _display_factor; // factor => screen units/range values.
470 class HUD::Gauge : public Scale {
472 Gauge(HUD *parent, const SGPropertyNode *, float x, float y);
478 // displays the indicated quantity on a scale that moves past the
479 // pointer. It may be horizontal or vertical.
481 class HUD::Tape : public Scale {
483 Tape(HUD *parent, const SGPropertyNode *, float x, float y);
487 void fixed(float, float, float, float, float, float);
488 void zoomed_scale(int, int);
492 float _half_width_units;
493 bool _draw_tick_bottom;
495 bool _draw_tick_right;
496 bool _draw_tick_left;
497 bool _draw_cap_bottom;
499 bool _draw_cap_right;
501 float _marker_offset;
506 enum PointerType { FIXED, MOVING } _pointer_type;
507 enum TickType { LINE, CIRCLE } _tick_type;
508 enum TickLength { VARIABLE, CONSTANT } _tick_length;
513 class HUD::Dial : public Scale {
515 Dial(HUD *parent, const SGPropertyNode *, float x, float y);
525 class HUD::TurnBankIndicator : public Item {
527 TurnBankIndicator(HUD *parent, const SGPropertyNode *, float x, float y);
536 float _bank_scale_radius;
541 class HUD::Ladder : public Item {
543 Ladder(HUD *parent, const SGPropertyNode *, float x, float y);
547 void draw_zenith(float, float);
548 void draw_nadir(float, float);
550 void draw_text(float x, float y, char *s) {
551 _locTextList.add(HUDText(x, y, s));
554 void draw_line(float x1, float y1, float x2, float y2) {
555 _locLineList.add(LineSegment(x1, y1, x2, y2));
558 void draw_stipple_line(float x1, float y1, float x2, float y2) {
559 _locStippleLineList.add(LineSegment(x1, y1, x2, y2));
562 enum Type { PITCH, CLIMB_DIVE } _type;
567 unsigned int _scr_hole;
571 bool _frl; // fuselage reference line
573 bool _velocity_vector;
577 bool _climb_dive_marker;
578 bool _glide_slope_marker;
581 bool _waypoint_marker;
586 // The Ladder has its own temporary display lists
587 TextList _locTextList;
588 LineList _locLineList;
589 LineList _locStippleLineList;
594 // responsible for rendering the active runway in the hud (if visible).
596 class HUD::Runway : public Item {
598 Runway(HUD *parent, const SGPropertyNode *, float x, float y);
602 void boundPoint(const sgdVec3& v, sgdVec3& m);
603 bool boundOutsidePoints(sgdVec3& v, sgdVec3& m);
604 bool drawLine(const sgdVec3& a1, const sgdVec3& a2, const sgdVec3& p1, const sgdVec3& p2);
606 bool get_active_runway(FGRunway& rwy);
607 void get_rwy_points(sgdVec3 *points);
610 SGPropertyNode_ptr _agl;
611 sgdVec3 _points3d[6], _points2d[6];
614 double _arrow_scale; // scales of runway indication arrow
615 double _arrow_radius;
616 double _line_scale; // maximum line scale
617 double _scale_dist; // distance where to start scaling the lines
618 double _default_pitch;
619 double _default_heading;
622 FGViewer* _cockpit_view;
623 unsigned short _stipple_out; // stipple pattern of the outline of the runway
624 unsigned short _stipple_center; // stipple pattern of the center line of the runway
625 bool _draw_arrow; // draw arrow when runway is not visible in HUD
626 bool _draw_arrow_always; // always draws arrow
627 float _left, _right, _top, _bottom;
631 class HUD::AimingReticle : public Item {
633 AimingReticle(HUD *parent, const SGPropertyNode *, float x, float y);
637 SGCondition *_active_condition; // stadiametric (true) or standby (false)
638 Input _diameter; // inner/outer radius relation