-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibcw.c
More file actions
2541 lines (2347 loc) · 103 KB
/
libcw.c
File metadata and controls
2541 lines (2347 loc) · 103 KB
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
/*
* libcw.c
* Implementation of libcw
* Why are you reading this
* Come on don't you have anything better to do
* Created on: Feb 9, 2026
* Author: harma
*/
#include "libcw.h"
// Internals
static const byte YsFont6x7[1792];
static const byte YsFont6x8[2048];
static const byte YsFont6x10[2560];
static const byte YsFont7x10[2560];
static const byte YsFont8x8[2048];
static const byte YsFont8x12[3072];
static const byte YsFont12x16[8192];
static const sbyte tui_sin_table[360];
static const sbyte tui_cos_table[360];
static void __tui_clear_screen_real_buf_2(void);
static void __tui_clear_screen_real_buf_1(void);
static void __tui_set_pixel_real(byte x, byte y, byte colour);
static void __tui_set_pixel(byte x, byte y, byte colour);
// Rotates point (ax, ay) around anchor (px, py) by angle (in degrees)
void tui_rotate_point(byte ax, byte ay, byte px, byte py, word angle, byte *out_x, byte *out_y) {
if (!angle) {
*out_x = px;
*out_y = py;
return;
}
// Nerdy trig stuff
sword x = (sword)px - (sword)ax;
sword y = (sword)py - (sword)ay;
sbyte cs = tui_cos_table[angle];
sbyte sn = tui_sin_table[angle];
sword xr = ((sword)x * cs - (sword)y * sn ) >> 7;
sword yr = ((sword)x * sn + (sword)y * cs ) >> 7;
*out_x = (byte)(ax + xr);
*out_y = (byte)(ay + yr);
}
// Simple Bresenham line drawing
void tui_simple_line(byte x0, byte y0, byte x1, byte y1, byte colour) {
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
int err = dx + dy, e2;
while (1) {
__tui_set_pixel(x0, y0, colour);
if (x0 == x1 && y0 == y1) break;
e2 = err << 1;
if (e2 >= dy) {
err += dy;
x0 += sx;
}
if (e2 <= dx) {
err += dx;
y0 += sy;
}
}
}
// Draw a line with a pattern stretched across its length
void tui_advanced_draw_line(byte* data, byte bit_length, byte x0, byte y0, byte x1, byte y1, byte colour, byte thickness)
{
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
int err = dx + dy, e2;
int line_len = dx > -dy ? dx : -dy;
int pix_index = 0;
while (1) {
int bit_index = ((long)pix_index * bit_length) / line_len;
if (data[bit_index >> 3] & (0x80 >> (bit_index & 7))) {
tui_set_pixel(x0, y0, colour, thickness);
}
if (x0 == x1 && y0 == y1) break;
pix_index++;
e2 = err << 1;
if (e2 >= dy) {
err += dy;
x0 += sx;
}
if (e2 <= dx) {
err += dx;
y0 += sy;
}
}
}
// Draw a line with a repeating pattern
void tui_pattern_draw_line(byte pattern, byte x0, byte y0, byte x1, byte y1, byte colour, byte thickness) {
byte lcount = 0;
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
int err = dx + dy, e2;
while (1) {
// Draw pixel based on pattern
if (pattern == 0xFF) {
tui_set_pixel(x0, y0, colour, thickness);
} else if (pattern & (0x80 >> (lcount & 7))) {
tui_set_pixel(x0, y0, colour, thickness);
} else {
tui_set_pixel(x0, y0, (byte)TUI_COLOUR_WHITE, thickness);
}
lcount++;
if (x0 == x1 && y0 == y1) break;
e2 = err << 1;
if (e2 >= dy) {
err += dy;
x0 += sx;
}
if (e2 <= dx) {
err += dx;
y0 += sy;
}
}
}
// Draw the contents of the VRAM buffer to the real VRAM
void tui_render_buffer(void) {
word i = 0;
word j = 0;
// Lower bitplane
#ifndef IS_CWX
BufSelSFR= 0;
#endif
for(i = 0; i < 0x0600; i+=2)
{
derefw(0xf800 + j) = derefw(VRAM + i);
j+=2;
if((j & 0x001F) == 0x18)
{
j+=8;
}
}
// Upper bitplane
#ifndef IS_CWX
BufSelSFR = 4;
j = 0;
for(i = 0; i < 0x0600; i+=2)
{
derefw(0xf800 + j) = derefw(VRAM + i + 0x600);
j+=2;
if((j & 0x001F) == 0x18)
{
j+=8;
}
}
#endif
}
// An attempt to work around compiler optimization issues
#ifndef IS_CWX
static void __tui_clear_screen_real_buf_2(void) {
word i;
BufSelSFR = 4;
for (i = 0; i < 0x800; i+=2) {
if ((i & 0x1F) == 0x18) i += 8;
hw_derefw(0xF800 + i) = 0x0000;
}
}
#endif
static void __tui_clear_screen_real_buf_1(void) {
word i;
#ifndef IS_CWX
BufSelSFR = 0;
#endif
for (i = 0; i < 0x800; i+=2) {
if ((i & 0x1F) == 0x18) i += 8;
hw_derefw(0xF800 + i) = 0x00;
}
}
// I wonder what this does
void tui_clear_screen(void) {
// Determine whether to clear real screen or buffer
if (Write2RealScreen) {
__tui_clear_screen_real_buf_1();
#ifndef IS_CWX
__tui_clear_screen_real_buf_2();
#endif
return;
} else {
word i;
#ifndef IS_CWX
for (i = 0; i < 0xC00; i+=2) {
#else
for (i = 0; i < 0x600; i+=2) {
#endif
derefw(VRAM + i) = 0x0000;
}
}
}
// Some testing stuff that may be useful later
byte tui_get_pixel_b(byte x, byte y, byte buf) {
if (x > 191 || y > 63) {
return 0;
}
if (buf == 2) {
return tui_get_pixel(x, y);
}
byte a, b;
byte ty = 0x80>>(x & 7);
if (Write2RealScreen) {
#ifndef IS_CWX
if (buf) {
BufSelSFR = 0;
} else {
BufSelSFR = 4;
}
#endif
return deref((y<<5) + (x >> 3) + 0xF800) & ty ? 1 : 0;
} else {
word addr = (y << 4) + (y << 3) + (x >> 3) + VRAM;
#ifndef IS_CWX
if (buf) {
return deref(addr) & ty ? 1 : 0;
} else {
return deref(addr+0x600) & ty ? 2 : 0;
}
#else
return deref(addr) & ty ? 1 : 0;
#endif
}
}
// Get pixel colour at x,y
byte tui_get_pixel(byte x, byte y) {
if (x > 191 || y > 63) {
return 0;
}
byte a = 0, b = 0;
byte ty = 0x80>>(x & 7);
// Determine whether to read from real screen or buffer
if (Write2RealScreen) {
#ifndef IS_CWX
BufSelSFR = 0;
a = (deref((y<<5) + (x >> 3) + 0xF800) & ty) ? 1 : 0;
BufSelSFR = 4;
#endif
b = (deref((y<<5) + (x >> 3) + 0xF800) & ty) ? 2 : 0;
return a + b;
} else {
word addr = (y << 4) + (y << 3) + (x >> 3) + VRAM;
a = (deref(addr) & ty) ? 1 : 0;
#ifndef IS_CWX
b = (deref(addr+0x600) & ty) ? 2 : 0;
#endif
return a + b;
}
}
// Internal function to set a pixel in real VRAM
static void __tui_set_pixel_real(byte x, byte y, byte colour) {
if (x > 191 || y > 63) {
return;
}
word addr = (y<<5) + (x >> 3) + 0xF800;
byte ty = 0x80>>(x & 7);
#ifndef IS_CWX
BufSelSFR = 0;
#endif
// Use switch for efficiency
switch(colour) {
case TUI_COLOUR_WHITE:
deref(addr) &= ~ty; // Set both bitplanes bits to 0
#ifndef IS_CWX
BufSelSFR = 4;
deref(addr) &= ~ty;
#endif
break;
#ifndef IS_CWX
case TUI_COLOUR_LIGHT_GREY:
deref(addr) |= ty; // Set lower bitplane bit to 1, upper to 0
BufSelSFR = 4;
deref(addr) &= ~ty;
break;
case TUI_COLOUR_DARK_GREY:
deref(addr) &= ~ty; // Set lower bitplane bit to 0, upper to 1
BufSelSFR = 4;
deref(addr) |= ty;
break;
#endif
case TUI_COLOUR_BLACK:
deref(addr) |= ty; // Set both bitplanes bits to 1
#ifndef IS_CWX
BufSelSFR = 4;
deref(addr) |= ty;
#endif
break;
default:
break;
}
}
// Internal function to set a pixel in VRAM buffer
static void __tui_set_pixel(byte x, byte y, byte colour) {
if (x > 191 || y > 63) {
return;
} //else
// STUPID COMPILER OPTIMIZATIONS
void (*volatile fp)(byte, byte, byte) = __tui_set_pixel_real;
if (Write2RealScreen) {
fp(x, y, colour);
return;
}
// Set pixel in VRAM buffer
word addr = (y << 4) + (y << 3) + (x >> 3) + VRAM;
byte ty = 0x80>>(x & 7);
switch(colour) {
case TUI_COLOUR_WHITE:
deref(addr) &= ~ty; // Set both bitplanes bits to 0
#ifndef IS_CWX
deref(addr+0x600) &= ~ty;
#endif
break;
#ifndef IS_CWX
case TUI_COLOUR_LIGHT_GREY:
deref(addr) |= ty; // Set lower bitplane bit to 1, upper to 0
deref(addr+0x600) &= ~ty;
break;
case TUI_COLOUR_DARK_GREY:
deref(addr) &= ~ty; // Set lower bitplane bit to 0, upper to 1
deref(addr+0x600) |= ty;
break;
#endif
case TUI_COLOUR_BLACK:
deref(addr) |= ty; // Set both bitplanes bits to 1
#ifndef IS_CWX
deref(addr+0x600) |= ty;
#endif
break;
default:
break;
}
}
// Basically drawing a circle, but has some optimizations for small sizes
void tui_set_pixel(byte x, byte y, byte colour, byte size) {
switch(size) {
case 0:
return;
case 1:
__tui_set_pixel(x, y, colour);
return;
case 2:
__tui_set_pixel(x, y, colour);
__tui_set_pixel(x+1, y, colour);
__tui_set_pixel(x, y+1, colour);
__tui_set_pixel(x-1, y, colour);
__tui_set_pixel(x, y-1, colour);
return;
default:
tui_circle(x, y, size>>1, colour);
return;
}
}
// Square but filled in
void tui_circle(byte cx, byte cy, byte r, byte col)
{
int x = r;
int y = 0;
int err = 1 - r;
while (x >= y)
{
for (int yy = cy - x; yy <= cy + x; yy++)
{
__tui_set_pixel(cx + y, yy, col);
__tui_set_pixel(cx - y, yy, col);
}
for (int yy = cy - y; yy <= cy + y; yy++)
{
__tui_set_pixel(cx + x, yy, col);
__tui_set_pixel(cx - x, yy, col);
}
y++;
if (err < 0)
err += (y << 1) + 1;
else {
x--;
err += ((y - x) << 1) + 1;
}
}
}
// Draw a line with specified style and thickness
void tui_draw_line(byte x0, byte y0, byte x1, byte y1, byte colour, byte thickness, byte style) {
byte pattern = style;
switch(style) {
case TUI_STYLE_NONE:
case TUI_STYLE_SOLID:
pattern = 0xFF;
break;
case TUI_STYLE_DOTTED:
pattern = 0xAA;
break;
case TUI_STYLE_DASHED:
pattern = 0xF8;
break;
case TUI_STYLE_DOUBLE:
break;
default:
pattern = style; // Assume custom pattern
break;
}
tui_pattern_draw_line(pattern, x0, y0, x1, y1, colour, thickness);
}
// Draw a rectangle with rotation around anchor point
void tui_draw_rectangle(byte x, byte y, byte width, byte height, sbyte ax, sbyte ay, word rotation, byte colour, byte thickness, byte style) {
// Assume rotation is 0
byte nx0 = x - ax;
byte ny0 = y - ay;
byte nx1 = x + width - ax;
byte ny1 = y - ay;
byte nx2 = x - ax;
byte ny2 = y - ay + height;
byte nx3 = x + width - ax;
byte ny3 = y - ay + height;
if (rotation >= 360) rotation -= 360;
if (rotation < 0) rotation += 360;
if (rotation) {
// Rotate all points around x, y
if (ax || ay) {
tui_rotate_point(x, y, x - ax, y - ay, rotation, &nx0, &ny0); // Optimizationg because usually ax and ay are 0, so the top left corner doesn't need rotation
}
// Top right
tui_rotate_point(x, y, x + width - ax, y - ay, rotation, &nx1, &ny1);
// Bottom left
tui_rotate_point(x, y, x - ax, y - ay + height, rotation, &nx2, &ny2);
// Bottom right
tui_rotate_point(x, y, x + width - ax, y - ay + height, rotation, &nx3, &ny3);
}
// Draw lines between points
tui_draw_line(nx0, ny0, nx1, ny1, colour, thickness, style);
tui_draw_line(nx0, ny0, nx2, ny2, colour, thickness, style);
tui_draw_line(nx2, ny2, nx3, ny3, colour, thickness, style);
tui_draw_line(nx1, ny1, nx3, ny3, colour, thickness, style);
}
// Helper function to draw circles
void tui_draw_points(byte cx, byte cy, byte x, byte y, byte thickness, byte colour) {
// Blah blah blah
tui_set_pixel(cx + x, cy + y, colour, thickness);
tui_set_pixel(cx - x, cy + y, colour, thickness);
tui_set_pixel(cx + x, cy - y, colour, thickness);
tui_set_pixel(cx - x, cy - y, colour, thickness);
tui_set_pixel(cx + y, cy + x, colour, thickness);
tui_set_pixel(cx - y, cy + x, colour, thickness);
tui_set_pixel(cx + y, cy - x, colour, thickness);
tui_set_pixel(cx - y, cy - x, colour, thickness);
}
// Draw a circle with rotation around cx, cy with anchor point
void tui_draw_circle(byte cx, byte cy, byte radius, sbyte ax, sbyte ay, byte thickness, byte colour) {
byte x = 0;
byte y = radius;
sword d = 3 - (radius << 1);
tui_draw_points(cx - ax, cy - ay, x, y, thickness, colour);
while (y >= x) {
if (d > 0) {
y--;
d = d + ((x - y) << 2) + 10;
} else {
d = d + (x << 2) + 6;
}
x++;
tui_draw_points(cx - ax, cy - ay, x, y, thickness, colour);
}
}
// Returns the width and height of a given font
void tui_get_font_size(byte font_size, byte* width, byte* height) {
switch(font_size) {
case TUI_FONT_SIZE_6x7:
*width = 6;
*height = 7;
return;
case TUI_FONT_SIZE_6x8:
*width = 6;
*height = 8;
return;
case TUI_FONT_SIZE_6x10:
*width = 6;
*height = 10;
return;
case TUI_FONT_SIZE_7x10:
*width = 7;
*height = 10;
return;
case TUI_FONT_SIZE_8x8:
*width = 8;
*height = 8;
return;
case TUI_FONT_SIZE_8x12:
*width = 8;
*height = 12;
return;
case TUI_FONT_SIZE_12x16:
*width = 12;
*height = 16;
return;
default:
*width = 6;
*height = 8;
return;
}
}
// Returns the width and height of the given text
void tui_get_text_size(byte font_size, const char* text, byte* width, byte* height) {
byte cwidth = 0, cheight = 0, i = 0;
tui_get_font_size(font_size, &cwidth, &cheight);
// Count characters and multiply by width
while (text[i]) i++;
*width = cwidth * i;
*height = cheight;
}
// Draw text with specified font size, rotation, and style
void tui_draw_text(byte x, byte y, const char* text, byte font_size, sbyte ax, sbyte ay, word rotation, byte colour) {
byte cheight;
byte cwidth;
byte i = 0;
byte tx, ty;
byte ox = x;
// Get character dimensions
tui_get_font_size(font_size, &cwidth, &cheight);
while (text[i]) {
if (rotation) tui_rotate_point(x, y, ox - ax, y - ay, rotation, &tx, &ty); // Rotate around anchor
else {
tx = ox - ax; // No rotation, just offset by anchor
ty = y - ay;
}
// Draw character, anchor x, y isn't really used since it was done before but whatever
tui_draw_char(tx, ty, text[i], font_size, 0, 0, rotation, colour);
ox += cwidth;
i++;
}
}
// Draw a byte at x,y with mask
void tui_draw_byte(byte x, byte y, byte data, byte data2, byte mask) {
if (x > 191 || y > 63) {
return;
}
// Prepare bit-shifted data
byte bitpos = x & 7;
byte lbyte = data >> bitpos;
byte hbyte = data << (8 - bitpos);
byte lbyte2 = data2 >> bitpos;
byte hbyte2 = data2 << (8 - bitpos);
byte lmask = mask >> bitpos;
byte hmask = mask << (8 - bitpos);
byte temp;
word addr;
// Determine starting address based on real screen or buffer flag
if (Write2RealScreen) {
addr = (y << 5) + (x >> 3) + 0xF800;
} else {
addr = (y << 4) + (y << 3) + (x >> 3) + VRAM;
}
// Write high bytes first if there's spillover
if (bitpos) {
#ifndef IS_CWX
if (Write2RealScreen) {
BufSelSFR = 0;
}
#endif
// Draw first bitplane high byte
temp = deref(addr + 1);
temp &= ~hmask;
temp |= (hbyte & hmask);
deref(addr + 1) = temp;
#ifndef IS_CWX
if (Write2RealScreen) {
BufSelSFR = 4;
} else {
addr += 0x600;
}
// Draw second bitplane high byte
temp = deref(addr + 1);
temp &= ~hmask;
temp |= (hbyte2 & hmask);
deref(addr + 1) = temp;
if (!Write2RealScreen) {
addr -= 0x600;
}
#endif
}
#ifndef IS_CWX
if (Write2RealScreen) {
BufSelSFR = 0;
}
#endif
// Draw low byte (first bitplane)
temp = deref(addr);
temp &= ~lmask;
temp |= (lbyte & lmask);
deref(addr) = temp;
#ifndef IS_CWX
if (Write2RealScreen) {
BufSelSFR = 4;
} else {
addr += 0x600;
}
// Draw low byte (second bitplane)
temp = deref(addr);
temp &= ~lmask;
temp |= (lbyte2 & lmask);
deref(addr) = temp;
#endif
}
// Helper functions to get min and max of 4 bytes
static byte tui_min4(byte a, byte b, byte c, byte d) {
byte m = a;
if (b < m) m = b;
if (c < m) m = c;
if (d < m) m = d;
return m;
}
static byte tui_max4(byte a, byte b, byte c, byte d) {
byte m = a;
if (b > m) m = b;
if (c > m) m = c;
if (d > m) m = d;
return m;
}
// Draw an image bitmap with rotation around anchor point
void tui_draw_image(byte x, byte y, byte width, byte height, const byte* bitmap, sbyte ax, sbyte ay, word rotation, byte colour) {
word bwidth = (width + 7) >> 3;
word psize = bwidth * height;
byte iy, ix;
word mapindex;
if (!rotation) {
byte osf = (width & 7) ? 1 : 0; // If size is not a multiple of 8
byte rem = width & 7; // Remaining bits in last byte
byte last_mask = rem ? (0xFF << (8 - rem)) : 0xFF; // Mask for last byte
// No rotation, simple blit, but it's massive becuase doing the switch when it comes to draw the byte decreases the amount of cycles
switch (colour) {
case TUI_COLOUR_WHITE:
return;
#ifndef IS_CWX
case TUI_COLOUR_LIGHT_GREY:
for (iy = 0; iy < height; iy++) {
for (ix = 0; ix < bwidth; ix++) {
mapindex = iy * bwidth + ix; // Calculate index in bitmap
byte mask = (ix == bwidth - osf && osf) ? last_mask : 0xFF; // If it is the last byte and there is an offset, use last mask
tui_draw_byte(x + (ix << 3) - ax, y - ay + iy, bitmap[mapindex], 0, mask); // The draw byte function is surprisingly complicated
}
}
return;
case TUI_COLOUR_DARK_GREY:
for (iy = 0; iy < height; iy++) {
for (ix = 0; ix < bwidth; ix++) {
mapindex = iy * bwidth + ix;
byte mask = (ix == bwidth - osf && osf) ? last_mask : 0xFF;
tui_draw_byte(x + (ix << 3) - ax, y - ay + iy, 0, bitmap[mapindex], mask);
}
}
return;
#endif
case TUI_COLOUR_BLACK:
for (iy = 0; iy < height; iy++) {
for (ix = 0; ix < bwidth; ix++) {
mapindex = iy * bwidth + ix;
byte mask = (ix == bwidth - osf && osf) ? last_mask : 0xFF;
tui_draw_byte(x + (ix << 3) - ax, y - ay + iy, bitmap[mapindex], bitmap[mapindex], mask);
}
}
return;
default:
for (iy = 0; iy < height; iy++) {
for (ix = 0; ix < bwidth; ix++) {
mapindex = iy * bwidth + ix;
byte mask = (ix == bwidth - osf && osf) ? last_mask : 0xFF;
#ifndef IS_CWX
tui_draw_byte(x + (ix << 3) - ax, y - ay + iy, bitmap[mapindex], bitmap[mapindex + psize], mask);
#else
tui_draw_byte(x + (ix << 3) - ax, y - ay + iy, bitmap[mapindex], bitmap[mapindex], mask);
#endif
}
}
return;
}
} else {
// If there is rotation, we have to do it pixel by pixel :(
byte nx, ny, cx1, cy1, cx2, cy2, cx3, cy3, bx, by, rx, ry, hx, hy;
byte cx0 = x, cy0 = y;
// Rotate corners
if (ax || ay) {
tui_rotate_point(x, y, x - ax, y - ay, rotation, &cx0, &cy0);
}
tui_rotate_point(x, y, x + width - ax, y - ay, rotation, &cx1, &cy1);
tui_rotate_point(x, y, x - ax, y - ay + height, rotation, &cx2, &cy2);
tui_rotate_point(x, y, x + width - ax, y - ay + height, rotation, &cx3, &cy3);
// Get bounding box from the rotated corners
bx = tui_min4(cx0, cx1, cx2, cx3);
by = tui_min4(cy0, cy1, cy2, cy3);
rx = tui_max4(cx0, cx1, cx2, cx3) + 1;
ry = tui_max4(cy0, cy1, cy2, cy3) + 1;
if (rx > 192) rx = 192;
if (ry > 64) ry = 64;
// For each pixel in bounding box, rotate backwards and sample from bitmap, that way there are no gaps
for (byte i = bx; i < rx; i++) {
for (byte j = by; j < ry; j++) {
tui_rotate_point(x, y, i, j, 360 - rotation, &hx, &hy); // Rotate backwards to get sample point
hx -= (x - ax);
hy -= (y - ay);
// When out of bounds, skip to not write junk data
if (hx < 0 || hy < 0 || hx >= width || hy >= height) {
continue;
}
byte col = 0;
word addr = hy * bwidth + (hx >> 3); // Calculate address in bitmap
byte bdata = bitmap[addr]; // Get byte from bitmap
#ifndef IS_CWX
byte bdata2 = (colour == TUI_COLOUR_IMAGE) ? bitmap[addr + psize] : 0; // Get second bitplane if needed
#endif
byte bmask = 0x80 >> (hx & 7); // Calculate bitmask for pixel
// Determine colour based on bitplanes and requested colour
#ifndef IS_CWX
if (colour == TUI_COLOUR_IMAGE) {
if (bdata2 & bmask) {
col |= 2;
}
}
if (bdata & bmask) {
if (colour != TUI_COLOUR_IMAGE) {
col = colour;
} else {
col |= 1;
}
}
#else
if (bdata & bmask) {
col = colour;
}
#endif
tui_set_pixel(i, j, col, 1); // Finally set the pixel
}
}
}
}
// Draw_image wrapper to draw a character from a font
void tui_draw_char(byte x, byte y, char c, byte font_size, sbyte ax, sbyte ay, word rotation, byte colour) {
const byte* font_data;
byte font_width;
byte font_height;
// This can probably use tui_get_font_size but tui_get_font_size doesnt get dont_data so like idc
switch(font_size) {
case TUI_FONT_SIZE_6x7:
font_data = YsFont6x7;
font_width = 6;
font_height = 7;
break;
case TUI_FONT_SIZE_6x8:
font_data = YsFont6x8;
font_width = 6;
font_height = 8;
break;
case TUI_FONT_SIZE_6x10:
font_data = YsFont6x10;
font_width = 6;
font_height = 10;
break;
case TUI_FONT_SIZE_7x10:
font_data = YsFont7x10;
font_width = 7;
font_height = 10;
break;
case TUI_FONT_SIZE_8x8:
font_data = YsFont8x8;
font_width = 8;
font_height = 8;
break;
case TUI_FONT_SIZE_8x12:
font_data = YsFont8x12;
font_width = 8;
font_height = 12;
break;
case TUI_FONT_SIZE_12x16:
font_data = YsFont12x16;
font_width = 12;
font_height = 16;
break;
default:
return;
}
// Font data is selected based on the given font_size
// Draw the character using draw_image
// Font data is extracted by subtracting the character byte 32 (first printable ascii (" "))
// And then it is multiplied by the height of the character, times the width in bytes (width + 7) >> 3
// And then that is used as the index into the font data
tui_draw_image(x, y, font_width, font_height, &font_data[(byte)(c - 32) * (font_height * ((font_width + 7) >> 3))], ax, ay, rotation, colour);
}
// Idk what this is, ill fix latersss
void tui_draw_full_image(const word* bitmap, byte colour) {
word i = 0;
word j = 0;
word* dest = (word*)0xF800;
if (colour == TUI_COLOUR_IMAGE) {
// Lower bitplane
#ifndef IS_CWX
BufSelSFR = 0;
#endif
for(i = 0; i < 0x600; i++)
{
dest[j] = bitmap[i];
j++;
if((j & 0x001F) == 0x0C)
{
j+=4;
}
}
// Upper bitplane
#ifndef IS_CWX
BufSelSFR = 4;
j = 0;
for(i = 0x600; i < 0xC00; i++)
{
dest[j] = bitmap[i];
j++;
if((j & 0x001F) == 0x0C)
{
j+=4;
}
}
#endif
#ifndef IS_CWX
} else if (colour == TUI_COLOUR_LIGHT_GREY) {
BufSelSFR = 0;
for(i = 0; i < 0x600; i++)
{
dest[j] = bitmap[i];
j++;
if((j & 0x001F) == 0x0C)
{
j+=4;
}
}
} else if (colour == TUI_COLOUR_DARK_GREY) {
BufSelSFR = 4;
for(i = 0x600; i < 0xC00; i++)
{
dest[j] = bitmap[i];
j++;
if((j & 0x001F) == 0x0C)
{
j+=4;
}
}
#endif
}
}
// Get user data pointer from block header
static void *hdata(block_t *b) {
return (void *)((byte *)b + sizeof(block_t));
}
// Get block header from user data pointer
static block_t *hblock(void *ptr) {
return (block_t *)((byte *)ptr - sizeof(block_t));
}
// Align size to heap block alignment
static word halign(word size) {
if (size % HEAP_BLOCK_ALIGN == 0) {
return size;
}
return size + (HEAP_BLOCK_ALIGN - (size % HEAP_BLOCK_ALIGN));
}
// Initialize heap with single free block
void hinit(void) {
block_t *initial_block = (block_t *)HEAP_START_ADDR;
initial_block->size = HEAP_MAX_SIZE - sizeof(block_t);
initial_block->next = 0;
initial_block->free = 1;
}
// Split block into two if larger than needed
static void hsplit(block_t *b, word size) {
block_t *new_block = (block_t *)((byte *)hdata(b) + size);
new_block->size = b->size - size - sizeof(block_t);
new_block->next = b->next;
new_block->free = 1;
b->size = size;
b->next = new_block;
}
// Merge free blocks
void hmerge(void) {
block_t *current = (block_t *)HEAP_START_ADDR;
while (current && current->next) {
if (current->free && current->next->free) {
current->size += sizeof(block_t) + current->next->size;
current->next = current->next->next;
} else {
current = current->next;
}
}
}
// Find memory block (helper for halloc)
static block_t *hfind(word size) {
block_t *current = (block_t *)HEAP_START_ADDR;
while (current) {
if (current->free && current->size >= size) {
return current;
}
current = current->next;
}
return 0;
}
// Allocate memory block
void *halloc(word size) {
size = halign(size);
block_t *b = hfind(size);
if (!b) {
hmerge();
b = hfind(size);
if (!b) return 0;
}
if (b->size >= size + sizeof(block_t) + HEAP_BLOCK_ALIGN)
hsplit(b, size);
b->free = 0;
return hdata(b);
}
// Allocate and zero-initialize memory block
void *hcalloc(word num, word size) {
word total_size = num * size;
total_size = halign(total_size);
void *ptr = halloc(total_size);
if (ptr) {
byte *bptr = (byte *)ptr;
for (word i = 0; i < total_size; i++) {
bptr[i] = 0;
}
}
return ptr;
}
// Reallocate memory block for new size
void *hrealloc(void *ptr, word size) {
size = halign(size);
if (!ptr) return halloc(size);
block_t *old = hblock(ptr);
word old_size = old->size;
if (size <= old_size) return ptr;
void *new_ptr = halloc(size);
if (!new_ptr) return 0;
byte *src = (byte *)ptr;
byte *dst = (byte *)new_ptr;
for (word i = 0; i < old_size; i++) dst[i] = src[i];
old->free = 1;
return new_ptr;
}
// Free memory block
void hfree(void *ptr) {
if (!ptr) return;
block_t *b = hblock(ptr);
b->free = 1;
}
//Internals
static byte fs_node_index(fs_node_t *node) {
return (byte)(node - FS_NODES);
}
static fs_node_t *fs_find_free_node(void) {