-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.cpp
396 lines (325 loc) · 13.4 KB
/
Menu.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*--------------------------------****************************************----------------------------------
| * * |
| Ether's Quest * Menu class Definitions * |
| * * |
---------------------------------****************************************----------------------------------*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
The Menu class creates game menu object (Play, Information, Exit the game)
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "Menu.hpp"
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//---------------------------------------------- Class Menu ------------------------------------------------------
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------------------
// Constructors
//----------------------------------------------------------------------------------
Menu::Menu() {
// Empty
}
Menu::Menu(Texture2D &img, vector<Button> &menu_bars, Exit_game &x_game, Instance &info, Instance &roll_info, Instance credits, Sound &ambience)
{
this->img = img;
this->menu_bars = menu_bars;
this->ambience = ambience;
Instance inst(MENU, img, menu_bars[0], menu_bars[1], menu_bars[2], this->ambience);
this->instance = inst;
this->info = info;
this->roll_info = roll_info;
this->credits = credits;
this->x_game = x_game;
}
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
// Accessors
//----------------------------------------------------------------------------------
vector<Button> Menu::bars() {
return menu_bars;
}
//////////////////////////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------
Renders Menu instance
--------------------------------------------------------------------*/
void Menu::render_menu()
{
//--------------------------------------------------------------------------------------
// variables
//--------------------------------------------------------------------------------------
// Window
float window_width = (float)O_WIN_WD,
window_height = (float)O_WIN_HT;
// Mouse position, window scale
Vector2 mouse_point = { 0.0f, 0.0f },
window_scale = { 1.0f, 1.0f };
// Backgrounsd image and buttons resizing
Rectangle img_resized_rec,
bar1_resized_rec,
bar2_resized_rec,
bar3_resized_rec;
// Buttons
Texture2D bar1, bar2, bar3;
bool bar1_pressed = false,
bar2_pressed = false,
bar3_pressed = false;
//--------------------------------------------------------------------------------------
// Function operations
//--------------------------------------------------------------------------------------
//---- Ambience volume
SetSoundVolume(instance.get_ambience(), 0.05);
PlaySound(instance.get_ambience());
// Window loop
do
{
//----------------------------------------------------------------------------------
// Update
//----------------------------------------------------------------------------------
// Window state
if (WindowShouldClose())
{
x_game.exit_game_now(); // exit game
}
// window scale, window not resized scale is 1:1
window_scale = { (float)GetScreenWidth() / window_width, (float)GetScreenHeight() / window_height };
// Mousse
mouse_point = GetMousePosition();
// Backgound image resized
img_resized_rec = {
0.0f, 0.0f,
(float)GetScreenWidth(),
(float)GetScreenHeight()
};
// Bars resized
bar1_resized_rec = {
(window_width / 9.0f) * window_scale.x,
(window_height - window_height / 1.4f ) * window_scale.y,
(instance.btn_source().width) * window_scale.x,
(instance.btn_source().height) * window_scale.y
};
bar2_resized_rec = {
(window_width / 9.0f) * window_scale.x,
(window_height - window_height / 1.9f) * window_scale.y,
(instance.btn_source().width) * window_scale.x,
(instance.btn_source().height) * window_scale.y
};
bar3_resized_rec = {
(window_width / 9.0f) * window_scale.x,
(window_height - window_height / 2.9f) * window_scale.y,
(instance.btn_source().width) * window_scale.x,
(instance.btn_source().height) * window_scale.y
};
// Check bar-1 state, play
if (CheckCollisionPointRec(mouse_point, bar1_resized_rec))
{
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
{
bar1 = instance.button3()[0].pressed;
}
else
{
bar1 = instance.button3()[0].hover;
}
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
{
bar1_pressed = true;
}
}
else
{
bar1 = instance.button3()[0].idle;
}
// Check bar-2 state, info
if (CheckCollisionPointRec(mouse_point, bar2_resized_rec))
{
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
{
bar2 = instance.button3()[1].pressed;
}
else
{
bar2 = instance.button3()[1].hover;
}
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
{
bar2_pressed = true;
}
}
else
{
bar2 = instance.button3()[1].idle;
}
// Check bar-3 state, exit game
if (CheckCollisionPointRec(mouse_point, bar3_resized_rec))
{
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
{
bar3 = instance.button3()[2].pressed;
}
else
{
bar3 = instance.button3()[2].hover;
}
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
{
bar3_pressed = true;
}
}
else
{
bar3 = instance.button3()[2].idle;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
// Background image rezises with window
DrawTexturePro( // Draw a part of a texture (images) defined by a rectangle with 'pro' parameters
instance.image(),
instance.img_source(),
img_resized_rec,
{ 0, 0 }, 0.0f, WHITE
);
// Bar-1
DrawTexturePro( // Draw a part of a texture (images) defined by a rectangle with 'pro' parameters
bar1,
instance.btn_source(), // all 3 bars ahave the same original size
bar1_resized_rec,
{ 0, 0 }, 0.0f, WHITE
);
// Bar-2
DrawTexturePro( // Draw a part of a texture (images) defined by a rectangle with 'pro' parameters
bar2,
instance.btn_source(), // all 3 bars ahave the same original size
bar2_resized_rec,
{ 0, 0 }, 0.0f, WHITE
);
// Bar-3
DrawTexturePro( // Draw a part of a texture (images) defined by a rectangle with 'pro' parameters
bar3,
instance.btn_source(), // all 3 bars ahave the same original size
bar3_resized_rec,
{ 0, 0 }, 0.0f, WHITE
);
EndDrawing();
//----------------------------------------------------------------------------------
// Checks button pressed
if (bar1_pressed) { break; } // Exits game
else if (bar3_pressed) { x_game.exit_game_now(); } // Exits game
else if (bar2_pressed) // info intance
{
render_sub_instance(info, x_game);
render_sub_instance(roll_info, x_game);
render_sub_instance(credits, x_game);
bar2_pressed = false;
}
//----- Loops instance ambience sound
if (!IsSoundPlaying(instance.get_ambience())) PlaySound(instance.get_ambience());
} while (true); // do loop
if (IsSoundPlaying(instance.get_ambience())) StopSound(instance.get_ambience());
}// render_menu()
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------------------------------
Renders sub instance menu, info, roll_info, and credits
--------------------------------------------------------------------------------------------*/
void Menu::render_sub_instance(Instance &instance, Exit_game &x_game)
{
//--------------------------------------------------------------------------------------
// variables
//--------------------------------------------------------------------------------------
// Window
float window_width = (float)O_WIN_WD,
window_height = (float)O_WIN_HT;
// Mouse position, window scale
Vector2 mouse_point = { 0.0f, 0.0f },
window_scale = { 1.0f, 1.0f };
// Backgrounsd image and buttons resizing
Rectangle img_resized_rec,
btn_resized_rec;
// Button
Texture2D button;
bool btn1_pressed = false;
//--------------------------------------------------------------------------------------
// Function operations
//--------------------------------------------------------------------------------------
// Event loop
do
{
//----------------------------------------------------------------------------------
// Update
//----------------------------------------------------------------------------------
// Window state, closed, escape button pressed
if (WindowShouldClose())
{
x_game.exit_game_now();
}
else if (IsKeyPressed(KEY_ESCAPE))
{
break; // exist do loop
}
// window scale, window not resized scale is 1:1
window_scale = { (float)GetScreenWidth() / window_width, (float)GetScreenHeight() / window_height };
// Mousse
mouse_point = GetMousePosition();
// Backgound image resized
img_resized_rec = {
0.0f, 0.0f,
(float)GetScreenWidth(),
(float)GetScreenHeight()
};
// Button resized
btn_resized_rec = {
(window_width / 2.0f) * window_scale.x,
(window_height - window_height / 9.0f) * window_scale.y,
(instance.btn_source().width / 11.0f) * window_scale.x,
(instance.btn_source().height / 11.0f) * window_scale.y
};
// Check button state
if (CheckCollisionPointRec(mouse_point, btn_resized_rec))
{
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
{
button = instance.button().pressed;
}
else
{
button = instance.button().hover;
}
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
{
btn1_pressed = true;
}
}
else
{
button = instance.button().idle;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
// Background image rezises with window
DrawTexturePro( // Draw a part of a texture (images) defined by a rectangle with 'pro' parameters
instance.image(),
instance.img_source(),
img_resized_rec,
{ 0, 0 }, 0.0f, WHITE
);
// Button
DrawTexturePro( // Draw a part of a texture (images) defined by a rectangle with 'pro' parameters
button,
instance.btn_source(),
btn_resized_rec,
{ 0, 0 }, 0.0f, WHITE
);
EndDrawing();
//----------------------------------------------------------------------------------
// Exits do loop
if (btn1_pressed) break;
//----- Loops instance ambience sound
if (!IsSoundPlaying(instance.get_ambience())) PlaySound(instance.get_ambience());
} while (true); // do loop
} // render_sub_instance()