/
hud.c
295 lines (265 loc) · 7.76 KB
/
hud.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <ncurses.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "hud.h"
#include "tools.h"
#include "mPrint.h"
// hud_color
// terminal_height
// terminal_width
//void draw_hud_background() {
void draw_hud_background(
int color,
int h,
int w) {
if (h < 3) {
mPrint("Error: terminal height is too small to draw the hud\n");
exit(EXIT_FAILURE);
}
else if (w < 1) {
mPrint("Error: terminal width is too small to draw the hud\n");
exit(EXIT_FAILURE);
}
int starting_row = h - HUD_NUM_ROWS;
attron(COLOR_PAIR(color));
for (int j = starting_row; j < h; j++) {
for (int i = 0; i < w; i++) {
mvaddstr(j, i, " ");
}
}
attroff(COLOR_PAIR(color));
}
void draw_hud_row_1(
canvas_pixel_t **canvas,
int fgcolor,
int y,
int x,
int cy,
int hud_color,
int term_h,
int term_w,
int canvas_h,
int canvas_w,
int current_color_pair,
int mode_value,
wchar_t block_char
) {
if (canvas == NULL) {
fprintf(stderr, "canvas is null");
exit(EXIT_FAILURE);
}
else if (term_h < 1) {
fprintf(stderr, "term_h is less than 1");
exit(EXIT_FAILURE);
}
else if (term_w < 1) {
fprintf(stderr, "term_w is less than 1");
exit(EXIT_FAILURE);
}
else if (fgcolor < 0) {
fprintf(stderr, "fgcolor is less than 0");
exit(EXIT_FAILURE);
}
else if (canvas_h < 1) {
fprintf(stderr, "canvas_h < 1\n");
exit(EXIT_FAILURE);
}
else if (canvas_w < 1) {
fprintf(stderr, "canvas_w < 1\n");
exit(EXIT_FAILURE);
}
attron(COLOR_PAIR(hud_color));
int hud_y = term_h-HUD_NUM_ROWS;
int hud_x = 0;
//int hud_terminal_width = w;
int fg_color_cursor = canvas[y][x].foreground_color;
// perhaps the string could be longer than the hud_terminal_width,
// since we are going to stop printing there anyway
// estimating length of the hud status row
// filename could be NULL
// so this would be at minimum (24 + 1)*2 length
// it would make more sense to make it the terminal width
//int len_of_str = (24 + strlen(filename) + 1)*2;
//char *str = calloc(1, len_of_str);
//char *str = calloc(1, terminal_width);
char *str = calloc(1, term_w);
if (str == NULL) {
fprintf(stderr, "Error: calloc failed\n");
exit(EXIT_FAILURE);
}
// before we do this sprintf, we need to make sure the terminal is wide enough
// to display the entire string. if it's not, we need to truncate the string
// and display a warning message
//sprintf(
snprintf(
str,
term_w,
"y:%03d|%03d|#%02d(%02x)F%02d Canvas:%dx%d [%c] %s",
y,
cy,
fgcolor,
current_color_pair,
fg_color_cursor,
canvas_h,
canvas_w,
canvas[y][x].character, // this is buggy, block chars display funky
(
mode_value == 0 ? "NORMAL" :
mode_value == 1 ? "TEXT" :
mode_value == 2 ? "LINE" :
mode_value == 3 ? "RECT" :
"ERROR"
)
);
// truncate the string even if the string isnt longer than term_w
// its just faster this way
str[term_w-1] = '\0';
move(hud_y,hud_x);
for (char c = str[0]; c != '\0'; c = str[hud_x]) {
if (hud_x > term_w) {
break;
}
if (c=='#') {
switch_between_hud_and_current_color(hud_color, current_color_pair);
//addstr("█");
char *block_str = convert_wchar_block_to_str(block_char);
if (block_str) {
addstr(block_str);
}
switch_between_current_and_hud_color(hud_color, current_color_pair);
}
else {
addch(c);
}
hud_x++;
}
free(str);
attroff(COLOR_PAIR(hud_color));
}
void draw_hud_row_2(
canvas_pixel_t **canvas,
int **color_array,
int color_array_len,
int **color_pair_array,
int color_pair_array_len,
int terminal_height,
int terminal_width,
int hud_color,
int current_color_pair,
int y,
int x,
int cx,
int last_char_pressed) {
if (canvas == NULL) {
mPrint("Error: canvas is NULL\n");
exit(EXIT_FAILURE);
}
if (terminal_height < 0) {
mPrint("Error: terminal_height is negative\n");
exit(EXIT_FAILURE);
}
if (terminal_width < 0) {
mPrint("Error: terminal_width is negative\n");
exit(EXIT_FAILURE);
}
if (hud_color < 0) {
mPrint("Error: hud_color is negative\n");
exit(EXIT_FAILURE);
}
if (current_color_pair < 0) {
mPrint("Error: current_color_pair is negative\n");
exit(EXIT_FAILURE);
}
if (color_array == NULL) {
mPrint("Error: color_array is NULL\n");
exit(EXIT_FAILURE);
}
if (color_array_len <= 0) {
mPrint("Error: color_array_len is less than or equal to 0\n");
exit(EXIT_FAILURE);
}
if (color_pair_array == NULL) {
mPrint("Error: color_pair_array is NULL\n");
exit(EXIT_FAILURE);
}
if (color_pair_array_len <= 0) {
mPrint("Error: color_pair_array_len is less than or equal to 0\n");
exit(EXIT_FAILURE);
}
attron(COLOR_PAIR(hud_color));
int hud_y = terminal_height-HUD_NUM_ROWS+1;
int hud_x = 0;
//const int len_of_str = 40;
move(hud_y,hud_x);
char *str = calloc(1, terminal_width);
if (str == NULL) {
mPrint("Error allocating memory for str\n");
exit(EXIT_FAILURE);
}
int bg_color = get_bg_color(color_array, color_array_len, current_color_pair);
int fg_color_cursor = canvas[y][x].foreground_color;
int bg_color_cursor = canvas[y][x].background_color;
int color_pair_num = color_pair_array[fg_color_cursor][bg_color_cursor];
// copy into str up to terminal_width characters
snprintf(str, terminal_width,
"x:%03d|%03d|#%02d(%02x)B%02d Term: %dx%d Last Char: (0x%04X)",
x,
cx,
bg_color,
color_pair_num,
bg_color_cursor,
terminal_height,
terminal_width,
last_char_pressed
);
for (char c = str[0]; c != '\0'; c = str[hud_x]) {
if (hud_x > terminal_width) {
break;
}
else if (c=='#') {
switch_between_hud_and_current_color(hud_color, current_color_pair);
addstr(" ");
switch_between_current_and_hud_color(hud_color, current_color_pair);
}
else {
addch(c);
}
hud_x++;
}
free(str);
attroff(COLOR_PAIR(hud_color));
}
void draw_hud_row_3(
int terminal_height,
int terminal_width,
int hud_color,
double last_cmd_ms
) {
if (terminal_height < 0) {
mPrint("Error: terminal_height is negative\n");
exit(EXIT_FAILURE);
}
if (terminal_width < 0) {
mPrint("Error: terminal_width is negative\n");
exit(EXIT_FAILURE);
}
int starting_row = terminal_height - HUD_NUM_ROWS + 2;
char *str = calloc(1, terminal_width);
if (str == NULL) {
mPrint("Error: calloc failed\n");
exit(EXIT_FAILURE);
}
snprintf(str, terminal_width, "Press q to quit | %.06fms", last_cmd_ms);
attron(COLOR_PAIR(hud_color));
mvaddstr(starting_row, 0, str);
attroff(COLOR_PAIR(hud_color));
}
void switch_between_hud_and_current_color(int hud_color, int current_color_pair) {
attroff(COLOR_PAIR(hud_color));
attron(COLOR_PAIR(current_color_pair));
}
void switch_between_current_and_hud_color(int hud_color, int current_color_pair) {
attroff(COLOR_PAIR(current_color_pair));
attron(COLOR_PAIR(hud_color));
}