]> Dogcows Code - chaz/yoink/blob - src/Hud.cc
preliminary physics, sound, hud
[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/OpenGL.hh>
30
31 #include "Hud.hh"
32
33 #include <iostream>
34
35
36 ProgressBar::ProgressBar(const Mf::Tilemap& tilemap, Mf::Tilemap::Index index) :
37 progress_(0.0),
38 tilemap_(tilemap)
39 {
40 tilemap.getTileCoords(index, texCoords_);
41
42 Mf::Scalar half = (texCoords_[2] - texCoords_[0]) / 2.0 + texCoords_[0];
43 midCoords_[0] = half - 0.01;
44 midCoords_[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 width_ = rect.max[0] - rect.min[0] - height;
53 // assert width > 0
54
55 vertices_[0] = rect.min;
56 vertices_[1] = Mf::Vector2(rect.min[0] + halfHeight, rect.min[1]);
57 vertices_[2] = vertices_[1];
58 vertices_[3] = Mf::Vector2(rect.min[0] + height, rect.min[1]);
59 vertices_[4] = Mf::Vector2(rect.min[0] + height, rect.max[1]);
60 vertices_[5] = Mf::Vector2(rect.min[0] + halfHeight, rect.max[1]);
61 vertices_[6] = vertices_[5];
62 vertices_[7] = Mf::Vector2(rect.min[0], rect.max[1]);
63
64 setProgress(progress_);
65 }
66
67 void ProgressBar::setProgress(Mf::Scalar progress)
68 {
69 Mf::Scalar halfHeight = (vertices_[7][1] - vertices_[0][1]) / 2.0;
70
71 vertices_[2][0] = vertices_[1][0] + progress * width_;
72 vertices_[3][0] = vertices_[1][0] + progress * width_ + halfHeight;
73 vertices_[4][0] = vertices_[1][0] + progress * width_ + halfHeight;
74 vertices_[5][0] = vertices_[1][0] + progress * width_;
75
76 progress_ = progress;
77 }
78
79 void ProgressBar::draw(Mf::Scalar alpha) const
80 {
81 if (Mf::isEqual(progress_, 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 tilemap_.bind();
89
90 glBegin(GL_QUADS);
91 glTexCoord2(texCoords_[0], texCoords_[1]);
92 glVertex2v(vertices_[0].data());
93 glTexCoord2(midCoords_[0], texCoords_[3]);
94 glVertex2v(vertices_[1].data());
95 glTexCoord2(midCoords_[0], texCoords_[5]);
96 glVertex2v(vertices_[6].data());
97 glTexCoord2(texCoords_[6], texCoords_[7]);
98 glVertex2v(vertices_[7].data());
99
100 glTexCoord2(midCoords_[0], texCoords_[1]);
101 glVertex2v(vertices_[1].data());
102 glTexCoord2(midCoords_[1], texCoords_[3]);
103 glVertex2v(vertices_[2].data());
104 glTexCoord2(midCoords_[1], texCoords_[5]);
105 glVertex2v(vertices_[5].data());
106 glTexCoord2(midCoords_[0], texCoords_[7]);
107 glVertex2v(vertices_[6].data());
108
109 glTexCoord2(midCoords_[1], texCoords_[1]);
110 glVertex2v(vertices_[2].data());
111 glTexCoord2(texCoords_[2], texCoords_[3]);
112 glVertex2v(vertices_[3].data());
113 glTexCoord2(texCoords_[4], texCoords_[5]);
114 glVertex2v(vertices_[4].data());
115 glTexCoord2(midCoords_[1], texCoords_[7]);
116 glVertex2v(vertices_[5].data());
117 glEnd();
118 }
119
120
121 Hud::Hud() :
122 bar1_(Mf::Tilemap("StatusBars"), 0),
123 bar2_(Mf::Tilemap("StatusBars"), 2),
124 font_("Font")
125 {
126 resize(800, 600);
127 }
128
129
130 void Hud::resize(int width, int height)
131 {
132 cml::matrix_orthographic_RH( projection_,
133 0.0,
134 Mf::Scalar(width), 0.0, Mf::Scalar(height),
135 1.0, -1.0, cml::z_clip_neg_one);
136
137 // position the two progress bars at the top-left of the screen
138 bar1_.resize(Mf::Rectangle(20, height - 51,
139 0.7 * width, height - 3));
140 bar2_.resize(Mf::Rectangle(20, height - 28,
141 0.7 * width, height - 70));
142
143 setBar1Progress(0.05);
144 setBar2Progress(0.0);
145 }
146
147
148 void Hud::draw(Mf::Scalar alpha) const
149 {
150 glMatrixMode(GL_PROJECTION);
151 glPushMatrix();
152 glLoadMatrix(projection_.data());
153
154 glMatrixMode(GL_MODELVIEW);
155 glPushMatrix();
156 glLoadIdentity();
157
158 glDisable(GL_DEPTH_TEST);
159 glEnable(GL_BLEND);
160
161 bar1_.draw();
162 bar2_.draw();
163
164 glDisable(GL_BLEND);
165 glEnable(GL_DEPTH_TEST);
166
167 glMatrixMode(GL_PROJECTION);
168 glPopMatrix();
169
170 glMatrixMode(GL_MODELVIEW);
171 glPopMatrix();
172 }
173
174
175 /** vim: set ts=4 sw=4 tw=80: *************************************************/
176
This page took 0.038891 seconds and 4 git commands to generate.