-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.c
514 lines (422 loc) · 14.6 KB
/
renderer.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#include "renderer.h"
#include "canvas.h"
#include "ccodoc.h"
#include "math.h"
#include "string.h"
#include <assert.h>
#include <math.h>
static void draw_canvas(struct renderer* renderer, struct vec2d point, struct drawing_attr attr, const char* s);
static void drawf_canvas(struct renderer* renderer, struct vec2d point, struct drawing_attr attr, const char* format, ...);
static void render_kakehi(struct renderer* renderer, struct drawing_ctx* ctx, const struct kakehi* kakehi);
static void render_tsutsu(struct renderer* renderer, struct drawing_ctx* ctx, const struct tsutsu* tsutsu);
static void render_hachi(struct renderer* renderer, struct drawing_ctx* ctx, const struct hachi* hachi);
static void render_roji(struct renderer* renderer, struct drawing_ctx* ctx);
void render_ccodoc(struct renderer* const renderer, struct drawing_ctx* const ctx, const struct ccodoc* const ccodoc)
{
render_kakehi(renderer, ctx, &ccodoc->kakehi);
ctx->current = vec2d_add(ctx->current, (struct vec2d) { .x = 3 });
render_tsutsu(renderer, ctx, &ccodoc->tsutsu);
render_hachi(renderer, ctx, &ccodoc->hachi);
render_roji(renderer, ctx);
}
static void render_kakehi(struct renderer* const renderer, struct drawing_ctx* const ctx, const struct kakehi* const kakehi)
{
static const char* const art_ki = u8"━══"; // ki(起)
static const char* const art_sho = u8"═━═"; // sho(承)
static const char* const art_ten = u8"══━"; // ten(転)
static const char* const art_ketsu = u8"═══"; // ketsu(結)
const char* art = NULL;
switch (kakehi->state) {
case holding_water: {
static const float holding_ratio_sho = 1.0f / 3 * 1;
static const float holding_ratio_ten = 1.0f / 3 * 2;
const float ratio = get_action_progress_ratio(&kakehi->holding_water);
if (0 <= ratio && ratio < holding_ratio_sho) {
art = art_ki;
} else if (holding_ratio_sho <= ratio && ratio < holding_ratio_ten) {
art = art_sho;
} else {
art = art_ten;
}
break;
}
case releasing_water:
art = art_ketsu;
break;
}
assert(art != NULL);
{
int i = 0;
for (const char* c = art; *c;) {
const struct char_descriptor desc = decode_char_utf8(c);
const bool has_water = str_equals_n(c, u8"━", desc.len);
drawf_canvas(
renderer,
vec2d_add(ctx->current, (struct vec2d) { .x = i }),
(struct drawing_attr) {
.color = has_water ? color_blue : color_yellow,
},
"%.*s", desc.len, c
);
i++;
c += desc.len;
}
}
wrap_drawing_lines(ctx, 1);
}
static void render_tsutsu(struct renderer* const renderer, struct drawing_ctx* const ctx, const struct tsutsu* const tsutsu)
{
static const size_t art_height = 4;
// jo (序)
static const char* const art_jo[] = {
u8"◥◣",
u8" ◥◣",
u8" ▕ ◥◣",
u8" ▕ ◥◣",
};
// ha (破)
static const char* const art_ha[] = {
u8"",
u8"◢◤◢◤◢◤◢◤",
u8" ▕ ",
u8" ▕ ",
};
// kyu (急)
static const char* const art_kyu[] = {
u8" ◢◤",
u8" ◢◤",
u8" ◢◤",
u8"◢◤▕",
};
const char* const* art = NULL;
switch (tsutsu->state) {
case holding_water: {
const float ratio = get_tsutsu_water_amount_ratio(tsutsu);
if (ratio < 0.8) {
art = art_jo;
} else if (ratio < 1) {
art = art_ha;
} else {
art = art_kyu;
}
break;
}
case releasing_water: {
const float ratio = get_action_progress_ratio(&tsutsu->releasing_water);
if (ratio < 0.55) {
art = art_kyu;
} else if (ratio < 1) {
art = art_ha;
} else {
art = art_jo;
}
break;
}
}
assert(art != NULL);
for (size_t h = 0; h < art_height; h++) {
int i = 0;
const char* c = art[h];
while (*c) {
const struct char_descriptor desc = decode_char_utf8(c);
struct drawing_attr attr = (struct drawing_attr) { .color = color_white };
if (
str_equals_n(c, u8"◥", desc.len)
|| str_equals_n(c, u8"◣", desc.len)
|| str_equals_n(c, u8"◢", desc.len)
|| str_equals_n(c, u8"◤", desc.len)
) {
attr.color = color_green;
}
if (str_equals_n(c, u8"▕", desc.len)) {
attr.color = color_yellow;
}
drawf_canvas(
renderer,
vec2d_add(ctx->current, (struct vec2d) { .y = h, .x = i }),
attr,
"%.*s", desc.len, c
);
i++;
c += desc.len;
}
}
wrap_drawing_lines(ctx, art_height);
}
static void render_hachi(struct renderer* const renderer, struct drawing_ctx* const ctx, const struct hachi* const hachi)
{
static const unsigned int art_width = 4;
static const char* const art_jo = u8"▭▭▭▭";
static const char* const art_ha = u8"▭▬▬▭";
static const char* const art_kyu = u8"▬▭▭▬";
const char* art = NULL;
switch (hachi->state) {
case holding_water:
art = art_jo;
break;
case releasing_water: {
float ratio = get_action_progress_ratio(&hachi->releasing_water);
if (ratio < 0.35) {
art = art_ha;
} else if (ratio < 0.65) {
art = art_kyu;
} else {
art = art_jo;
}
break;
}
}
{
int i = 0;
const char* c = art;
while (*c) {
const struct char_descriptor desc = decode_char_utf8(c);
const bool has_water = str_equals_n(c, u8"▬", desc.len);
drawf_canvas(
renderer,
vec2d_add(ctx->current, (struct vec2d) { .x = i }),
(struct drawing_attr) {
.color = has_water ? color_blue : color_grey,
},
"%.*s", desc.len, c
);
i++;
c += desc.len;
}
}
ctx->current = vec2d_add(ctx->current, (struct vec2d) { .x = art_width });
}
static void render_roji(struct renderer* const renderer, struct drawing_ctx* const ctx)
{
draw_canvas(
renderer,
ctx->current,
(struct drawing_attr) { .color = color_green, .dim = true },
"━━━━━━"
);
ctx->current = vec2d_add(ctx->current, (struct vec2d) { .x = 6 });
draw_canvas(
renderer,
ctx->current,
(struct drawing_attr) { .color = color_grey },
"▨▨▨▨"
);
wrap_drawing_lines(ctx, 1);
}
void render_timer(struct renderer* const renderer, struct drawing_ctx* const ctx, const struct timer* const timer)
{
{
const struct moment moment = moment_from_duration(get_remaining_time(timer), time_min);
#if PLATFORM != PLATFORM_MACOS
const char* const format = "%02dᴴ%02dᴹ";
#else
// Curses available in MacOS fails to handle some multibyte characters including 'ᴴ' and 'ᴹ',
// requiring us to use alternative ones for them.
const char* const format = "%02dh%02dm";
#endif
drawf_canvas(
renderer,
vec2d_add(ctx->current, (struct vec2d) { .x = 4 }),
(struct drawing_attr) { .color = color_white },
format, moment.hours, moment.mins
);
wrap_drawing_lines(ctx, 1);
}
{
static const size_t progress_bar_width = 14;
static const size_t progress_bar_index_timeout_away1 = (size_t)((float)progress_bar_width * 0.2f);
static const size_t progress_bar_index_timeout_away2 = (size_t)((float)progress_bar_width * 0.4f);
struct drawing_attr attr = { 0 };
const float remaining_ratio = 1 - get_elapsed_time_ratio(timer);
const size_t remaining_index = (size_t)((float)progress_bar_width * remaining_ratio);
if (remaining_index <= progress_bar_index_timeout_away1) {
attr.color = color_red;
} else if (remaining_index <= progress_bar_index_timeout_away2) {
attr.color = color_yellow;
} else {
attr.color = color_green;
}
for (size_t i = 0; i < progress_bar_width; i++) {
const float ratio = (float)i / (float)progress_bar_width;
const bool remaining = ratio < remaining_ratio;
attr.dim = !remaining;
const char* art = "─";
if (!renderer->ornamental) {
art = remaining ? "─" : " ";
}
draw_canvas(
renderer,
vec2d_add(ctx->current, (struct vec2d) { .x = i }),
attr,
art
);
}
wrap_drawing_lines(ctx, 1);
}
}
static void render_debug_info_ccodoc(struct renderer* renderer, struct drawing_ctx* ctx, const struct ccodoc* ccodoc);
static void render_debug_info_timer(struct renderer* renderer, struct drawing_ctx* ctx, const struct timer* timer);
static const char* water_flow_state_to_str(enum water_flow_state state);
void render_debug_info(
struct renderer* const renderer,
const struct duration delta,
const struct ccodoc* const ccodoc,
const struct timer* const timer
)
{
struct drawing_ctx ctx = {
.attr = { .color = color_white },
.origin = { .x = 0, .y = 0 },
};
ctx.current = ctx.origin;
{
struct drawing_attr attr = ctx.attr;
attr.bold = true;
draw_canvas(renderer, ctx.current, attr, "DEBUG -------");
wrap_drawing_lines(&ctx, 1);
}
{
wrap_drawing_lines(&ctx, 1);
draw_canvas(renderer, ctx.current, ctx.attr, "# process");
wrap_drawing_lines(&ctx, 1);
drawf_canvas(
renderer,
ctx.current,
ctx.attr,
"fps: %d", delta.msecs != 0 ? (int)round(1000.0 / (double)delta.msecs) : 0
);
wrap_drawing_lines(&ctx, 1);
}
{
draw_canvas(renderer, ctx.current, ctx.attr, "# rendering");
wrap_drawing_lines(&ctx, 1);
drawf_canvas(
renderer,
ctx.current,
ctx.attr,
"ornamental: %s", renderer->ornamental ? "yes" : "no"
);
wrap_drawing_lines(&ctx, 1);
}
wrap_drawing_lines(&ctx, 1);
render_debug_info_ccodoc(renderer, &ctx, ccodoc);
if (timer != NULL) {
wrap_drawing_lines(&ctx, 1);
render_debug_info_timer(renderer, &ctx, timer);
}
}
static void render_debug_info_ccodoc(struct renderer* const renderer, struct drawing_ctx* const ctx, const struct ccodoc* const ccodoc)
{
draw_canvas(renderer, ctx->current, ctx->attr, "# ccodoc");
wrap_drawing_lines(ctx, 1);
{
draw_canvas(renderer, ctx->current, ctx->attr, "## kakehi");
wrap_drawing_lines(ctx, 1);
drawf_canvas(
renderer,
ctx->current,
ctx->attr,
"state: %s", water_flow_state_to_str(ccodoc->kakehi.state)
);
wrap_drawing_lines(ctx, 1);
drawf_canvas(
renderer,
ctx->current,
ctx->attr,
"holding_water_ratio: %f", get_action_progress_ratio(&ccodoc->kakehi.holding_water)
);
wrap_drawing_lines(ctx, 1);
drawf_canvas(
renderer,
ctx->current,
ctx->attr,
"releasing_water_ratio: %f", get_action_progress_ratio(&ccodoc->kakehi.releasing_water)
);
wrap_drawing_lines(ctx, 1);
}
{
draw_canvas(renderer, ctx->current, ctx->attr, "## tsutsu");
wrap_drawing_lines(ctx, 1);
drawf_canvas(
renderer,
ctx->current,
ctx->attr,
"state: %s", water_flow_state_to_str(ccodoc->tsutsu.state)
);
wrap_drawing_lines(ctx, 1);
drawf_canvas(
renderer,
ctx->current,
ctx->attr,
"water_amount_ratio: %f", get_tsutsu_water_amount_ratio(&ccodoc->tsutsu)
);
wrap_drawing_lines(ctx, 1);
drawf_canvas(
renderer,
ctx->current,
ctx->attr,
"releasing_water_ratio: %f", get_action_progress_ratio(&ccodoc->tsutsu.releasing_water)
);
wrap_drawing_lines(ctx, 1);
}
{
draw_canvas(renderer, ctx->current, ctx->attr, "## hachi");
wrap_drawing_lines(ctx, 1);
drawf_canvas(
renderer,
ctx->current,
ctx->attr,
"state: %s", water_flow_state_to_str(ccodoc->hachi.state)
);
wrap_drawing_lines(ctx, 1);
drawf_canvas(
renderer,
ctx->current,
ctx->attr,
"releasing_water_ratio: %f", get_action_progress_ratio(&ccodoc->hachi.releasing_water)
);
wrap_drawing_lines(ctx, 1);
}
}
static void render_debug_info_timer(struct renderer* const renderer, struct drawing_ctx* const ctx, const struct timer* const timer)
{
draw_canvas(renderer, ctx->current, ctx->attr, "# timer");
wrap_drawing_lines(ctx, 1);
const struct moment m = moment_from_duration(get_remaining_time(timer), time_msec);
drawf_canvas(
renderer,
ctx->current,
ctx->attr,
"remaining: %02d:%02d:%02d:%02d", m.hours, m.mins, m.secs, m.msecs
);
wrap_drawing_lines(ctx, 1);
drawf_canvas(
renderer,
ctx->current,
ctx->attr,
"elapsed time ratio: %f", get_elapsed_time_ratio(timer)
);
wrap_drawing_lines(ctx, 1);
}
static const char* water_flow_state_to_str(enum water_flow_state state)
{
switch (state) {
case holding_water:
return "holding";
case releasing_water:
return "releasing";
}
}
static void draw_canvas(struct renderer* const renderer, const struct vec2d point, const struct drawing_attr attr, const char* const s)
{
const struct drawing_attr attr2 = renderer->ornamental ? attr : (struct drawing_attr) { 0 };
draw(renderer->canvas, point, attr2, s);
}
static void drawf_canvas(struct renderer* const renderer, const struct vec2d point, const struct drawing_attr attr, const char* const format, ...)
{
const struct drawing_attr attr2 = renderer->ornamental ? attr : (struct drawing_attr) { 0 };
va_list args = { 0 };
va_start(args, format);
drawfv(renderer->canvas, point, attr2, format, args);
va_end(args);
}