]>
Dogcows Code - chaz/yoink/blob - src/Hud.cc
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
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.
10 **************************************************************************/
12 #include <moof/aabb.hh>
13 #include <moof/log.hh>
14 #include <moof/opengl.hh>
15 #include <moof/video.hh>
20 ProgressBar::ProgressBar(const moof::texture
& tilemap
, int index
) :
24 tilemap
.tile_coordinates(index
, mTexCoords
);
26 moof::scalar half
= (mTexCoords
[2] - mTexCoords
[0]) / 2.0 + mTexCoords
[0];
27 mMidCoords
[0] = half
- 0.01;
28 mMidCoords
[1] = half
+ 0.01;
31 void ProgressBar::resize(const moof::rectangle
& rect
)
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;
37 mWidth
= rect
.max
[0] - rect
.min
[0] - height
;
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]);
49 setProgress(mProgress
);
52 void ProgressBar::setProgress(moof::scalar progress
)
54 moof::scalar halfHeight
= (mVertices
[7][1] - mVertices
[0][1]) / 2.0;
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
;
64 void ProgressBar::draw(moof::scalar alpha
) const
66 if (moof::is_equal(mProgress
, 0.0))
68 // don't draw anything if the progress is 0%
72 glColor4f(1.0f
, 1.0f
, 1.0f
, 0.85f
);
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]);
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]);
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]);
106 Hud::Hud(GameState
& state
) :
108 mBar1(moof::texture("StatusBars"), 0),
109 mBar2(moof::texture("StatusBars"), 2),
112 moof::video
* video
= moof::video::current();
113 ASSERT(video
&& "a current video context should be set");
114 resize(video
->width(), video
->height());
118 void Hud::resize(int width
, int height
)
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
);
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));
129 setBar1Progress(0.05);
130 setBar2Progress(0.0);
134 void Hud::update(moof::scalar t
, moof::scalar dt
)
136 state_
.interp
.update(t
, dt
);
137 setBar1Progress(state_
.interp
.state(dt
));
138 setBar2Progress(1.0 - state_
.interp
.state(dt
));
141 void Hud::draw(moof::scalar alpha
) const
143 glMatrixMode(GL_PROJECTION
);
145 glLoadMatrix(mProjection
.data());
147 glMatrixMode(GL_MODELVIEW
);
151 glDisable(GL_DEPTH_TEST
);
158 glEnable(GL_DEPTH_TEST
);
160 glMatrixMode(GL_PROJECTION
);
163 glMatrixMode(GL_MODELVIEW
);
167 bool Hud::handle_event(const moof::event
& event
)
172 if (event
.key
.keysym
.sym
== SDLK_h
)
174 // don't want the hud anymore
175 parent().remove_child(this);
177 moof::log_warning("okay bye bye hud");
182 case SDL_VIDEORESIZE
:
183 resize(event
.resize
.w
, event
.resize
.h
);
This page took 0.047298 seconds and 4 git commands to generate.