-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttrs_display.c
79 lines (69 loc) · 1.53 KB
/
ttrs_display.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
#include <ncurses.h>
#include <stdlib.h>
#include <string.h>
#include "tetris.h"
#include "ttrs_display.h"
#include "ttrs_matrix.h"
#include "wrapper.h"
static void print_header()
{
for (int i = 0; i < MATRIX_COL / 2; i++) {
if (printw(" ") == ERR) {
exit_fatal_err(EXIT_FAILURE);
}
}
if (printw(GAME_TITLE) == ERR) {
exit_fatal_err(EXIT_FAILURE);
}
}
static void print_matrix(const t_matrix matrix, t_print_func print_func)
{
for (int i = 0; i < MATRIX_ROW; i++) {
for (int j = 0; j < MATRIX_COL; j++) {
char texture = matrix[i][j] ? BLOCK_TEXTURE : EMPTY_TEXTURE;
if (print_func("%c ", texture) == ERR) {
exit_fatal_err(EXIT_FAILURE);
}
}
if (print_func("\n") == ERR) {
exit_fatal_err(EXIT_FAILURE);
}
}
}
static void print_body(const t_matrix matrix, t_current_mino *current)
{
t_matrix current_matrix;
memcpy(current_matrix, matrix, sizeof(t_matrix));
place_mino_on_matrix(current_matrix, current);
print_matrix(current_matrix, printw);
}
static void print_score(int score, t_print_func print_func)
{
if (print_func("\n" SCORE_TEXT "%d\n", score) == ERR) {
exit_fatal_err(EXIT_FAILURE);
}
}
void print_tetris_result(const t_tetris *tetris)
{
print_matrix(tetris->matrix, printf);
Puts(GAME_OVER_TEXT);
print_score(tetris->score, printf);
}
void refresh_screen(t_tetris *tetris)
{
clear();
print_header();
print_body(tetris->matrix, &tetris->current);
print_score(tetris->score, printw);
}
void init_ncurses()
{
Initscr();
timeout(1);
noecho();
curs_set(0);
}
void end_ncurses()
{
Endwin();
}