-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.c
132 lines (113 loc) · 2.93 KB
/
helper.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// Created by marosm on 2/21/19.
//
#include "helper.h"
inline void gotoXY(const int x, const int y)
{
#if defined(__linux__)
printf("%c[%d;%df",0x1B,y,x);
#endif
#ifdef WIN32
COORD coord;
coord.X = (short) x;
coord.Y = (short) y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
#endif
}
inline void clearScreen(void)
{
#if defined(__linux__)
(void)system("clear");
#endif
#if defined(_WIN32) || defined(WIN32)
system("cls");
#endif
}
char parseKey()
{
#if defined(__linux__)
char c = 0;
while(kbhit()) c = getch(); //Skip escape characters
switch(c)
{
case 'A': //arrow up
return 'w';
case 'B': //arrow down
return 's';
case 'C': //arrow right
return 'd';
case 'D': //arrow left
return 'a';
case 10: //enter
return 'n';
case 8: //backspace
return 'b';
default:
return c;
}
#endif
#if defined(WIN32) || defined(_WIN32)
char c = getch();
switch(c)
{
case 'H': //arrow up
return 'w';
case 'P': //arrow down
return 's';
case 'M': //arrow right
return 'd';
case 'K': //arrow left
return 'a';
default: return c;
}
#endif
}
void msDelay(const unsigned time)
{
#if defined(_WIN32) || defined(WIN32)
Sleep(time);
#endif
#if defined(__linux__)
usleep((__useconds_t) time * 1000);
#endif
}
void usElapsed(unsigned long long *timer)
{
#if defined(_WIN32) || defined(WIN32)
LARGE_INTEGER frequency, spec;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&spec);
*timer = (unsigned long long) spec.QuadPart*1000000 / frequency.QuadPart; //sec + ns to us
#endif
#if defined(__linux__)
struct timespec spec;
clock_gettime(CLOCK_MONOTONIC, &spec);
*timer = (unsigned long long) (spec.tv_sec * 1e6) + (spec.tv_nsec / 1000); //sec + ns to us
#endif
}
inline unsigned short getConsoleHeight()
{
#if defined(__linux__)
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
return w.ws_row;
#endif
#ifdef WIN32
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
return csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
#endif
}
inline unsigned short getConsoleWidth()
{
#if defined(__linux__)
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
return w.ws_col;
#endif
#ifdef WIN32
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
return csbi.srWindow.Right - csbi.srWindow.Left + 1;
#endif
}