xTetris-Game
Loading...
Searching...
No Matches
common.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include <stdio.h>
3#include <sys/ioctl.h>
4#include <wchar.h>
5
6#include <termios.h>
7#include <unistd.h>
8#include <sys/time.h>
9#include <sys/types.h>
10
11#include "common.h"
12#include "definitions.h"
13
14const wchar_t chSq = 0x2705; /* checkSquare ✅ */
15const wchar_t crSq = 0x274E; /* crossSquare ❎ */
16const wchar_t eSq = 0x2B1C; /* emptySquare ⬜ */
17const wchar_t fSq = 0x2B1B; /* fullSquare ⬛ */
18
19const int timeLimit = 1000;
20
21void sound(){
22 wprintf(L"\a");
23}
24
25void clearCLI(void){
26
27 int res;
28
29 res = system("clear");
30 if (res == 1){
31 clearCLI();
32 exit(EXIT_FAILURE);
33 }
34}
35
36void exitFailure(void){
37 clearCLI();
38 exit(EXIT_FAILURE);
39}
40
41void delayTimer(int time){
42 sleep(time);
43}
44
45struct winsize w;
46
47int intLen(int value){
48
49 int length = 0;
50
51 if (value != 0){
52 while(value!=0){
53 value = value/10;
54 length++;
55 }
56 }
57
58 return (length==0)?1:length;
59
60}
61
62void widthSpacing(int filledWidth){
63
64 int i;
65
66 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
67 for(i=0; i<(w.ws_col-filledWidth)/2; i++)
68 wprintf(L" ");
69}
70
71void printCentered(wchar_t *text){
72 widthSpacing(wcslen(text));
73 wprintf(L"%ls\r\n", text);
74}
75
76void heightSpacing(int filledHeight){
77
78 int i;
79
80 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
81 for(i=0; i<(w.ws_row-filledHeight)/2; i++)
82 wprintf(L"\r\n");
83}
84
85static struct termios orig_term;
86
87void u_cleanup(void){
88 tcsetattr(0, TCSANOW, &orig_term);
89}
90
91int u_kbhit(void){
92
93 struct termios t;
94 fd_set rfd;
95 struct timeval to;
96 static int first_hit=0;
97
98 if(first_hit==0){
99 if(tcgetattr(0, &t)!=0)
100 exit(0);
101 orig_term=t;
102 cfmakeraw(&t);
103 if(tcsetattr(0, TCSANOW, &t)!=0)
104 exit(0);
105 atexit(u_cleanup);
106 first_hit=1;
107 }
108
109 FD_ZERO(&rfd);
110 FD_SET(0, &rfd);
111 to.tv_sec=0;
112 to.tv_usec=0;
113 if(select(1, &rfd, NULL, NULL, &to)==1)
114 return 1;
115 return 0;
116}
117
118int u_getchar(void){
119
120 int ret;
121 unsigned char buf;
122
123 if(read(0, &buf, 1)!=1)
124 ret=0;
125 else
126 ret=buf;
127 return ret;
128}
129
131
132 int key=0;
133
134 while(1){
135 if(u_kbhit()){
136 key=u_getchar();
137 if (key == CTRL_C)
138 exitFailure();
139 return key;
140 }
141 usleep(NSEC_TO_SLEEP);
142 }
143 exitFailure();
144}
145
146void waitUser(void){
147 int key=2;
148 while(key!=CARRIAGE_RETURN){
149 key = waitUserInput();
150 }
151}
void printCentered(wchar_t *text)
Print text centered horizontally based on string length.
Definition common.c:71
struct winsize w
Global variable type struct winsize used for terminal width and height.
Definition common.c:45
const wchar_t crSq
Assign unicode character (green crossed square) to this global variable.
Definition common.c:15
void delayTimer(int time)
Call the system sleep.
Definition common.c:41
int intLen(int value)
Calculate the length of a given number.
Definition common.c:47
int waitUserInput(void)
Loop until user press a valid key.
Definition common.c:130
void sound()
Makes a beep sound.
Definition common.c:21
void heightSpacing(int filledHeight)
Apply a vertical spacing based on window heigth and what need to be printed.
Definition common.c:76
const wchar_t fSq
Assign unicode character (black square) to this global variable.
Definition common.c:17
void widthSpacing(int filledWidth)
Apply an horizontal spacing based on window width and what need to be printed.
Definition common.c:62
void u_cleanup(void)
Definition common.c:87
const int timeLimit
Time waiting before letting the tetromino fall one row below.
Definition common.c:19
const wchar_t eSq
Assign unicode character (white square) to this global variable.
Definition common.c:16
void waitUser(void)
Wait until Enter key is pressed.
Definition common.c:146
void exitFailure(void)
Exit the program after cleaning everything on screen.
Definition common.c:36
void clearCLI(void)
Call the system function to clear the cli.
Definition common.c:25
int u_getchar(void)
Definition common.c:118
int u_kbhit(void)
Definition common.c:91
const wchar_t chSq
Assign unicode character (green check square) to this global variable.
Definition common.c:14
#define CARRIAGE_RETURN
Definition definitions.h:20
#define CTRL_C
Definition definitions.h:19
#define NSEC_TO_SLEEP
Definition definitions.h:43