-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.c
337 lines (298 loc) · 9.67 KB
/
display.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "utils.h"
#include "config.h"
static int update_title_win(Title_Win *title_win);
static int draw_title_win(Title_Win *title_win);
static int update_text_win(Text_Win *text_win, Game_Status *status);
static int draw_text_win(Text_Win *text_win);
static int update_prompt_win(Prompt_Win *promt_win);
static int draw_prompt_win(Prompt_Win *promt_win);
static int update_user_win(User_Win *user_win, Game_Status *status);
static int draw_user_win(User_Win *user_win);
static int update_stat_win(Stat_Win *stat_win, Game_Status *status);
static int draw_stat_win(Stat_Win *stat_win);
static int update_cursor_pos(Text_Win *text_win);
#ifdef DEBUG
static int update_debug_win(Debug_Win *debug_win);
static int draw_debug_win(Debug_Win *debug_win);
#endif
inline static size_t min(size_t a, size_t b)
{
return a < b ? a : b;
}
int init_display_updates()
{
int ret_val = 0;
//common initializations
initscr();
clear();
noecho();
cbreak();
curs_set(2);
return ret_val;
}
int
get_user_input(Game_Win *game_window, Game_Status *status)
{
int ret_val = 0;
status->input = wgetch(game_window->user->window);
return ret_val;
}
int
display_status_connect(Game_Win *game_window, Game_Status *status, Stats *measures)
{
int ret_val = 0;
game_window->text->content = status->text;
game_window->text->frame_start_ptr = status->text;
game_window->stats->statistics = measures;
game_window->user->buffer = status->buffer->buffer;
#ifdef DEBUG
game_window->debug->status = status;
game_window->debug->statistics = measures;
game_window->debug->user_buff = status->buffer;
#endif
return ret_val;
}
int
update_game_display(Game_Win *game_window, Game_Status *status)
{
int ret_val = 0;
ret_val |= update_title_win(game_window->title);
ret_val |= update_text_win(game_window->text, status);
ret_val |= update_prompt_win(game_window->prompt);
ret_val |= update_user_win(game_window->user, status);
ret_val |= update_stat_win(game_window->stats, status);
#ifdef DEBUG
ret_val |= update_debug_win(game_window->debug);
#endif
ret_val |= update_cursor_pos(game_window->text);
return ret_val;
}
static int
update_title_win(Title_Win *title_win)
{
int ret_val = 0;
ret_val |= draw_title_win(title_win);
return ret_val;
}
static int
draw_title_win(Title_Win *title_win)
{
int ret_val = 0;
werase(title_win->window);
wattr_on(title_win->window, A_BOLD|A_ITALIC|A_UNDERLINE, NULL);
mvwprintw(title_win->window, TITLE_Y, TITLE_X, "%s", TITLE);
wattr_off(title_win->window, A_BOLD|A_ITALIC|A_UNDERLINE, NULL);
wrefresh(title_win->window);
return ret_val;
}
static int
update_text_win(Text_Win *text_win, Game_Status *status)
{
int ret_val = 0;
text_win->start_x = TEXT_WIN_START_X;
text_win->start_y = TEXT_WIN_START_Y;
text_win->frame_size = (TEXT_WIN_HEIGHT) * (TEXT_WIN_WIDTH);
int index_in_frame = (status->text + status->index - text_win->frame_start_ptr)\
% text_win->frame_size;
if ((index_in_frame / TEXT_WIN_WIDTH) == (TEXT_WIN_HEIGHT - 2))
{
text_win->frame_start_ptr += (TEXT_WIN_WIDTH * (TEXT_WIN_HEIGHT - 3));
}
text_win->correct = status->text + status->index - text_win->frame_start_ptr;
size_t correct_lines = text_win->correct / TEXT_WIN_WIDTH;
size_t correct_tail = text_win->correct % TEXT_WIN_WIDTH;
text_win->incorrect = min(status->err_char,
text_win->frame_size - text_win->correct);
text_win->incorrect = min(text_win->incorrect,
status->text_len - status->index);
size_t incorrect_lines = 0;
size_t incorrect_tail = 0;
size_t incorrect_head = 0;
if (text_win->correct_tail)
incorrect_head = min(TEXT_WIN_WIDTH - correct_tail,
text_win->incorrect);
if (incorrect_head < text_win->incorrect)
{
incorrect_lines = (text_win->incorrect - incorrect_head) / \
TEXT_WIN_WIDTH;
incorrect_tail = (text_win->incorrect - incorrect_head) % \
TEXT_WIN_WIDTH;
}
text_win->correct_lines = correct_lines;
text_win->correct_tail = correct_tail;
text_win->incorrect_head = incorrect_head;
text_win->incorrect_lines = incorrect_lines;
text_win->incorrect_tail = incorrect_tail;
ret_val |= draw_text_win(text_win);
return ret_val;
}
static int
draw_text_win(Text_Win *text_win)
{
int ret_val = 0;
start_color();
init_pair(1, COLOR_RED, COLOR_WHITE);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
werase(text_win->window);
box(text_win->window, 0, 0);
size_t i = 0;
char curr_char;
size_t start_x = text_win->start_x, start_y = text_win->start_y;
wmove(text_win->window, start_x, start_y);
int x = (int)start_x, y = (int)start_y;
while ((curr_char = text_win->frame_start_ptr[i++]) &&
i<text_win->frame_size)
{
mvwaddch(text_win->window, y, x++, curr_char);
if (x==(TEXT_WIN_WIDTH+(int)start_x)) // +1 because start_x starts at 1
{
x=start_x;
y++;
}
}
//green
for (size_t row_num=0; row_num<text_win->correct_lines; row_num++)
{
mvwchgat(text_win->window, row_num+start_y, start_x,
TEXT_WIN_WIDTH, A_COLOR, 2, NULL);
}
mvwchgat(text_win->window, text_win->correct_lines+start_y, start_x,
text_win->correct_tail, A_COLOR, 2, NULL);
//red
mvwchgat(text_win->window, text_win->correct_lines+start_y,
text_win->correct_tail+start_x,
text_win->incorrect_head, A_REVERSE, 1, NULL);
for (size_t row_num=0; row_num<text_win->incorrect_lines; row_num++)
{
mvwchgat(text_win->window,
text_win->correct_lines+!!(text_win->incorrect_head)+row_num+start_y,
start_x, TEXT_WIN_WIDTH,
A_REVERSE, 1, NULL);
}
mvwchgat(text_win->window,
text_win->correct_lines + \
!!(text_win->incorrect_head)+text_win->incorrect_lines+start_y,
start_x, text_win->incorrect_tail, A_REVERSE, 1, NULL);
wrefresh(text_win->window);
return ret_val;
}
static int
update_cursor_pos(Text_Win *text_win)
{
size_t col, row;
if (text_win->incorrect)
{
if (text_win->incorrect_tail) col = text_win->incorrect_tail;
else col = text_win->correct_tail + text_win->incorrect_head;
}
else col = text_win->correct_tail;
row = text_win->correct_lines + \
!!(text_win->correct_tail + text_win->incorrect_head) +\
text_win->incorrect_lines + \
!!(text_win->incorrect_tail) - 1;
if (!(text_win->correct_tail + text_win->incorrect_head +\
text_win->incorrect_tail))
row++;
col += text_win->start_x;
row += text_win->start_y;
wmove(text_win->window, row, col);
wrefresh(text_win->window);
return 0;
}
static int
update_prompt_win(Prompt_Win *prompt_win)
{
int ret_val = 0;
ret_val |= draw_prompt_win(prompt_win);
return ret_val;
}
static int
draw_prompt_win(Prompt_Win *prompt_win)
{
int ret_val = 0;
werase(prompt_win->window);
mvwprintw(prompt_win->window, 0, 0, PROMPT);
wrefresh(prompt_win->window);
return ret_val;
}
static int
update_user_win(User_Win *user_win, Game_Status *status)
{
int ret_val = 0;
user_win->buffer = status->buffer->buffer;
ret_val |= draw_user_win(user_win);
return ret_val;
}
static int
draw_user_win(User_Win *user_win)
{
int ret_val = 0;
werase(user_win->window);
wmove(user_win->window, 0, 0);
if (!*(user_win->buffer)) mvwprintw(user_win->window, 0, 0, " ");
else mvwprintw(user_win->window, 0, 0, "%s", user_win->buffer);
wrefresh(user_win->window);
return ret_val;
}
static int
update_stat_win(Stat_Win *stat_win, Game_Status *status)
{
int ret_val = 0;
if (status->status != -1)
{
stat_win->hide = 1;
return ret_val;
}
// status = -1 means game over
stat_win->hide = 0;
ret_val |= draw_stat_win(stat_win);
return ret_val;
}
static int
draw_stat_win(Stat_Win *stat_win)
{
int ret_val = 0;
if (stat_win->hide) return ret_val;
Stats *statistics = stat_win->statistics;
wattron(stat_win->window, A_BOLD);
wprintw(stat_win->window, "wpm: %.2lf\n", statistics->wpm);
wprintw(stat_win->window, "cps: %.2lf\n", statistics->cps);
wprintw(stat_win->window, "wpm (raw): %.2lf\n", statistics->wpm_raw);
wprintw(stat_win->window, "cps (raw): %.2lf\n", statistics->cps_raw);
wprintw(stat_win->window,
"accuracy: %.2lf%%\n", (1 - statistics->err) * 100);
wattroff(stat_win->window, A_BOLD);
wrefresh(stat_win->window);
return ret_val;
}
#ifdef DEBUG
static int
update_debug_win(Debug_Win *debug_win)
{
int ret_val = 0;
ret_val |= draw_debug_win(debug_win);
return ret_val;
}
static int
draw_debug_win(Debug_Win *debug_win)
{
int ret_val = 0;
Game_Status *status = debug_win->status;
Stats *statistics = debug_win->statistics;
Text_Buff *buff = debug_win->user_buff;
werase(debug_win->window);
wprintw(debug_win->window,
"index: %zu, start: %zu, input: %c (%d), correct: %c (%d), err_char: %zu",
status->index, status->start,
(char)status->input, status->input, *(status->text_ptr),
(int)(*(status->text_ptr)), status->err_char);
char curr_char = buff->top ? buff->buffer[buff->top-1]: '_';
wprintw(debug_win->window, "\ntop: %zu (%c), capacity: %zu",
buff->top, curr_char, buff->capacity);
wprintw(debug_win->window, "\nch = %d, raw_ch = %d, elapsed = %lf",
statistics->ch,
statistics->raw_ch, statistics->end_t - statistics->start_t);
wrefresh(debug_win->window);
return ret_val;
}
#endif