]> Dogcows Code - chaz/yoink/blob - src/Hud.cc
the massive refactoring effort
[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 moof::texture& tilemap, int index) :
21 mProgress(0.0),
22 mTilemap(tilemap)
23 {
24 tilemap.tile_coordinates(index, mTexCoords);
25
26 moof::scalar half = (mTexCoords[2] - mTexCoords[0]) / 2.0 + mTexCoords[0];
27 mMidCoords[0] = half - 0.01;
28 mMidCoords[1] = half + 0.01;
29 }
30
31 void ProgressBar::resize(const moof::rectangle& rect)
32 {
33 moof::log_info << "rect: " << rect.min << ", " << rect.max << std::endl;
34 moof::scalar height = rect.max[1] - rect.min[1];
35 moof::scalar halfHeight = height / 2.0;
36
37 mWidth = rect.max[0] - rect.min[0] - height;
38 ASSERT(mWidth > 0);
39
40 mVertices[0] = rect.min;
41 mVertices[1] = moof::vector2(rect.min[0] + halfHeight, rect.min[1]);
42 mVertices[2] = mVertices[1];
43 mVertices[3] = moof::vector2(rect.min[0] + height, rect.min[1]);
44 mVertices[4] = moof::vector2(rect.min[0] + height, rect.max[1]);
45 mVertices[5] = moof::vector2(rect.min[0] + halfHeight, rect.max[1]);
46 mVertices[6] = mVertices[5];
47 mVertices[7] = moof::vector2(rect.min[0], rect.max[1]);
48
49 setProgress(mProgress);
50 }
51
52 void ProgressBar::setProgress(moof::scalar progress)
53 {
54 moof::scalar halfHeight = (mVertices[7][1] - mVertices[0][1]) / 2.0;
55
56 mVertices[2][0] = mVertices[1][0] + progress * mWidth;
57 mVertices[3][0] = mVertices[1][0] + progress * mWidth + halfHeight;
58 mVertices[4][0] = mVertices[1][0] + progress * mWidth + halfHeight;
59 mVertices[5][0] = mVertices[1][0] + progress * mWidth;
60
61 mProgress = progress;
62 }
63
64 void ProgressBar::draw(moof::scalar alpha) const
65 {
66 if (moof::is_equal(mProgress, 0.0))
67 {
68 // don't draw anything if the progress is 0%
69 return;
70 }
71
72 glColor4f(1.0f, 1.0f, 1.0f, 0.85f);
73 mTilemap.bind();
74
75 glBegin(GL_QUADS);
76 glTexCoord(mTexCoords[0], mTexCoords[1]);
77 glVertex(mVertices[0]);
78 glTexCoord(mMidCoords[0], mTexCoords[3]);
79 glVertex(mVertices[1]);
80 glTexCoord(mMidCoords[0], mTexCoords[5]);
81 glVertex(mVertices[6]);
82 glTexCoord(mTexCoords[6], mTexCoords[7]);
83 glVertex(mVertices[7]);
84
85 glTexCoord(mMidCoords[0], mTexCoords[1]);
86 glVertex(mVertices[1]);
87 glTexCoord(mMidCoords[1], mTexCoords[3]);
88 glVertex(mVertices[2]);
89 glTexCoord(mMidCoords[1], mTexCoords[5]);
90 glVertex(mVertices[5]);
91 glTexCoord(mMidCoords[0], mTexCoords[7]);
92 glVertex(mVertices[6]);
93
94 glTexCoord(mMidCoords[1], mTexCoords[1]);
95 glVertex(mVertices[2]);
96 glTexCoord(mTexCoords[2], mTexCoords[3]);
97 glVertex(mVertices[3]);
98 glTexCoord(mTexCoords[4], mTexCoords[5]);
99 glVertex(mVertices[4]);
100 glTexCoord(mMidCoords[1], mTexCoords[7]);
101 glVertex(mVertices[5]);
102 glEnd();
103 }
104
105
106 Hud::Hud(GameState& state) :
107 state_(state),
108 mBar1(moof::texture("StatusBars"), 0),
109 mBar2(moof::texture("StatusBars"), 2),
110 mFont("Font")
111 {
112 moof::video* video = moof::video::current();
113 ASSERT(video && "a current video context should be set");
114 resize(video->width(), video->height());
115 }
116
117
118 void Hud::resize(int width, int height)
119 {
120 moof::matrix_orthographic_RH(mProjection, SCALAR(0.0),
121 moof::scalar(width), SCALAR(0.0),
122 moof::scalar(height),
123 SCALAR(1.0), SCALAR(-1.0), moof::z_clip_neg_one);
124
125 // position the two progress bars at the top-left of the screen
126 mBar1.resize(moof::rectangle(20, height - 51, 0.7 * width, height - 3));
127 mBar2.resize(moof::rectangle(20, height - 28, 0.7 * width, height - 70));
128
129 setBar1Progress(0.05);
130 setBar2Progress(0.0);
131 }
132
133
134 void Hud::update(moof::scalar t, moof::scalar dt)
135 {
136 state_.interp.update(t, dt);
137 setBar1Progress(state_.interp.state(dt));
138 setBar2Progress(1.0 - state_.interp.state(dt));
139 }
140
141 void Hud::draw(moof::scalar alpha) const
142 {
143 glMatrixMode(GL_PROJECTION);
144 glPushMatrix();
145 glLoadMatrix(mProjection.data());
146
147 glMatrixMode(GL_MODELVIEW);
148 glPushMatrix();
149 glLoadIdentity();
150
151 glDisable(GL_DEPTH_TEST);
152 glEnable(GL_BLEND);
153
154 mBar1.draw();
155 mBar2.draw();
156
157 glDisable(GL_BLEND);
158 glEnable(GL_DEPTH_TEST);
159
160 glMatrixMode(GL_PROJECTION);
161 glPopMatrix();
162
163 glMatrixMode(GL_MODELVIEW);
164 glPopMatrix();
165 }
166
167 bool Hud::handle_event(const moof::event& event)
168 {
169 switch (event.type)
170 {
171 case SDL_KEYUP:
172 if (event.key.keysym.sym == SDLK_h)
173 {
174 // don't want the hud anymore
175 parent().remove_child(this);
176
177 moof::log_warning("okay bye bye hud");
178 return true;
179 }
180 break;
181
182 case SDL_VIDEORESIZE:
183 resize(event.resize.w, event.resize.h);
184 break;
185 }
186
187 return false;
188 }
189
This page took 0.042245 seconds and 4 git commands to generate.