-
Notifications
You must be signed in to change notification settings - Fork 3
/
GraphicOps.h
1391 lines (1215 loc) · 48.7 KB
/
GraphicOps.h
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
//----------------------------------------------------------------------------------------------------------------------
// Extended graphics routines
// Copyright (c) 2024 Samuel Gomes
//----------------------------------------------------------------------------------------------------------------------
#pragma once
// #define TOOLBOX64_DEBUG 1
#include "Debug.h"
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#ifndef INC_COMMON_CPP
// This stuff is from QB64-PE common.h and is only here for debugging purposes
struct img_struct
{
void *lock_offset;
int64_t lock_id;
uint8_t valid; // 0,1 0=invalid
uint8_t text; // if set, surface is a text surface
uint8_t console; // dummy surface to absorb unimplemented console functionality
uint16_t width, height;
uint8_t bytes_per_pixel; // 1,2,4
uint8_t bits_per_pixel; // 1,2,4,8,16(text),32
uint32_t mask; // 1,3,0xF,0xFF,0xFFFF,0xFFFFFFFF
uint16_t compatible_mode; // 0,1,2,7,8,9,10,11,12,13,32,256
uint32_t color, background_color, draw_color;
uint32_t font; // 8,14,16,?
int16_t top_row, bottom_row; // VIEW PRINT settings, unique (as in QB) to each "page"
int16_t cursor_x, cursor_y; // unique (as in QB) to each "page"
uint8_t cursor_show, cursor_firstvalue, cursor_lastvalue;
union
{
uint8_t *offset;
uint32_t *offset32;
};
uint32_t flags;
uint32_t *pal;
int32_t transparent_color; //-1 means no color is transparent
uint8_t alpha_disabled;
uint8_t holding_cursor;
uint8_t print_mode;
// BEGIN apm ('active page migration')
// everything between apm points is migrated during active page changes
// note: apm data is only relevant to graphics modes
uint8_t apm_p1;
int32_t view_x1, view_y1, view_x2, view_y2;
int32_t view_offset_x, view_offset_y;
float x, y;
uint8_t clipping_or_scaling;
float scaling_x, scaling_y, scaling_offset_x, scaling_offset_y;
float window_x1, window_y1, window_x2, window_y2;
double draw_ta;
double draw_scale;
uint8_t apm_p2;
// END apm
};
extern uint8_t image_get_bgra_red(uint32_t c);
extern uint8_t image_get_bgra_green(uint32_t c);
extern uint8_t image_get_bgra_blue(uint32_t c);
extern uint8_t image_get_bgra_alpha(uint32_t c);
extern uint32_t image_get_bgra_bgr(uint32_t c);
extern uint32_t image_set_bgra_alpha(uint32_t c, uint8_t a = 0xFFu);
extern uint32_t image_make_bgra(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 0xFFu);
extern uint32_t image_swap_red_blue(uint32_t clr);
extern uint8_t image_clamp_color_component(int n);
extern float image_calculate_rgb_distance(uint8_t r1, uint8_t g1, uint8_t b1, uint8_t r2, uint8_t g2, uint8_t b2);
extern uint32_t image_get_color_delta(uint8_t r1, uint8_t g1, uint8_t b1, uint8_t r2, uint8_t g2, uint8_t b2);
extern uint32_t func__rgb32(int32_t r, int32_t g, int32_t b, int32_t a);
extern uint32_t func__rgb32(int32_t r, int32_t g, int32_t b);
extern uint32_t func__rgb32(int32_t i, int32_t a);
extern uint32_t func__rgb32(int32_t i);
extern uint32_t func__rgba32(int32_t r, int32_t g, int32_t b, int32_t a);
extern int32_t func__alpha32(uint32_t col);
extern int32_t func__red32(uint32_t col);
extern int32_t func__green32(uint32_t col);
extern int32_t func__blue32(uint32_t col);
#else
struct img_struct;
#endif
// These are QB64-PE internal structures
extern img_struct *write_page;
extern img_struct *img;
extern const int32_t *page;
extern const int32_t nextimg;
extern const uint8_t charset8x8[256][8][8];
extern const uint8_t charset8x16[256][16][8];
// These are QB64-PE internal functions
extern void pset_and_clip(int32_t x, int32_t y, uint32_t color);
extern void fast_boxfill(int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color);
extern void validatepage(int32_t n);
/// @brief This is a function pointer type that we'll use to plot "pixels" on graphics as well and "text" surfaces
typedef void (*Graphics_SetPixelFunction)(int32_t x, int32_t y, uint32_t clrAtr);
/// @brief We'll use this internally so that we do not have the overhead of calling _Graphics_SetSetPixelFunction() for every pixel
static Graphics_SetPixelFunction _Graphics_SetPixelInternal = nullptr;
/// @brief This is used to plot a text "pixel" on a "text" surface. The pixel is clipped if it is outside bounds
/// @param x The x position
/// @param y The y position
/// @param clrAtr A combination of the ASCII character and the text color attributes
inline static void _Graphics_SetTextPixelClipped(int32_t x, int32_t y, uint32_t clrAtr)
{
if (x >= 0 and x < write_page->width and y >= 0 and y < write_page->height)
{
*(reinterpret_cast<uint16_t *>(write_page->offset) + write_page->width * y + x) = (uint16_t)clrAtr;
}
}
/// @brief This selects the correct "SetPixel" function for later rendering
inline static void _Graphics_SelectSetPixelFunction()
{
_Graphics_SetPixelInternal = write_page->text ? _Graphics_SetTextPixelClipped : pset_and_clip;
}
/// @brief Public library function for plotting pixels on text and graphic surfaces. This will clip out-of-bounds pixels
/// @param x The x position
/// @param y The y position
/// @param clrAtr A color index for index graphics surfaces or a text color attribute for text surfaces or a 32-bit RGBA color
inline void Graphics_DrawPixel(int32_t x, int32_t y, uint32_t clrAtr)
{
_Graphics_SelectSetPixelFunction();
_Graphics_SetPixelInternal(x, y, clrAtr);
}
/// @brief Makes a character + text attribute pair for text mode images
/// @param character An ASCII character
/// @param fColor The foreground color (0 - 15)
/// @param bColor The background color (0 - 15)
/// @return A 16-bit text + color attribute pair
inline constexpr uint16_t Graphics_MakeTextColorAttribute(uint8_t character, uint8_t fColor, uint8_t bColor)
{
return (uint16_t)character | ((((bColor > 7) << 7) | (bColor << 4) | (fColor & 0x0F)) << 8);
}
/// @brief Makes a character + text attribute pair for text mode images using the _DEFAULTCOLOR and _BACKGROUNDCOLOR
/// @param character An ASCII character
/// @return A 16-bit text + color attribute pair
inline uint16_t Graphics_MakeDefaultTextColorAttribute(uint8_t character)
{
return (uint16_t)character | ((((write_page->color > 15) << 7) | ((write_page->background_color & 0xFF) << 4) | (write_page->color & 0x0F)) << 8);
}
/// @brief Sets the foreground color for the current destination image
/// @param fColor The foreground color (0 - 15 for text mode)
inline void Graphics_SetForegroundColor(uint32_t fColor)
{
write_page->color = write_page->text ? fColor & 0x0F : fColor;
}
/// @brief Gets the foreground color for the current destination image
/// @return The foreground color (0 - 15 for text mode)
inline uint32_t Graphics_GetForegroundColor()
{
return write_page->text ? write_page->color & 0x0F : write_page->color;
}
/// @brief Sets the background color for the current destination image
/// @param bColor The background color (0 - 15 for text mode)
inline void Graphics_SetBackgroundColor(uint32_t bColor)
{
if (write_page->text)
{
write_page->background_color = bColor & 0x07;
write_page->color = write_page->color | ((bColor > 7) << 4);
}
else
{
write_page->background_color = bColor;
}
}
/// @brief Gets the background color for the current destination image
/// @return The background color (0 - 15 for text mode)
inline uint32_t Graphics_GetBackgroundColor()
{
return write_page->text ? (write_page->background_color & 0x07) | ((write_page->color > 15) << 3) : write_page->background_color;
}
/// @brief Draws a horizontal line (works in both text and graphics modes)
/// @param lx Left x position
/// @param ty Top y position
/// @param rx Right x position
/// @param c A color index for index graphics surfaces or a text color attribute for text surfaces or a 32-bit RGBA color
void Graphics_DrawHorizontalLine(int32_t lx, int32_t ty, int32_t rx, uint32_t clrAtr)
{
// Ensure the starting and ending coordinates are ordered correctly
if (lx > rx)
std::swap(lx, rx);
// Ensure ty is within the image height
if (ty < 0 || ty >= write_page->height || lx >= write_page->width || rx < 0)
return; // Line is completely outside the image
// Clip the line to the image boundaries
if (lx < 0)
lx = 0;
if (rx >= write_page->width)
rx = write_page->width - 1;
TOOLBOX64_DEBUG_PRINT("Drawing line segment: (%i, %i) - (%i, %i)", lx, ty, rx, ty);
if (write_page->text)
{
// Get pointer to the starting "pixel"
auto buffer = (reinterpret_cast<uint16_t *>(write_page->offset) + write_page->width * ty + lx);
// Draw the complete line
std::fill(buffer, buffer + (1 + rx - lx), clrAtr);
}
else
{
// Use a different method for non-text mode
fast_boxfill(lx, ty, rx, ty, clrAtr);
}
}
/// @brief Draws a vertical line (works in both text and graphics modes)
/// @param lx Left x position
/// @param ty Top y position
/// @param by Bottom y position
/// @param c A color index for index graphics surfaces or a text color attribute for text surfaces or a 32-bit RGBA color
void Graphics_DrawVerticalLine(int32_t lx, int32_t ty, int32_t by, uint32_t clrAtr)
{
// Ensure the starting and ending coordinates are ordered correctly
if (ty > by)
std::swap(ty, by);
// Ensure lx is within the image width
if (lx < 0 || lx >= write_page->width || ty >= write_page->height || by < 0)
return; // Line is completely outside the image
// Clip the line to the image boundaries
if (ty < 0)
ty = 0;
if (by >= write_page->height)
by = write_page->height - 1;
TOOLBOX64_DEBUG_PRINT("Drawing line segment: (%i, %i) - (%i, %i)", lx, ty, lx, by);
if (write_page->text)
{
// Get pointer to the starting "pixel"
auto buffer = (reinterpret_cast<uint16_t *>(write_page->offset) + write_page->width * ty + lx);
// Draw the "pixels" and then step by image width to get to the next pixel
for (auto y = ty; y <= by; y++)
{
*buffer = clrAtr;
buffer += write_page->width;
}
}
else
{
// Use a different method for non-text mode
fast_boxfill(lx, ty, lx, by, clrAtr);
}
}
/// @brief Draws a rectangle (works in both text and graphics modes)
/// @param lx Left x position
/// @param ty Top y position
/// @param rx Right x position
/// @param by Bottom y position
/// @param clrAtr A color index for index graphics surfaces or a text color attribute for text surfaces or a 32-bit RGBA color
void Graphics_DrawRectangle(int32_t lx, int32_t ty, int32_t rx, int32_t by, uint32_t clrAtr)
{
// Draw the top and bottom sides. No need to re-order; Graphics_DrawHorizontalLine() will do that
Graphics_DrawHorizontalLine(lx, ty, rx, clrAtr);
Graphics_DrawHorizontalLine(lx, by, rx, clrAtr);
// Ensure the starting and ending coordinates are ordered correctly
if (ty > by)
std::swap(ty, by);
// Avoid re-drawing corners
++ty;
--by;
// Draw the left and right sides
if (by >= ty)
{
Graphics_DrawVerticalLine(lx, ty, by, clrAtr);
Graphics_DrawVerticalLine(rx, ty, by, clrAtr);
}
}
/// @brief Draws a filled rectangle (works in both text and graphics modes)
/// @param lx Left x position
/// @param ty Top y position
/// @param rx Right x position
/// @param by Bottom y position
/// @param clrAtr A color index for index graphics surfaces or a text color attribute for text surfaces or a 32-bit RGBA color
void Graphics_DrawFilledRectangle(int32_t lx, int32_t ty, int32_t rx, int32_t by, uint32_t clrAtr)
{
// Ensure the starting and ending coordinates are ordered correctly
if (lx > rx)
std::swap(lx, rx);
if (ty > by)
std::swap(ty, by);
// Leave if rectangle is completely outside the image
if (lx >= write_page->width || ty >= write_page->height || rx < 0 || by < 0)
return;
// Clip rectangle to image
if (lx < 0)
lx = 0;
if (rx >= write_page->width)
rx = write_page->width - 1;
if (ty < 0)
ty = 0;
if (by >= write_page->height)
by = write_page->height - 1;
TOOLBOX64_DEBUG_PRINT("Drawing filled rectangle: (%i, %i) - (%i, %i)", lx, ty, rx, ty);
if (write_page->text)
{
// Get pointer to the starting "pixel"
auto buffer = (reinterpret_cast<uint16_t *>(write_page->offset) + write_page->width * ty + lx);
// Draw one complete line
auto rectWidth = 1 + rx - lx;
std::fill(buffer, buffer + rectWidth, clrAtr);
// Copy the remaining lines
rectWidth <<= 1; // Since we are dealing with 2 byte attributes
auto dest = buffer + write_page->width; // Get the pointer to the next line
for (auto y = ty; y < by; y++) // "y < by" since we already rendered the first line using std::fill
{
memcpy(dest, buffer, rectWidth); // Copy the first line
dest += write_page->width; // Move to the next line
}
}
else
{
// Use a different method for non-text mode
fast_boxfill(lx, ty, rx, by, clrAtr);
}
}
/// @brief This is an internal line drawing routine. This does not draw the last pixel and hence can be used to make multi-line shapes
/// @param x1 Starting position x
/// @param y1 Starting position y
/// @param x2 Ending position x
/// @param y2 Ending position y
/// @param clrAtr A color index for index graphics surfaces or a text color attribute for text surfaces or a 32-bit RGBA color
static inline void _Graphics_DrawLineInternal(int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t clrAtr)
{
bool isVerticalLonger = false;
int32_t shortDistance = y2 - y1;
int32_t longDistance = x2 - x1;
if (abs(shortDistance) > abs(longDistance))
{
std::swap(shortDistance, longDistance);
isVerticalLonger = true;
}
int32_t increment, endDistance = longDistance;
if (longDistance < 0)
{
increment = -1;
longDistance = -longDistance;
}
else
{
increment = 1;
}
int32_t deltaIncrement;
if (longDistance == 0)
{
deltaIncrement = 0;
}
else
{
deltaIncrement = (shortDistance << 16) / longDistance;
}
int32_t j = 0;
if (isVerticalLonger)
{
for (int32_t i = 0; i != endDistance; i += increment)
{
_Graphics_SetPixelInternal(x1 + (j >> 16), y1 + i, clrAtr);
j += deltaIncrement;
}
}
else
{
for (int32_t i = 0; i != endDistance; i += increment)
{
_Graphics_SetPixelInternal(x1 + i, y1 + (j >> 16), clrAtr);
j += deltaIncrement;
}
}
}
/// @brief Draws a line from x1, y1 to x2, y2 (works in both text and graphics modes)
/// @param x1 Starting position x
/// @param y1 Starting position y
/// @param x2 Ending position x
/// @param y2 Ending position y
/// @param clrAtr A color index for index graphics surfaces or a text color attribute for text surfaces or a 32-bit RGBA color
void Graphics_DrawLine(int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t clrAtr)
{
// Check if both endpoints of the line are outside the image bounds
if ((x1 < 0 && x2 < 0) || (x1 >= write_page->width && x2 >= write_page->width) || (y1 < 0 && y2 < 0) || (y1 >= write_page->height && y2 >= write_page->height))
return; // Line is completely outside the image
// Select the correct pixel drawing routine just once
_Graphics_SelectSetPixelFunction();
// Call the internal line-drawing routine. This will use whatever pixel drawing routine was selected
_Graphics_DrawLineInternal(x1, y1, x2, y2, clrAtr);
// Plot the ending pixel
_Graphics_SetPixelInternal(x2, y2, clrAtr);
}
/// @brief Draws a circle (works in both text and graphics modes)
/// @param x The circles center x position
/// @param y The circles center y position
/// @param radius The radius of the circle
/// @param clrAtr A color index for index graphics surfaces or a text color attribute for text surfaces or a 32-bit RGBA color
void Graphics_DrawCircle(int32_t x, int32_t y, int32_t radius, uint32_t clrAtr)
{
// Clip the circle completely if bounding box is completely off-image
if (x + radius < 0 || x - radius >= write_page->width || y + radius < 0 || y - radius >= write_page->height)
return;
_Graphics_SelectSetPixelFunction();
// Special case: draw a single pixel if the radius is <= zero
if (radius <= 0)
{
_Graphics_SetPixelInternal(x, y, clrAtr);
return;
}
int32_t p = 1 - radius, cx = 0, cy = radius, px, py;
do
{
// Calculate the eight symmetric points and set the pixels
px = x + cx;
py = y + cy;
_Graphics_SetPixelInternal(px, py, clrAtr);
px = x - cx;
_Graphics_SetPixelInternal(px, py, clrAtr);
px = x + cx;
py = y - cy;
_Graphics_SetPixelInternal(px, py, clrAtr);
px = x - cx;
_Graphics_SetPixelInternal(px, py, clrAtr);
px = x + cy;
py = y + cx;
_Graphics_SetPixelInternal(px, py, clrAtr);
py = y - cx;
_Graphics_SetPixelInternal(px, py, clrAtr);
px = x - cy;
py = y + cx;
_Graphics_SetPixelInternal(px, py, clrAtr);
py = y - cx;
_Graphics_SetPixelInternal(px, py, clrAtr);
++cx;
if (p < 0)
{
p += ((cx << 1) + 1);
}
else
{
cy--;
p += (((cx - cy) << 1) + 1);
}
} while (cx <= cy);
}
/// @brief Draws a filled circle (works in both text and graphics modes)
/// @param x The circles center x position
/// @param y The circles center y position
/// @param radius The radius of the circle
/// @param clrAtr A color index for index graphics surfaces or a text color attribute for text surfaces or a 32-bit RGBA color
void Graphics_DrawFilledCircle(int32_t x, int32_t y, int32_t radius, uint32_t clrAtr)
{
// Clip the circle completely if bounding box is completely off-image
if (x + radius < 0 || x - radius >= write_page->width || y + radius < 0 || y - radius >= write_page->height)
return;
// Special case: draw a single pixel if the radius is < zero
if (radius <= 0)
{
Graphics_DrawPixel(x, y, clrAtr);
return;
}
// Initialize the coordinates and error term
auto px = radius, py = 0, radiusError = -radius;
// Draw the central horizontal line
Graphics_DrawFilledRectangle(x - px, y, x + px, y, clrAtr);
while (px > py)
{
// Update the error term
radiusError += (py << 1) + 1;
if (radiusError >= 0)
{
if (px != py + 1)
{
// Draw horizontal lines at the top and bottom of the circle
Graphics_DrawFilledRectangle(x - py, y - px, x + py, y - px, clrAtr);
Graphics_DrawFilledRectangle(x - py, y + px, x + py, y + px, clrAtr);
}
// Decrease the x coordinate and update the error term
--px;
radiusError -= px << 1;
}
// Increase the y coordinate
++py;
// Draw horizontal lines at the top and bottom of the circle
Graphics_DrawFilledRectangle(x - px, y - py, x + px, y - py, clrAtr);
Graphics_DrawFilledRectangle(x - px, y + py, x + px, y + py, clrAtr);
}
}
/// @brief Draws an ellipse (works in both text and graphics modes)
/// @param x The circles center x position
/// @param y The circles center y position
/// @param rx The horizontal radius
/// @param ry The vertical radius
/// @param clrAtr A color index for index graphics surfaces or a text color attribute for text surfaces or a 32-bit RGBA color
void Graphics_DrawEllipse(int32_t x, int32_t y, int32_t rx, int32_t ry, uint32_t clrAtr)
{
// Calculate the bounding box
auto left = x - rx;
auto right = x + rx;
auto top = y - ry;
auto bottom = y + ry;
// Clip the ellipse completely if bounding box is completely off-image
if (right < 0 || left >= write_page->width || bottom < 0 || top >= write_page->height)
return;
_Graphics_SelectSetPixelFunction();
// Special case: draw a single pixel if both rx and ry are <= zero
if (rx <= 0 && ry <= 0)
{
_Graphics_SetPixelInternal(x, y, clrAtr);
return;
}
// Special case for rx = 0: draw a vline
if (rx == 0)
{
Graphics_DrawVerticalLine(x, top, bottom, clrAtr);
return;
}
// Special case for ry = 0: draw a hline
if (ry == 0)
{
Graphics_DrawHorizontalLine(left, y, right, clrAtr);
return;
}
int32_t ix, iy;
int32_t h, i, j, k;
h = i = j = k = 0xFFFF;
if (rx > ry)
{
ix = 0;
iy = rx << 6;
do
{
int32_t oh = h;
int32_t oi = i;
int32_t oj = j;
int32_t ok = k;
h = (ix + 32) >> 6;
i = (iy + 32) >> 6;
j = (h * ry) / rx;
k = (i * ry) / rx;
if ((h != oh || k != ok) && (h < oi))
{
_Graphics_SetPixelInternal(x + h, y + k, clrAtr);
_Graphics_SetPixelInternal(x - h, y + k, clrAtr);
_Graphics_SetPixelInternal(x + h, y - k, clrAtr);
_Graphics_SetPixelInternal(x - h, y - k, clrAtr);
}
if ((i != oi || j != oj) && (h < i))
{
_Graphics_SetPixelInternal(x + i, y + j, clrAtr);
_Graphics_SetPixelInternal(x - i, y + j, clrAtr);
_Graphics_SetPixelInternal(x + i, y - j, clrAtr);
_Graphics_SetPixelInternal(x - i, y - j, clrAtr);
}
ix = ix + (iy / rx);
iy = iy - (ix / rx);
} while (i > h);
}
else
{
ix = 0;
iy = ry << 6;
do
{
int32_t oh = h;
int32_t oi = i;
int32_t oj = j;
int32_t ok = k;
h = (ix + 32) >> 6;
i = (iy + 32) >> 6;
j = (h * rx) / ry;
k = (i * rx) / ry;
if ((j != oj || i != oi) && (h < i))
{
_Graphics_SetPixelInternal(x + j, y + i, clrAtr);
_Graphics_SetPixelInternal(x - j, y + i, clrAtr);
_Graphics_SetPixelInternal(x + j, y - i, clrAtr);
_Graphics_SetPixelInternal(x - j, y - i, clrAtr);
}
if ((k != ok || h != oh) && (h < oi))
{
_Graphics_SetPixelInternal(x + k, y + h, clrAtr);
_Graphics_SetPixelInternal(x - k, y + h, clrAtr);
_Graphics_SetPixelInternal(x + k, y - h, clrAtr);
_Graphics_SetPixelInternal(x - k, y - h, clrAtr);
}
ix = ix + (iy / ry);
iy = iy - (ix / ry);
} while (i > h);
}
}
/// @brief Draws a filled ellipse (works in both text and graphics modes)
/// @param cx The circles center x position
/// @param cy The circles center y position
/// @param rx The horizontal radius
/// @param ry The vertical radius
/// @param clrAtr A color index for index graphics surfaces or a text color attribute for text surfaces or a 32-bit RGBA color
void Graphics_DrawFilledEllipse(int32_t cx, int32_t cy, int32_t rx, int32_t ry, uint32_t clrAtr)
{
// Calculate the bounding box
auto left = cx - rx;
auto right = cx + rx;
auto top = cy - ry;
auto bottom = cy + ry;
// Clip the ellipse completely if bounding box is outside the image bounds
if (right < 0 || left >= write_page->width || bottom < 0 || top >= write_page->height)
return;
// Special case if both rx and ry are <= zero: draw a single pixel
if (rx <= 0 && ry <= 0)
{
Graphics_DrawPixel(cx, cy, clrAtr);
return;
}
// Special case for rx = 0: draw a vline
if (rx == 0)
{
Graphics_DrawVerticalLine(cx, top, bottom, clrAtr);
return;
}
// Special case for ry = 0: draw a hline
if (ry == 0)
{
Graphics_DrawHorizontalLine(left, cy, right, clrAtr);
return;
}
int32_t a = 2 * rx * rx;
int32_t b = 2 * ry * ry;
int32_t x = rx;
int32_t y = 0;
int32_t xx = ry * ry * (1 - rx - rx);
int32_t yy = rx * rx;
int32_t sx = b * rx;
int32_t sy = 0;
int32_t e = 0;
while (sx >= sy)
{
Graphics_DrawHorizontalLine(cx - x, cy - y, cx + x, clrAtr);
if (y != 0)
Graphics_DrawHorizontalLine(cx - x, cy + y, cx + x, clrAtr);
y = y + 1;
sy = sy + a;
e = e + yy;
yy = yy + a;
if ((e + e + xx) > 0)
{
x = x - 1;
sx = sx - b;
e = e + xx;
xx = xx + b;
}
}
x = 0;
y = ry;
xx = rx * ry;
yy = rx * rx * (1 - ry - ry);
e = 0;
sx = 0;
sy = a * ry;
while (sx <= sy)
{
Graphics_DrawHorizontalLine(cx - x, cy - y, cx + x, clrAtr);
Graphics_DrawHorizontalLine(cx - x, cy + y, cx + x, clrAtr);
do
{
x = x + 1;
sx = sx + b;
e = e + xx;
xx = xx + b;
} while ((e + e + yy) <= 0);
y = y - 1;
sy = sy - a;
e = e + yy;
yy = yy + a;
}
}
/// @brief Draws a triangle outline (works in both text and graphics modes)
/// @param x1 Vertex 1 x
/// @param y1 Vertex 1 y
/// @param x2 Vertex 2 x
/// @param y2 Vertex 2 y
/// @param x3 Vertex 3 x
/// @param y3 Vertex 3 y
/// @param clrAtr A color index for index graphics surfaces or a text color attribute for text surfaces or a 32-bit RGBA color
void Graphics_DrawTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, uint32_t clrAtr)
{
// Sort vertices by their y-coordinates
if (y1 > y2)
{
std::swap(x1, x2);
std::swap(y1, y2);
}
if (y1 > y3)
{
std::swap(x1, x3);
std::swap(y1, y3);
}
if (y2 > y3)
{
std::swap(x2, x3);
std::swap(y2, y3);
}
// Check if the entire triangle is outside the image bounds
if (x3 < 0 || x1 >= write_page->width || y3 < 0 || y1 >= write_page->height)
return; // The triangle is completely outside the image
// Select the correct pixel drawing routine just once
_Graphics_SelectSetPixelFunction();
// Now draw the 3 sides. Since we are using the internal line drawing function, this will not re-draw the vertices
_Graphics_DrawLineInternal(x1, y1, x2, y2, clrAtr);
_Graphics_DrawLineInternal(x2, y2, x3, y3, clrAtr);
_Graphics_DrawLineInternal(x3, y3, x1, y1, clrAtr);
}
/// @brief Draws a filled triangle (works in both text and graphics modes)
/// @param x1 Vertex 1 x
/// @param y1 Vertex 1 y
/// @param x2 Vertex 2 x
/// @param y2 Vertex 2 y
/// @param x3 Vertex 3 x
/// @param y3 Vertex 3 y
/// @param clrAtr A color index for index graphics surfaces or a text color attribute for text surfaces or a 32-bit RGBA color
void Graphics_DrawFilledTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, uint32_t clrAtr)
{
// Sort vertices by their y-coordinates
if (y1 > y2)
{
std::swap(x1, x2);
std::swap(y1, y2);
}
if (y1 > y3)
{
std::swap(x1, x3);
std::swap(y1, y3);
}
if (y2 > y3)
{
std::swap(x2, x3);
std::swap(y2, y3);
}
// Check if the entire triangle is outside the image bounds
if (x3 < 0 || x1 >= write_page->width || y3 < 0 || y1 >= write_page->height)
{
return; // The triangle is completely outside the image
}
// Calculate slopes of the two lines
float invSlope1 = (float)(x2 - x1) / (y2 - y1);
float invSlope2 = (float)(x3 - x1) / (y3 - y1);
float curX1 = x1;
float curX2 = x1;
// Fill the top part of the triangle
for (int32_t scanlineY = y1; scanlineY <= y2; scanlineY++)
{
Graphics_DrawHorizontalLine(curX1, scanlineY, curX2, clrAtr);
curX1 += invSlope1;
curX2 += invSlope2;
}
// Calculate the new slope for the bottom part of the clipped triangle
invSlope1 = (float)(x3 - x2) / (y3 - y2);
curX1 = x2;
// Fill the bottom part of the triangle
for (int32_t scanlineY = y2 + 1; scanlineY <= y3; scanlineY++)
{
Graphics_DrawHorizontalLine(curX1, scanlineY, curX2, clrAtr);
curX1 += invSlope1;
curX2 += invSlope2;
}
}
/// @brief Makes a RGBA color from RGBA components
/// @param r Red (0 - 255)
/// @param g Green (0 - 255)
/// @param b Blue (0 - 255)
/// @param a Alpha (0 - 255)
/// @return Returns an RGBA color
inline constexpr uint32_t Graphics_MakeRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
return ((static_cast<uint32_t>(a) << 24) | (static_cast<uint32_t>(b) << 16) | (static_cast<uint32_t>(g) << 8) | static_cast<uint32_t>(r));
}
/// @brief Returns the Red component
/// @param rgba An RGBA color
/// @return Red
inline constexpr uint8_t Graphics_GetRedFromRGBA(uint32_t rgba)
{
return static_cast<uint8_t>(rgba);
}
/// @brief Returns the Green component
/// @param rgba An RGBA color
/// @return Green
inline constexpr uint8_t Graphics_GetGreenFromRGBA(uint32_t rgba)
{
return static_cast<uint8_t>(rgba >> 8);
}
/// @brief Returns the Blue component
/// @param rgba An RGBA color
/// @return Blue
inline constexpr uint8_t Graphics_GetBlueFromRGBA(uint32_t rgba)
{
return static_cast<uint8_t>(rgba >> 16);
}
/// @brief Returns the Alpha value.
/// @param rgba An RGBA color.
/// @return Alpha.
inline constexpr uint8_t Graphics_GetAlphaFromRGBA(uint32_t rgba)
{
return static_cast<uint8_t>(rgba >> 24);
}
/// @brief Interpolates between two colors.
/// @param colorA The first color.
/// @param colorB The second color.
/// @param factor The interpolation factor.
/// @return The interpolated color.
inline constexpr auto Graphics_InterpolateColor(uint32_t colorA, uint32_t colorB, float factor)
{
auto a = func__alpha32(colorA);
auto r = func__red32(colorA);
auto g = func__green32(colorA);
auto b = func__blue32(colorA);
return func__rgba32(r + int32_t((func__red32(colorB) - r) * factor), g + int32_t((func__green32(colorB) - g) * factor), b + int32_t((func__blue32(colorB) - b) * factor), a + int32_t((func__alpha32(colorB) - a) * factor));
}
/// @brief Calculates the distance between two colors.
/// @param colorA The first color.
/// @param colorB The second color.
/// @return The distance between the two colors.
inline auto Graphics_GetRGBDistance(uint32_t colorA, uint32_t colorB)
{
return image_calculate_rgb_distance(image_get_bgra_red(colorA), image_get_bgra_green(colorA), image_get_bgra_blue(colorA), image_get_bgra_red(colorB), image_get_bgra_green(colorB), image_get_bgra_blue(colorB));
}
/// @brief Calculates the delta between two colors.
/// @param colorA The first color.
/// @param colorB The second color.
/// @return The delta between the two colors.
inline auto Graphics_GetRGBDelta(uint32_t colorA, uint32_t colorB)
{
return image_get_color_delta(image_get_bgra_red(colorA), image_get_bgra_green(colorA), image_get_bgra_blue(colorA), image_get_bgra_red(colorB), image_get_bgra_green(colorB), image_get_bgra_blue(colorB));
}
/// @brief Set the text image's transparent "color" that will be used by Graphics_PutTextImage()
/// @param imageHandle A valid text image handle
/// @param clrAtr A text color attribute for text surfaces. See Graphics_MakeTextColorAttribute()
void Graphics_SetTextImageClearColor(int32_t imageHandle, uint32_t clrAtr)
{
img_struct *textImage;
// Validate image handle
if (imageHandle >= 0)
{
validatepage(imageHandle);
textImage = &img[page[imageHandle]];
}
else
{
imageHandle = -imageHandle;
if (imageHandle >= nextimg)
{
error(QB_ERROR_INVALID_HANDLE);
return;
}
textImage = &img[imageHandle];
if (!textImage->valid)
{
error(QB_ERROR_INVALID_HANDLE);
return;
}
}
// Check if the this is a text mode handle
if (!textImage->text)
{
error(QB_ERROR_INVALID_HANDLE);
return;
}
textImage->transparent_color = clrAtr;
}
/// @brief Blits a text image on another text / graphics image (works only with text source image; for graphics source use _PUTIMAGE)
/// @param imageHandle A valid text image handle
/// @param x The x position on _DEST
/// @param y The y position on _DEST
/// @param lx [OPTIONAL] The left side to start in imageHandle
/// @param ty [OPTIONAL] The top side to start in imageHandle
/// @param rx [OPTIONAL] The right side to start in imageHandle
/// @param by [OPTIONAL] The bottom side to start in imageHandle
void Graphics_PutTextImage(int32_t imageHandle, int32_t x, int32_t y, int32_t lx = -1, int32_t ty = -1, int32_t rx = -1, int32_t by = -1)
{
img_struct *textImage;
// Validate image handle
if (imageHandle >= 0)
{
validatepage(imageHandle);
textImage = &img[page[imageHandle]];
}
else
{
imageHandle = -imageHandle;
if (imageHandle >= nextimg)