-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
85 lines (72 loc) · 1.91 KB
/
main.c
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
#include <limits.h>
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "tetris.h"
#include "ttrs_action.h"
#include "ttrs_display.h"
#include "ttrs_matrix.h"
#include "ttrs_mino.h"
#include "ttrs_time.h"
#include "wrapper.h"
static t_tetris init_tetris()
{
t_tetris tetris = {0};
tetris.time.interval = INIT_INTERVAL_TIME;
tetris.time.decrease_time = INIT_DECREASE_TIME;
tetris.current = generate_random_mino();
return tetris;
}
static int calculate_score(int num_of_cleared_lines)
{
return SCORE_UNIT * num_of_cleared_lines;
}
static t_is_gamover exec_one_frame(t_tetris *tetris)
{
t_is_gamover is_gameover = false;
t_status status = handle_key_input(tetris, &tetris->current);
if (status == TETRIS_PLAY) {
status = handle_auto_drop(tetris, &tetris->current);
}
if (status == TETRIS_LOCK_DOWN) {
const int num_of_cleared_lines = lock_down_current_mino(tetris);
tetris->score += calculate_score(num_of_cleared_lines);
tetris->current = generate_random_mino();
is_gameover = !can_place_on_matrix(tetris->matrix, &tetris->current.mino, tetris->current.pos);
}
return is_gameover;
}
static void wait_next_frame(t_unix_time_usec start)
{
static const t_unix_time_usec one_frame = SEC_TO_USEC(1) / TTRS_FPS;
t_unix_time_usec elapsed = get_current_usec() - start;
t_unix_time_usec sleep_time = one_frame - elapsed;
if (sleep_time > 0)
Usleep((useconds_t)sleep_time);
}
static void run_tetris_loop(t_tetris *tetris)
{
t_is_gamover is_gameover = false;
while (!is_gameover) {
t_unix_time_usec start = get_current_usec();
is_gameover = exec_one_frame(tetris);
refresh_screen(tetris);
wait_next_frame(start);
}
}
static void init_game()
{
srand((unsigned int)time(NULL));
init_keyhook_func_ptr_array();
}
int main()
{
init_game();
t_tetris tetris = init_tetris();
init_ncurses();
run_tetris_loop(&tetris);
end_ncurses();
print_tetris_result(&tetris);
}