]> Dogcows Code - chaz/yoink/blob - src/Hud.cc
game loop tweaks; shapes hierarchy defined
[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/Core.hh>
14 #include <Moof/Log.hh>
15 #include <Moof/OpenGL.hh>
16 #include <Moof/Video.hh>
17
18 #include "Hud.hh"
19
20
21 ProgressBar::ProgressBar(const Mf::Texture& tilemap,
22 Mf::Texture::TileIndex index) :
23 mProgress(0.0),
24 mTilemap(tilemap)
25 {
26 tilemap.getTileCoords(index, mTexCoords);
27
28 Mf::Scalar half = (mTexCoords[2] - mTexCoords[0]) / 2.0 + mTexCoords[0];
29 mMidCoords[0] = half - 0.01;
30 mMidCoords[1] = half + 0.01;
31 }
32
33 void ProgressBar::resize(const Mf::Rectangle& rect)
34 {
35 Mf::logInfo << "rect: " << rect.min << ", " << rect.max << std::endl;
36 Mf::Scalar height = rect.max[1] - rect.min[1];
37 Mf::Scalar halfHeight = height / 2.0;
38
39 mWidth = rect.max[0] - rect.min[0] - height;
40 ASSERT(mWidth > 0);
41
42 mVertices[0] = rect.min;
43 mVertices[1] = Mf::Vector2(rect.min[0] + halfHeight, rect.min[1]);
44 mVertices[2] = mVertices[1];
45 mVertices[3] = Mf::Vector2(rect.min[0] + height, rect.min[1]);
46 mVertices[4] = Mf::Vector2(rect.min[0] + height, rect.max[1]);
47 mVertices[5] = Mf::Vector2(rect.min[0] + halfHeight, rect.max[1]);
48 mVertices[6] = mVertices[5];
49 mVertices[7] = Mf::Vector2(rect.min[0], rect.max[1]);
50
51 setProgress(mProgress);
52 }
53
54 void ProgressBar::setProgress(Mf::Scalar progress)
55 {
56 Mf::Scalar halfHeight = (mVertices[7][1] - mVertices[0][1]) / 2.0;
57
58 mVertices[2][0] = mVertices[1][0] + progress * mWidth;
59 mVertices[3][0] = mVertices[1][0] + progress * mWidth + halfHeight;
60 mVertices[4][0] = mVertices[1][0] + progress * mWidth + halfHeight;
61 mVertices[5][0] = mVertices[1][0] + progress * mWidth;
62
63 mProgress = progress;
64 }
65
66 void ProgressBar::draw(Mf::Scalar alpha) const
67 {
68 if (Mf::isEqual(mProgress, 0.0))
69 {
70 // don't draw anything if the progress is 0%
71 return;
72 }
73
74 glColor4f(1.0f, 1.0f, 1.0f, 0.85f);
75 mTilemap.bind();
76
77 glBegin(GL_QUADS);
78 glTexCoord(mTexCoords[0], mTexCoords[1]);
79 glVertex(mVertices[0]);
80 glTexCoord(mMidCoords[0], mTexCoords[3]);
81 glVertex(mVertices[1]);
82 glTexCoord(mMidCoords[0], mTexCoords[5]);
83 glVertex(mVertices[6]);
84 glTexCoord(mTexCoords[6], mTexCoords[7]);
85 glVertex(mVertices[7]);
86
87 glTexCoord(mMidCoords[0], mTexCoords[1]);
88 glVertex(mVertices[1]);
89 glTexCoord(mMidCoords[1], mTexCoords[3]);
90 glVertex(mVertices[2]);
91 glTexCoord(mMidCoords[1], mTexCoords[5]);
92 glVertex(mVertices[5]);
93 glTexCoord(mMidCoords[0], mTexCoords[7]);
94 glVertex(mVertices[6]);
95
96 glTexCoord(mMidCoords[1], mTexCoords[1]);
97 glVertex(mVertices[2]);
98 glTexCoord(mTexCoords[2], mTexCoords[3]);
99 glVertex(mVertices[3]);
100 glTexCoord(mTexCoords[4], mTexCoords[5]);
101 glVertex(mVertices[4]);
102 glTexCoord(mMidCoords[1], mTexCoords[7]);
103 glVertex(mVertices[5]);
104 glEnd();
105 }
106
107
108 Hud::Hud(GameState& state) :
109 mState(state),
110 mBar1(Mf::Texture("StatusBars"), 0),
111 mBar2(Mf::Texture("StatusBars"), 2),
112 mFont("Font")
113 {
114 ASSERT(Mf::video &&
115 "no current video context from which to get dimensions");
116 resize(Mf::video->getWidth(), Mf::video->getHeight());
117 }
118
119
120 void Hud::resize(int width, int height)
121 {
122 cml::matrix_orthographic_RH(mProjection,
123 SCALAR(0.0),
124 Mf::Scalar(width), SCALAR(0.0), Mf::Scalar(height),
125 SCALAR(1.0), SCALAR(-1.0), cml::z_clip_neg_one);
126
127 // position the two progress bars at the top-left of the screen
128 mBar1.resize(Mf::Rectangle(20, height - 51, 0.7 * width, height - 3));
129 mBar2.resize(Mf::Rectangle(20, height - 28, 0.7 * width, height - 70));
130
131 setBar1Progress(0.05);
132 setBar2Progress(0.0);
133 }
134
135
136 void Hud::update(Mf::Scalar t, Mf::Scalar dt)
137 {
138 mState.interp.update(t, dt);
139 setBar1Progress(mState.interp.getState(dt));
140 setBar2Progress(1.0 - mState.interp.getState(dt));
141 }
142
143 void Hud::draw(Mf::Scalar alpha) const
144 {
145 glMatrixMode(GL_PROJECTION);
146 glPushMatrix();
147 glLoadMatrix(mProjection.data());
148
149 glMatrixMode(GL_MODELVIEW);
150 glPushMatrix();
151 glLoadIdentity();
152
153 glDisable(GL_DEPTH_TEST);
154 glEnable(GL_BLEND);
155
156 mBar1.draw();
157 mBar2.draw();
158
159 glDisable(GL_BLEND);
160 glEnable(GL_DEPTH_TEST);
161
162 glMatrixMode(GL_PROJECTION);
163 glPopMatrix();
164
165 glMatrixMode(GL_MODELVIEW);
166 glPopMatrix();
167 }
168
169 bool Hud::handleEvent(const Mf::Event& event)
170 {
171 switch (event.type)
172 {
173 case SDL_KEYUP:
174 if (event.key.keysym.sym == SDLK_h)
175 {
176 // don't want the hud anymore
177 Mf::core.pop(this);
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.042293 seconds and 4 git commands to generate.