]> Dogcows Code - chaz/yoink/blob - src/Hud.cc
sockets documentation and cleanup
[chaz/yoink] / src / Hud.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
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.
9 *
10 **************************************************************************/
11
12 #include <Moof/Aabb.hh>
13 #include <Moof/Log.hh>
14 #include <Moof/OpenGL.hh>
15 #include <Moof/Video.hh>
16
17 #include "Hud.hh"
18
19
20 ProgressBar::ProgressBar(const Mf::Texture& tilemap,
21 Mf::Texture::TileIndex index) :
22 mProgress(0.0),
23 mTilemap(tilemap)
24 {
25 tilemap.getTileCoords(index, mTexCoords);
26
27 Mf::Scalar half = (mTexCoords[2] - mTexCoords[0]) / 2.0 + mTexCoords[0];
28 mMidCoords[0] = half - 0.01;
29 mMidCoords[1] = half + 0.01;
30 }
31
32 void ProgressBar::resize(const Mf::Rectangle& rect)
33 {
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;
37
38 mWidth = rect.max[0] - rect.min[0] - height;
39 ASSERT(mWidth > 0);
40
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]);
49
50 setProgress(mProgress);
51 }
52
53 void ProgressBar::setProgress(Mf::Scalar progress)
54 {
55 Mf::Scalar halfHeight = (mVertices[7][1] - mVertices[0][1]) / 2.0;
56
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;
61
62 mProgress = progress;
63 }
64
65 void ProgressBar::draw(Mf::Scalar alpha) const
66 {
67 if (Mf::isEqual(mProgress, 0.0))
68 {
69 // don't draw anything if the progress is 0%
70 return;
71 }
72
73 glColor4f(1.0f, 1.0f, 1.0f, 0.85f);
74 mTilemap.bind();
75
76 glBegin(GL_QUADS);
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]);
85
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]);
94
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]);
103 glEnd();
104 }
105
106
107 Hud::Hud(GameState& state) :
108 mState(state),
109 mBar1(Mf::Texture("StatusBars"), 0),
110 mBar2(Mf::Texture("StatusBars"), 2),
111 mFont("Font")
112 {
113 Mf::Video* video = Mf::Video::current();
114 ASSERT(video && "a current video context should be set");
115 resize(video->getWidth(), video->getHeight());
116 }
117
118
119 void Hud::resize(int width, int height)
120 {
121 cml::matrix_orthographic_RH(mProjection,
122 SCALAR(0.0),
123 Mf::Scalar(width), SCALAR(0.0), Mf::Scalar(height),
124 SCALAR(1.0), SCALAR(-1.0), cml::z_clip_neg_one);
125
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));
129
130 setBar1Progress(0.05);
131 setBar2Progress(0.0);
132 }
133
134
135 void Hud::update(Mf::Scalar t, Mf::Scalar dt)
136 {
137 mState.interp.update(t, dt);
138 setBar1Progress(mState.interp.getState(dt));
139 setBar2Progress(1.0 - mState.interp.getState(dt));
140 }
141
142 void Hud::draw(Mf::Scalar alpha) const
143 {
144 glMatrixMode(GL_PROJECTION);
145 glPushMatrix();
146 glLoadMatrix(mProjection.data());
147
148 glMatrixMode(GL_MODELVIEW);
149 glPushMatrix();
150 glLoadIdentity();
151
152 glDisable(GL_DEPTH_TEST);
153 glEnable(GL_BLEND);
154
155 mBar1.draw();
156 mBar2.draw();
157
158 glDisable(GL_BLEND);
159 glEnable(GL_DEPTH_TEST);
160
161 glMatrixMode(GL_PROJECTION);
162 glPopMatrix();
163
164 glMatrixMode(GL_MODELVIEW);
165 glPopMatrix();
166 }
167
168 bool Hud::handleEvent(const Mf::Event& event)
169 {
170 switch (event.type)
171 {
172 case SDL_KEYUP:
173 if (event.key.keysym.sym == SDLK_h)
174 {
175 // don't want the hud anymore
176 parent().removeChild(this);
177
178 Mf::logWarning("okay bye bye hud");
179 return true;
180 }
181 break;
182
183 case SDL_VIDEORESIZE:
184 resize(event.resize.w, event.resize.h);
185 break;
186 }
187
188 return false;
189 }
190
This page took 0.03754 seconds and 4 git commands to generate.