]>
Dogcows Code - chaz/yoink/blob - src/Hud.cc
d77bf57bd88146de508d155fa9a2c23dbc1f89d4
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
29 #include <Moof/Engine.hh>
30 #include <Moof/OpenGL.hh>
31 #include <Moof/Video.hh>
36 ProgressBar::ProgressBar(const Tilemap
& tilemap
, Tilemap::Index index
) :
40 tilemap
.getTileCoords(index
, mTexCoords
);
42 Mf::Scalar half
= (mTexCoords
[2] - mTexCoords
[0]) / 2.0 + mTexCoords
[0];
43 mMidCoords
[0] = half
- 0.01;
44 mMidCoords
[1] = half
+ 0.01;
47 void ProgressBar::resize(const Mf::Rectangle
& rect
)
49 Mf::Scalar height
= rect
.max
[1] - rect
.min
[1];
50 Mf::Scalar halfHeight
= height
/ 2.0;
52 mWidth
= rect
.max
[0] - rect
.min
[0] - height
;
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]);
64 setProgress(mProgress
);
67 void ProgressBar::setProgress(Mf::Scalar progress
)
69 Mf::Scalar halfHeight
= (mVertices
[7][1] - mVertices
[0][1]) / 2.0;
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
;
79 void ProgressBar::draw(Mf::Scalar alpha
) const
81 if (Mf::isEqual(mProgress
, 0.0))
83 // don't draw anything if the progress is 0%
87 glColor4f(1.0f
, 1.0f
, 1.0f
, 0.85f
);
91 glTexCoord(mTexCoords
[0], mTexCoords
[1]);
92 glVertex(mVertices
[0]);
93 glTexCoord(mMidCoords
[0], mTexCoords
[3]);
94 glVertex(mVertices
[1]);
95 glTexCoord(mMidCoords
[0], mTexCoords
[5]);
96 glVertex(mVertices
[6]);
97 glTexCoord(mTexCoords
[6], mTexCoords
[7]);
98 glVertex(mVertices
[7]);
100 glTexCoord(mMidCoords
[0], mTexCoords
[1]);
101 glVertex(mVertices
[1]);
102 glTexCoord(mMidCoords
[1], mTexCoords
[3]);
103 glVertex(mVertices
[2]);
104 glTexCoord(mMidCoords
[1], mTexCoords
[5]);
105 glVertex(mVertices
[5]);
106 glTexCoord(mMidCoords
[0], mTexCoords
[7]);
107 glVertex(mVertices
[6]);
109 glTexCoord(mMidCoords
[1], mTexCoords
[1]);
110 glVertex(mVertices
[2]);
111 glTexCoord(mTexCoords
[2], mTexCoords
[3]);
112 glVertex(mVertices
[3]);
113 glTexCoord(mTexCoords
[4], mTexCoords
[5]);
114 glVertex(mVertices
[4]);
115 glTexCoord(mMidCoords
[1], mTexCoords
[7]);
116 glVertex(mVertices
[5]);
121 Hud::Hud(GameState
& state
) :
123 mBar1(Tilemap("StatusBars"), 0),
124 mBar2(Tilemap("StatusBars"), 2),
127 Mf::VideoP video
= Mf::engine
.getVideo();
128 resize(video
->getWidth(), video
->getHeight());
132 void Hud::resize(int width
, int height
)
134 cml::matrix_orthographic_RH(mProjection
,
136 Mf::Scalar(width
), SCALAR(0.0), Mf::Scalar(height
),
137 SCALAR(1.0), SCALAR(-1.0), cml::z_clip_neg_one
);
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));
145 setBar1Progress(0.05);
146 setBar2Progress(0.0);
150 void Hud::update(Mf::Engine
& engine
, Mf::Scalar t
, Mf::Scalar dt
)
152 mState
.interp
.update(t
, dt
);
153 setBar1Progress(mState
.interp
.getState(dt
));
154 setBar2Progress(1.0 - mState
.interp
.getState(dt
));
157 void Hud::draw(Mf::Engine
& engine
, Mf::Scalar alpha
) const
159 glMatrixMode(GL_PROJECTION
);
161 glLoadMatrix(mProjection
.data());
163 glMatrixMode(GL_MODELVIEW
);
167 glDisable(GL_DEPTH_TEST
);
174 glEnable(GL_DEPTH_TEST
);
176 glMatrixMode(GL_PROJECTION
);
179 glMatrixMode(GL_MODELVIEW
);
183 bool Hud::handleEvent(Mf::Engine
& engine
, const Mf::Event
& event
)
188 if (event
.key
.keysym
.sym
== SDLK_h
)
190 // don't want the hud anymore
196 case SDL_VIDEORESIZE
:
197 resize(event
.resize
.w
, event
.resize
.h
);
205 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.041408 seconds and 4 git commands to generate.