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