-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.c
90 lines (85 loc) · 2.97 KB
/
play.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
86
87
88
89
90
#include <ncurses.h>
#include "status.h"
#include "pattern.h"
static void print_pattern(WINDOW *pattern_win, Pattern *pattern)
{
for (size_t y = 0; y < pattern->size.y; y++)
{
for (size_t x = 0; x < pattern->size.x; x++)
{
if (pattern->cells[y][x] == state_alive)
{
wattron(pattern_win, A_STANDOUT);
}
mvwprintw(pattern_win, y + 1, x * 2 + 1, " ");
wattroff(pattern_win, A_STANDOUT);
}
waddch(pattern_win, ACS_VLINE);
}
wrefresh(pattern_win);
}
void play_pattern(Pattern *pattern)
{
size_t screen_size_y, screen_size_x; // = Terminal magassag, szelesseg
getmaxyx(stdscr, screen_size_y, screen_size_x);
if (screen_size_y - 2 < pattern->size.y || (screen_size_x + 2) / 2 < pattern->size.x)
{
print_status(warning, "The pattern (%dx%d) is bigger than your terminal window. (%dx%d)", pattern->size.y, pattern->size.x, screen_size_y - 2, (screen_size_x + 2) / 2);
return;
}
WINDOW *pattern_win = newwin(pattern->size.y + 2, ((pattern->size.x + 1) * 2), ((screen_size_y - pattern->size.y) - 2) / 2, ((screen_size_x - (pattern->size.x + 1) * 2)) / 2);
if (pattern_win == NULL)
{
print_status(error, "Pattern window allocation failed. (Make the terminal a little bit bigger.)");
return;
}
clear();
print_status(info, "Loaded.");
keypad(pattern_win, TRUE);
box(pattern_win, ACS_VLINE, ACS_HLINE);
print_pattern(pattern_win, pattern);
wrefresh(pattern_win);
int delay_amount = 10; // = 10 tized másodperc
bool continuous_mode = false;
int c;
while ((c = wgetch(pattern_win)) != 'x')
{
if (continuous_mode)
halfdelay(delay_amount);
switch (c)
{
case ' ':
next_generation(pattern);
print_pattern(pattern_win, pattern);
wrefresh(pattern_win);
break;
case KEY_UP:
if (delay_amount > 1) // = max fps az ncurses miatt 1 tized mp
delay_amount--;
print_status(info, "Step frequency: %d FPS", 11 - delay_amount);
break;
case KEY_DOWN:
if (delay_amount < 10) // = min fps = 10/10=1 mp
delay_amount++;
print_status(info, "Step frequency: %d FPS", 11 - delay_amount);
break;
case 'c':
continuous_mode = !continuous_mode;
if (continuous_mode)
halfdelay(delay_amount);
else
cbreak();
print_status(info, "Continous mode: %s", continuous_mode ? "ON" : "OFF");
break;
default:
if (continuous_mode)
{
ungetch(' '); // Mintha megnyomtam volna a space-t
}
break;
}
}
// Ha folyamatos üzemmódban kilép a felhasználó, a halfdelay üzemmód megmarad.
cbreak();
// Nem szükséges, de hatékonyabb, ha visszaállítom.
}