forked from samelinux/rlTutorial2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.c
executable file
·136 lines (122 loc) · 3.26 KB
/
screen.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
133
134
135
#include <stdio.h>
#include <stdarg.h>
#include <sys/ioctl.h>
#include "screen.h"
//this whole file is based on ansi escape codes:
//https://en.wikipedia.org/wiki/ANSI_escape_code
//These two varaibles will hold the terminal size after a call to screenInit
uint16_t screenWidth=0;
uint16_t screenHeight=0;
//Initialize the screen: get terminal size and hide the cursor
void screenInit(void)
{
struct winsize window;
//get window attributes
ioctl(0,TIOCGWINSZ,&window);
//save terminal width and height
screenWidth=window.ws_col;
screenHeight=window.ws_row;
//hide the cursor
printf("\033[?25l");
//reset attributes so we don't have any terminal color affecting our print
screenReset();
//clear the screen so we don't have any terminal text left
screenClear();
fflush(stdout);
}
//Deinitialize the screen: reset attributes and show the cursor
void screenDeinit(void)
{
//clear the screen so we don't leave any mess
screenClear();
//reset attributes so we don't leave any color
screenReset();
//show the cursor
printf("\033[?25h");
fflush(stdout);
}
//reset printing attributes
void screenReset(void)
{
printf("\033[0m");
fflush(stdout);
}
//clear the screen
void screenClear(void)
{
printf("\033[H\033[J");
fflush(stdout);
}
//put a char at x,y using current attributes
void screenPut(int8_t x,int8_t y,uint8_t c)
{
printf("\033[%d;%dH%c",y+1,x+1,c);
fflush(stdout);
}
//put a char at x,y changing foreground and background colors
void screenColorPut(int8_t x,int8_t y,int8_t fg,int8_t bg,uint8_t c)
{
printf("\033[%d;%dH\033[%d;%dm%c",y+1,x+1,bg+10,fg,c);
//reset the screen attributes since this fg and bg are just for this print
screenReset();
fflush(stdout);
}
//print a string at x,y using current attributes
void screenPrint(int8_t x,int8_t y,char* format,...)
{
va_list argumentList;
//get the variadic function arguments
va_start(argumentList,format);
printf("\033[%d;%dH",y+1,x+1);
//use vprintf since it takes a va_list while printf takes a ...
vprintf(format,argumentList);
fflush(stdout);
va_end(argumentList);
}
//print a string at x,y changing foreground and background colors
void screenColorPrint(int8_t x,int8_t y,int8_t fg,int8_t bg,char* format,...)
{
va_list argumentList;
va_start(argumentList,format);
printf("\033[%d;%dH\033[%d;%dm",y+1,x+1,bg+10,fg);
vprintf(format,argumentList);
//reset the screen attributes since this fg and bg are just for this print
screenReset();
fflush(stdout);
va_end(argumentList);
}
//draw a box from x0,y0 to x1,y1 with fg and bg colors
void screenBox(int8_t x0,int8_t y0,int8_t x1,int8_t y1,int8_t fg,int8_t bg)
{
//draw all four corner
screenColorPut(x0,y0,fg,bg,'+');
screenColorPut(x1,y0,fg,bg,'+');
screenColorPut(x0,y1,fg,bg,'+');
screenColorPut(x1,y1,fg,bg,'+');
//draw 2 horizontal line
for(int8_t dx=x0+1;dx<x1;dx++)
{
screenColorPut(dx,y0,fg,bg,'-');
screenColorPut(dx,y1,fg,bg,'-');
}
//draw two vertical line
for(int8_t dy=y0+1;dy<y1;dy++)
{
screenColorPut(x0,dy,fg,bg,'|');
screenColorPut(x1,dy,fg,bg,'|');
}
//fill the box
for(int8_t dy=y0+1;dy<y1-1;dy++)
{
for(int8_t dx=x0+1;dx<x1-1;dx++)
{
screenColorPut(dx,dy,fg,bg,' ');
}
}
}
//change foreground and background colors attribute
void screenColor(int8_t fg,int8_t bg)
{
printf("\033[%d;%dm",bg+10,fg);
fflush(stdout);
}