-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainMenu.cpp
More file actions
125 lines (105 loc) · 3.96 KB
/
MainMenu.cpp
File metadata and controls
125 lines (105 loc) · 3.96 KB
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
#include "MainMenu.h"
#include "Animation.h"
void MainMenu::initTextures()
{
if (!texture.loadFromFile("Textures/mainMenu.png")) { cout << "No texture found" << endl; }
if (!background_texture.loadFromFile("Textures/mainMenu_background.png")) { cout << "No texture found" << endl; }
background_texture.setRepeated(true);
if (!music.openFromFile("Textures/wino.wav")) { cout << "No music found" << endl; }
music.setVolume(30);
bufferTick.loadFromFile("Textures/menuTick.wav");
soundTick.setBuffer(bufferTick);
}
void MainMenu::initSprites(RenderWindow& gameWindow)
{
animation = new Animation(texture, { {60, 0, 0, 100, 100} });
sprite.setTexture(texture);
sprite.setTextureRect(IntRect(0, 0, 100, 100));
sprite.setScale(gameWindow.getSize().x / 68, gameWindow.getSize().x / 68);
int x = gameWindow.getSize().x / 2.5;
sprite.setPosition(-x, gameWindow.getSize().y / 3);
background.setTexture(background_texture);
background.setTextureRect(IntRect(0,0, gameWindow.getSize().x, gameWindow.getSize().y));
}
void MainMenu::initTexts(Font& gameFont, RenderWindow& gameWindow)
{
texts[0].setFont(gameFont);
texts[0].setString("PROJECT TERRA");
texts[0].setPosition(gameWindow.getSize().x / 5, gameWindow.getSize().y / 14);
texts[0].setCharacterSize(gameWindow.getSize().x / 10);
texts[0].setFillColor(Color::White);
texts[0].setOutlineColor(Color::Black);
texts[0].setOutlineThickness(2);
texts[1].setFont(gameFont);
texts[1].setString("Play");
texts[1].setPosition(gameWindow.getSize().x / 1.3, gameWindow.getSize().y / 3);
texts[1].setFillColor(Color::White);
texts[1].setCharacterSize(gameWindow.getSize().x / 16);
texts[1].setOutlineColor(Color::Black);
texts[1].setOutlineThickness(2);
texts[2].setFont(gameFont);
texts[2].setString("Exit");
texts[2].setPosition(gameWindow.getSize().x / 1.3, gameWindow.getSize().y / 2.2);
texts[2].setCharacterSize(gameWindow.getSize().x / 16);
texts[2].setFillColor(Color::White);
texts[2].setOutlineColor(Color::Black);
texts[2].setOutlineThickness(2);
}
void MainMenu::update(RenderWindow& gameWindow)
{
if (Mouse::getPosition(gameWindow).x > texts[1].getGlobalBounds().left && Mouse::getPosition(gameWindow).x < (texts[1].getGlobalBounds().left + texts[1].getGlobalBounds().width) && Mouse::getPosition(gameWindow).y > texts[1].getGlobalBounds().top && Mouse::getPosition(gameWindow).y < texts[1].getGlobalBounds().top + texts[1].getGlobalBounds().height) {
if (selectedButton != Play) soundTick.play();
selectedButton = Play;
mouseOnButton = play;
}
else if (Mouse::getPosition(gameWindow).x > texts[2].getGlobalBounds().left && Mouse::getPosition(gameWindow).x < texts[2].getGlobalBounds().left + texts[2].getGlobalBounds().width && Mouse::getPosition(gameWindow).y > texts[2].getGlobalBounds().top && Mouse::getPosition(gameWindow).y < texts[2].getGlobalBounds().top + texts[2].getGlobalBounds().height) {
if (selectedButton != Exit) soundTick.play();
selectedButton = Exit;
mouseOnButton = exit;
}
else { mouseOnButton = none; }
if (selectedButton == Play) {
texts[1].setFillColor(Color::Green);
texts[2].setFillColor(Color::White);
}
else if (selectedButton == Exit) {
texts[2].setFillColor(Color::Green);
texts[1].setFillColor(Color::White);
}
}
MainMenu::MainMenu(Font& gameFont, RenderWindow& gameWindow)
{
initTextures();
initSprites(gameWindow);
initTexts(gameFont,gameWindow);
selectedButton = Play;
}
void MainMenu::display(RenderWindow& gameWindow, Time *elapsed)
{
gameWindow.draw(background);
animation->Update(elapsed->asSeconds());
animation->ApplyToSprite(&sprite);
gameWindow.draw(sprite);
update(gameWindow);
for (int i = 0; i < 3; i++) {
gameWindow.draw(texts[i]);
}
}
void MainMenu::selectUp()
{
if (selectedButton != Play) soundTick.play();
selectedButton = Play;
}
void MainMenu::selectDown()
{
if (selectedButton != Exit) soundTick.play();
selectedButton = Exit;
}
int MainMenu::returnSelectedButton()
{
return selectedButton;
}
int MainMenu::returnMouseOnButton()
{
return mouseOnButton;
}