-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.h
164 lines (139 loc) · 3.34 KB
/
screen.h
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* screen.h
* Created on Aug, 23th 2023
* Author: Tiago Barros
* Based on "From C to C++ course - 2002"
* Reference: https://en.wikipedia.org/wiki/ANSI_escape_code
*/
#ifndef __SCREEN_H__
#define __SCREEN_H__
#include <stdio.h>
// Terminal control sequences
#define ESC "\033"
#define NORMALTEXT "[0m"
#define BOLDTEXT "[1m"
#define ITALICTEXT "[3m"
#define BLINKTEXT "[5m"
#define REVERSETEXT "[7m"
#define HOMECURSOR "[f"
#define SHOWCURSOR "[?25h"
#define HIDECURSOR "[?25l"
#define CLEARSCREEN "[2J"
// BOX Drawing - Unix like terminals
#define BOX_ENABLE "(0"
#define BOX_DISABLE "(B"
#define BOX_VLINE 0x78
#define BOX_HLINE 0x71
#define BOX_UPLEFT 0x6C
#define BOX_UPRIGHT 0x6B
#define BOX_DWNLEFT 0x6D
#define BOX_DWNRIGHT 0x6A
#define BOX_CROSS 0x6E
#define BOX_TLEFT 0X74
#define BOX_TRIGHT 0X75
#define BOX_TUP 0X77
#define BOX_TDOWN 0X76
#define BOX_DIAMOND 0x60
#define BOX_BLOCK 0x61
#define BOX_DOT 0x7E
// screen constants
#define SCRSTARTX 3 // Initial and final screen positions for the game
#define SCRENDX 75 // It means the area that can be drawn
#define SCRSTARTY 1
#define SCRENDY 23
#define MINX 1 // min screen horizontal pos
#define MINY 1 // min screen vertical pos
#define MAXX 80 // max screen horizontal pos
#define MAXY 48 // max screen vertical pos
/**
* Screen Colors type
*/
typedef enum {
BLACK,
RED,
GREEN,
BROWN,
BLUE,
MAGENTA,
CYAN,
LIGHTGRAY,
DARKGRAY,
LIGHTRED,
LIGHTGREEN,
YELLOW,
LIGHTBLUE,
LIGHTMAGENTA,
LIGHTCYAN,
WHITE
} screenColor;
/**
* Move the cursor to position (0,0)
*/
static inline void screenHomeCursor() { printf("%s%s", ESC, HOMECURSOR); }
/**
* Show the cursor
*/
static inline void screenShowCursor() { printf("%s%s", ESC, SHOWCURSOR); }
/**
* Hide the cursor
*/
static inline void screenHideCursor() { printf("%s%s", ESC, HIDECURSOR); }
/**
* Clear the screen
*/
static inline void screenClear() {
screenHomeCursor();
printf("%s%s", ESC, CLEARSCREEN);
}
/**
* Update screen imediatelly
*/
static inline void screenUpdate() { fflush(stdout); }
/**
* Set screen mode to "normal"
*/
static inline void screenSetNormal() { printf("%s%s", ESC, NORMALTEXT); }
/**
* Set screen mode to "bold"
*/
static inline void screenSetBold() { printf("%s%s", ESC, BOLDTEXT); }
/**
* Set screen mode to "blink"
*/
static inline void screenSetBlink() { printf("%s%s", ESC, BLINKTEXT); }
/**
* Set screen mode to "reverse"
*/
static inline void screenSetReverse() { printf("%s%s", ESC, REVERSETEXT); }
/**
* Enable BOX characters in terminal
*/
static inline void screenBoxEnable() { printf("%s%s", ESC, BOX_ENABLE); }
/**
* Disable BOX characters in terminal
*/
static inline void screenBoxDisable() { printf("%s%s", ESC, BOX_DISABLE); }
/**
* Clear the screen, set cursor to home position
* and optionally draw borders on it.
*
* @param drawBorders if not zero, draw borders on screen.
*/
void screenInit(int drawBorders);
/**
* Clear the screen and restores to initial state.
*/
void screenDestroy();
/**
* Move cursor to position (x,y)
* @param x x position
* @param y y position
*/
void screenGotoxy(int x, int y);
/**
* Define text colors
* @param fg foreground color, can assume values from BLACK to WHITE
* @param bg background color, can assume values from BLACK to LIGHTGRAY
*/
void screenSetColor(screenColor fg, screenColor bg);
#endif /* __SCREEN_H__ */