]> Dogcows Code - chaz/yoink/blob - src/Hud.cc
foundational changes; tying up some loose ends
[chaz/yoink] / src / Hud.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include <Moof/Engine.hh>
30 #include <Moof/OpenGL.hh>
31 #include <Moof/Video.hh>
32
33 #include "Hud.hh"
34
35
36 ProgressBar::ProgressBar(const Tilemap& tilemap, Tilemap::Index index) :
37 mProgress(0.0),
38 mTilemap(tilemap)
39 {
40 tilemap.getTileCoords(index, mTexCoords);
41
42 Mf::Scalar half = (mTexCoords[2] - mTexCoords[0]) / 2.0 + mTexCoords[0];
43 mMidCoords[0] = half - 0.01;
44 mMidCoords[1] = half + 0.01;
45 }
46
47 void ProgressBar::resize(const Mf::Rectangle& rect)
48 {
49 Mf::Scalar height = rect.max[1] - rect.min[1];
50 Mf::Scalar halfHeight = height / 2.0;
51
52 mWidth = rect.max[0] - rect.min[0] - height;
53 // assert width > 0
54
55 mVertices[0] = rect.min;
56 mVertices[1] = Mf::Vector2(rect.min[0] + halfHeight, rect.min[1]);
57 mVertices[2] = mVertices[1];
58 mVertices[3] = Mf::Vector2(rect.min[0] + height, rect.min[1]);
59 mVertices[4] = Mf::Vector2(rect.min[0] + height, rect.max[1]);
60 mVertices[5] = Mf::Vector2(rect.min[0] + halfHeight, rect.max[1]);
61 mVertices[6] = mVertices[5];
62 mVertices[7] = Mf::Vector2(rect.min[0], rect.max[1]);
63
64 setProgress(mProgress);
65 }
66
67 void ProgressBar::setProgress(Mf::Scalar progress)
68 {
69 Mf::Scalar halfHeight = (mVertices[7][1] - mVertices[0][1]) / 2.0;
70
71 mVertices[2][0] = mVertices[1][0] + progress * mWidth;
72 mVertices[3][0] = mVertices[1][0] + progress * mWidth + halfHeight;
73 mVertices[4][0] = mVertices[1][0] + progress * mWidth + halfHeight;
74 mVertices[5][0] = mVertices[1][0] + progress * mWidth;
75
76 mProgress = progress;
77 }
78
79 void ProgressBar::draw(Mf::Scalar alpha) const
80 {
81 if (Mf::isEqual(mProgress, 0.0))
82 {
83 // don't draw anything if the progress is 0%
84 return;
85 }
86
87 glColor4f(1.0f, 1.0f, 1.0f, 0.85f);
88 mTilemap.bind();
89
90 glBegin(GL_QUADS);
91 glTexCoord2(mTexCoords[0], mTexCoords[1]);
92 glVertex2v(mVertices[0].data());
93 glTexCoord2(mMidCoords[0], mTexCoords[3]);
94 glVertex2v(mVertices[1].data());
95 glTexCoord2(mMidCoords[0], mTexCoords[5]);
96 glVertex2v(mVertices[6].data());
97 glTexCoord2(mTexCoords[6], mTexCoords[7]);
98 glVertex2v(mVertices[7].data());
99
100 glTexCoord2(mMidCoords[0], mTexCoords[1]);
101 glVertex2v(mVertices[1].data());
102 glTexCoord2(mMidCoords[1], mTexCoords[3]);
103 glVertex2v(mVertices[2].data());
104 glTexCoord2(mMidCoords[1], mTexCoords[5]);
105 glVertex2v(mVertices[5].data());
106 glTexCoord2(mMidCoords[0], mTexCoords[7]);
107 glVertex2v(mVertices[6].data());
108
109 glTexCoord2(mMidCoords[1], mTexCoords[1]);
110 glVertex2v(mVertices[2].data());
111 glTexCoord2(mTexCoords[2], mTexCoords[3]);
112 glVertex2v(mVertices[3].data());
113 glTexCoord2(mTexCoords[4], mTexCoords[5]);
114 glVertex2v(mVertices[4].data());
115 glTexCoord2(mMidCoords[1], mTexCoords[7]);
116 glVertex2v(mVertices[5].data());
117 glEnd();
118 }
119
120
121 Hud::Hud(GameLayer::State& state) :
122 mState(state),
123 mBar1(Tilemap("StatusBars"), 0),
124 mBar2(Tilemap("StatusBars"), 2),
125 mFont("Font")
126 {
127 Mf::VideoP video = Mf::Engine::getInstance().getVideo();
128 resize(video->getWidth(), video->getHeight());
129 }
130
131
132 void Hud::resize(int width, int height)
133 {
134 cml::matrix_orthographic_RH(mProjection,
135 SCALAR(0.0),
136 Mf::Scalar(width), SCALAR(0.0), Mf::Scalar(height),
137 SCALAR(1.0), SCALAR(-1.0), cml::z_clip_neg_one);
138
139 // position the two progress bars at the top-left of the screen
140 mBar1.resize(Mf::Rectangle(20, height - 51,
141 0.7 * width, height - 3));
142 mBar2.resize(Mf::Rectangle(20, height - 28,
143 0.7 * width, height - 70));
144
145 setBar1Progress(0.05);
146 setBar2Progress(0.0);
147 }
148
149
150 void Hud::update(Mf::Engine& engine, Mf::Scalar t, Mf::Scalar dt)
151 {
152 mState.interp.update(t, dt);
153 setBar1Progress(mState.interp.getState(dt));
154 setBar2Progress(1.0 - mState.interp.getState(dt));
155 }
156
157 void Hud::draw(Mf::Engine& engine, Mf::Scalar alpha) const
158 {
159 glMatrixMode(GL_PROJECTION);
160 glPushMatrix();
161 glLoadMatrix(mProjection.data());
162
163 glMatrixMode(GL_MODELVIEW);
164 glPushMatrix();
165 glLoadIdentity();
166
167 glDisable(GL_DEPTH_TEST);
168 glEnable(GL_BLEND);
169
170 mBar1.draw();
171 mBar2.draw();
172
173 glDisable(GL_BLEND);
174 glEnable(GL_DEPTH_TEST);
175
176 glMatrixMode(GL_PROJECTION);
177 glPopMatrix();
178
179 glMatrixMode(GL_MODELVIEW);
180 glPopMatrix();
181 }
182
183 bool Hud::handleEvent(Mf::Engine& engine, const Mf::Event& event)
184 {
185 switch (event.type)
186 {
187 case SDL_KEYUP:
188 if (event.key.keysym.sym == SDLK_h)
189 {
190 // don't want the hud anymore
191 engine.pop(this);
192 return true;
193 }
194 break;
195
196 case SDL_VIDEORESIZE:
197 resize(event.resize.w, event.resize.h);
198 break;
199 }
200
201 return false;
202 }
203
204
205 /** vim: set ts=4 sw=4 tw=80: *************************************************/
206
This page took 0.041312 seconds and 4 git commands to generate.