-
Notifications
You must be signed in to change notification settings - Fork 0
/
Options.cpp
164 lines (138 loc) · 4.76 KB
/
Options.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
#include "stdafx.h"
#include "Options.h"
#pragma region Options
Options::Options(Game* g) {
_game = g;
_settings = &(g->_settings);
CreateButtons();
LoadAudio();
}
Options::~Options() {
_clickSound.resetBuffer();
}
void Options::LoadAudio() {
if (_clickBuffer.loadFromFile("Audio\\click.wav"))
std::cout << "\n_Options: Audio loaded";
else return;
_clickSound.setBuffer(_clickBuffer);
_clickSound.setVolume(50);
_clickSound.setPlayingOffset(sf::seconds(.15f));
}
void Options::Show() {
LoadStrings();
DrawButtons(_game->GetWindow());
HandleEvents(_game->GetWindow());
}
void Options::CreateButtons() {
MenuButton* _Fullscreen = new MenuButton;
MenuButton* _Resolution = new MenuButton;
MenuButton* _Antialiasing = new MenuButton;
MenuButton* _VSync = new MenuButton;
MenuButton* _Apply = new MenuButton;
MenuButton* _Back = new MenuButton;
sf::Font *f;
f = &_game->_font;
_Fullscreen->ac = Fullscreen;
_Resolution->ac = Resolution;
_Antialiasing->ac = Antialiasing;
_VSync->ac = VSync;
_Apply->ac = Apply;
_Back->ac = Back;
_Fullscreen-> text.setFillColor(MenuFGColor);
_Resolution-> text.setFillColor(MenuFGColor);
_Antialiasing-> text.setFillColor(MenuFGColor);
_VSync-> text.setFillColor(MenuFGColor);
_Apply-> text.setFillColor(MenuFGColor);
_Back-> text.setFillColor(MenuFGColor);
_Fullscreen->text.setFont(*f);
_Resolution->text.setFont(*f);
_Antialiasing->text.setFont(*f);
_VSync->text.setFont(*f);
_Apply->text.setFont(*f);
_Back->text.setFont(*f);
_buttons.push_back(_Fullscreen);
_buttons.push_back(_Resolution);
_buttons.push_back(_Antialiasing);
_buttons.push_back(_VSync);
_buttons.push_back(_Apply);
_buttons.push_back(_Back);
}
void Options::LoadStrings() {
std::string aux = (_settings->gFullscreen() == 1) ? "[Enabled]" : "[Disabled]";
_buttons[0]->text.setString("Fullscreen " + aux);
_buttons[1]->text.setString("Resolution [" +
std::to_string((int)_settings->gResolution().x) + "x" +
std::to_string((int)_settings->gResolution().y) + "]");
_buttons[2]->text.setString("Antialiasing Level [" +
std::to_string((int)_settings->gAntialiasing()) + "]");
aux = (_settings->gVSync() == 1) ? "[Enabled]" : "[Disabled]";
_buttons[3]->text.setString("V-Sync " + aux);
_buttons[4]->text.setString("Apply");
_buttons[5]->text.setString("Back");
int nr = 3;
sf::Vector2u s = _game->GetWindow().getSize();
for (int i = 0; i < _buttons.size(); i++) {
_buttons[i]->text.setOrigin(_buttons[i]->text.getGlobalBounds().width / 2, _buttons[i]->text.getGlobalBounds().height / 2);
_buttons[i]->text.setCharacterSize(s.y / 15);
_buttons[i]->text.setPosition(s.x / 2, s.y / 10 * (nr++));
}
}
void Options::DrawButtons(sf::RenderWindow& window) {
window.setView(window.getDefaultView());
window.clear(MenuBGColor);
for (int i = 0; i < _buttons.size(); i++)
window.draw(_buttons[i]->text);
window.display();
}
void Options::HandleEvents(sf::RenderWindow& window) {
sf::Event e;
while (window.pollEvent(e)) {
if (e.type == sf::Event::MouseButtonPressed)
return HandleClicks(e.mouseButton.x, e.mouseButton.y);
if (e.type == sf::Event::Closed)
_game->Quit();
if (e.type == sf::Event::KeyPressed &&
e.key.code == sf::Keyboard::Escape)
_game->PopState();
}
}
void Options::HandleClicks(int x, int y) {
for (int i = 0; i < _buttons.size(); i++)
if (_buttons[i]->text.getGlobalBounds().contains(x, y))
switch (_buttons[i]->ac) {
case Nothing: break;
case Fullscreen: _clickSound.play(); ToggleFullscreen(); return;
case Resolution: _clickSound.play(); ChangeResolution(); return;
case Antialiasing: _clickSound.play(); ChangeAntialiasing(); return;
case VSync: _clickSound.play(); ToggleVSync(); return;
case Apply: /*_clickSound.play();*/ SaveSettings(); return;
case Back: _clickSound.play(); _game->PopState(); return;
default: break;
}
}
void Options::ToggleFullscreen() {
_settings->sFullscreen(_settings->gFullscreen() == 1 ? 0 : 1);
}
void Options::ChangeResolution() {
if (_settings->gResolution() == sf::Vector2f( 800, 600))
_settings->sResolution( sf::Vector2f(1024, 768));
else if (_settings->gResolution() == sf::Vector2f(1024, 768))
_settings->sResolution( sf::Vector2f(1280, 720));
else if (_settings->gResolution() == sf::Vector2f(1280, 720))
_settings->sResolution( sf::Vector2f(1366, 768));
else _settings->sResolution( sf::Vector2f( 800, 600));
}
void Options::ChangeAntialiasing() {
_settings->sAntialiasing(
(_settings->gAntialiasing() == 8) ? 0 : (_settings->gAntialiasing() + 1));
}
void Options::ToggleVSync() {
_settings->sVSync(_settings->gVSync() == 1 ? 0 : 1);
}
void Options::SaveSettings() {
_settings->Save();
_game->LoadGame();
//LoadWindow();
//_score.Load(_settings->gResolution(), &internalClock);
}
#pragma endregion Options