mElite  1.0
An Elite clone based on TextElite by Jan-Philipp Kappmeier and Melanie Schmidt.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
HudLoadBar.cpp
Go to the documentation of this file.
1 
6 #include "GlobalParameters.h"
7 #include "HudLoadBar.h"
8 #include "Textures.h"
9 #include "ColorConstants.h"
10 #include "Structures.h"
11 
13  m_current = 5.0f;
14  m_max = 10.0f;
15  m_min = 0.0f;
16  m_slope = 0;
17  update();
18 }
19 
20 HudLoadBar::HudLoadBar( float min, float max, float current ) {
21  if( min < max ) {
22  m_min = min;
23  m_max = max;
24  setCurrent( current );
25  } else {
26  m_current = 5.0f;
27  m_max = 10.0f;
28  m_min = 0.0f;
29  }
30  m_slope = 0;
31  update();
32 }
33 
35 }
36 
38  draw( 0, 0, 50, 10 );
39 }
40 
41 void HudLoadBar::draw( int x, int y, int width, int height ) {
42  //glDisable( GL_BLEND );
43  glColor3fv( colorWhite );
44  glDisable( GL_LIGHTING );
46  // calculate percentage of current
47  float percent = (m_current-currentDifference) / currentRange;
48 
49  glBegin( GL_QUADS );
50  glTexCoord2f( 0, 1 );
51  glVertex3f( m_slope + x + offset*width, y+height, 0);
52  glTexCoord2f( 0, 0 );
53  glVertex3f( x + offset*width, y, 0);
54  glTexCoord2f( isNegative*percent, 0 );
55  glVertex3f( x+ offset*width +currentPercentage * width*percent, y, 0 );
56  glTexCoord2f( isNegative*percent, 1 );
57  glVertex3f( m_slope + x+ offset*width +currentPercentage * width*percent, y + height, 0 );
58  glEnd();
59  glEnable( GL_BLEND );
60 }
61 
62 void HudLoadBar::setCurrent( float current ) {
63  if( current > m_max)
64  m_current = m_max;
65  else if( current < m_min )
66  m_current = m_min;
67  else
68  m_current = current;
69  update();
70 }
71 
72 void HudLoadBar::setMax( float max ) {
73  if( max > m_min )
74  m_max = max;
75  if( m_max < m_current )
76  m_current = max;
77  update();
78 }
79 
80 void HudLoadBar::setMin( float min ) {
81  if( min < m_max )
82  m_min = min;
83  if( m_min > m_current )
84  m_current = m_min;
85  update();
86 }
87 
88 void HudLoadBar::setSlopePixel( int pixel ) {
89  m_slope = pixel;
90 }
91 
92 void HudLoadBar::update() {
93  // thecases both values greater than 0 and both values smaller than 0 are not tested!
94  float positivePercentage;
95  float negativePercentage;
96  if( m_min >= 0) {
97  positivePercentage = 1;
98  currentRange = m_max - m_min;
99  currentDifference = m_min;
100  } else if( m_max <= 0 ) {
101  negativePercentage = 1;
102  currentRange = m_max - m_min;
103  currentDifference = m_max;
104  } else {
105  currentDifference = 0;
106  positivePercentage = m_max / (m_max - m_min);
107  currentRange = m_current < 0 ? -m_min : m_max;
108  }
109  negativePercentage = 1 - positivePercentage;
110 
111  if( m_current < 0)
112  currentPercentage = negativePercentage;
113  else
114  currentPercentage = positivePercentage;
115 
116  offset = negativePercentage;
117  isNegative = m_current < 0 ? -1.0 : 1.0;
118 }