-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathcxxmatrix.cpp
2156 lines (1970 loc) · 59.1 KB
/
cxxmatrix.cpp
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cstdio>
#include <cstdint>
#include <cstddef>
#include <csignal>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cmath>
#include <cctype>
#include <vector>
#include <algorithm>
#include <iterator>
#include <unordered_map>
#include <chrono>
#include <thread>
#include <functional>
#include <unistd.h>
#include "cxxmatrix.hpp"
#include "mandel.hpp"
#include "conway.hpp"
namespace cxxmatrix {
// term_*.cpp
void term_init();
bool term_get_size(int& cols, int&rows);
void term_enter();
void term_leave();
std::ptrdiff_t term_read(byte* buffer, std::size_t size);
bool term_winsize_from_env(int& cols, int& rows) {
int int_cols = -1, int_rows = -1;
if (const char* const env_cols = std::getenv("COLUMNS"))
int_cols = std::atoi(env_cols);
if (const char* const env_rows = std::getenv("LINES"))
int_rows = std::atoi(env_rows);
if (int_cols > 0 && int_rows > 0) {
cols = int_cols;
rows = int_rows;
return true;
} else
return false;
}
}
namespace cxxmatrix::config {
constexpr std::chrono::milliseconds default_frame_interval {40};
constexpr int default_decay = 100; // 既定の寿命
}
namespace cxxmatrix {
typedef byte level_t;
typedef std::uint32_t color_t;
static color_t index2color(byte index) {
byte r, g, b;
if (index < 16) {
int const mx = index < 8 ? 0x80 : 0xFF;
r = mx * (1 & index );
g = mx * (1 & index / 2);
b = mx * (1 & index / 4);
} else if (index < 232) {
index -= 16;
r = index / 36 ;
g = index / 6 % 6;
b = index % 6;
if (r) r = r * 40 + 55;
if (g) g = g * 40 + 55;
if (b) b = b * 40 + 55;
} else {
r = g = b = 8 + 10 * (index - 232);
}
return r | g << 8 | b << 16;
}
enum colorspace_t {
colorspace_ansi_8 = 11,
colorspace_aix_16 = 12,
colorspace_xterm_88 = 101,
colorspace_xterm_256 = 102,
colorspace_xterm_rgb = 103,
colorspace_iso8613_6_rgb = 2,
colorspace_iso8613_6_cmy = 3,
colorspace_iso8613_6_cmyk = 4,
colorspace_iso8613_6_index = 5,
};
struct frame_scheduler {
using clock_type = std::chrono::high_resolution_clock;
clock_type::time_point prev;
std::chrono::milliseconds frame_interval;
frame_scheduler() {
frame_interval = config::default_frame_interval;
prev = clock_type::now();
}
void next_frame() {
clock_type::time_point until = prev + frame_interval;
clock_type::time_point now = clock_type::now();
if (until > now)
std::this_thread::sleep_for(until - now);
prev = now;
}
};
struct tcell_t {
char32_t c = U' ';
level_t fg = 0;
level_t bg = 0;
bool bold = false;
double diffuse = 0;
};
enum cell_flags {
cflag_disable_bold = 0x1,
};
struct cell_t {
char32_t c = U' ';
int birth = 0; // 設置時刻
double power = 0; // 初期の明るさ
double decay = config::default_decay; // 寿命
std::uint32_t flags = 0;
double stage = 0; // 現在の消滅段階 (0..1.0)
double current_power = 0; // 現在の明るさ(瞬き処理の前) (0..1.0)
};
struct thread_t {
int x, y;
int age, speed;
double power;
int decay;
};
struct layer_t {
int cols, rows;
int scrollx, scrolly;
std::vector<cell_t> content;
std::vector<thread_t> threads;
private:
int error_rate_modulo = 20;
public:
void set_error_rate(double value) {
error_rate_modulo = value > 0.0 ? std::ceil(20 / value) : 0;
}
public:
void resize(int cols, int rows) {
content.clear();
content.resize(cols * rows);
this->cols = cols;
this->rows = rows;
scrollx = 0;
scrolly = 0;
}
cell_t& cell(int x, int y) {
return content[y * cols + x];
}
cell_t& rcell(int x, int y) {
x = util::mod(x + scrollx, cols);
y = util::mod(y + scrolly, rows);
return cell(x, y);
}
cell_t const& cell(int x, int y) const {
return const_cast<layer_t*>(this)->cell(x, y);
}
cell_t const& rcell(int x, int y) const {
return const_cast<layer_t*>(this)->rcell(x, y);
}
public:
void add_thread(thread_t const& thread) {
threads.emplace_back(thread);
threads.back().x += scrollx;
threads.back().y += scrolly;
}
void step_threads(int now) {
// remove out of range threads
threads.erase(
std::remove_if(threads.begin(), threads.end(),
[this] (auto const& pos) -> bool {
int const y = pos.y - scrolly;
return y < 0 || rows <= y;
}), threads.end());
// grow threads
for (thread_t& pos : threads) {
if (pos.age++ % pos.speed == 0) {
cell_t& cell = this->cell(util::mod(pos.x, cols), util::mod(pos.y, rows));
cell.birth = now;
cell.power = pos.power;
cell.decay = pos.decay;
cell.flags = 0;
cell.c = util::rand_char();
pos.y++;
}
}
}
public:
void resolve_level(int now) {
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
cell_t& cell = this->rcell(x, y);
if (cell.c == ' ') continue;
int const age = now - cell.birth;
cell.stage = 1.0 - age / cell.decay;
if (cell.stage < 0.0) {
cell.c = ' ';
continue;
}
cell.current_power = cell.power * cell.stage;
if (error_rate_modulo && util::rand() % error_rate_modulo == 0)
cell.c = util::rand_char();
}
}
}
};
typedef std::uint32_t key_t;
enum key_flags {
key_up = 0x110000,
key_down = 0x110001,
key_right = 0x110002,
key_left = 0x110003,
};
inline constexpr key_t key_ctrl(key_t k) { return k & 0x1F; }
struct key_reader {
bool term_internal = false;
bool term_nonblock_save = false;
std::function<void(key_t)> proc;
public:
void leave() {
if (!term_internal) return;
term_internal = false;
term_leave();
}
void enter() {
if (term_internal) return;
term_internal = true;
term_enter();
}
private:
void process_key(key_t k) {
if (proc) proc(k);
}
bool esc = false;
void process_byte(byte b) {
if (b == 0x1b) {
esc = true;
return;
}
if (esc) {
if (0x40 <= b && b < 0x80) {
switch (b) {
case 'A': esc = false; process_key(key_up ); break;
case 'B': esc = false; process_key(key_down ); break;
case 'C': esc = false; process_key(key_right); break;
case 'D': esc = false; process_key(key_left ); break;
case '[': break;
case 'O': break;
default: esc = false; break;
}
} else if (0x80 <= b) {
process_key(0x1b);
process_key(b);
esc = false;
}
} else {
process_key(b);
}
}
public:
void process() {
byte buffer[1024];
ssize_t nread;
while ((nread = term_read(buffer, 1024)) > 0) {
for (ssize_t i = 0; i < nread; i++)
process_byte(buffer[i]);
}
}
};
enum scene_t {
scene_none = 0,
scene_number = 1,
scene_banner = 2,
scene_rain = 3,
scene_conway = 4,
scene_mandelbrot = 5,
scene_rain_forever = 6,
scene_exit = 7, // Exit (menu)
scene_loop = 99,
scene_count = 7,
};
struct buffer {
private:
bool setting_diffuse_enabled = true;
bool setting_twinkle_enabled = true;
bool setting_preserve_background = false;
double setting_rain_interval = 150;
bool setting_term_synchronized_update = false;
public:
void set_diffuse_enabled(bool value) {
this->setting_diffuse_enabled = value;
}
void set_twinkle_enabled(bool value) {
this->setting_twinkle_enabled = value;
this->update_twinkle_rendering();
}
void set_preserve_background(bool value) {
this->setting_preserve_background = value;
}
void set_rain_density(double value) {
this->setting_rain_interval = 150 / value;
}
void set_term_synchronized_update(bool value) {
this->setting_term_synchronized_update = value;
}
private:
layer_t layers[3];
public:
void set_error_rate(double value) {
for (auto& layer : layers)
layer.set_error_rate(value);
}
private:
int cols = 80, rows = 25;
std::vector<tcell_t> old_content;
std::vector<tcell_t> new_content;
std::FILE* file;
private:
bool flag_sigint = false;
bool flag_winch = false;
public:
void notify_sigint() { flag_sigint = true; }
void notify_winch() { flag_winch = true; }
void process_signals() {
if (flag_sigint) {
this->finalize();
std::signal(SIGINT, SIG_DFL);
std::raise(SIGINT);
std::exit(128 + SIGINT);
}
if (flag_winch) {
flag_winch = false;
initialize();
redraw();
}
}
private:
frame_scheduler scheduler;
void next_frame() {
process_signals();
scheduler.next_frame();
}
public:
void set_frame_rate(double frame_rate) {
using msec_rep = std::chrono::milliseconds::rep;
constexpr msec_rep max_frame_interval = 1000 * 3600;
double const frame_interval = 1000 / frame_rate;
scheduler.frame_interval = std::chrono::milliseconds((msec_rep) std::clamp(frame_interval, 1.0, (double) max_frame_interval));
}
public:
key_reader kreader;
public:
buffer() {
initialize_color_table(index2color(47), colorspace_xterm_256);
}
private:
void put_utf8(char32_t uc) {
std::uint32_t u = uc;
if (u < 0x80) {
std::putc(u, file);
} else if (u < 0x800) {
std::putc(0xC0 | (u >> 6), file);
std::putc(0x80 | (u & 0x3F), file);
} else if (u < 0x10000) {
std::putc(0xE0 | (u >> 12), file);
std::putc(0x80 | (0x3F & u >> 6), file);
std::putc(0x80 | (0x3F & u), file);
} else if (u < 0x200000) {
std::putc(0xF0 | (u >> 18), file);
std::putc(0x80 | (0x3F & u >> 12), file);
std::putc(0x80 | (0x3F & u >> 6), file);
std::putc(0x80 | (0x3F & u), file);
}
}
level_t fg;
level_t bg;
bool bold;
void sgr0() {
std::fprintf(file, "\x1b[H\x1b[m");
px = py = 0;
fg = -1;
bg = -1;
bold = false;
}
void set_color(tcell_t const& tcell) {
if (tcell.bg != this->bg) {
this->bg = tcell.bg;
if (setting_preserve_background && this->bg == level_background)
std::fprintf(file, "\x1b[49m");
else {
auto const& seq = setbg_table[this->bg];
std::fwrite(seq.data(), seq.size(), 1, file);
}
}
if (tcell.c != ' ') {
if (tcell.fg != fg) {
this->fg = tcell.fg;
auto const& seq = setfg_table[this->fg];
std::fwrite(seq.data(), seq.size(), 1, file);
}
if (tcell.bold != bold) {
this->bold = tcell.bold;
std::fprintf(file, "\x1b[%dm", this->bold ? 1 : 22);
}
}
}
private:
int px = -1, py = -1;
void goto_xy(int x, int y) {
if (y == py) {
if (x != px) {
if (x == 0) {
std::putc('\r', file);
} else if (px - 3 <= x && x < px) {
while (x < px--)
std::putc('\b', file);
} else {
std::fprintf(file, "\x1b[%dG", x + 1);
}
px = x;
}
return;
}
// \v が思うように動いていない?
// if (x <= px && py < y && (x == 0 ? 1 : px - x) + (y - py) <= (x == 0 || x == px ? 3 : 5)) {
// if (x != px) {
// if (x == 0) {
// std::putc('\r', file);
// px = 0;
// } else {
// while (x < px--)
// std::putc('\b', file);
// }
// }
// while (py++ < y)
// std::putc('\v', file);
// return;
// }
if (x == 0) {
std::fprintf(file, "\x1b[%dH", y + 1);
px = x;
py = y;
return;
} else if (x == px) {
if (y < py) {
std::fprintf(file, "\x1b[%dA", py - y);
} else {
std::fprintf(file, "\x1b[%dB", y - py);
}
py = y;
return;
}
std::fprintf(file, "\x1b[%d;%dH", y + 1, x + 1);
px = x;
py = y;
}
private:
static bool is_changed(tcell_t const& ncell, tcell_t const& ocell) {
if (ncell.c != ocell.c || ncell.bg != ocell.bg) return true;
if (ncell.c == ' ') return false;
if (ncell.fg != ocell.fg || ncell.bold != ocell.bold) return true;
return false;
}
bool term_draw_cell(int x, int y, std::size_t index, bool force_write) {
tcell_t& ncell = new_content[index];
tcell_t& ocell = old_content[index];
if (ncell.fg == ncell.bg) ncell.c = ' ';
if (force_write || is_changed(ncell, ocell)) {
goto_xy(x, y);
set_color(ncell);
put_utf8(ncell.c);
px++;
ocell = ncell;
return true;
}
return false;
}
public:
void redraw() {
goto_xy(0, 0);
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
// 行末 xenl 対策
if (y == rows - 1) {
if (x == cols - 2) {
tcell_t const& cell = new_content[y * cols + x + 1];
set_color(cell);
put_utf8(cell.c);
std::fprintf(file, "\b\x1b[@");
} else if (x == cols -1) {
continue;
}
}
tcell_t const& tcell = new_content[y * cols + x];
set_color(tcell);
put_utf8(tcell.c);
}
}
std::fprintf(file, "\x1b[H");
std::fflush(file);
old_content.resize(new_content.size());
for (std::size_t i = 0; i < new_content.size(); i++)
old_content[i] = new_content[i];
}
void draw_content() {
if (setting_term_synchronized_update)
std::fprintf(file, "\x1b[?2026h");
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols - 1; x++) {
std::size_t const index = y * cols + x;
bool dirty = false;
// 行末 xenl 対策
if (x == cols - 2 && term_draw_cell(x, y, index + 1, false)) {
std::fprintf(file, "\b\x1b[@");
px--;
dirty = true;
}
term_draw_cell(x, y, index, dirty);
}
}
if (setting_term_synchronized_update)
std::fprintf(file, "\x1b[?2026l");
std::fflush(file);
process_signals();
}
private:
void clear_diffuse() {
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
std::size_t const index = y * cols + x;
tcell_t& tcell = new_content[index];
tcell.diffuse = 0;
tcell.bg = level_zero;
}
}
}
void add_diffuse(int x, int y, double value) {
if (0 <= y && y < rows && 0 <= x && x < cols && value > 0) {
std::size_t const index = y * cols + x;
tcell_t& tcell = new_content[index];
tcell.diffuse += value;
}
}
void resolve_diffuse() {
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
std::size_t const index = y * cols + x;
tcell_t& tcell = new_content[index];
double const diffuse = std::min(0.04 * tcell.diffuse, 0.3);
tcell.bg = intensity2level(diffuse);
}
}
}
public:
int now = 100;
private:
static constexpr double default_twinkle = 0.2;
double m_twinkle = default_twinkle;
double m_twinkle_rendering = m_twinkle;
void update_twinkle_rendering() {
if (setting_twinkle_enabled)
m_twinkle_rendering = m_twinkle;
else
m_twinkle_rendering = 0.0;
}
void set_twinkle(double value) {
this->m_twinkle = value;
}
private:
colorspace_t m_colorspace = colorspace_xterm_256;
std::vector<std::string> setfg_table;
std::vector<std::string> setbg_table;
const int level_background = 0;
const int level_zero = 0;
std::size_t level_count;
level_t intensity2level(double value) { return (level_t) ((level_count - 1) * value); }
void initialize_palette_rgb(color_t color) {
std::vector<color_t> colors;
{
byte const R = 0xFF & color;
byte const G = 0xFF & color >> 8;
byte const B = 0xFF & color >> 16;
int const mx = std::max({R, G, B});
int const mn = std::min({R, G, B});
// 最大128レベル (0..254)
for (int i = 0; i <= mx; i += 2) {
byte const r = std::round(R * ((double) i / mx));
byte const g = std::round(G * ((double) i / mx));
byte const b = std::round(B * ((double) i / mx));
colors.push_back(r | g << 8 | b << 16);
}
// 最大127レベル {126..0}
int const n = (255 - mn) / 2;
for (int i = n - 1; i >= 0; i--) {
double const frac = (i * 2.0) / (255 - mn);
byte const r = std::round(255 - (255 - R) * frac);
byte const g = std::round(255 - (255 - G) * frac);
byte const b = std::round(255 - (255 - B) * frac);
colors.push_back(r | g << 8 | b << 16);
}
}
level_count = colors.size();
char seq[256];
const char* fmt_sgr;
switch (m_colorspace) {
case colorspace_xterm_rgb:
fmt_sgr = "\x1b[%c8;2;%d;%d;%dm";
goto rgb;
case colorspace_iso8613_6_rgb:
fmt_sgr = "\x1b[%c8:2::%d:%d:%dm";
goto rgb;
rgb:
setfg_table.clear();
setbg_table.clear();
for (color_t color: colors) {
if (color == 0) {
setfg_table.push_back("\x1b[30m");
setbg_table.push_back("\x1b[40m");
} else {
byte const r = 0xFF & color;
byte const g = 0xFF & color >> 8;
byte const b = 0xFF & color >> 16;
std::sprintf(seq, fmt_sgr, '3', r, g, b);
setfg_table.push_back(seq);
std::sprintf(seq, fmt_sgr, '4', r, g, b);
setbg_table.push_back(seq);
}
}
break;
case colorspace_iso8613_6_cmy:
fmt_sgr = "\x1b[%c8:3::%d:%d:%dm";
goto cmyk;
case colorspace_iso8613_6_cmyk:
fmt_sgr = "\x1b[%c8:4::%d:%d:%d:%dm";
goto cmyk;
cmyk:
setfg_table.clear();
setbg_table.clear();
for (color_t color: colors) {
if (color == 0) {
setfg_table.push_back("\x1b[30m");
setbg_table.push_back("\x1b[40m");
} else {
byte r = 0xFF & color;
byte g = 0xFF & color >> 8;
byte b = 0xFF & color >> 16;
byte a = 0xFF;
if (m_colorspace == colorspace_iso8613_6_cmyk && (a = std::max({r, g, b}))) {
r = r * 255 / a;
g = g * 255 / a;
b = b * 255 / a;
}
std::sprintf(seq, fmt_sgr, '3', 0xFF ^ r, 0xFF ^ g, 0xFF ^ b, 0xFF ^ a);
setfg_table.push_back(seq);
std::sprintf(seq, fmt_sgr, '4', 0xFF ^ r, 0xFF ^ g, 0xFF ^ b, 0xFF ^ a);
setbg_table.push_back(seq);
}
}
break;
default:
assert(0);
}
}
static int color2index(double R, double G, double B, int L) {
int const r = std::round(R * (L - 1));
int const g = std::round(G * (L - 1));
int const b = std::round(B * (L - 1));
return 16 + (r * L + g) * L + b;
}
void initialize_palette_index(color_t color) {
std::vector<byte> indices;
{
int offset = 35, modulo = 40, L = 6, ncolor = 256;
if (m_colorspace == colorspace_xterm_88)
offset = 52, modulo = 58, L = 4, ncolor = 88;
double const edge = L - 1;
int const gray0 = 16 + L * L * L;
// 6x6x6 cube / 4x4x4 cube
int const R = (int(0xFF & color ) - offset) / modulo;
int const G = (int(0xFF & color >> 8 ) - offset) / modulo;
int const B = (int(0xFF & color >> 16) - offset) / modulo;
int const mx = std::max({R, G, B});
int const mn = std::min({R, G, B});
if (mx != mn) {
indices.reserve(2 * L - 1 - mn);
for (int i = 0; i < L; i++) {
double const R1 = (R / edge) * (i / edge);
double const G1 = (G / edge) * (i / edge);
double const B1 = (B / edge) * (i / edge);
indices.push_back(color2index(R1, G1, B1, L));
}
for (int i = mn + 1; i < L; i++) {
double const R1 = 1.0 - (1.0 - R / edge) * (edge - i) / (edge - mn);
double const G1 = 1.0 - (1.0 - G / edge) * (edge - i) / (edge - mn);
double const B1 = 1.0 - (1.0 - B / edge) * (edge - i) / (edge - mn);
indices.push_back(color2index(R1, G1, B1, L));
}
} else {
// 24 grayscale / 8 grayscale
indices.reserve(ncolor - gray0 + 2);
indices.push_back(16);
for (int i = gray0; i < ncolor; i++)
indices.push_back(i);
indices.push_back(gray0 - 1);
}
}
level_count = indices.size();
const char* fmt_sgr = "\x1b[%c8;5;%dm";
if (m_colorspace == colorspace_iso8613_6_index)
fmt_sgr = "\x1b[%c8:5:%dm";
char seq[100];
setfg_table.clear();
setbg_table.clear();
for (byte index: indices) {
if (index == 0 || index == 16) {
setfg_table.push_back("\x1b[30m");
setbg_table.push_back("\x1b[40m");
} else {
std::sprintf(seq, fmt_sgr, '3', index);
setfg_table.push_back(seq);
std::sprintf(seq, fmt_sgr, '4', index);
setbg_table.push_back(seq);
}
}
}
void initialize_palette_ansi(color_t color) {
std::vector<byte> indices;
{
byte const R = 0xFF & color;
byte const G = 0xFF & color >> 8;
byte const B = 0xFF & color >> 16;
byte const M = std::max({R, G, B});
int c =
(R > M / 2 ? 1 : 0) +
(G > M / 2 ? 2 : 0) +
(B > M / 2 ? 4 : 0);
if (c == 0) c = 7;
indices.push_back(0);
if (m_colorspace == colorspace_aix_16)
indices.push_back(8);
if (1 <= c && c <= 6) {
indices.push_back(c);
indices.push_back(c);
if (m_colorspace == colorspace_aix_16) {
indices.push_back(8 + c);
indices.push_back(8 + c);
}
indices.push_back(7);
if (m_colorspace == colorspace_aix_16)
indices.push_back(15);
} else {
// monochrome
indices.push_back(7);
indices.push_back(7);
if (m_colorspace == colorspace_aix_16) {
indices.push_back(15);
indices.push_back(15);
}
}
}
char seq[100];
setfg_table.clear();
setbg_table.clear();
for (byte index: indices) {
if (index < 8) {
std::sprintf(seq, "\x1b[3%dm", index);
setfg_table.push_back(seq);
std::sprintf(seq, "\x1b[4%dm", index);
setbg_table.push_back(seq);
} else {
std::sprintf(seq, "\x1b[9%dm", index & 7);
setfg_table.push_back(seq);
std::sprintf(seq, "\x1b[10%dm", index & 7);
setbg_table.push_back(seq);
}
}
level_count = setfg_table.size();
}
public:
void initialize_color_table(color_t color, colorspace_t colorspace) {
this->m_colorspace = colorspace;
switch (m_colorspace) {
case colorspace_iso8613_6_rgb:
case colorspace_iso8613_6_cmy:
case colorspace_iso8613_6_cmyk:
case colorspace_xterm_rgb:
initialize_palette_rgb(color);
break;
case colorspace_iso8613_6_index:
case colorspace_xterm_256:
case colorspace_xterm_88:
default:
initialize_palette_index(color);
break;
case colorspace_ansi_8:
case colorspace_aix_16:
initialize_palette_ansi(color);
break;
}
}
private:
void clear_content() {
for (auto& tcell: new_content) {
tcell.c = ' ';
tcell.fg = level_zero;
tcell.bg = level_zero;
tcell.bold = false;
}
}
cell_t const* rend_cell(int x, int y, double& power) {
cell_t const* ret = nullptr;
for (auto& layer: layers) {
auto const& cell = layer.rcell(x, y);
if (cell.c != ' ') {
if (!ret) ret = &cell;
if (cell.current_power > power) power = cell.current_power;
}
}
return ret;
}
void construct_render_content() {
clear_diffuse();
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
std::size_t const index = y * cols + x;
tcell_t& tcell = new_content[index];
double current_power = 0.0;
cell_t const* lcell = this->rend_cell(x, y, current_power);
if (!lcell) {
tcell.c = ' ';
continue;
}
tcell.c = lcell->c;
// current_power = 現在の輝度 (瞬き)
if (m_twinkle_rendering != 0.0) {
current_power -= std::hypot(current_power * m_twinkle_rendering, 0.1) * util::randf();
if (current_power < 0.0) current_power = 0.0;
}
// level = 色番号
double const fractional_level = util::interpolate(current_power, 0.6, level_count);
int level = fractional_level;
if (m_twinkle_rendering != 0.0 && util::randf() > fractional_level - level) level++;
level = std::min<int>(level, level_count - 1);
tcell.fg = level;
tcell.bold = !(lcell->flags & cflag_disable_bold) && lcell->stage > 0.5;
if (!setting_diffuse_enabled) continue;
double const twinkle_power = (double) level / (level_count - 1);
double const p0 = ((1.0 / 0.3) * (twinkle_power - 0.0));
double const p1 = ((1.0 / 0.3) * (twinkle_power - 0.3));
double const p2 = ((1.0 / 0.5) * (twinkle_power - 0.7));
tcell.diffuse += p0;
add_diffuse(x - 1, y, p1);
add_diffuse(x + 1, y, p1);
add_diffuse(x, y - 1, p1);
add_diffuse(x, y + 1, p1);
add_diffuse(x - 1, y - 1, p2);
add_diffuse(x + 1, y - 1, p2);
add_diffuse(x - 1, y + 1, p2);
add_diffuse(x + 1, y + 1, p2);
}
}
if (setting_diffuse_enabled)
resolve_diffuse();
}
public:
void render_direct() {
now++;
this->draw_content();
}
void render_layers() {
now++;
for (auto& layer: layers) {
layer.step_threads(now);
layer.resolve_level(now);
}
this->construct_render_content();
this->draw_content();
}
bool term_internal = false;
void term_leave() {
if (!term_internal) return;
term_internal = false;
std::fprintf(file, "\x18"); // CAN
std::fprintf(file, "\x1b[m\x1b[%dH\n", rows);
std::fprintf(file, "\x1b[?1049l\x1b[?25h");
std::fflush(file);
kreader.leave();
}
void term_enter() {
if (term_internal) return;
term_internal = true;
kreader.enter();
std::fprintf(file, "\x1b[?1049h\x1b[?25l");
sgr0();
redraw();
std::fflush(file);
}
bool is_menu = false;
void process_key(key_t k) {
switch (k) {
case 'q':
case 'Q':
case key_ctrl('c'):
this->finalize();
std::exit(0);
return;
#ifdef SIGTSTP
case key_ctrl('z'):
traptstp(SIGTSTP);