-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.cpp
78 lines (65 loc) · 2.23 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
#include "GameWindow.h"
#include "menu.h"
Menu::Menu() {
}
Menu::~Menu() {
}
void Menu::Init() {
title = al_load_bitmap("./images/title.png");
click = al_load_bitmap("./images/click.png");
trans = 1.0;
transIncrease = false;
for (int i = 0; i < 115; ++i) {
std::string filename = "./menu/" + std::to_string(i+1) + ".png";
frames[i] = al_load_bitmap(filename.c_str());
}
currentFrame = 0;
lastFrameSwitchTime = al_get_time();
}
bool Menu::Update(ALLEGRO_EVENT_QUEUE* event_queue) {
ALLEGRO_EVENT event;
while (al_get_next_event(event_queue, &event)) {
static double lastAlphaUpdateTime = al_get_time();
if (al_get_time() - lastAlphaUpdateTime >= 1.0) {
transIncrease = !transIncrease;
lastAlphaUpdateTime = al_get_time();
}
if (transIncrease) {
trans += 0.02;
} else {
trans -= 0.02;
}
trans = std::max(0.2f, std::min(1.0f, trans));
//printf("Transparency: %f\n", trans);
if (event.type == ALLEGRO_EVENT_TIMER) {
if (al_get_time() - lastFrameSwitchTime >= 0.1) {
currentFrame = (currentFrame + 1) % 115;
lastFrameSwitchTime = al_get_time();
//printf("Frame updated to: %d\n", currentFrame);
}
} else if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
mouseX = event.mouse.x;
mouseY = event.mouse.y;
//printf("Mouse click at: (%d, %d)\n", mouseX, mouseY);
if (mouseX > 681 && mouseX < 681 + 439 && mouseY > 786 && mouseY < 786 + 67){
//printf("Exiting menu loop.\n");
return true;
}
}
}
return false;
}
void Menu::Draw() {
//printf("Drawing frame: %d\n", currentFrame);
al_draw_bitmap(frames[currentFrame], 0, 0, 0);
al_draw_bitmap(title, 522, 280, 0);
al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
ALLEGRO_COLOR tint = al_map_rgba_f(1.0, 1.0, 1.0, trans);
al_draw_tinted_bitmap(click, tint, 681, 786, 0);
}
void Menu::Destroy(){
for(int i=0; i<115; i++)
al_destroy_bitmap(frames[i]);
al_destroy_bitmap(title);
al_destroy_bitmap(click);
}