/
main.c
1300 lines (1161 loc) · 35.4 KB
/
main.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
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
#define _XOPEN_SOURCE_EXTENDED 1
#include <ncurses.h>
#include <ctype.h>
#include <locale.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include <getopt.h>
#include <time.h>
#include <assert.h>
#include "mPrint.h"
#include "canvas.h"
#include "tools.h"
#include "hud.h"
#include "colors.h"
#include "mPrint.h"
#define DEFAULT_COLOR_PAIR 1
extern int g_color_palette[100][100];
// we can technically handle 99 colors and have defined them
// BUT we can only have 256 color pairs (16x16) active at a time
int MAX_FG_COLORS = 16;
int MAX_BG_COLORS = 16;
int MAX_COLOR_PAIRS = 256;
struct timespec ts0;
struct timespec ts1;
bool can_change_color_mode = true;
bool is_line_draw_mode = false;
bool is_rect_draw_mode = false;
bool is_text_mode = false;
bool is_cam_mode = false;
bool was_loaded_from_file = false;
bool ncurses_initialized = false;
bool color_array_initialized = false;
bool color_pair_array_initialized = false;
double last_cmd_ms = -1;
long last_cmd_ns = -1;
long last_cmd_us = -1;
int last_char_pressed = -1;
int canvas_width = -1;
int canvas_height = -1;
int terminal_height = -1;
int terminal_width = -1;
int y = 0;
int x = 0;
int cy = 0;
int cx = 0;
int quit = 0;
int hud_color = 7;
int current_color_pair = 0;
int max_color_pairs = -1;
int max_colors = -1;
int line_draw_y0 = 0;
int line_draw_x0 = 0;
int line_draw_y1 = 0;
int line_draw_x1 = 0;
char filename[1024] = {0};
canvas_pixel_t **canvas = NULL;
wchar_t gblock = FULL_BLOCK;
int **color_array = NULL;
int **color_pair_array = NULL;
int get_current_fg_color();
int get_current_bg_color();
int get_new_width_from_user();
int get_new_height_from_user();
void add_block();
void add_character(wchar_t c);
void add_character_and_move_right(wchar_t c);
void cleanup();
void decr_cam_y();
void decr_cam_x();
void decr_color_pair();
void decr_color_pair_by_max();
void define_color_pairs();
void define_color_pairs_for_ascii();
void delete_block();
void draw_canvas();
void draw_hud();
void draw_initial_ascii();
void draw_line(int y1, int x1, int y2, int x2, int fg, int bg);
void draw_rect(int y1, int x1, int y2, int x2, int fg, int bg);
void exit_with_error(char *error_msg);
void fail_with_msg(const char *msg);
void free_color_arrays();
void free_color_pair_array();
void get_filename_from_user();
void get_int_str_from_user(char *prompt);
void handle_cam_mode_arrow_keys(int c);
void handle_canvas_load();
void handle_canvas_resize_width();
void handle_canvas_resize_height();
void handle_color_pair_change(int c);
void handle_escape_key_normal();
void handle_line_draw();
void handle_rect_draw();
void handle_normal_mode_arrow_keys(int c);
void handle_input();
void handle_text_mode_input(int c);
void handle_normal_mode_input(int c);
void handle_move_right();
void handle_move_left();
void handle_move_up();
void handle_move_down();
void handle_save_inner_loop(FILE *outfile);
void handle_save();
void handle_quit();
void incr_cam_y();
void incr_cam_x();
void incr_color_pair();
void incr_color_pair_by_max();
void init_color_arrays();
void init_program();
void invert_current_color_pair();
void paintbucket(int y, int x, wchar_t old_char, wchar_t new_char, int old_fg, int old_bg, int new_fg, int new_bg);
void parse_arguments(int argc, char **argv);
void print_help(char **argv);
void render_temp_line(int y1, int x1, int y2, int x2);
void render_temp_rect(int y1, int x1, int y2, int x2);
void reset_cursor();
void reset_line_draw_vars();
void rotate_gblock_forward();
void rotate_gblock_backward();
void show_error(char *error_msg);
void show_help();
void write_char_to_canvas(int y, int x, wchar_t c, int fg_color, int bg_color);
int main(int argc, char *argv[]) {
parse_arguments(argc, argv);
init_program();
refresh();
while (!quit) {
clear();
draw_canvas();
draw_hud();
handle_input();
refresh();
}
cleanup();
return EXIT_SUCCESS;
}
void reset_cursor() { move(y, x); }
void print_help(char **argv) {
printf("Usage: %s [OPTION]...\n", argv[0]);
printf(" -f, --filename=FILENAME specify a filename to save to\n"
" -w, --width=WIDTH specify the width of the canvas\n"
" -h, --height=HEIGHT specify the height of the canvas\n\n");
}
void parse_arguments(int argc, char **argv) {
int c = -1;
int option_index = 0;
static struct option longoptions[] = {
{"filename", 1, NULL, 'f'},
{"width", 1, NULL, 'w'},
{"height", 1, NULL, 'h'},
{0, 0, 0, 0}
};
while (1) {
c = getopt_long(argc, argv, "f:w:h:", longoptions, &option_index);
if (c == -1) { break; }
switch (c) {
case 'f':
strncpy(filename, optarg, 1024);
break;
case 'w':
canvas_width = atoi(optarg);
break;
case 'h':
canvas_height = atoi(optarg);
break;
case '?':
// handle input file info
if (optopt == 'f') {
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
}
else if (optopt == 'w') {
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
}
else if (optopt == 'h') {
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
}
else if (isprint(optopt)) {
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
}
else {
fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
}
print_help(argv);
exit(EXIT_FAILURE);
break;
default:
abort();
}
}
}
void init_color_arrays() {
//mPrint("Initializing color arrays\n");
color_array = calloc(MAX_COLOR_PAIRS, sizeof(int *));
if (color_array == NULL) {
mPrint("Failed to allocate memory for color_array\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < MAX_COLOR_PAIRS; i++) {
color_array[i] = calloc(2, sizeof(int));
if (color_array[i] == NULL) {
mPrint("Failed to allocate memory for color_array\n");
exit(EXIT_FAILURE);
}
}
color_pair_array = calloc(MAX_FG_COLORS, sizeof(int *));
if (color_pair_array == NULL) {
mPrint("Failed to allocate memory for color_pair_array\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < MAX_FG_COLORS; i++) {
color_pair_array[i] = calloc(MAX_BG_COLORS, sizeof(int));
if (color_pair_array[i] == NULL) {
mPrint("Failed to allocate memory for color_pair_array\n");
exit(EXIT_FAILURE);
}
}
}
void invert_current_color_pair() {
int fg = -1;
int bg = -1;
int index = -1;
int inverse_index = -1;
if (current_color_pair > MAX_COLOR_PAIRS) {
exit_with_error("invert_current_color_pair: current_color_pair > MAX_COLOR_PAIRS");
}
fg = color_array[current_color_pair][0];
bg = color_array[current_color_pair][1];
index = color_pair_array[fg][bg];
if (index != current_color_pair) {
char msg[1024] = {0};
snprintf(msg, 1024, "invert_current_color_pair: index != current_color_pair: %d != %d", index, current_color_pair);
exit_with_error(msg);
}
inverse_index = color_pair_array[bg][fg];
current_color_pair = inverse_index;
}
// depending on if the ascii was loaded from file or not
// we may wish to define custom color pairs
void define_color_pairs() {
mPrint("defining color pairs\n");
int current_pair = 0;
int bg_color_start = 0;
int fg_color_start = 0;
for (int bg_color = bg_color_start; bg_color < MAX_BG_COLORS; bg_color++) {
for (int fg_color = fg_color_start; fg_color < MAX_FG_COLORS; fg_color++) {
init_pair(current_pair, fg_color, bg_color);
color_array[current_pair][0] = fg_color;
color_array[current_pair][1] = bg_color;
color_pair_array[fg_color][bg_color] = current_pair;
current_pair++;
}
}
max_color_pairs = current_pair;
max_colors = MAX_FG_COLORS;
assert(max_color_pairs == MAX_COLOR_PAIRS);
}
// this is a custom pair definer based on the ascii
void define_color_pairs_for_ascii() {
mPrint("defining color pairs for ascii\n");
int current_pair = 0;
for (int i=0; i < 100; i++) {
for (int j=0; i < 100; i++) {
if (g_color_palette[i][j] == 1) {
char buffer[1024] = {0};
snprintf(buffer, 1024, "i: %d, j: %d current_pair: %d\n", i, j, current_pair);
mPrint(buffer);
mPrint("init_pair\n");
init_pair(current_pair, i, j);
mPrint("color_array assignment\n");
color_array[current_pair][0] = i;
color_array[current_pair][1] = j;
mPrint("color_pair_array assignment\n");
color_pair_array[i][j] = current_pair;
mPrint("incrementing current_pair\n");
current_pair++;
}
}
}
max_color_pairs = current_pair;
MAX_COLOR_PAIRS = current_pair;
max_colors = MAX_FG_COLORS;
//max_colors = MAX_FG_COLORS;
//assert(max_color_pairs == MAX_COLOR_PAIRS);
}
void cleanup() {
endwin();
free_canvas(canvas, canvas_height);
free_color_arrays();
free_color_pair_array();
}
void fail_with_msg(const char *msg) {
cleanup();
fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}
void write_char_to_canvas(int y, int x, wchar_t c, int fg_color, int bg_color) {
if (y < 0 || y >= canvas_height || x < 0 || x >= canvas_width) {
return;
}
else if (fg_color < 0 || fg_color >= max_colors) {
clear();
refresh();
cleanup();
fprintf(stderr, "fg_color: %d\n", fg_color);
mPrint("write_char_to_canvas: fg_color is out of bounds");
exit(EXIT_FAILURE);
}
else if (bg_color < 0 || bg_color >= max_colors) {
clear();
refresh();
cleanup();
fprintf(stderr, "bg_color: %d\n", bg_color);
fprintf(stderr, "max_colors: %d\n", max_colors);
mPrint("write_char_to_canvas: bg_color is out of bounds");
exit(EXIT_FAILURE);
}
else {
canvas[y][x].character = c;
canvas[y][x].foreground_color = fg_color;
canvas[y][x].background_color = bg_color;
}
}
int get_current_fg_color() {return color_array[current_color_pair][0];}
int get_current_bg_color() {return color_array[current_color_pair][1];}
void draw_canvas() {
wchar_t c = L' ';
int fg_color = 0;
int bg_color = 0;
int color_pair = 0;
int i = -1;
int j = -1;
int local_y = -1;
int local_x = -1;
// this loop renders the real canvas
for (i = 0; i + cy < canvas_height; i++) {
local_y = i + cy;
for (j = 0; j + cx < canvas_width; j++) {
// first, get the color pair
local_x = j + cx;
if (local_x < 0 || local_x >= canvas_width || local_y < 0 || local_y >= canvas_height) {
continue;
}
fg_color = canvas[local_y][local_x].foreground_color;
bg_color = canvas[local_y][local_x].background_color;
color_pair = color_pair_array[fg_color][bg_color];
attron(COLOR_PAIR(color_pair));
c = canvas[local_y][local_x].character;
// this fixes the block-rendering bug for now...
// there has to be a better way to do this
if ( c == UPPER_HALF_BLOCK ) {
mvaddstr(i, j, "▀");
}
else if ( c == BOTTOM_HALF_BLOCK ) {
mvaddstr(i, j, "▄");
}
else if ( c == FULL_BLOCK ) {
mvaddstr(i, j, "█");
}
else if (c==LEFT_HALF_BLOCK) {
mvaddstr(i, j, "▌");
}
else if (c==RIGHT_HALF_BLOCK) {
mvaddstr(i, j, "▐");
}
else if (c == DARK_SHADE ) {
mvaddstr(i, j, "░");
}
else if (c == MEDIUM_SHADE ) {
mvaddstr(i, j, "▒");
}
else if (c == LIGHT_SHADE ) {
mvaddstr(i, j, "▓");
}
else if (c == UPPER_LEFT_CORNER ) {
mvaddstr(i, j, "▛");
}
else if (c == UPPER_RIGHT_CORNER ) {
mvaddstr(i, j, "▜");
}
else if (c == BOTTOM_LEFT_CORNER ) {
mvaddstr(i, j, "▙");
}
else if (c == BOTTOM_RIGHT_CORNER ) {
mvaddstr(i, j, "▟");
}
else if (c == UPPER_LEFT_BLOCK ) {
mvaddstr(i, j, "▘");
}
else if (c == UPPER_RIGHT_BLOCK ) {
mvaddstr(i, j, "▝");
}
else if (c == BOTTOM_RIGHT_BLOCK ) {
mvaddstr(i, j, "▗");
}
else if (c == BOTTOM_LEFT_BLOCK ) {
mvaddstr(i, j, "▖");
}
else {
mvaddch(i, j, c);
}
attroff(COLOR_PAIR(color_pair));
}
}
// if we are currently in line draw mode...
// we need to draw a temporary representation of the line
// after we draw the real canvas
if (is_line_draw_mode) {
render_temp_line(line_draw_y0, line_draw_x0, y, x);
}
else if (is_rect_draw_mode) {
render_temp_rect(line_draw_y0, line_draw_x0, y, x);
}
refresh();
}
void render_temp_rect(int y1, int x1, int y2, int x2) {
render_temp_line(y1, x1, y1, x2);
render_temp_line(y1, x2, y2, x2);
render_temp_line(y2, x2, y2, x1);
render_temp_line(y2, x1, y1, x1);
}
void render_temp_line(int y1, int x1, int y2, int x2) {
attron(COLOR_PAIR(current_color_pair));
int dx = abs(x2-x1);
int dy = abs(y2-y1);
int err = dx-dy;
int e2 = -1;
int sx = -1;
int sy = -1;
if (x1 < x2) {
sx = 1;
}
if (y1 < y2) {
sy = 1;
}
while (true) {
mvaddstr(y1, x1, convert_wchar_block_to_str(gblock));
if (x1==x2 && y1==y2) {
break;
}
e2 = 2*err;
if (e2 > -dy) {
err = err - dy;
x1 = x1 + sx;
}
if (e2 < dx) {
err = err + dx;
y1 = y1 + sy;
}
}
attroff(COLOR_PAIR(current_color_pair));
}
void add_character(wchar_t c) {
int fg = get_fg_color(color_array, MAX_COLOR_PAIRS, current_color_pair);
int bg = get_bg_color(color_array, MAX_COLOR_PAIRS, current_color_pair);
write_char_to_canvas(cy+y, cx+x, c, fg, bg);
}
void add_character_and_move_right(wchar_t c) {
add_character(c);
handle_move_right();
}
void add_block() { add_character(gblock); }
void delete_block() { write_char_to_canvas(cy + y, cx + x, L' ', 0, 0); }
void incr_color_pair() {
current_color_pair++;
if (current_color_pair >= max_color_pairs) {
current_color_pair = 0;
}
}
void decr_color_pair() {
current_color_pair--;
if (current_color_pair < 0) {
current_color_pair = max_color_pairs - 1;
}
}
void incr_color_pair_by_max(){ for(int i=0;i<max_colors;i++){ incr_color_pair(); } }
void decr_color_pair_by_max(){ for(int i=0;i<max_colors;i++){ decr_color_pair(); } }
void handle_save_inner_loop(FILE *outfile) {
if (outfile == NULL) {
fail_with_msg("Error opening file for writing");
}
for (int i = 0; i < canvas_height; i++) {
int prev_irc_fg = -1;
int prev_irc_bg = -1;
for (int j = 0; j < canvas_width; j++) {
// now, instead of grabbing characters from stdscr
// and having to do all this shit on the fly
// we can just render from the canvas
wchar_t wc = canvas[i][j].character;
int fg = canvas[i][j].foreground_color;
int bg = canvas[i][j].background_color;
int irc_fg = convert_to_irc_color(fg);
int irc_bg = convert_to_irc_color(bg);
bool color_changed = prev_irc_fg != irc_fg || prev_irc_bg != irc_bg;
//old code here for historic reasons
//cchar_t character;
//mvwin_wch(stdscr, i, j, &character); // Read wide character from the canvas
//wchar_t wc = character.chars[0];
//attr_t attribute = character.attr;
//int color_pair_number = PAIR_NUMBER(attribute);
if (color_changed) {
fprintf(outfile, "\x03%02d,%02d%lc", irc_fg, irc_bg, wc);
}
else {
fprintf(outfile, "%lc", wc);
}
prev_irc_fg = irc_fg;
prev_irc_bg = irc_bg;
}
fprintf(outfile, "\n");
}
}
void get_filename_from_user() {
char buffer[32] = {0};
while(strlen(buffer) == 0) {
clear();
echo();
mvprintw(0, 0, "Enter filename: ");
refresh();
getnstr(buffer, 32);
}
strncpy(filename, buffer, 1024);
noecho();
}
void handle_save() {
// 1. filename is empty
bool filename_is_empty = strcmp(filename, "") == 0;
if (filename_is_empty) {
get_filename_from_user();
}
filename_is_empty = strcmp(filename, "") == 0;
// 2. filename is not empty
if (!filename_is_empty) {
// test writing out file
FILE *outfile = fopen(filename, "w");
if (outfile == NULL) {
perror("Error opening file for writing");
exit(-1);
}
handle_save_inner_loop(outfile);
fclose(outfile);
}
}
void handle_move_up() { if (y-1 >= 0) { y--; } }
void handle_move_left() { if (x-1 >= 0) { x--; } }
void handle_move_down() { if (y+1 < canvas_height) { y++; } }
void handle_move_right() { if (x+1 < canvas_width) { x++; } }
void incr_cam_x() { cx++; }
void incr_cam_y() { cy++; }
void decr_cam_x() { if (cx-1 >= 0) { cx--; } }
void decr_cam_y() { if (cy-1 >= 0) { cy--; } }
void handle_normal_mode_arrow_keys(int c) {
if (c==KEY_DOWN) {
handle_move_down();
}
else if (c==KEY_UP) {
handle_move_up();
}
else if (c==KEY_LEFT) {
handle_move_left();
}
else if (c==KEY_RIGHT) {
handle_move_right();
}
}
void handle_cam_mode_arrow_keys(int c) {
if (c==KEY_DOWN) {
incr_cam_y();
}
else if (c==KEY_UP) {
decr_cam_y();
}
else if (c==KEY_LEFT) {
decr_cam_x();
}
else if (c==KEY_RIGHT) {
incr_cam_x();
}
}
void handle_color_pair_change(int c) {
if (c=='o') {
decr_color_pair();
}
else if (c=='p') {
incr_color_pair();
}
else if (c=='O') {
decr_color_pair_by_max();
}
else if (c=='P') {
incr_color_pair_by_max();
}
}
// this doesn't work *exactly* as intended yet
void paintbucket(int y, int x, wchar_t old_char, wchar_t new_char, int old_fg, int old_bg, int new_fg, int new_bg) {
if ( y < 0 || y >= canvas_height || x < 0 || x >= canvas_width ||
canvas[y][x].foreground_color != old_fg ||
canvas[y][x].background_color != old_bg ||
canvas[y][x].character != old_char ||
canvas[y][x].foreground_color == new_fg) {
return;
}
canvas[y][x].character = new_char;
canvas[y][x].foreground_color = new_fg;
canvas[y][x].background_color = new_bg;
paintbucket(y-1, x, old_char, new_char, old_fg, old_bg, new_fg, new_bg);
paintbucket(y+1, x, old_char, new_char, old_fg, old_bg, new_fg, new_bg);
paintbucket(y, x-1, old_char, new_char, old_fg, old_bg,new_fg, new_bg);
paintbucket(y, x+1, old_char, new_char, old_fg, old_bg,new_fg, new_bg);
}
void draw_line(int y1, int x1, int y2, int x2, int fg, int bg) {
int dx = abs(x2-x1);
int dy = abs(y2-y1);
int sx = x1 < x2 ? 1 : -1;
int sy = y1 < y2 ? 1 : -1;
int err = dx-dy;
int e2;
// TODO: check if x1,y1 is in bounds
if (x1 < 0 || x1 >= canvas_width || y1 < 0 || y1 >= canvas_height || x2 < 0 || x2 >= canvas_width || y2 < 0 || y2 >= canvas_height) {
return;
}
if (x1==x2 && y1==y2) {
write_char_to_canvas(y1, x1, gblock, fg, bg);
return;
}
while (true) {
write_char_to_canvas(y1, x1, gblock, fg, bg);
if (x1==x2 && y1==y2) {
break;
}
e2 = 2*err;
if (e2 > -dy) {
err = err - dy;
x1 = x1 + sx;
}
if (e2 < dx) {
err = err + dx;
y1 = y1 + sy;
}
}
}
void draw_rect(int y1, int x1, int y2, int x2, int fg, int bg) {
draw_line(y1, x1, y1, x2, fg, bg);
draw_line(y1, x2, y2, x2, fg, bg);
draw_line(y2, x2, y2, x1, fg, bg);
draw_line(y2, x1, y1, x1, fg, bg);
}
// the 13 is hard-coded AF we need to extend this
// eventually the pixels will no longer be wchar_t but rather a 5-byte array
#define GBLOCK_PALETTE_SIZE 13
static int gblock_palette_index = 0;
static wchar_t gblock_palette[GBLOCK_PALETTE_SIZE] = {
FULL_BLOCK,
UPPER_HALF_BLOCK,
BOTTOM_HALF_BLOCK,
LEFT_HALF_BLOCK,
RIGHT_HALF_BLOCK,
UPPER_LEFT_BLOCK,
UPPER_LEFT_CORNER,
UPPER_RIGHT_BLOCK,
UPPER_RIGHT_CORNER,
BOTTOM_LEFT_BLOCK,
BOTTOM_LEFT_CORNER,
BOTTOM_RIGHT_BLOCK,
BOTTOM_RIGHT_CORNER
};
void rotate_gblock_forward() {
gblock_palette_index = (gblock_palette_index + 1) % GBLOCK_PALETTE_SIZE;
gblock = gblock_palette[gblock_palette_index];
}
void rotate_gblock_backward() {
gblock_palette_index = (gblock_palette_index - 1) % GBLOCK_PALETTE_SIZE;
if (gblock_palette_index < 0) {
gblock_palette_index = GBLOCK_PALETTE_SIZE - 1;
}
gblock = gblock_palette[gblock_palette_index];
}
void handle_gblock_rotation(int c) {
if (c=='[') {
rotate_gblock_backward();
}
else if (c==']') {
rotate_gblock_forward();
}
}
void handle_quit() {
clear();
// ask the user to confirm the quit
mvprintw(0, 0, "Are you sure you want to quit?");
mvprintw(1, 0, "Press q again or press any other key to cancel.");
refresh();
int c = getch();
if (c == 'q') {
quit = 1;
}
}
void reset_line_draw_vars() {
line_draw_x0 = line_draw_y0 = line_draw_x1 = line_draw_y1 = -1;
curs_set(1);
}
void handle_escape_key_normal() {
if (!is_line_draw_mode && !is_rect_draw_mode) {
is_text_mode = true;
}
else if (is_line_draw_mode) {
is_line_draw_mode = false;
reset_line_draw_vars();
}
else if (is_rect_draw_mode) {
is_rect_draw_mode = false;
reset_line_draw_vars();
}
}
void handle_mouse_normal() {
MEVENT event;
if (getmouse(&event) == OK) {
if (event.bstate & BUTTON1_CLICKED) {
if (event.x < canvas_width) {
x = event.x;
}
else if (event.x >= canvas_width) {
x = canvas_width - 1;
}
else {
x = 0;
}
if (event.y < canvas_height) {
y = event.y;
}
else if (event.y >= canvas_height) {
y = canvas_height - 1;
}
else {
y = 0;
}
}
}
}
void handle_line_draw() {
// y0 and x0 must be set and it must set that we are in line draw mode
// enter line draw mode
if (!is_line_draw_mode) {
is_line_draw_mode = true;
line_draw_y0 = y;
line_draw_x0 = x;
curs_set(0);
}
// draw the line and exit line draw mode
else {
is_line_draw_mode = false;
draw_line(line_draw_y0, line_draw_x0, y, x, get_current_fg_color(), get_current_bg_color());
reset_line_draw_vars();
}
// user must either
// 1. move to the y1 x1 that they want to drawn the line to and then press space or l or something
// 2. press escape or something to exit line draw mode
}
void handle_rect_draw() {
// it is going to be a lot like 4 separate line draws
if (!is_rect_draw_mode) {
is_rect_draw_mode = true;
line_draw_y0 = y;
line_draw_x0 = x;
curs_set(0);
}
else {
is_rect_draw_mode = false;
draw_rect(line_draw_y0, line_draw_x0, y, x, get_current_fg_color(), get_current_bg_color());
reset_line_draw_vars();
}
}
void handle_normal_mode_input(int c) {
// escape key switches back and forth between normal & text modes
if (c == 27) {
handle_escape_key_normal();
}
else if (c=='q') {
handle_quit();
}
else if (c==KEY_MOUSE) {
handle_mouse_normal();
}
// to draw a line, we will need a function line(y0,x0,y1,x1)
// you will press l, then navigate to where you want the line drawn
else if (c=='l') {
handle_line_draw();
}
// to draw a square, we will need a function square(y0,x0,y1,x1)
// you will press s, then navigate to where you want the square drawn
else if (c=='s') {
handle_rect_draw();
}
else if (c=='C') {
clear_canvas(canvas, canvas_height, canvas_width);
}
else if (c=='c') {
is_cam_mode = ! is_cam_mode;
is_text_mode = false;
}
// fill canvas
else if (c=='F') {
fill_canvas(canvas, canvas_height, canvas_width, get_current_fg_color(), get_current_bg_color());
}
else if (c==KEY_DC) {
delete_block();
}
else if (c==KEY_BACKSPACE) {
delete_block();
handle_move_left();
}
else if (c=='S') {
handle_save();
}
else if (c==KEY_DOWN || c==KEY_UP || c==KEY_RIGHT || c==KEY_LEFT) {
if (is_cam_mode) {
handle_cam_mode_arrow_keys(c);
}
else {
handle_normal_mode_arrow_keys(c);
}
}
else if (c=='G') {
// paintbucket tool - smart fill
paintbucket(y, x, canvas[y][x].character, gblock, canvas[y][x].foreground_color, canvas[y][x].background_color, get_current_fg_color(), get_current_bg_color());
}
else if (c==' ') {
add_block();
handle_move_right();
}
else if (c=='o' || c=='O' || c=='p' || c=='P') {
handle_color_pair_change(c);
}
else if (c=='[' || c==']') {
handle_gblock_rotation(c);
}
else if (c=='?') {
show_help();
}
// resize the canvas' width
else if (c=='W') {
handle_canvas_resize_width();
}
// resize the canvas' height
else if (c=='H') {
handle_canvas_resize_height();
}
else if (c==0x09) { // tab
// toggle between normal mode and cam mode
invert_current_color_pair();
}
else if (c=='v') {
canvas_flip_vertical(canvas, canvas_height, canvas_width);
}
else if (c=='h') {
canvas_flip_horizontal(canvas, canvas_height, canvas_width);
}
else if (c==KEY_RESIZE) {
getmaxyx(stdscr, terminal_height, terminal_width);
}
}
void handle_canvas_resize_width() {
int w = -1;
// TODO: this is a temporary max width
// eventually we will decide on something resonable
const int max_width = 150;
canvas_pixel_t **new_canvas = NULL;
canvas_pixel_t **old_canvas = NULL;
while (w == -1 || w > max_width) {
w = get_new_width_from_user();
}
new_canvas = init_canvas(canvas_height, w);
if (new_canvas == NULL) {
show_error("Error creating new canvas");
return;
}
y = 0;
x = 0;
move(y,x);
copy_canvas(new_canvas, canvas, canvas_height, w, canvas_height, canvas_width);
canvas_width = w;
old_canvas = canvas;
canvas = new_canvas;
free_canvas(old_canvas, canvas_height);
}
void handle_canvas_resize_height() {
int h = -1;
// whats the max height?
// eventually we will decide on something resonable
const int tmp_max_height = 1000;