]>
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/Aabb.hh>
13 #include <Moof/Log.hh>
14 #include <Moof/OpenGL.hh>
15 #include <Moof/Video.hh>
20 ProgressBar::ProgressBar(const Mf::Texture
& tilemap
,
21 Mf::Texture::TileIndex index
) :
25 tilemap
.getTileCoords(index
, mTexCoords
);
27 Mf::Scalar half
= (mTexCoords
[2] - mTexCoords
[0]) / 2.0 + mTexCoords
[0];
28 mMidCoords
[0] = half
- 0.01;
29 mMidCoords
[1] = half
+ 0.01;
32 void ProgressBar::resize(const Mf::Rectangle
& rect
)
34 Mf::logInfo
<< "rect: " << rect
.min
<< ", " << rect
.max
<< std::endl
;
35 Mf::Scalar height
= rect
.max
[1] - rect
.min
[1];
36 Mf::Scalar halfHeight
= height
/ 2.0;
38 mWidth
= rect
.max
[0] - rect
.min
[0] - height
;
41 mVertices
[0] = rect
.min
;
42 mVertices
[1] = Mf::Vector2(rect
.min
[0] + halfHeight
, rect
.min
[1]);
43 mVertices
[2] = mVertices
[1];
44 mVertices
[3] = Mf::Vector2(rect
.min
[0] + height
, rect
.min
[1]);
45 mVertices
[4] = Mf::Vector2(rect
.min
[0] + height
, rect
.max
[1]);
46 mVertices
[5] = Mf::Vector2(rect
.min
[0] + halfHeight
, rect
.max
[1]);
47 mVertices
[6] = mVertices
[5];
48 mVertices
[7] = Mf::Vector2(rect
.min
[0], rect
.max
[1]);
50 setProgress(mProgress
);
53 void ProgressBar::setProgress(Mf::Scalar progress
)
55 Mf::Scalar halfHeight
= (mVertices
[7][1] - mVertices
[0][1]) / 2.0;
57 mVertices
[2][0] = mVertices
[1][0] + progress
* mWidth
;
58 mVertices
[3][0] = mVertices
[1][0] + progress
* mWidth
+ halfHeight
;
59 mVertices
[4][0] = mVertices
[1][0] + progress
* mWidth
+ halfHeight
;
60 mVertices
[5][0] = mVertices
[1][0] + progress
* mWidth
;
65 void ProgressBar::draw(Mf::Scalar alpha
) const
67 if (Mf::isEqual(mProgress
, 0.0))
69 // don't draw anything if the progress is 0%
73 glColor4f(1.0f
, 1.0f
, 1.0f
, 0.85f
);
77 glTexCoord(mTexCoords
[0], mTexCoords
[1]);
78 glVertex(mVertices
[0]);
79 glTexCoord(mMidCoords
[0], mTexCoords
[3]);
80 glVertex(mVertices
[1]);
81 glTexCoord(mMidCoords
[0], mTexCoords
[5]);
82 glVertex(mVertices
[6]);
83 glTexCoord(mTexCoords
[6], mTexCoords
[7]);
84 glVertex(mVertices
[7]);
86 glTexCoord(mMidCoords
[0], mTexCoords
[1]);
87 glVertex(mVertices
[1]);
88 glTexCoord(mMidCoords
[1], mTexCoords
[3]);
89 glVertex(mVertices
[2]);
90 glTexCoord(mMidCoords
[1], mTexCoords
[5]);
91 glVertex(mVertices
[5]);
92 glTexCoord(mMidCoords
[0], mTexCoords
[7]);
93 glVertex(mVertices
[6]);
95 glTexCoord(mMidCoords
[1], mTexCoords
[1]);
96 glVertex(mVertices
[2]);
97 glTexCoord(mTexCoords
[2], mTexCoords
[3]);
98 glVertex(mVertices
[3]);
99 glTexCoord(mTexCoords
[4], mTexCoords
[5]);
100 glVertex(mVertices
[4]);
101 glTexCoord(mMidCoords
[1], mTexCoords
[7]);
102 glVertex(mVertices
[5]);
107 Hud::Hud(GameState
& state
) :
109 mBar1(Mf::Texture("StatusBars"), 0),
110 mBar2(Mf::Texture("StatusBars"), 2),
113 Mf::Video
* video
= Mf::Video::current();
114 ASSERT(video
&& "a current video context should be set");
115 resize(video
->getWidth(), video
->getHeight());
119 void Hud::resize(int width
, int height
)
121 cml::matrix_orthographic_RH(mProjection
,
123 Mf::Scalar(width
), SCALAR(0.0), Mf::Scalar(height
),
124 SCALAR(1.0), SCALAR(-1.0), cml::z_clip_neg_one
);
126 // position the two progress bars at the top-left of the screen
127 mBar1
.resize(Mf::Rectangle(20, height
- 51, 0.7 * width
, height
- 3));
128 mBar2
.resize(Mf::Rectangle(20, height
- 28, 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
176 parent().removeChild(this);
178 Mf::logWarning("okay bye bye hud");
183 case SDL_VIDEORESIZE
:
184 resize(event
.resize
.w
, event
.resize
.h
);
This page took 0.044842 seconds and 4 git commands to generate.