forked from Milez213/CityTrial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameHUD.cpp
247 lines (175 loc) · 5.81 KB
/
GameHUD.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
// GameHUD.cpp
//
//
// Created by Bryan Bell on 2/9/13.
//
//
#include "GameHUD.h"
extern HUDShader *hudShader;
GameHUD::GameHUD()
{
modelMatrix.useModelViewMatrix();
modelMatrix.loadIdentity();
setOrthographicMatrix();
setHUDView();
currentSpeed = 0.0;
currentScore = 0;
currentTime = 0.0;
playerColor = vec3(1.0, 0.5, 0.5);
#ifdef DEBUG_VBO
//printf("VBO Arrived at its Destination: %d\n", (int)indexBufferLength[0]);
#endif
}
void GameHUD::setOrthographicMatrix() {
proj = glm::ortho(0.0f, (float)hudWidth, (float)hudHeight, 0.0f, -1.0f, 1.0f);
}
void GameHUD::setHUDView() {
view = glm::lookAt( glm::vec3( 0.0f, 0.0f, 1.0f ),glm::vec3( 0.0f, 0.0f, 0.0f ),glm::vec3( 0.0f, 1.0f, 0.0f ) );
}
void GameHUD::setScreen(float width, float height)
{
hudWidth = width;
hudHeight = height;
setOrthographicMatrix();
}
void GameHUD::prepareShader()
{
hudShader->use();
hudShader->setViewMatrix(view);
hudShader->setProjectionMatrix(proj);
hudShader->setColor(playerColor);
}
void GameHUD::drawWin()
{
float width, height;
width = hudWidth/2.0;
height = hudHeight/2.0;
modelMatrix.loadIdentity();
modelMatrix.translate(vec3(width/2.0, height/2.0, 0.0));
modelMatrix.scale(width, height, 1.0);
hudShader->setModelMatrix(modelMatrix.getMatrix());
hudShader->draw(string("win"));
}
void GameHUD::drawLose()
{
float width, height;
width = hudWidth/2.0;
height = hudHeight/2.0;
modelMatrix.loadIdentity();
modelMatrix.translate(vec3(width/2.0, height/2.0, 0.0));
modelMatrix.scale(width, height, 1.0);
hudShader->setModelMatrix(modelMatrix.getMatrix());
hudShader->draw(string("lose"));
}
#define TIME_WDT 70.0f
#define TIME_HGT 70.0f
#define GROW 20.0f
void GameHUD::drawTimer(float dt, bool rushed)
{
currentTime += dt;
if (currentTime > 1.0f)
currentTime -= 1.0f;
float scl = GROW * currentTime;
if (!rushed)
scl = 0;
float posX = hudWidth / 2.0f - (TIME_WDT + scl) / 2.0f;
float posY = -(scl) / 2.0f;
modelMatrix.loadIdentity();
modelMatrix.translate(vec3(posX, posY, 0.0));
modelMatrix.scale(TIME_WDT + scl, TIME_HGT + scl, 1.0);
hudShader->setModelMatrix(modelMatrix.getMatrix());
hudShader->useTextureColor();
hudShader->draw(string("timer"));
modelMatrix.loadIdentity();
modelMatrix.translate(vec3(posX + TIME_WDT / 2.0f, posY + TIME_HGT / 2.0f, 0.0));
modelMatrix.scale(TIME_WDT, TIME_HGT, 1.0);
modelMatrix.rotate(360.0 * currentTime, vec3(0.0, 0.0, 1.0));
modelMatrix.translate(vec3(-0.5, -0.5, 0.0));
hudShader->setModelMatrix(modelMatrix.getMatrix());
hudShader->draw(string("tneedle"));
hudShader->deactivate();
}
#define SPD 150.0
void GameHUD::drawSpeed(float speed)
{
if (speed - currentSpeed > 1.0)
currentSpeed++;
else if (currentSpeed - speed > 1.0)
currentSpeed--;
else
currentSpeed = speed;
if (currentSpeed < 0.0)
currentSpeed = 0.0;
float width = SPD;
if (width > hudHeight/2.0) {
width = hudHeight/2.0;
}
// this is messy.
// We should use x and y offsets in Screen coordinates, etc
// and pass them to the shader
// Then we won't need any matrices, like in TTFRenderer.h --Mustafa
modelMatrix.loadIdentity();
modelMatrix.translate(vec3(hudWidth - width, hudHeight - width, 0.0));
modelMatrix.scale(width, width, 1.0);
hudShader->setModelMatrix(modelMatrix.getMatrix());
hudShader->draw(string("speedometer"));
modelMatrix.loadIdentity();
modelMatrix.translate(vec3(hudWidth - width/2.0, hudHeight - width/2.0, 0.0));
modelMatrix.scale(width, width, 1.0);
modelMatrix.rotate(30.0 * currentSpeed/10.0, vec3(0.0, 0.0, 1.0));
modelMatrix.translate(vec3(-0.5, -0.5, 0.0));
hudShader->usePlayerColor();
hudShader->setModelMatrix(modelMatrix.getMatrix());
hudShader->draw(string("needle"));
hudShader->useTextureColor();
hudShader->deactivate();
}
#define ENG_HGT 50
#define ENG_SCL 3.0
#define BCK_SCL 150.0
void GameHUD::drawEnergy(float maxEnergy, float energy, string name)
{
if (name != "none") {
string textName = name + "Energy";
modelMatrix.loadIdentity();
modelMatrix.translate(vec3(0.0, hudHeight - BCK_SCL, 0.0));
modelMatrix.scale(BCK_SCL, BCK_SCL, 1.0);
hudShader->setModelMatrix(modelMatrix.getMatrix());
hudShader->draw(string(textName));
}
modelMatrix.loadIdentity();
modelMatrix.translate(vec3(0.0, hudHeight - ENG_HGT - 15.0, 0.0));
modelMatrix.scale(maxEnergy * ENG_SCL, ENG_HGT, 1.0);
hudShader->setModelMatrix(modelMatrix.getMatrix());
hudShader->draw(string("energyBack"));
modelMatrix.loadIdentity();
modelMatrix.translate(vec3(0.0, hudHeight - ENG_HGT - 15.0, 0.0));
modelMatrix.scale(maxEnergy * ENG_SCL * (energy/maxEnergy), ENG_HGT, 1.0);
hudShader->setModelMatrix(modelMatrix.getMatrix());
hudShader->usePlayerColor();
hudShader->draw(string("energy"));
hudShader->useTextureColor();
hudShader->deactivate();
}
#define PT_HGT 100.0
#define PT_WDT 175.0
void GameHUD::drawScore(int score)
{
if (currentScore < score)
currentScore++;
float sclWdt = (score - currentScore) * 3.0;
if (sclWdt > 25.0f) {
sclWdt = 25.0f;
currentScore = score - 25.0f / 3.0f;
}
float sclHgt = (sclWdt * (PT_HGT/PT_WDT)) * 3.0;
modelMatrix.loadIdentity();
modelMatrix.translate(vec3(hudWidth - PT_WDT - sclWdt + 2.0, 0.0, 0.0));
modelMatrix.scale(PT_WDT + sclWdt, PT_HGT + sclHgt, 1.0);
hudShader->setModelMatrix(modelMatrix.getMatrix());
hudShader->usePlayerColor();
hudShader->draw(string("score"));
hudShader->useTextureColor();
hudShader->deactivate();
}