-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_text.c
More file actions
1638 lines (1288 loc) · 58.8 KB
/
Copy pathlib_text.c
File metadata and controls
1638 lines (1288 loc) · 58.8 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
//! @file lib_text.c
/*
* lib_text.c
*
* Created on: Feb 19, 2022
* Author: micahbly
*/
/*****************************************************************************/
/* Includes */
/*****************************************************************************/
// project includes
#include "lib_text.h"
// C includes
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// A2560 includes
#include <mcp/syscalls.h>
#include <mb/a2560_platform.h>
#include <mb/lib_general.h>
#include <mb/lib_sys.h>
/*****************************************************************************/
/* Definitions */
/*****************************************************************************/
/*****************************************************************************/
/* Enumerations */
/*****************************************************************************/
/*****************************************************************************/
/* Global Variables */
/*****************************************************************************/
/*****************************************************************************/
/* Private Function Prototypes */
/*****************************************************************************/
//! \cond PRIVATE
// validate screen id, x, y, and colors
boolean Text_ValidateAll(Screen* the_screen, signed int x, signed int y, unsigned char fore_color, unsigned char back_color);
// validate the coordinates are within the bounds of the specified screen
boolean Text_ValidateXY(Screen* the_screen, signed int x, signed int y);
// calculate the VRAM location of the specified coordinate
char* Text_GetMemLocForXY(Screen* the_screen, signed int x, signed int y, boolean for_attr);
// Fill attribute or text char memory. Writes to char memory if for_attr is false.
// calling function must validate the screen ID before passing!
//! @return returns false on any error/invalid input.
boolean Text_FillMemory(Screen* the_screen, boolean for_attr, unsigned char the_fill);
// Fill character and attribute memory for a specific box area
// calling function must validate screen id, coords, attribute value before passing!
//! @return returns false on any error/invalid input.
boolean Text_FillMemoryBoxBoth(Screen* the_screen, signed int x, signed int y, signed int width, signed int height, unsigned char the_char, unsigned char the_attribute_value);
// Fill character OR attribute memory for a specific box area
// calling function must validate screen id, coords, attribute value before passing!
//! @return returns false on any error/invalid input.
boolean Text_FillMemoryBox(Screen* the_screen, signed int x, signed int y, signed int width, signed int height, boolean for_attr, unsigned char the_fill);
//! \endcond
/*****************************************************************************/
/* Private Function Definitions */
/*****************************************************************************/
// **** NOTE: all functions in private section REQUIRE pre-validated parameters.
// **** NEVER call these from your own functions. Always use the public interface. You have been warned!
//! \cond PRIVATE
//! Validate screen id, x, y, and colors
//! @param fore_color: Index to the desired foreground color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param back_color: Index to the desired background color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @return returns false on any error/invalid input.
boolean Text_ValidateAll(Screen* the_screen, signed int x, signed int y, unsigned char fore_color, unsigned char back_color)
{
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
if (Text_ValidateXY(the_screen, x, y) == false)
{
LOG_ERR(("%s %d: illegal coordinates %li, %li", __func__, __LINE__, x, y));
return false;
}
if (fore_color > 15 || back_color > 15)
{
LOG_INFO(("%s %d: illegal foreground (%u) or background (%u) color", __func__, __LINE__, fore_color, back_color));
return false;
}
return true;
}
//! Validate the coordinates are within the bounds of the specified screen.
boolean Text_ValidateXY(Screen* the_screen, signed int x, signed int y)
{
signed int max_row;
signed int max_col;
max_col = the_screen->text_cols_vis_ - 1;
max_row = the_screen->text_rows_vis_ - 1;
if (x < 0 || x > max_col || y < 0 || y > max_row)
{
return false;
}
return true;
}
//! Calculate the VRAM location of the specified coordinate
//! @param for_attr: true to work with attribute data, false to work character data. Recommend using SCREEN_FOR_TEXT_ATTR/SCREEN_FOR_TEXT_CHAR.
char* Text_GetMemLocForXY(Screen* the_screen, signed int x, signed int y, boolean for_attr)
{
char* the_write_loc;
//signed int num_cols;
// LOGIC:
// For plotting the VRAM, A2560 uses the full width, regardless of borders.
// So even if only 72 are showing, the screen is arranged from 0-71 for row 1, then 80-151 for row 2, etc.
//num_cols = the_screen->text_cols_vis_;
// the_write_loc = the_screen->text_ram_ + (the_screen->text_mem_cols_ * y) + x;
//the_write_loc = the_screen->text_ram_ + (num_cols * y) + x;
//DEBUG_OUT(("%s %d: screen=%i, x=%i, y=%i, num_cols=%i, calc=%i", __func__, __LINE__, (signed int)the_screen_id, x, y, num_cols, (num_cols * y) + x));
if (for_attr)
{
the_write_loc = the_screen->text_attr_ram_ + (the_screen->text_mem_cols_ * y) + x;
}
else
{
the_write_loc = the_screen->text_ram_ + (the_screen->text_mem_cols_ * y) + x;
}
//DEBUG_OUT(("%s %d: screen=%i, x=%i, y=%i, for-attr=%i, calc=%i, loc=%p", __func__, __LINE__, (signed int)the_screen_id, x, y, for_attr, (the_screen->text_mem_cols_ * y) + x, the_write_loc));
return the_write_loc;
}
//! Fill attribute or text char memory.
//! calling function must validate the screen ID before passing!
//! @param for_attr: true to work with attribute data, false to work character data. Recommend using SCREEN_FOR_TEXT_ATTR/SCREEN_FOR_TEXT_CHAR.
//! @param the_fill: either a 1-byte character code, or a 1-byte attribute code (foreground in high nibble, background in low nibble)
//! returns false on any error/invalid input.
boolean Text_FillMemory(Screen* the_screen, boolean for_attr, unsigned char the_fill)
{
char* the_write_loc;
unsigned long the_write_len;
if (for_attr)
{
the_write_loc = the_screen->text_attr_ram_;
}
else
{
the_write_loc = the_screen->text_ram_;
}
the_write_len = the_screen->text_mem_cols_ * the_screen->text_mem_rows_;
memset(the_write_loc, the_fill, the_write_len);
return true;
}
//! Fill character and attribute memory for a specific box area
//! calling function must validate screen id, coords, attribute value before passing!
//! @param width: width, in character cells, of the rectangle to be filled
//! @param height: height, in character cells, of the rectangle to be filled
//! @param the_attribute_value: a 1-byte attribute code (foreground in high nibble, background in low nibble)
//! @return returns false on any error/invalid input.
boolean Text_FillMemoryBoxBoth(Screen* the_screen, signed int x, signed int y, signed int width, signed int height, unsigned char the_char, unsigned char the_attribute_value)
{
char* the_char_loc;
char* the_attr_loc;
signed int max_row;
// set up char and attribute memory initial loc
the_char_loc = Text_GetMemLocForXY(the_screen, x, y, SCREEN_FOR_TEXT_CHAR);
the_attr_loc = the_char_loc + (the_screen->text_attr_ram_ - the_screen->text_ram_);
max_row = y + height;
for (; y <= max_row; y++)
{
memset(the_char_loc, the_char, width);
memset(the_attr_loc, the_attribute_value, width);
the_char_loc += the_screen->text_mem_cols_;
the_attr_loc += the_screen->text_mem_cols_;
}
return true;
}
//! Fill character OR attribute memory for a specific box area
//! calling function must validate screen id, coords, attribute value before passing!
//! @param width: width, in character cells, of the rectangle to be filled
//! @param height: height, in character cells, of the rectangle to be filled
//! @param for_attr: true to work with attribute data, false to work character data. Recommend using SCREEN_FOR_TEXT_ATTR/SCREEN_FOR_TEXT_CHAR.
//! @param the_fill: either a 1-byte character code, or a 1-byte attribute code (foreground in high nibble, background in low nibble)
//! @return returns false on any error/invalid input.
boolean Text_FillMemoryBox(Screen* the_screen, signed int x, signed int y, signed int width, signed int height, boolean for_attr, unsigned char the_fill)
{
char* the_write_loc;
signed int max_row;
// set up initial loc
the_write_loc = Text_GetMemLocForXY(the_screen, x, y, for_attr);
max_row = y + height;
for (; y <= max_row; y++)
{
memset(the_write_loc, the_fill, width);
the_write_loc += the_screen->text_mem_cols_;
}
return true;
}
//! \endcond
/*****************************************************************************/
/* Public Function Definitions */
/*****************************************************************************/
// ** NOTE: there is no destructor or constructor for this library, as it does not track any allocated memory. It works on the basis of a screen ID, which corresponds to the text memory for Vicky's Channel A and Channel B video memory.
// **** Block copy functions ****
//! Copy a full screen of attr from an off-screen buffer
//! @param the_screen: valid pointer to a configured Screen object (a2560_platform.h)
//! @param the_source_buffer: valid pointer to attribute value data that will be copied to the screen. It must be at least as large as the visible rows X visible columns of the screen.
//! @return returns false on any error/invalid input.
boolean Text_CopyAttrMemToScreen(Screen* the_screen, char* the_source_buffer)
{
char* the_vram_loc;
unsigned long the_write_len;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
the_vram_loc = the_screen->text_attr_ram_;
the_write_len = the_screen->text_cols_vis_ * the_screen->text_rows_vis_;
memcpy(the_vram_loc, the_source_buffer, the_write_len);
return true;
}
//! Copy a full screen of text attributes to an off-screen buffer.
//! @param the_screen: valid pointer to a configured Screen object (a2560_platform.h)
//! @param the_target_buffer: valid pointer to a block of memory big enough to hold the attribute data copied from the screen. It must be at least as large as the visible rows X visible columns of the screen.
//! @return returns false on any error/invalid input.
boolean Text_CopyAttrMemFromScreen(Screen* the_screen, char* the_target_buffer)
{
char* the_vram_loc;
unsigned long the_write_len;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
the_vram_loc = the_screen->text_attr_ram_;
the_write_len = the_screen->text_cols_vis_ * the_screen->text_rows_vis_;
memcpy(the_target_buffer, the_vram_loc, the_write_len);
return true;
}
//! Copy a full screen of text from an off-screen buffer.
//! @param the_source_buffer: valid pointer to character data that will be copied to the screen. It must be at least as large as the visible rows X visible columns of the screen.
//! @return returns false on any error/invalid input.
boolean Text_CopyCharMemToScreen(Screen* the_screen, char* the_source_buffer)
{
char* the_vram_loc;
unsigned long the_write_len;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
the_vram_loc = the_screen->text_ram_;
the_write_len = the_screen->text_cols_vis_ * the_screen->text_rows_vis_;
memcpy(the_vram_loc, the_source_buffer, the_write_len);
return true;
}
//! Copy a full screen of text to an off-screen buffer
//! @param the_target_buffer: valid pointer to a block of memory big enough to hold the character data copied from the screen. It must be at least as large as the visible rows X visible columns of the screen.
//! @return returns false on any error/invalid input.
boolean Text_CopyCharMemFromScreen(Screen* the_screen, char* the_target_buffer)
{
char* the_vram_loc;
unsigned long the_write_len;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
the_vram_loc = the_screen->text_ram_;
the_write_len = the_screen->text_cols_vis_ * the_screen->text_rows_vis_;
memcpy(the_target_buffer, the_vram_loc, the_write_len);
return true;
}
//! Copy a full screen worth of EITHER text or attribute data to OR from an off-screen buffer
//! @param the_buffer: valid pointer to a block of memory big enough to store (or alternatively act as the source of) the character or attribute data for the specified screen. It must be at least as large as the visible rows X visible columns of the screen.
//! @param to_screen: true to copy to the screen from the buffer, false to copy from the screen to the buffer. Recommend using SCREEN_COPY_TO_SCREEN/SCREEN_COPY_FROM_SCREEN.
//! @param for_attr: true to work with attribute data, false to work character data. Recommend using SCREEN_FOR_TEXT_ATTR/SCREEN_FOR_TEXT_CHAR.
//! @return returns false on any error/invalid input.
boolean Text_CopyScreen(Screen* the_screen, char* the_buffer, boolean to_screen, boolean for_attr)
{
char* the_vram_loc;
signed int the_write_len;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
if (the_buffer == NULL)
{
LOG_ERR(("%s %d: passed off-screen buffer was NULL", __func__, __LINE__));
return false;
}
if (for_attr)
{
the_vram_loc = the_screen->text_attr_ram_;
}
else
{
the_vram_loc = the_screen->text_ram_;
}
//the_write_len = the_screen->text_cols_vis_ * the_screen->text_rows_vis_;
the_write_len = the_screen->text_mem_cols_ * the_screen->text_mem_rows_;
if (to_screen)
{
memcpy(the_vram_loc, the_buffer, the_write_len);
}
else
{
memcpy(the_buffer, the_vram_loc, the_write_len);
}
return true;
}
//! Copy a rectangular area of text or attr to or from an off-screen buffer
//! @param the_buffer: valid pointer to a block of memory big enough to store (or alternatively act as the source of) the character or attribute data for the specified rectangle of screen memory.
//! @param to_screen: true to copy to the screen from the buffer, false to copy from the screen to the buffer. Recommend using SCREEN_COPY_TO_SCREEN/SCREEN_COPY_FROM_SCREEN.
//! @param for_attr: true to work with attribute data, false to work character data. Recommend using SCREEN_FOR_TEXT_ATTR/SCREEN_FOR_TEXT_CHAR.
//! @return returns false on any error/invalid input.
boolean Text_CopyMemBox(Screen* the_screen, char* the_buffer, signed int x1, signed int y1, signed int x2, signed int y2, boolean to_screen, boolean for_attr)
{
char* the_vram_loc;
char* the_buffer_loc;
signed int the_write_len;
signed int initial_offset;
if (!Text_ValidateAll(the_screen, x1, y1, 0, 0))
{
LOG_ERR(("%s %d: illegal screen id, coordinate, or color", __func__, __LINE__));
return false;
}
if (!Text_ValidateXY(the_screen, x2, y2))
{
LOG_ERR(("%s %d: illegal coordinate", __func__, __LINE__));
return false;
}
if (x1 > x2 || y1 > y2)
{
LOG_ERR(("%s %d: illegal coordinates", __func__, __LINE__));
return false;
}
if (the_buffer == NULL)
{
LOG_ERR(("%s %d: passed off-screen buffer was NULL", __func__, __LINE__));
return false;
}
// get initial read/write locs
initial_offset = (the_screen->text_mem_cols_ * y1) + x1;
the_buffer_loc = the_buffer + initial_offset;
if (for_attr)
{
the_vram_loc = the_screen->text_attr_ram_ + initial_offset;
}
else
{
the_vram_loc = the_screen->text_ram_ + initial_offset;
}
// do copy one line at a time
the_write_len = x2 - x1 + 1;
// DEBUG_OUT(("%s %d: vramloc=%p, buffer=%p, bufferloc=%p, to_screen=%i, the_write_len=%i", the_vram_loc, the_buffer, the_buffer_loc, to_screen, the_write_len));
for (; y1 <= y2; y1++)
{
if (to_screen)
{
memcpy(the_vram_loc, the_buffer_loc, the_write_len);
}
else
{
memcpy(the_buffer_loc, the_vram_loc, the_write_len);
}
the_buffer_loc += the_screen->text_mem_cols_;
the_vram_loc += the_screen->text_mem_cols_;
}
return true;
}
// **** Block fill functions ****
//! Fill attribute memory for the passed screen
//! @param the_fill: either a 1-byte character code, or a 1-byte attribute code (foreground in high nibble, background in low nibble)
//! @return returns false on any error/invalid input.
boolean Text_FillAttrMem(Screen* the_screen, unsigned char the_fill)
{
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
return Text_FillMemory(the_screen, SCREEN_FOR_TEXT_ATTR, the_fill);
}
//! Fill character memory for the passed screen
//! @param the_fill: either a 1-byte character code, or a 1-byte attribute code (foreground in high nibble, background in low nibble)
//! @return returns false on any error/invalid input.
boolean Text_FillCharMem(Screen* the_screen, unsigned char the_fill)
{
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
return Text_FillMemory(the_screen, SCREEN_FOR_TEXT_CHAR, the_fill);
}
//! Fill character and/or attribute memory for a specific box area
//! This version uses char-by-char functions, so it is very slow. It will be removed before release.
//! @param fore_color: Index to the desired foreground color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param back_color: Index to the desired background color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param the_draw_choice: controls the scope of the action, and is one of CHAR_ONLY, ATTR_ONLY, or CHAR_AND_ATTR. See the text_draw_choice enum.
//! @return returns false on any error/invalid input.
boolean Text_FillBoxSlow(Screen* the_screen, signed int x1, signed int y1, signed int x2, signed int y2, unsigned char the_char, unsigned char fore_color, unsigned char back_color, text_draw_choice the_draw_choice)
{
signed int dx;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
if (!Text_ValidateAll(the_screen, x1, y1,fore_color, back_color))
{
LOG_ERR(("%s %d: illegal screen id, coordinate, or color", __func__, __LINE__));
return false;
}
if (!Text_ValidateXY(the_screen, x2, y2))
{
LOG_ERR(("%s %d: illegal coordinate", __func__, __LINE__));
return false;
}
if (x1 > x2 || y1 > y2)
{
LOG_ERR(("%s %d: illegal coordinates", __func__, __LINE__));
return false;
}
// add 1 to line len, because desired behavior is a box that connects fully to the passed coords
dx = x2 - x1 + 1;
for (; y1 <= y2; y1++)
{
if (!Text_DrawHLine(the_screen, x1, y1, dx, the_char, fore_color, back_color, the_draw_choice))
{
LOG_ERR(("%s %d: fill failed", __func__, __LINE__));
return false;
}
}
return true;
}
//! Fill character and attribute memory for a specific box area
//! @param fore_color: Index to the desired foreground color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param back_color: Index to the desired background color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @return returns false on any error/invalid input.
boolean Text_FillBox(Screen* the_screen, signed int x1, signed int y1, signed int x2, signed int y2, unsigned char the_char, unsigned char fore_color, unsigned char back_color)
{
signed int dy;
signed int dx;
unsigned char the_attribute_value;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
if (!Text_ValidateAll(the_screen, x1, y1, fore_color, back_color))
{
LOG_ERR(("%s %d: illegal screen id, coordinate, or color", __func__, __LINE__));
return false;
}
if (!Text_ValidateXY(the_screen, x2, y2))
{
LOG_ERR(("%s %d: illegal coordinate", __func__, __LINE__));
return false;
}
if (x1 > x2 || y1 > y2)
{
LOG_ERR(("%s %d: illegal coordinates", __func__, __LINE__));
return false;
}
// add 1 to line len, because desired behavior is a box that connects fully to the passed coords
dx = x2 - x1 + 1;
dy = y2 - y1 + 0;
// calculate attribute value from passed fore and back colors
// LOGIC: text mode only supports 16 colors. lower 4 bits are back, upper 4 bits are foreground
the_attribute_value = ((fore_color << 4) | back_color);
return Text_FillMemoryBoxBoth(the_screen, x1, y1, dx, dy, the_char, the_attribute_value);
}
//! Fill character memory for a specific box area
//! @return returns false on any error/invalid input.
boolean Text_FillBoxCharOnly(Screen* the_screen, signed int x1, signed int y1, signed int x2, signed int y2, unsigned char the_char)
{
signed int dy;
signed int dx;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
if (!Text_ValidateAll(the_screen, x1, y1, 0, 0))
{
LOG_ERR(("%s %d: illegal screen id, coordinate, or color", __func__, __LINE__));
return false;
}
if (!Text_ValidateXY(the_screen, x2, y2))
{
LOG_ERR(("%s %d: illegal coordinate", __func__, __LINE__));
return false;
}
if (x1 > x2 || y1 > y2)
{
LOG_ERR(("%s %d: illegal coordinates", __func__, __LINE__));
return false;
}
// add 1 to line len, because desired behavior is a box that connects fully to the passed coords
dx = x2 - x1 + 1;
dy = y2 - y1 + 1;
return Text_FillMemoryBox(the_screen, x1, y1, dx, dy, SCREEN_FOR_TEXT_CHAR, the_char);
}
//! Fill attribute memory for a specific box area
//! @param fore_color: Index to the desired foreground color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param back_color: Index to the desired background color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @return returns false on any error/invalid input.
boolean Text_FillBoxAttrOnly(Screen* the_screen, signed int x1, signed int y1, signed int x2, signed int y2, unsigned char fore_color, unsigned char back_color)
{
signed int dy;
signed int dx;
unsigned char the_attribute_value;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
if (!Text_ValidateAll(the_screen, x1, y1, 0, 0))
{
LOG_ERR(("%s %d: illegal screen id, coordinate, or color", __func__, __LINE__));
return false;
}
if (!Text_ValidateXY(the_screen, x2, y2))
{
LOG_ERR(("%s %d: illegal coordinate", __func__, __LINE__));
return false;
}
if (x1 > x2 || y1 > y2)
{
LOG_ERR(("%s %d: illegal coordinates", __func__, __LINE__));
return false;
}
// add 1 to line len, because desired behavior is a box that connects fully to the passed coords
dx = x2 - x1 + 1;
dy = y2 - y1 + 1;
// calculate attribute value from passed fore and back colors
// LOGIC: text mode only supports 16 colors. lower 4 bits are back, upper 4 bits are foreground
the_attribute_value = ((fore_color << 4) | back_color);
return Text_FillMemoryBox(the_screen, x1, y1, dx, dy, SCREEN_FOR_TEXT_ATTR, the_attribute_value);
}
//! Invert the colors of a rectangular block.
//! As this requires sampling each character cell, it is no faster (per cell) to do for entire screen as opposed to a subset box
//! @return returns false on any error/invalid input.
boolean Text_InvertBox(Screen* the_screen, signed int x1, signed int y1, signed int x2, signed int y2)
{
unsigned char the_attribute_value;
unsigned char the_inversed_value;
char* the_write_loc;
signed int the_col;
signed int skip_len;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
if (!Text_ValidateAll(the_screen, x1, y1, 0, 0))
{
LOG_ERR(("%s %d: illegal screen id, coordinate, or color", __func__, __LINE__));
return false;
}
if (!Text_ValidateXY(the_screen, x2, y2))
{
LOG_ERR(("%s %d: illegal coordinate", __func__, __LINE__));
return false;
}
if (x1 > x2 || y1 > y2)
{
LOG_ERR(("%s %d: illegal coordinates", __func__, __LINE__));
return false;
}
// get initial read/write loc
the_write_loc = Text_GetMemLocForXY(the_screen, x1, y1, SCREEN_FOR_TEXT_ATTR);
// amount of cells to skip past once we have written the specified line len
skip_len = the_screen->text_mem_cols_ - (x2 - x1) - 1;
for (; y1 <= y2; y1++)
{
for (the_col = x1; the_col <= x2; the_col++)
{
the_attribute_value = (unsigned char)*the_write_loc;
// LOGIC: text mode only supports 16 colors. lower 4 bits are back, upper 4 bits are foreground
the_inversed_value = (((the_attribute_value & 0x0F) << 4) | ((the_attribute_value & 0xF0) >> 4));
*the_write_loc++ = the_inversed_value;
}
the_write_loc += skip_len;
}
return true;
}
// **** FONT RELATED *****
//! replace the current font data with the data at the passed memory buffer
//! @param new_font_data: Pointer to 2K (256 characters x 8 lines/bytes each) of font data. Each byte represents one line of an 8x8 font glyph.
//! @return returns false on any error/invalid input.
boolean Text_UpdateFontData(Screen* the_screen, char* new_font_data)
{
memcpy(the_screen->text_font_ram_, new_font_data, 8*256*1);
return true;
}
//! Test function to display all 256 font characters.
//! Characters are rendered in 8 rows of 32 characters.
//! @param y: the starting row to draw the font characters.
//! @return returns false on any error/invalid input.
boolean Text_ShowFontChars(Screen* the_screen, unsigned int y)
{
unsigned char the_char = 0;
char* the_write_loc;
signed short i;
signed short j;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
the_write_loc = the_screen->text_ram_ + (the_screen->text_mem_cols_ * y);
// print rows of 32 characters at a time
for (j = 0; j < 8; j++)
{
for (i = 0; i < 32; i++)
{
*the_write_loc++ = the_char++;
}
the_write_loc += the_screen->text_mem_cols_ - i;
}
return true;
}
// **** Set char/attr functions *****
//! Set a char at a specified x, y coord
//! @return returns false on any error/invalid input.
boolean Text_SetCharAtXY(Screen* the_screen, signed int x, signed int y, unsigned char the_char)
{
char* the_write_loc;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
if (!Text_ValidateAll(the_screen, x, y, 0, 0))
{
LOG_ERR(("%s %d: illegal screen id or coordinate", __func__, __LINE__));
return false;
}
the_write_loc = Text_GetMemLocForXY(the_screen, x, y, SCREEN_FOR_TEXT_CHAR);
*the_write_loc = the_char;
return true;
}
//! Set the attribute value at a specified x, y coord
//! @param fore_color: Index to the desired foreground color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param back_color: Index to the desired background color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @return returns false on any error/invalid input.
boolean Text_SetAttrAtXY(Screen* the_screen, signed int x, signed int y, unsigned char fore_color, unsigned char back_color)
{
char* the_write_loc;
unsigned char the_attribute_value;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
if (!Text_ValidateAll(the_screen, x, y, fore_color, back_color))
{
LOG_ERR(("%s %d: illegal screen id, coordinate, or color", __func__, __LINE__));
return false;
}
// calculate attribute value from passed fore and back colors
// LOGIC: text mode only supports 16 colors. lower 4 bits are back, upper 4 bits are foreground
the_attribute_value = ((fore_color << 4) | back_color);
the_write_loc = Text_GetMemLocForXY(the_screen, x, y, SCREEN_FOR_TEXT_ATTR);
*the_write_loc = the_attribute_value;
return true;
}
//! Draw a char at a specified x, y coord, also setting the color attributes
//! @param fore_color: Index to the desired foreground color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param back_color: Index to the desired background color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @return returns false on any error/invalid input.
boolean Text_SetCharAndColorAtXY(Screen* the_screen, signed int x, signed int y, unsigned char the_char, unsigned char fore_color, unsigned char back_color)
{
char* the_write_loc;
unsigned char the_attribute_value;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
if (!Text_ValidateAll(the_screen, x, y, fore_color, back_color))
{
LOG_ERR(("%s %d: illegal screen id, coordinate, or color", __func__, __LINE__));
return false;
}
// calculate attribute value from passed fore and back colors
// LOGIC: text mode only supports 16 colors. lower 4 bits are back, upper 4 bits are foreground
the_attribute_value = ((fore_color << 4) | back_color);
// write character memory
the_write_loc = Text_GetMemLocForXY(the_screen, x, y, SCREEN_FOR_TEXT_CHAR);
*the_write_loc = the_char;
// write attribute memory (reuse same calc, just add attr ram delta)
the_write_loc += the_screen->text_attr_ram_ - the_screen->text_ram_;
*the_write_loc = the_attribute_value;
return true;
}
// **** Get char/attr functions *****
//! Get the char at a specified x, y coord
//! @return returns a character code
unsigned char Text_GetCharAtXY(Screen* the_screen, signed int x, signed int y)
{
char* the_read_loc;
unsigned char the_char;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
if (!Text_ValidateAll(the_screen, x, y, 0, 0))
{
LOG_ERR(("%s %d: illegal screen id or coordinate", __func__, __LINE__));
return false;
}
the_read_loc = Text_GetMemLocForXY(the_screen, x, y, SCREEN_FOR_TEXT_CHAR);
the_char = (unsigned char)*the_read_loc;
return the_char;
}
//! Get the attribute value at a specified x, y coord
//! @return returns a 1-byte attribute code (foreground in high nibble, background in low nibble)
unsigned char Text_GetAttrAtXY(Screen* the_screen, signed int x, signed int y)
{
char* the_read_loc;
unsigned char the_value;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
if (!Text_ValidateAll(the_screen, x, y, 0, 0))
{
LOG_ERR(("%s %d: illegal screen id or coordinate", __func__, __LINE__));
return false;
}
the_read_loc = Text_GetMemLocForXY(the_screen, x, y, SCREEN_FOR_TEXT_ATTR);
the_value = (unsigned char)*the_read_loc;
return the_value;
}
//! Get the foreground color at a specified x, y coord
//! @return returns an index (0-15) to the foreground color LUT
unsigned char Text_GetForeColorAtXY(Screen* the_screen, signed int x, signed int y)
{
char* the_read_loc;
unsigned char the_value;
unsigned char the_color;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
if (!Text_ValidateAll(the_screen, x, y, 0, 0))
{
LOG_ERR(("%s %d: illegal screen id or coordinate", __func__, __LINE__));
return false;
}
the_read_loc = Text_GetMemLocForXY(the_screen, x, y, SCREEN_FOR_TEXT_ATTR);
the_value = (unsigned char)*the_read_loc;
the_color = (the_value & 0xF0) >> 4;
return the_color;
}
//! Get the background color at a specified x, y coord
//! @return returns an index (0-15) to the background color LUT
unsigned char Text_GetBackColorAtXY(Screen* the_screen, signed int x, signed int y)
{
char* the_read_loc;
unsigned char the_value;
unsigned char the_color;
if (the_screen == NULL)
{
LOG_ERR(("%s %d: passed screen was NULL", __func__, __LINE__));
return false;
}
if (!Text_ValidateAll(the_screen, x, y, 0, 0))
{