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