xTetris-Game
Loading...
Searching...
No Matches
Functions | Variables
common.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <wchar.h>
#include <termios.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include "common.h"
#include "definitions.h"

Go to the source code of this file.

Functions

void sound ()
 Makes a beep sound.
 
void clearCLI (void)
 Call the system function to clear the cli.
 
void exitFailure (void)
 Exit the program after cleaning everything on screen.
 
void delayTimer (int time)
 Call the system sleep.
 
int intLen (int value)
 Calculate the length of a given number.
 
void widthSpacing (int filledWidth)
 Apply an horizontal spacing based on window width and what need to be printed.
 
void printCentered (wchar_t *text)
 Print text centered horizontally based on string length.
 
void heightSpacing (int filledHeight)
 Apply a vertical spacing based on window heigth and what need to be printed.
 
void u_cleanup (void)
 
int u_kbhit (void)
 
int u_getchar (void)
 
int waitUserInput (void)
 Loop until user press a valid key.
 
void waitUser (void)
 Wait until Enter key is pressed.
 

Variables

const wchar_t chSq = 0x2705
 Assign unicode character (green check square) to this global variable.
 
const wchar_t crSq = 0x274E
 Assign unicode character (green crossed square) to this global variable.
 
const wchar_t eSq = 0x2B1C
 Assign unicode character (white square) to this global variable.
 
const wchar_t fSq = 0x2B1B
 Assign unicode character (black square) to this global variable.
 
const int timeLimit = 1000
 Time waiting before letting the tetromino fall one row below.
 
struct winsize w
 Global variable type struct winsize used for terminal width and height.
 

Function Documentation

◆ clearCLI()

void clearCLI ( void )

Call the system function to clear the cli.

Definition at line 25 of file common.c.

25 {
26
27 int res;
28
29 res = system("clear");
30 if (res == 1){
31 clearCLI();
32 exit(EXIT_FAILURE);
33 }
34}
void clearCLI(void)
Call the system function to clear the cli.
Definition common.c:25

◆ delayTimer()

void delayTimer ( int timeDelay)

Call the system sleep.

Parameters
[in]timeDelayTime of the sleep in seconds.

Definition at line 41 of file common.c.

41 {
42 sleep(time);
43}

◆ exitFailure()

void exitFailure ( void )

Exit the program after cleaning everything on screen.

Definition at line 36 of file common.c.

36 {
37 clearCLI();
38 exit(EXIT_FAILURE);
39}

◆ heightSpacing()

void heightSpacing ( int filledHeight)

Apply a vertical spacing based on window heigth and what need to be printed.

Parameters
[in]filledHeightHeight of what is printed that need to be centered.

Definition at line 76 of file common.c.

76 {
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}
struct winsize w
Global variable type struct winsize used for terminal width and height.
Definition common.c:45

◆ intLen()

int intLen ( int value)

Calculate the length of a given number.

Parameters
[in]valueNumber which needs his length calculated.
[in]lengthReturn the length of the number.

Definition at line 47 of file common.c.

47 {
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}

◆ printCentered()

void printCentered ( wchar_t * text)

Print text centered horizontally based on string length.

Parameters
[in]textText that need to be printed on screen (doesn't include special characters).

Definition at line 71 of file common.c.

71 {
72 widthSpacing(wcslen(text));
73 wprintf(L"%ls\r\n", text);
74}
void widthSpacing(int filledWidth)
Apply an horizontal spacing based on window width and what need to be printed.
Definition common.c:62

◆ sound()

void sound ( void )

Makes a beep sound.

Definition at line 21 of file common.c.

21 {
22 wprintf(L"\a");
23}

◆ u_cleanup()

void u_cleanup ( void )

Definition at line 87 of file common.c.

87 {
88 tcsetattr(0, TCSANOW, &orig_term);
89}

◆ u_getchar()

int u_getchar ( void )

Definition at line 118 of file common.c.

118 {
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}

◆ u_kbhit()

int u_kbhit ( void )

Definition at line 91 of file common.c.

91 {
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}
void u_cleanup(void)
Definition common.c:87

◆ waitUser()

void waitUser ( void )

Wait until Enter key is pressed.

Definition at line 146 of file common.c.

146 {
147 int key=2;
148 while(key!=CARRIAGE_RETURN){
149 key = waitUserInput();
150 }
151}
int waitUserInput(void)
Loop until user press a valid key.
Definition common.c:130
#define CARRIAGE_RETURN
Definition definitions.h:20

◆ waitUserInput()

int waitUserInput ( void )

Loop until user press a valid key.

Parameters
[in]timeTime in microseconds which the usleep is called.

Definition at line 130 of file common.c.

130 {
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}
void exitFailure(void)
Exit the program after cleaning everything on screen.
Definition common.c:36
int u_getchar(void)
Definition common.c:118
int u_kbhit(void)
Definition common.c:91
#define CTRL_C
Definition definitions.h:19
#define NSEC_TO_SLEEP
Definition definitions.h:43

◆ widthSpacing()

void widthSpacing ( int filledWidth)

Apply an horizontal spacing based on window width and what need to be printed.

Parameters
[in]filledWidthWidth of what is printed that need to be centered.

Definition at line 62 of file common.c.

62 {
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}

Variable Documentation

◆ chSq

const wchar_t chSq = 0x2705

Assign unicode character (green check square) to this global variable.

Definition at line 14 of file common.c.

◆ crSq

const wchar_t crSq = 0x274E

Assign unicode character (green crossed square) to this global variable.

Definition at line 15 of file common.c.

◆ eSq

const wchar_t eSq = 0x2B1C

Assign unicode character (white square) to this global variable.

Definition at line 16 of file common.c.

◆ fSq

const wchar_t fSq = 0x2B1B

Assign unicode character (black square) to this global variable.

Definition at line 17 of file common.c.

◆ timeLimit

const int timeLimit = 1000

Time waiting before letting the tetromino fall one row below.

Definition at line 19 of file common.c.

◆ w

struct winsize w

Global variable type struct winsize used for terminal width and height.

Definition at line 45 of file common.c.