-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
99 lines (83 loc) · 2.22 KB
/
util.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
91
92
93
94
95
96
97
98
99
#include "util.h"
void goto_xy_print(const size_t x, const size_t y, const char* str, ...)
{
HANDLE console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
va_list list;
size_t count = 0;
char* str_copy = (char*)str;
pos.X = (SHORT)x;
pos.Y = (SHORT)y;
SetConsoleCursorPosition(console_handle, pos);
while (*str_copy != '\0') {
if (*str_copy == '%') {
++count;
}
++str_copy;
}
str_copy = (char*)str;
va_start(list, count);
while (*str_copy != '\0') {
char print = *str_copy;
if (*str_copy == '%') {
++str_copy;
if (*str_copy == 'c') {
print = va_arg(list, char);
} else if (*str_copy == 'd') {
int num = va_arg(list, int);
printf("%d", num);
++str_copy;
continue;
}
}
putchar(print);
++str_copy;
}
va_end(list);
}
input_key_t key_control(void)
{
char pressed_key = _getch();
switch (pressed_key)
{
case 'w':
/* intentional fallthrough */
case 'W':
return INPUT_KEY_UP;
case 's':
/* intentional fallthrough */
case 'S':
return INPUT_KEY_DOWN;
case 'a':
/* intentional fallthrough */
case 'A':
return INPUT_KEY_LEFT;
case 'd':
/* intentional fallthrough */
case 'D':
return INPUT_KEY_RIGHT;
case 'p':
/* intentional fallthrough */
case 'P':
return INPUT_KEY_PAUSE;
case ' ':
return INPUT_KEY_SUBMIT;
default:
return INPUT_KEY_INVALID;
}
}
void set_color(const int fore_ground, const int back_ground)
{
int code = fore_ground + back_ground * 16;
HANDLE console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(console_handle, code);
}
void set_direction(direction_t* direction, const input_key_t input) {
if ((*direction == NORTH && input != INPUT_KEY_DOWN)
|| (*direction == SOUTH && input != INPUT_KEY_UP)
|| (*direction == WEST && input != INPUT_KEY_RIGHT)
|| (*direction == EAST && input != INPUT_KEY_LEFT))
{
*direction = (direction_t)input;
}
}