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(fntRenderer *f, float x, float y, const char *s, int align = 0, int digits = 0);
103 CENTER = (HCENTER|VCENTER),
110 static const int BUFSIZE = 64;
118 TextList() { _font = 0; }
120 void setFont(fntRenderer *Renderer) { _font = Renderer; }
121 void add(float x, float y, const char *s, int align = 0, int digit = 0) {
122 _list.push_back(HUDText(_font, x, y, s, align, digit));
124 void erase() { _list.erase(_list.begin(), _list.end()); }
129 glPushAttrib(GL_COLOR_BUFFER_BIT);
133 vector<HUDText>::iterator it, end = _list.end();
134 for (it = _list.begin(); it != end; ++it)
138 glDisable(GL_TEXTURE_2D);
144 vector<HUDText> _list;
152 class HUD : public SGSubsystem, public SGPropertyChangeListener {
159 // called from Main/renderer.cxx to draw 2D and 3D HUD
162 // listener callback to read various HUD related properties
163 void valueChanged(SGPropertyNode *);
164 // set current glColor
165 void setColor() const;
166 inline bool isVisible() const { return _visible; }
167 inline bool isAntialiased() const { return _antialiased; }
168 inline bool isTransparent() const { return _transparent; }
169 inline bool is3D() const { return _3Denabled; }
170 inline float alphaClamp() const { return _cl; }
171 inline double timer() const { return _timer; }
173 enum Units { FEET, METER };
174 Units getUnits() const { return _units; }
177 HORIZONTAL = 0x0000, // keep that at zero?
190 int load(const char *, float x = 320.0f, float y = 240.0f,
191 int level = 0, const string& indent = "");
195 void draw2D(GLfloat, GLfloat, GLfloat, GLfloat);
204 class TurnBankIndicator;
209 deque<Item *> _items;
211 SGPropertyNode_ptr _current;
212 SGPropertyNode_ptr _visibility;
213 SGPropertyNode_ptr _3DenabledN;
214 SGPropertyNode_ptr _antialiasing;
215 SGPropertyNode_ptr _transparency;
216 SGPropertyNode_ptr _red, _green, _blue, _alpha;
217 SGPropertyNode_ptr _alpha_clamp;
218 SGPropertyNode_ptr _brightness;
223 float _r, _g, _b, _a, _cl;
225 SGPropertyNode_ptr _scr_widthN, _scr_heightN;
226 int _scr_width, _scr_height;
227 SGPropertyNode_ptr _unitsN;
231 fntRenderer *_font_renderer;
232 FGFontCache *_font_cache;
239 LineList _stipple_line_list;
246 Input(const SGPropertyNode *n, float factor = 1.0, float offset = 0.0,
247 float min = -SGLimitsf::max(), float max = SGLimitsf::max()) :
250 _damped(SGLimitsf::max())
254 _factor = n->getFloatValue("factor", factor);
255 _offset = n->getFloatValue("offset", offset);
256 _min = n->getFloatValue("min", min);
257 _max = n->getFloatValue("max", max);
258 _coeff = 1.0 - 1.0 / powf(10, fabsf(n->getFloatValue("damp", 0.0)));
259 SGPropertyNode *p = ((SGPropertyNode *)n)->getNode("property", false);
261 const char *path = p->getStringValue();
262 if (path && path[0]) {
263 _property = fgGetNode(path, true);
269 bool getBoolValue() const {
271 return _property->getBoolValue();
274 const char *getStringValue() const {
276 return _property->getStringValue();
279 float getFloatValue() {
281 float f = _property->getFloatValue() * _factor + _offset;
282 if (_damped == SGLimitsf::max())
285 f = _damped = f * (1.0f - _coeff) + _damped * _coeff;
289 inline float isValid() const { return _valid; }
290 inline float min() const { return _min; }
291 inline float max() const { return _max; }
292 inline float factor() const { return _factor; }
293 float clamp(float v) const { return v < _min ? _min : v > _max ? _max : v; }
295 void set_min(float m, bool force = true) {
296 if (force || _min == -SGLimitsf::max())
299 void set_max(float m, bool force = true) {
300 if (force || _max == SGLimitsf::max())
306 SGConstPropertyNode_ptr _property;
319 Item(HUD *parent, const SGPropertyNode *, float x = 0.0f, float y = 0.0f);
321 virtual void draw() = 0;
322 virtual bool isEnabled();
325 inline float get_span() const { return _scr_span; }
326 inline int get_digits() const { return _digits; }
328 inline bool option_vert() const { return (_options & VERTICAL) == VERTICAL; }
329 inline bool option_left() const { return (_options & LEFT) == LEFT; }
330 inline bool option_right() const { return (_options & RIGHT) == RIGHT; }
331 inline bool option_both() const { return (_options & BOTH) == BOTH; }
332 inline bool option_noticks() const { return (_options & NOTICKS) == NOTICKS; }
333 inline bool option_notext() const { return (_options & NOTEXT) == NOTEXT; }
334 inline bool option_top() const { return (_options & TOP) == TOP; }
335 inline bool option_bottom() const { return (_options & BOTTOM) == BOTTOM; }
337 void draw_line(float x1, float y1, float x2, float y2);
338 void draw_stipple_line(float x1, float y1, float x2, float y2);
339 void draw_text(float x, float y, char *msg, int align = 0, int digit = 0);
340 void draw_circle(float x1, float y1, float r) const;
341 void draw_bullet(float, float, float);
346 float _x, _y, _w, _h;
347 float _center_x, _center_y;
350 SGCondition *_condition;
351 float _disp_factor; // Multiply by to get numbers shown on scale.
352 float _scr_span; // Working values for draw;
358 class HUD::Label : public Item {
360 Label(HUD *parent, const SGPropertyNode *, float x, float y);
374 Format check_format(const char *) const;
380 int _halign; // HUDText alignment
384 float _pointer_width;
385 float _pointer_length;
387 SGCondition *_blink_condition;
388 double _blink_interval;
389 double _blink_target; // time for next blink state change
395 // abstract base class for both moving scale and moving needle (fixed scale)
398 class HUD::Scale : public Item {
400 Scale(HUD *parent, const SGPropertyNode *, float x, float y);
401 virtual void draw ( void ) {} // No-op here. Defined in derived classes.
404 inline float factor() const { return _display_factor; }
405 inline float range_to_show() const { return _range_shown; }
408 float _major_divs; // major division marker units
409 float _minor_divs; // minor division marker units
410 unsigned int _modulo; // Roll over point
413 float _range_shown; // Width Units.
414 float _display_factor; // factor => screen units/range values.
418 class HUD::Gauge : public Scale {
420 Gauge(HUD *parent, const SGPropertyNode *, float x, float y);
426 // displays the indicated quantity on a scale that moves past the
427 // pointer. It may be horizontal or vertical.
429 class HUD::Tape : public Scale {
431 Tape(HUD *parent, const SGPropertyNode *, float x, float y);
435 void fixed(float, float, float, float, float, float);
436 void zoomed_scale(int, int);
440 float _half_width_units;
441 bool _draw_tick_bottom;
443 bool _draw_tick_right;
444 bool _draw_tick_left;
445 bool _draw_cap_bottom;
447 bool _draw_cap_right;
449 float _marker_offset;
453 enum PointerType { FIXED, MOVING } _pointer_type;
454 enum TickType { LINE, CIRCLE } _tick_type;
455 enum TickLength { VARIABLE, CONSTANT } _tick_length;
460 class HUD::Dial : public Scale {
462 Dial(HUD *parent, const SGPropertyNode *, float x, float y);
472 class HUD::TurnBankIndicator : public Item {
474 TurnBankIndicator(HUD *parent, const SGPropertyNode *, float x, float y);
480 void draw_line(float, float, float, float);
481 void draw_tick(float angle, float r1, float r2, int side);
492 class HUD::Ladder : public Item {
494 Ladder(HUD *parent, const SGPropertyNode *, float x, float y);
498 void draw_zenith(float, float);
499 void draw_nadir(float, float);
501 void draw_text(float x, float y, char *s, int align = 0) {
502 _locTextList.add(x, y, s, align, 0);
505 void draw_line(float x1, float y1, float x2, float y2) {
506 _locLineList.add(LineSegment(x1, y1, x2, y2));
509 void draw_stipple_line(float x1, float y1, float x2, float y2) {
510 _locStippleLineList.add(LineSegment(x1, y1, x2, y2));
513 enum Type { PITCH, CLIMB_DIVE } _type;
518 unsigned int _scr_hole;
522 bool _dynamic_origin;
524 bool _frl; // fuselage reference line
526 bool _target_markers;
527 bool _velocity_vector;
531 bool _climb_dive_marker;
532 bool _glide_slope_marker;
535 bool _waypoint_marker;
540 // The Ladder has its own temporary display lists
541 TextList _locTextList;
542 LineList _locLineList;
543 LineList _locStippleLineList;
548 // responsible for rendering the active runway in the hud (if visible).
550 class HUD::Runway : public Item {
552 Runway(HUD *parent, const SGPropertyNode *, float x, float y);
556 void boundPoint(const sgdVec3& v, sgdVec3& m);
557 bool boundOutsidePoints(sgdVec3& v, sgdVec3& m);
558 bool drawLine(const sgdVec3& a1, const sgdVec3& a2, const sgdVec3& p1, const sgdVec3& p2);
560 bool get_active_runway(FGRunway& rwy);
561 void get_rwy_points(sgdVec3 *points);
564 SGPropertyNode_ptr _agl;
565 sgdVec3 _points3d[6], _points2d[6];
568 double _arrow_scale; // scales of runway indication arrow
569 double _arrow_radius;
570 double _line_scale; // maximum line scale
571 double _scale_dist; // distance where to start scaling the lines
572 double _default_pitch;
573 double _default_heading;
576 FGViewer* _cockpit_view;
577 unsigned short _stipple_out; // stipple pattern of the outline of the runway
578 unsigned short _stipple_center; // stipple pattern of the center line of the runway
579 bool _draw_arrow; // draw arrow when runway is not visible in HUD
580 bool _draw_arrow_always; // always draws arrow
581 float _left, _right, _top, _bottom;
585 class HUD::AimingReticle : public Item {
587 AimingReticle(HUD *parent, const SGPropertyNode *, float x, float y);
591 SGCondition *_active_condition; // stadiametric (true) or standby (false)
592 Input _diameter; // inner/outer radius relation