-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainMenu.h
92 lines (77 loc) · 2.07 KB
/
MainMenu.h
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
#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "TextureManager.h"
class MainMenu {
sf::Sprite background;
sf::Sprite title;
sf::Music music;
sf::Clock clock;
public:
sf::Sprite getBackground();
sf::Sprite getTitle();
float titleCenterX = 330.f;
float titleCenterY = 200.f;
float timeSinceLastFrame = 0;
void animateTitle();
void allowMenuMusic();
void update();
bool doesGameStart(bool leftClickReleased);
bool gameStart = false;
bool playMusic = true;
MainMenu(TextureManager* textureManagerInitialized) {
background.setTexture(textureManagerInitialized->getTexture("MainMenu_Background"));
title.setTexture(textureManagerInitialized->getTexture("MainMenu_Title"));
title.setPosition(titleCenterX, titleCenterY);
if (!music.openFromFile("./audio/Castle Theme-Super Mario World.wav")) {
std::cout << "couldn't load main menu music" << std::endl;
}
music.setLoop(true);
music.setVolume(50);
}
};
void MainMenu::animateTitle() {
float windowWidth = 1280;
float windowHeight = 720;
float pos_x_adjustment = (windowWidth - title.getGlobalBounds().width) / 2;
float pos_y_adjustment = (windowHeight - title.getGlobalBounds().height) / 2;
timeSinceLastFrame = clock.getElapsedTime().asSeconds();
if (timeSinceLastFrame < 3) {
title.scale(1.001, 1.001);
title.setPosition(pos_x_adjustment, pos_y_adjustment);
}
else if (timeSinceLastFrame < 6) {
title.scale(0.999, 0.999);
title.setPosition(pos_x_adjustment, pos_y_adjustment);
}
else {
clock.restart();
title.setPosition(pos_x_adjustment, pos_y_adjustment);
}
}
void MainMenu::allowMenuMusic() {
playMusic = true;
}
void MainMenu::update() {
animateTitle();
if (playMusic) {
music.play();
playMusic = false;
}
}
bool MainMenu::doesGameStart(bool leftClickReleased) {
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
if (leftClickReleased) {
music.stop();
}
return true;
}
return false;
}
sf::Sprite MainMenu::getBackground() {
return background;
}
sf::Sprite MainMenu::getTitle() {
return title;
}