-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.hpp
102 lines (88 loc) · 3.44 KB
/
settings.hpp
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
#include <fstream>
#include <iostream>
#include <string>
#include <tuple> // Should use struct instead of std::tuple, will change it later
#include <vector>
#define SETTINGS_PATH "C://Forsiktig/settings.txt"
#define DEBUG 0
#define DEFAULT_FRAME_DURATION 100
#define DEFAULT_STARTING_ENEMIES 4
#define DEFAULT_STARTING_AMMUNITIONS 100
#ifdef _WIN32
#include <conio.h>
#elif __linux__
#include <unistd.h>
#include <termios.h>
char getch(void) {
char buf = 0;
struct termios old = {0};
fflush(stdout);
if(tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if(tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
if(read(0, &buf, 1) < 0)
perror("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if(tcsetattr(0, TCSADRAIN, &old) < 0)
perror("tcsetattr ~ICANON");
// printf("%c\n", buf);
return buf;
}
#endif
unsigned int countLines(std::string file_) {
std::ifstream file(file_, std::ios::in);
unsigned int lines = 0;
std::string line;
while (std::getline(file, line))
lines++;
return lines;
}
// frame duration, starting enemies, starting ammunitions
std::tuple<unsigned int, unsigned int, unsigned int> loadSettings() {
std::ifstream file(SETTINGS_PATH, std::ios::in);
if (!file) {
std::cout << "You don't have saved settings." << std::endl;
std::cout << "You will play with default settings." << std::endl;
getch();
return std::make_tuple(DEFAULT_FRAME_DURATION, DEFAULT_STARTING_ENEMIES, DEFAULT_STARTING_AMMUNITIONS);
}
unsigned int lines = countLines(SETTINGS_PATH);
// frame duration, starting enemies, starting ammunitions
std::vector<std::tuple<unsigned int, unsigned int, unsigned int>> settings(lines);
for (int i=0; i<lines; i++) {
// Read
file >> std::get<0>(settings[i]) >> std::get<1>(settings[i]) >> std::get<2>(settings[i]);
// Print
std::cout << i+1 << ")" << std::endl;
std::cout << "Frame duration: " << std::get<0>(settings[i]) << std::endl;
std::cout << "Starting ammunitions: " << std::get<2>(settings[i]) << std::endl;
std::cout << "Starting enemies: " << std::get<1>(settings[i]) << std::endl << std::endl;
}
unsigned int choice;
do {
std::cout << "Choose a setting (1-" << lines << "): ";
std::cin >> choice;
} while (choice > lines);
return settings[choice-1];
}
void saveSettings(std::tuple<unsigned int, unsigned int, unsigned int> settings_) {
std::ifstream in_file(SETTINGS_PATH, std::ios::in);
unsigned int lines = countLines(SETTINGS_PATH);
unsigned int frame_duration, starting_enemies, starting_ammunitions;
for (int i=0; i<lines; i++) {
in_file >> frame_duration >> starting_enemies >> starting_ammunitions;
if (std::get<0>(settings_) == frame_duration && std::get<1>(settings_) == starting_enemies && std::get<2>(settings_) == starting_ammunitions) {
std::cout << "Settings already saved!" << std::endl;
return;
}
}
std::ofstream file(SETTINGS_PATH, std::ios::app);
file << std::get<0>(settings_) << ' ' << std::get<1>(settings_) << ' ' << std::get<2>(settings_) << std::endl;
std::cout << "Settings saved!" << std::endl;
}