]>
Dogcows Code - chaz/yoink/blob - src/Hud.cc
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
12 #include <Moof/Core.hh>
13 #include <Moof/OpenGL.hh>
14 #include <Moof/Video.hh>
19 ProgressBar::ProgressBar(const Mf::Texture
& tilemap
,
20 Mf::Texture::TileIndex index
) :
24 tilemap
.getTileCoords(index
, mTexCoords
);
26 Mf::Scalar half
= (mTexCoords
[2] - mTexCoords
[0]) / 2.0 + mTexCoords
[0];
27 mMidCoords
[0] = half
- 0.01;
28 mMidCoords
[1] = half
+ 0.01;
31 void ProgressBar::resize(const Mf::Rectangle
& rect
)
33 Mf::Scalar height
= rect
.max
[1] - rect
.min
[1];
34 Mf::Scalar halfHeight
= height
/ 2.0;
36 mWidth
= rect
.max
[0] - rect
.min
[0] - height
;
39 mVertices
[0] = rect
.min
;
40 mVertices
[1] = Mf::Vector2(rect
.min
[0] + halfHeight
, rect
.min
[1]);
41 mVertices
[2] = mVertices
[1];
42 mVertices
[3] = Mf::Vector2(rect
.min
[0] + height
, rect
.min
[1]);
43 mVertices
[4] = Mf::Vector2(rect
.min
[0] + height
, rect
.max
[1]);
44 mVertices
[5] = Mf::Vector2(rect
.min
[0] + halfHeight
, rect
.max
[1]);
45 mVertices
[6] = mVertices
[5];
46 mVertices
[7] = Mf::Vector2(rect
.min
[0], rect
.max
[1]);
48 setProgress(mProgress
);
51 void ProgressBar::setProgress(Mf::Scalar progress
)
53 Mf::Scalar halfHeight
= (mVertices
[7][1] - mVertices
[0][1]) / 2.0;
55 mVertices
[2][0] = mVertices
[1][0] + progress
* mWidth
;
56 mVertices
[3][0] = mVertices
[1][0] + progress
* mWidth
+ halfHeight
;
57 mVertices
[4][0] = mVertices
[1][0] + progress
* mWidth
+ halfHeight
;
58 mVertices
[5][0] = mVertices
[1][0] + progress
* mWidth
;
63 void ProgressBar::draw(Mf::Scalar alpha
) const
65 if (Mf::isEqual(mProgress
, 0.0))
67 // don't draw anything if the progress is 0%
71 glColor4f(1.0f
, 1.0f
, 1.0f
, 0.85f
);
75 glTexCoord(mTexCoords
[0], mTexCoords
[1]);
76 glVertex(mVertices
[0]);
77 glTexCoord(mMidCoords
[0], mTexCoords
[3]);
78 glVertex(mVertices
[1]);
79 glTexCoord(mMidCoords
[0], mTexCoords
[5]);
80 glVertex(mVertices
[6]);
81 glTexCoord(mTexCoords
[6], mTexCoords
[7]);
82 glVertex(mVertices
[7]);
84 glTexCoord(mMidCoords
[0], mTexCoords
[1]);
85 glVertex(mVertices
[1]);
86 glTexCoord(mMidCoords
[1], mTexCoords
[3]);
87 glVertex(mVertices
[2]);
88 glTexCoord(mMidCoords
[1], mTexCoords
[5]);
89 glVertex(mVertices
[5]);
90 glTexCoord(mMidCoords
[0], mTexCoords
[7]);
91 glVertex(mVertices
[6]);
93 glTexCoord(mMidCoords
[1], mTexCoords
[1]);
94 glVertex(mVertices
[2]);
95 glTexCoord(mTexCoords
[2], mTexCoords
[3]);
96 glVertex(mVertices
[3]);
97 glTexCoord(mTexCoords
[4], mTexCoords
[5]);
98 glVertex(mVertices
[4]);
99 glTexCoord(mMidCoords
[1], mTexCoords
[7]);
100 glVertex(mVertices
[5]);
105 Hud::Hud(GameState
& state
) :
107 mBar1(Mf::Texture("StatusBars"), 0),
108 mBar2(Mf::Texture("StatusBars"), 2),
112 "no current video context from which to get dimensions");
113 resize(Mf::video
->getWidth(), Mf::video
->getHeight());
117 void Hud::resize(int width
, int height
)
119 cml::matrix_orthographic_RH(mProjection
,
121 Mf::Scalar(width
), SCALAR(0.0), Mf::Scalar(height
),
122 SCALAR(1.0), SCALAR(-1.0), cml::z_clip_neg_one
);
124 // position the two progress bars at the top-left of the screen
125 mBar1
.resize(Mf::Rectangle(20, height
- 51,
126 0.7 * width
, height
- 3));
127 mBar2
.resize(Mf::Rectangle(20, height
- 28,
128 0.7 * width
, height
- 70));
130 setBar1Progress(0.05);
131 setBar2Progress(0.0);
135 void Hud::update(Mf::Scalar t
, Mf::Scalar dt
)
137 mState
.interp
.update(t
, dt
);
138 setBar1Progress(mState
.interp
.getState(dt
));
139 setBar2Progress(1.0 - mState
.interp
.getState(dt
));
142 void Hud::draw(Mf::Scalar alpha
) const
144 glMatrixMode(GL_PROJECTION
);
146 glLoadMatrix(mProjection
.data());
148 glMatrixMode(GL_MODELVIEW
);
152 glDisable(GL_DEPTH_TEST
);
159 glEnable(GL_DEPTH_TEST
);
161 glMatrixMode(GL_PROJECTION
);
164 glMatrixMode(GL_MODELVIEW
);
168 bool Hud::handleEvent(const Mf::Event
& event
)
173 if (event
.key
.keysym
.sym
== SDLK_h
)
175 // don't want the hud anymore
181 case SDL_VIDEORESIZE
:
182 resize(event
.resize
.w
, event
.resize
.h
);
This page took 0.038896 seconds and 4 git commands to generate.