-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
118 lines (108 loc) · 3.18 KB
/
Copy pathmain.cpp
File metadata and controls
118 lines (108 loc) · 3.18 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
#include "Menu.hpp"
#include "TetrisBoard.hpp"
#include "Leaderboard.hpp"
#include "platform.hpp"
#include <chrono>
using namespace std;
int showMenu(Menu* mMenu) {
int n = -1;
while ( n == -1 ) {
n = mMenu-> getInput();
}
return n;
}
int playTetris(TetrisBoard* tBoard) {
int n = 0;
int r = tBoard-> getFallRate();
char inpt;
chrono::time_point<std::chrono::high_resolution_clock> t_start, t_end;
// time diff
while ( n == 0 ) {
t_start = chrono::high_resolution_clock::now();
timeout(r);
n = tBoard-> getInput(getch());
t_end = chrono::high_resolution_clock::now();
r += (int)std::chrono::duration_cast<chrono::milliseconds>(t_start - t_end).count();
if ( r <= 0 ) {
r = tBoard-> getFallRate();
}
}
return n;
}
int showLeaderboard(Leaderboard* lBoard) {
int n = 1;
while ( n == 1 ) {
n = lBoard-> getInput();
}
return n;
}
int main() {
initscr();
if (has_colors()) {
start_color();
init_pair(1, COLOR_YELLOW, COLOR_YELLOW);
init_pair(2, COLOR_CYAN, COLOR_CYAN);
init_pair(3, COLOR_BLUE, COLOR_BLUE);
init_pair(4, COLOR_WHITE, COLOR_WHITE);
init_pair(5, COLOR_GREEN, COLOR_GREEN);
init_pair(6, COLOR_MAGENTA, COLOR_MAGENTA);
init_pair(7, COLOR_RED, COLOR_RED);
init_pair(11, COLOR_GREEN, COLOR_BLACK);
init_pair(12, COLOR_CYAN, COLOR_BLACK);
init_pair(13, COLOR_YELLOW, COLOR_BLACK);
init_pair(14, COLOR_RED, COLOR_BLACK);
init_pair(15, COLOR_BLUE, COLOR_BLACK);
init_pair(16, COLOR_MAGENTA, COLOR_BLACK);
} else {
printw("No colors were found, errors may occur. ");
}
refresh();
noecho();
keypad(stdscr, true);
curs_set(0);
Menu mainMenu = Menu();
TetrisBoard tBoard = TetrisBoard();
Leaderboard lBoard = Leaderboard();
bool inApp = true;
// NOTE inpt cases: -1, menu
// 0, game
// 1, leaderboard
// 2, exit
int inpt = -1;
while ( inApp ) {
switch ( inpt ) {
case -1:
// remove eventual loaded windows;
tBoard.remove();
lBoard.remove();
// load the menu;
mainMenu.reload();
inpt = showMenu(&mainMenu);
break;
case 0:
mainMenu.remove();
lBoard.remove();
// start the game;
tBoard.reload();
inpt = playTetris(&tBoard);
break;
case 1:
mainMenu.remove();
tBoard.remove();
// show the leaderboard;
lBoard.reload();
inpt = showLeaderboard(&lBoard);
break;
case 2:
// exit the app;
mainMenu.remove();
inApp = false;
break;
default:
printw("Somehow you managed to get here... interesting... ^C to exit and try again. ");
break;
}
}
endwin();
return 0;
}