raylib 6.0.0-rc.2

Safe Rust bindings for Raylib.
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
//! Immediate-mode drawing surfaces and RAII mode guards.
//!
//! All drawing in raylib-rs follows an immediate-mode model: you call [`RaylibHandle::begin_drawing`]
//! to receive a [`RaylibDrawHandle`] guard, issue draw calls on it, then let the guard drop —
//! which automatically calls `EndDrawing` (presenting the frame and timing). Types that can serve
//! as a drawing surface implement the [`RaylibDraw`] trait.
//!
//! Additional mode guards nest inside a draw frame:
//! - [`RaylibTextureMode`] — off-screen rendering via `begin_texture_mode` / `EndTextureMode`.
//! - [`RaylibMode2D`] — 2D camera transform via `begin_mode2D` / `EndMode2D`.
//! - [`RaylibMode3D`] — 3D camera via `begin_mode3D` / `EndMode3D`.
//! - [`RaylibVRMode`] — VR stereo rendering via `begin_vr_stereo_mode`.
//! - [`RaylibShaderMode`] — custom shader pass via `begin_shader_mode`.
//! - [`RaylibScissorMode`] — scissor-rectangle clipping via `begin_scissor_mode`.
//!
//! Each guard also implements [`RaylibDraw`] (and [`RaylibDraw3D`] where applicable), so you can
//! chain modes by calling `begin_mode3D` on a `RaylibDrawHandle`, for example.

use raylib_sys::Rectangle;

use crate::core::texture::Texture2D;
use crate::core::vr::VrStereoConfig;
use crate::core::{RaylibHandle, RaylibThread};
use crate::ffi;
use crate::math::Matrix;
use crate::math::Vector2;
use crate::math::Vector3;
use crate::models::WeakMaterial;
use std::ffi::CString;
use std::{convert::AsRef, marker::PhantomData};

use super::shaders::Shader;

/// Seems like all draw commands must be issued from the main thread
impl RaylibHandle {
    #[inline]
    #[must_use]
    /// Setup canvas (framebuffer) to start drawing.
    /// Prefer using the closure version, [RaylibHandle::draw]. This version returns a handle that calls [raylib_sys::EndDrawing] at the end of the scope and is provided as a fallback incase you run into issues with closures(such as lifetime or performance reasons)
    pub fn begin_drawing<'a>(&'a mut self, _: &RaylibThread) -> RaylibDrawHandle<'a> {
        unsafe {
            ffi::BeginDrawing();
        };

        let d = RaylibDrawHandle(self);
        d
    }
    /// Setup canvas (framebuffer) to start drawing.
    // Every FnMut is a FnOnce, but not every FnOnce is a FnMut. The closure may possibly execute multiple times throughout the program, but not multiple times in a single call to this method.
    // Taking a FnOnce instead of a FnMut when the function only needs to be called once in this method makes the method slightly more versatile/less needlessly restrictive for no actual cost.
    pub fn draw<'a>(&'a mut self, _: &RaylibThread, func: impl FnOnce(RaylibDrawHandle<'a>)) {
        unsafe {
            ffi::BeginDrawing();
        };
        func(RaylibDrawHandle(self));
        // Uncomment the following if RaylibDrawHandle has been changed to no longer call EndDrawing() in its drop implementation:
        // unsafe {
        //     ffi::EndDrawing();
        // }
    }
}

/// RAII draw handle returned by [`RaylibHandle::begin_drawing`] — calls `EndDrawing` on drop.
///
/// Holds an exclusive borrow of the [`RaylibHandle`] for the duration of a frame. Drop runs
/// raylib's `EndDrawing`, which flushes the queued GPU commands, swaps buffers, presents the
/// frame, and enforces the configured target frame rate. All [`RaylibDraw`] methods and the
/// `begin_*` mode entries (texture / 2D / 3D / VR / shader / blend / scissor) hang off this
/// guard via [`Deref`](std::ops::Deref) to `RaylibHandle` and the extension traits.
///
/// # Examples
///
/// ```no_run
/// use raylib::prelude::*;
///
/// let (mut rl, thread) = raylib::init().size(800, 600).title("demo").build();
/// while !rl.window_should_close() {
///     let mut d = rl.begin_drawing(&thread);
///     d.clear_background(Color::RAYWHITE);
///     d.draw_text("hello", 10, 10, 20, Color::BLACK);
///     // `d` drops here → EndDrawing is called automatically.
/// }
/// ```
///
/// # See also
///
/// - [`RaylibHandle::begin_drawing`] — constructor for this guard.
/// - [`RaylibHandle::draw`] — closure-form alternative that scopes the guard for you.
/// - [`RaylibDraw`] — drawing methods available on the guard.
/// - [`RaylibTextureMode`], [`RaylibMode2D`], [`RaylibMode3D`] — nested mode guards.
pub struct RaylibDrawHandle<'a>(&'a mut RaylibHandle);

impl RaylibDrawHandle<'_> {
    #[deprecated = "Calling begin_drawing within RaylibDrawHandle will result in a runtime error."]
    #[doc(hidden)]
    pub fn begin_drawing(&'_ mut self, _: &RaylibThread) -> RaylibDrawHandle<'_> {
        panic!("Nested begin_drawing call")
    }
    #[deprecated = "Calling draw within RaylibDrawHandle will result in a runtime error."]
    #[doc(hidden)]
    pub fn draw(&mut self, _: &RaylibThread, mut _func: impl FnMut(RaylibDrawHandle)) {
        panic!("Nested draw call")
    }
}

impl Drop for RaylibDrawHandle<'_> {
    fn drop(&mut self) {
        unsafe {
            ffi::EndDrawing();
        }
    }
}

impl std::ops::Deref for RaylibDrawHandle<'_> {
    type Target = RaylibHandle;

    fn deref(&self) -> &Self::Target {
        self.0
    }
}

impl std::ops::DerefMut for RaylibDrawHandle<'_> {
    fn deref_mut(&mut self) -> &mut RaylibHandle {
        self.0
    }
}
impl RaylibDraw for RaylibDrawHandle<'_> {}

// Texture2D Stuff

// The texture does not need to be held exclusively for the duration of the *previous* draw mode, nor does the *previous* draw mode need to outlive the texture reference.
// The texture exclusivity and previous draw mode only need to outlive the *current* draw mode. So they can (and should) have separate lifetimes.
//
// Additionally: the texture is not actually *used* by this wrapper after construction, only the previous draw mode.
// Some space can be saved by using a phantom instead of copying the actual reference.
// The PhantomData will ensure that the borrow checker still analyzes as though the mutable texture reference was held, without physically storing it in the runtime memory.
/// RAII draw handle for rendering to a `RenderTexture2D` — calls `EndTextureMode` on drop.
///
/// Drop runs raylib's `EndTextureMode`, unbinding the render-texture's framebuffer and
/// restoring the default framebuffer as the active draw target. Mutates the underlying
/// `RenderTexture2D`'s color attachment in place; the texture remains usable for sampling
/// (e.g. via [`RaylibDraw::draw_texture`]) once the guard drops.
///
/// Construct via [`RaylibTextureModeExt::begin_texture_mode`] on a [`RaylibDrawHandle`] or
/// directly on a [`RaylibHandle`]. The guard implements [`RaylibDraw`], so all 2D drawing
/// methods are available through it.
///
/// # Examples
///
/// ```rust
/// # #[cfg(feature = "software_renderer")] {
/// use raylib::prelude::*;
/// use raylib::test_harness::*;
///
/// with_headless(64, 64, |rl, thread| {
///     let mut target = rl
///         .load_render_texture(thread, 32, 32)
///         .expect("render texture");
///     let img = render_frame(rl, thread, |d| {
///         d.clear_background(Color::WHITE);
///         {
///             let mut t = d.begin_texture_mode(thread, &mut target);
///             t.clear_background(Color::BLUE);
///             t.draw_rectangle(0, 0, 32, 32, Color::RED);
///         }
///         d.draw_texture(target.texture(), 0, 0, Color::WHITE);
///     });
///     // Off-screen content blitted into the default framebuffer.
///     assert_pixel(&img, 5, 5, Color::RED, 0);
///     // Pixels outside the blitted region remain the WHITE clear.
///     assert_pixel(&img, 50, 50, Color::WHITE, 0);
/// });
/// # }
/// ```
///
/// # See also
///
/// - [`RaylibTextureModeExt::begin_texture_mode`] — constructor for this guard.
/// - [`RaylibTextureModeExt::draw_texture_mode`] — closure-form alternative.
/// - [`RaylibDraw`] — drawing methods available on this guard.
pub struct RaylibTextureMode<'a, 'b, T: 'a>(&'a mut T, PhantomData<&'b mut ffi::RenderTexture2D>);

impl<'a, T: 'a> Drop for RaylibTextureMode<'a, '_, T> {
    fn drop(&mut self) {
        unsafe { ffi::EndTextureMode() }
    }
}
impl<'a, T: 'a> std::ops::Deref for RaylibTextureMode<'a, '_, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.0
    }
}
impl<'a, T: 'a> std::ops::DerefMut for RaylibTextureMode<'a, '_, T> {
    fn deref_mut(&mut self) -> &mut T {
        self.0
    }
}
// framebuffer: &'a mut ffi::RenderTexture2D,

/// Extension trait providing render-texture mode entry on draw handles.
///
/// Implemented for [`RaylibDrawHandle`] (so you can begin a texture-mode pass inside a
/// regular draw frame) and for [`RaylibHandle`] itself (off-screen renders outside an
/// active draw frame are also valid). The trait's two methods are alternative forms of the
/// same operation: [`begin_texture_mode`](Self::begin_texture_mode) returns a RAII guard,
/// while [`draw_texture_mode`](Self::draw_texture_mode) takes a closure and ends the mode
/// when the closure returns.
///
/// # Examples
///
/// ```no_run
/// use raylib::prelude::*;
///
/// let (mut rl, thread) = raylib::init().size(800, 600).title("demo").build();
/// let mut target = rl
///     .load_render_texture(&thread, 256, 256)
///     .expect("render texture");
/// while !rl.window_should_close() {
///     let mut d = rl.begin_drawing(&thread);
///     d.draw_texture_mode(&thread, &mut target, |mut t| {
///         t.clear_background(Color::BLUE);
///         t.draw_circle(128, 128, 64.0, Color::YELLOW);
///     });
///     d.clear_background(Color::RAYWHITE);
///     d.draw_texture(target.texture(), 0, 0, Color::WHITE);
/// }
/// ```
///
/// # See also
///
/// - [`RaylibTextureMode`] — RAII guard returned by `begin_texture_mode`.
pub trait RaylibTextureModeExt
where
    Self: Sized,
{
    /// Begin drawing to render texture.
    /// Prefer using the closure version, [RaylibTextureModeExt::draw_texture_mode] . This version returns a handle that calls [raylib_sys::EndTextureMode] at the end of the scope and is provided as a fallback incase you run into issues with closures(such as lifetime or performance reasons)
    #[inline]
    #[must_use]
    fn begin_texture_mode<'a, 'b>(
        &'a mut self,
        _: &RaylibThread,
        framebuffer: &'b mut ffi::RenderTexture2D,
    ) -> RaylibTextureMode<'a, 'b, Self> {
        unsafe { ffi::BeginTextureMode(*framebuffer) }
        RaylibTextureMode(self, PhantomData)
    }

    /// Begin drawing to render texture.
    fn draw_texture_mode<'a, 'b>(
        &'a mut self,
        _: &RaylibThread,
        framebuffer: &'b mut ffi::RenderTexture2D,
        func: impl FnOnce(RaylibTextureMode<'a, 'b, Self>),
    ) {
        unsafe { ffi::BeginTextureMode(*framebuffer) }
        func(RaylibTextureMode(self, PhantomData));
        // Uncomment the following if RaylibTextureMode has been changed to no longer call EndTextureMode() in its drop implementation:
        // unsafe { ffi::EndTextureMode(); }
    }
}

// Only the DrawHandle and the RaylibHandle can start a texture
impl RaylibTextureModeExt for RaylibDrawHandle<'_> {}
impl RaylibTextureModeExt for RaylibHandle {}
impl<'a, T: 'a> RaylibDraw for RaylibTextureMode<'a, '_, T> {}

// VR Stuff

// Lifetime 'a is duplicatively stored in a PhantomData so that the borrow checker knows T is being held *exclusively* for the lifetime of the mode, without giving the false impression that it can actually be mutated by the library.
/// RAII draw handle for VR stereo rendering — calls `EndVrStereoMode` on drop.
///
/// While the guard is alive, raylib renders each draw call twice (once per eye) using the
/// stereo projection / view matrices in the borrowed [`VrStereoConfig`]. Drop runs
/// `EndVrStereoMode`, restoring the standard mono projection. The guard implements
/// [`RaylibDraw`] via deref, so all 2D / 3D draw calls issued during the scope are stereo.
///
/// Requires a real or simulator VR backend; not exercised by the headless software
/// renderer.
///
/// # Examples
///
/// ```no_run
/// use raylib::prelude::*;
///
/// let (mut rl, thread) = raylib::init().size(800, 600).title("vr-demo").build();
/// let device = VrDeviceInfo {
///     h_resolution: 2160,
///     v_resolution: 1200,
///     h_screen_size: 0.133793,
///     v_screen_size: 0.0669,
///     eye_to_screen_distance: 0.041,
///     lens_separation_distance: 0.07,
///     interpupillary_distance: 0.07,
///     lens_distortion_values: [1.0, 0.22, 0.24, 0.0],
///     chroma_ab_correction: [0.996, -0.004, 1.014, 0.0],
/// };
/// let mut config = rl.load_vr_stereo_config(&thread, device);
/// while !rl.window_should_close() {
///     let mut d = rl.begin_drawing(&thread);
///     d.clear_background(Color::RAYWHITE);
///     {
///         let mut v = d.begin_vr_stereo_mode(&thread, &mut config);
///         v.draw_rectangle(10, 10, 100, 50, Color::RED);
///         // Drop ends stereo mode automatically.
///     }
/// }
/// ```
///
/// # See also
///
/// - [`RaylibVRModeExt::begin_vr_stereo_mode`] — constructor for this guard.
/// - [`VrStereoConfig`] — stereo projection configuration the guard borrows.
pub struct RaylibVRMode<'a, 'b, T: 'a>(
    &'a T,
    PhantomData<&'a mut T>,
    PhantomData<&'b mut VrStereoConfig>,
);
impl<'a, T: 'a> Drop for RaylibVRMode<'a, '_, T> {
    fn drop(&mut self) {
        unsafe { ffi::EndVrStereoMode() }
    }
}
impl<'a, T: 'a> std::ops::Deref for RaylibVRMode<'a, '_, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.0
    }
}

/// Extension trait providing VR stereo mode entry on draw handles.
///
/// Implemented for every [`RaylibDraw`] surface so a VR stereo pass can begin inside any
/// active draw frame. The two methods are alternative forms of the same operation:
/// [`begin_vr_stereo_mode`](Self::begin_vr_stereo_mode) returns a RAII guard, while
/// [`draw_vr_stereo_mode`](Self::draw_vr_stereo_mode) takes a closure and ends the mode
/// automatically when the closure returns.
///
/// # Examples
///
/// Closure form (preferred when the scope is small enough to fit in one block):
///
/// ```no_run
/// use raylib::prelude::*;
///
/// let (mut rl, thread) = raylib::init().size(800, 600).title("vr-demo").build();
/// # let device = VrDeviceInfo {
/// #     h_resolution: 2160, v_resolution: 1200,
/// #     h_screen_size: 0.133793, v_screen_size: 0.0669,
/// #     eye_to_screen_distance: 0.041, lens_separation_distance: 0.07,
/// #     interpupillary_distance: 0.07,
/// #     lens_distortion_values: [1.0, 0.22, 0.24, 0.0],
/// #     chroma_ab_correction: [0.996, -0.004, 1.014, 0.0],
/// # };
/// let mut config = rl.load_vr_stereo_config(&thread, device);
/// while !rl.window_should_close() {
///     let mut d = rl.begin_drawing(&thread);
///     d.draw_vr_stereo_mode(&mut config, |mut v| {
///         v.clear_background(Color::RAYWHITE);
///         v.draw_rectangle(10, 10, 100, 50, Color::RED);
///     });
/// }
/// ```
///
/// See [`RaylibVRMode`] for the equivalent guard-form example.
///
/// # See also
///
/// - [`RaylibVRMode`] — RAII guard returned by `begin_vr_stereo_mode`.
/// - [`VrStereoConfig`] — stereo projection / lens configuration consumed by both methods.
pub trait RaylibVRModeExt
where
    Self: Sized,
{
    /// Begin stereo rendering (requires VR simulator).
    /// Prefer using the closure version, [RaylibVRModeExt::draw_vr_stereo_mode] . This version returns a handle that calls [raylib_sys::EndVrStereoMode] at the end of the scope and is provided as a fallback incase you run into issues with closures(such as lifetime or performance reasons)
    #[inline]
    #[must_use]
    fn begin_vr_stereo_mode<'a, 'b>(
        &'a mut self,
        _: &RaylibThread,
        vr_config: &'b mut VrStereoConfig,
    ) -> RaylibVRMode<'a, 'b, Self> {
        unsafe { ffi::BeginVrStereoMode(*vr_config.as_ref()) }
        RaylibVRMode(self, PhantomData, PhantomData)
    }

    /// Begin stereo rendering (requires VR simulator).
    fn draw_vr_stereo_mode<'a, 'b>(
        &'a mut self,
        vr_config: &'b mut VrStereoConfig,
        func: impl FnOnce(RaylibVRMode<'a, 'b, Self>),
    ) {
        unsafe { ffi::BeginVrStereoMode(*vr_config.as_ref()) }
        func(RaylibVRMode(self, PhantomData, PhantomData));
        // Uncomment the following if RaylibVRMode has been changed to no longer call EndTextureMode() in its drop implementation:
        // unsafe { ffi::EndVrStereoMode(); }
    }
}

impl<D: RaylibDraw> RaylibVRModeExt for D {}
impl<'a, T: 'a> RaylibDraw for RaylibVRMode<'a, '_, T> {}

// 2D Mode

/// RAII draw handle for 2D camera mode — calls `EndMode2D` on drop.
///
/// While alive, all 2D draw calls are transformed by the camera's `offset`, `target`,
/// `rotation`, and `zoom`. Drop runs `EndMode2D`, restoring the screen-space identity
/// transform — subsequent draws on the parent guard render in raw screen coordinates again.
/// Implements [`RaylibDraw`] via deref, so the full 2D drawing surface is available
/// through the camera.
///
/// # Examples
///
/// ```rust
/// # #[cfg(feature = "software_renderer")] {
/// use raylib::prelude::*;
/// use raylib::test_harness::*;
///
/// with_headless(64, 64, |rl, thread| {
///     let camera = Camera2D {
///         offset: Vector2::new(0.0, 0.0),
///         target: Vector2::new(10.0, 10.0), // shifts world origin
///         rotation: 0.0,
///         zoom: 1.0,
///     };
///     let img = render_frame(rl, thread, |d| {
///         d.clear_background(Color::WHITE);
///         let mut c = d.begin_mode2D(camera);
///         // World rect at (10, 10) → screen (0, 0) after the camera translation.
///         c.draw_rectangle(10, 10, 20, 20, Color::RED);
///     });
///     assert_pixel(&img, 5, 5, Color::RED, 0);
///     // Outside the translated rect → still WHITE.
///     assert_pixel(&img, 50, 50, Color::WHITE, 0);
/// });
/// # }
/// ```
///
/// # See also
///
/// - [`RaylibMode2DExt::begin_mode2D`] — constructor for this guard.
/// - [`Camera2D`](crate::core::camera::Camera2D) — the camera passed to `begin_mode2D`.
/// - [`RaylibMode3D`] — sibling guard for 3D camera projections.
pub struct RaylibMode2D<'a, T: 'a>(&'a mut T);
impl<'a, T: 'a> Drop for RaylibMode2D<'a, T> {
    fn drop(&mut self) {
        unsafe { ffi::EndMode2D() }
    }
}
impl<'a, T: 'a> std::ops::Deref for RaylibMode2D<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.0
    }
}
impl<'a, T: 'a> std::ops::DerefMut for RaylibMode2D<'a, T> {
    fn deref_mut(&mut self) -> &mut T {
        self.0
    }
}

/// Extension trait providing 2D camera mode entry on draw handles.
///
/// Implemented for every [`RaylibDraw`] surface (so 2D mode can begin inside texture-mode,
/// scissor-mode, blend-mode, etc.). The two methods are alternative forms:
/// [`begin_mode2D`](Self::begin_mode2D) returns a RAII guard;
/// [`draw_mode2D`](Self::draw_mode2D) takes a closure and ends 2D mode automatically when
/// the closure returns.
///
/// # Examples
///
/// Closure form:
///
/// ```no_run
/// use raylib::prelude::*;
///
/// let (mut rl, thread) = raylib::init().size(800, 600).title("demo").build();
/// let camera = Camera2D {
///     offset: Vector2::new(400.0, 300.0),
///     target: Vector2::zero(),
///     rotation: 0.0,
///     zoom: 2.0,
/// };
/// while !rl.window_should_close() {
///     let mut d = rl.begin_drawing(&thread);
///     d.clear_background(Color::RAYWHITE);
///     d.draw_mode2D(camera, |mut c| {
///         c.draw_rectangle(-10, -10, 20, 20, Color::RED);
///     });
/// }
/// ```
///
/// See [`RaylibMode2D`] for the equivalent guard-form example.
///
/// # See also
///
/// - [`RaylibMode2D`] — RAII guard returned by `begin_mode2D`.
/// - [`Camera2D`](crate::core::camera::Camera2D) — camera passed into both methods.
pub trait RaylibMode2DExt
where
    Self: Sized,
{
    /// Begin 2D mode with custom camera (2D).
    /// Prefer using the closure version, [RaylibMode2DExt::draw_mode2D]. This version returns a handle that calls [raylib_sys::EndMode2D] at the end of the scope and is provided as a fallback incase you run into issues with closures(such as lifetime or performance reasons)
    #[allow(non_snake_case)]
    #[inline]
    #[must_use]
    fn begin_mode2D(&mut self, camera: impl Into<ffi::Camera2D>) -> RaylibMode2D<'_, Self> {
        unsafe {
            ffi::BeginMode2D(camera.into());
        }
        RaylibMode2D(self)
    }

    /// Begin 2D mode with custom camera (2D).
    #[allow(non_snake_case)]
    fn draw_mode2D<'a>(
        &'a mut self,
        camera: impl Into<ffi::Camera2D>,
        func: impl FnOnce(RaylibMode2D<'a, Self>),
    ) {
        unsafe {
            ffi::BeginMode2D(camera.into());
        }
        func(RaylibMode2D(self));
        // Uncomment the following if RaylibMode2D has been changed to no longer call EndMode2D() in its drop implementation:
        // unsafe {
        //     ffi::EndMode2D();
        // }
    }
}

impl<D: RaylibDraw> RaylibMode2DExt for D {}
impl<'a, T: 'a> RaylibDraw for RaylibMode2D<'a, T> {}

// 3D Mode

/// RAII draw handle for 3D camera mode — calls `EndMode3D` on drop.
///
/// While alive, draw calls use the [`Camera3D`](crate::core::camera::Camera3D)'s view + projection matrices, so 3D draw
/// methods on [`RaylibDraw3D`] (`draw_cube`, `draw_sphere`, `draw_line_3D`, etc.) render in
/// world space. Drop runs `EndMode3D`, restoring the 2D screen-space transform. Implements
/// both [`RaylibDraw`] and [`RaylibDraw3D`] via deref.
///
/// # Examples
///
/// ```no_run
/// use raylib::prelude::*;
///
/// let (mut rl, thread) = raylib::init().size(800, 600).title("demo").build();
/// let camera = Camera3D::perspective(
///     Vector3::new(4.0, 4.0, 4.0),
///     Vector3::new(0.0, 0.0, 0.0),
///     Vector3::new(0.0, 1.0, 0.0),
///     45.0,
/// );
/// while !rl.window_should_close() {
///     let mut d = rl.begin_drawing(&thread);
///     d.clear_background(Color::RAYWHITE);
///     {
///         let mut c = d.begin_mode3D(camera);
///         c.draw_cube(Vector3::zero(), 2.0, 2.0, 2.0, Color::RED);
///         c.draw_grid(10, 1.0);
///         // Drop ends 3D mode automatically.
///     }
///     d.draw_text("hello 3D", 10, 10, 20, Color::BLACK);
/// }
/// ```
///
/// # See also
///
/// - [`RaylibMode3DExt::begin_mode3D`] — constructor for this guard.
/// - [`Camera3D`](crate::core::camera::Camera3D) — the camera passed to `begin_mode3D`.
/// - [`RaylibDraw3D`] — 3D-only draw methods available through this guard.
pub struct RaylibMode3D<'a, T: 'a>(&'a mut T);
impl<'a, T: 'a> Drop for RaylibMode3D<'a, T> {
    fn drop(&mut self) {
        unsafe { ffi::EndMode3D() }
    }
}
impl<'a, T: 'a> std::ops::Deref for RaylibMode3D<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.0
    }
}
impl<'a, T: 'a> std::ops::DerefMut for RaylibMode3D<'a, T> {
    fn deref_mut(&mut self) -> &mut T {
        self.0
    }
}

/// Extension trait providing 3D camera mode entry on draw handles.
///
/// Implemented for every [`RaylibDraw`] surface. The two methods are alternative forms:
/// [`begin_mode3D`](Self::begin_mode3D) returns a RAII guard;
/// [`draw_mode3D`](Self::draw_mode3D) takes a closure and ends 3D mode automatically when
/// the closure returns.
///
/// # Examples
///
/// Closure form:
///
/// ```no_run
/// use raylib::prelude::*;
///
/// let (mut rl, thread) = raylib::init().size(800, 600).title("demo").build();
/// let camera = Camera3D::perspective(
///     Vector3::new(4.0, 4.0, 4.0),
///     Vector3::new(0.0, 0.0, 0.0),
///     Vector3::new(0.0, 1.0, 0.0),
///     45.0,
/// );
/// while !rl.window_should_close() {
///     let mut d = rl.begin_drawing(&thread);
///     d.clear_background(Color::RAYWHITE);
///     d.draw_mode3D(camera, |mut c| {
///         c.draw_cube(Vector3::zero(), 2.0, 2.0, 2.0, Color::RED);
///         c.draw_grid(10, 1.0);
///     });
/// }
/// ```
///
/// See [`RaylibMode3D`] for the equivalent guard-form example.
///
/// # See also
///
/// - [`RaylibMode3D`] — RAII guard returned by `begin_mode3D`.
/// - [`Camera3D`](crate::core::camera::Camera3D) — camera passed into both methods.
/// - [`RaylibDraw3D`] — 3D-only draw methods exposed by the guard.
pub trait RaylibMode3DExt
where
    Self: Sized,
{
    /// Begin 3D mode with custom camera (3D).
    /// Prefer using the closure version, [RaylibMode3DExt::draw_mode3D]. This version returns a handle that calls [raylib_sys::EndMode3D] at the end of the scope and is provided as a fallback incase you run into issues with closures(such as lifetime or performance reasons)
    #[allow(non_snake_case)]
    #[must_use]
    #[inline]
    fn begin_mode3D(&mut self, camera: impl Into<ffi::Camera3D>) -> RaylibMode3D<'_, Self> {
        unsafe {
            ffi::BeginMode3D(camera.into());
        }
        RaylibMode3D(self)
    }

    /// Begin 3D mode with custom camera (3D).
    #[allow(non_snake_case)]
    fn draw_mode3D<'a>(
        &'a mut self,
        camera: impl Into<ffi::Camera3D>,
        func: impl FnOnce(RaylibMode3D<'a, Self>),
    ) {
        unsafe {
            ffi::BeginMode3D(camera.into());
        }
        func(RaylibMode3D(self));
        // Uncomment the following if RaylibMode3D has been changed to no longer call EndMode3D() in its drop implementation:
        // unsafe {
        //     ffi::EndMode3D();
        // }
    }
}

impl<D: RaylibDraw> RaylibMode3DExt for D {}
impl<'a, T: 'a> RaylibDraw for RaylibMode3D<'a, T> {}
impl<'a, T: 'a> RaylibDraw3D for RaylibMode3D<'a, T> {}

// shader Mode

/// RAII draw handle for custom shader mode — calls `EndShaderMode` on drop.
///
/// While alive, the borrowed [`Shader`] is bound as the active fragment / vertex program for
/// subsequent draw calls. Drop runs `EndShaderMode`, restoring raylib's default shader.
/// Implements [`RaylibDraw`] and [`RaylibDraw3D`] via deref, so the guard accepts both 2D
/// and 3D draw calls — the shader applies to whichever the parent surface supports.
///
/// # Examples
///
/// ```no_run
/// use raylib::prelude::*;
///
/// let (mut rl, thread) = raylib::init().size(800, 600).title("demo").build();
/// let mut shader = rl.load_shader(&thread, None, Some("grayscale.fs"));
/// while !rl.window_should_close() {
///     let mut d = rl.begin_drawing(&thread);
///     d.clear_background(Color::RAYWHITE);
///     {
///         let mut s = d.begin_shader_mode(&mut shader);
///         s.draw_rectangle(10, 10, 100, 50, Color::WHITE);
///         // Drop ends shader mode automatically.
///     }
/// }
/// ```
///
/// # See also
///
/// - [`RaylibShaderModeExt::begin_shader_mode`] — constructor for this guard.
/// - [`Shader`] — the shader the guard borrows.
pub struct RaylibShaderMode<'a, 'b, T: 'a>(&'a mut T, PhantomData<&'b mut Shader>);

impl<'a, T: 'a> Drop for RaylibShaderMode<'a, '_, T> {
    fn drop(&mut self) {
        unsafe { ffi::EndShaderMode() }
    }
}
impl<'a, T: 'a> std::ops::Deref for RaylibShaderMode<'a, '_, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.0
    }
}
impl<'a, T: 'a> std::ops::DerefMut for RaylibShaderMode<'a, '_, T> {
    fn deref_mut(&mut self) -> &mut T {
        self.0
    }
}

/// Extension trait providing custom shader mode entry on draw handles.
///
/// Implemented for every [`RaylibDraw`] surface. The two methods are alternative forms:
/// [`begin_shader_mode`](Self::begin_shader_mode) returns a RAII guard;
/// [`draw_shader_mode`](Self::draw_shader_mode) takes a closure and ends shader mode
/// automatically when the closure returns.
///
/// # Examples
///
/// Closure form:
///
/// ```no_run
/// use raylib::prelude::*;
///
/// let (mut rl, thread) = raylib::init().size(800, 600).title("demo").build();
/// let mut shader = rl.load_shader(&thread, None, Some("grayscale.fs"));
/// while !rl.window_should_close() {
///     let mut d = rl.begin_drawing(&thread);
///     d.clear_background(Color::RAYWHITE);
///     d.draw_shader_mode(&mut shader, |mut s| {
///         s.draw_rectangle(10, 10, 100, 50, Color::WHITE);
///     });
/// }
/// ```
///
/// See [`RaylibShaderMode`] for the equivalent guard-form example.
///
/// # See also
///
/// - [`RaylibShaderMode`] — RAII guard returned by `begin_shader_mode`.
/// - [`Shader`] — the shader bound for the duration of the mode.
pub trait RaylibShaderModeExt
where
    Self: Sized,
{
    /// Begin custom shader drawing.
    /// Prefer using the closure version, [RaylibShaderModeExt::draw_shader_mode]. This version returns a handle that calls [raylib_sys::EndShaderMode] at the end of the scope and is provided as a fallback incase you run into issues with closures(such as lifetime or performance reasons)
    #[must_use]
    #[inline]
    fn begin_shader_mode<'a, 'b>(
        &'a mut self,
        shader: &'b mut Shader,
    ) -> RaylibShaderMode<'a, 'b, Self> {
        unsafe { ffi::BeginShaderMode(*shader.as_ref()) }
        RaylibShaderMode(self, PhantomData)
    }

    /// Begin custom shader drawing.
    fn draw_shader_mode<'a, 'b>(
        &'a mut self,
        shader: &'b mut Shader,
        func: impl FnOnce(RaylibShaderMode<'a, 'b, Self>),
    ) {
        unsafe { ffi::BeginShaderMode(*shader.as_ref()) }
        func(RaylibShaderMode(self, PhantomData));
        // Uncomment the following if RaylibShaderMode has been changed to no longer call EndShaderMode() in its drop implementation:
        // unsafe { ffi::EndShaderMode(); }
    }
}

impl<D: RaylibDraw> RaylibShaderModeExt for D {}
impl<'a, T: 'a> RaylibDraw for RaylibShaderMode<'a, '_, T> {}
impl<'a, T: 'a> RaylibDraw3D for RaylibShaderMode<'a, '_, T> {}

// Blend Mode

/// RAII draw handle for a blend mode scope — calls `EndBlendMode` on drop.
///
/// While alive, the active GPU blend equation is the one specified in the constructor
/// (`BLEND_ALPHA`, `BLEND_ADDITIVE`, `BLEND_MULTIPLIED`, `BLEND_ADD_COLORS`,
/// `BLEND_SUBTRACT_COLORS`, `BLEND_ALPHA_PREMULTIPLY`, or `BLEND_CUSTOM`). Drop runs
/// `EndBlendMode`, restoring the previous blend mode. Implements [`RaylibDraw`] and
/// [`RaylibDraw3D`] via deref.
///
/// # Examples
///
/// ```rust
/// # #[cfg(feature = "software_renderer")] {
/// use raylib::prelude::*;
/// use raylib::test_harness::*;
///
/// with_headless(64, 64, |rl, thread| {
///     let img = render_frame(rl, thread, |d| {
///         d.clear_background(Color::WHITE);
///         // Half-alpha RED over WHITE: BLEND_ALPHA mixes to roughly (255, 127, 127).
///         let mut b = d.begin_blend_mode(BlendMode::BLEND_ALPHA);
///         b.draw_rectangle(0, 0, 32, 32, Color::new(255, 0, 0, 128));
///     });
///     // Blended pixel: roughly half-RED over WHITE. Tolerance accommodates rounding.
///     assert_pixel(&img, 5, 5, Color::new(255, 127, 127, 255), 2);
///     // Outside the rectangle: still WHITE.
///     assert_pixel(&img, 50, 50, Color::WHITE, 0);
/// });
/// # }
/// ```
///
/// # See also
///
/// - [`RaylibBlendModeExt::begin_blend_mode`] — constructor for this guard.
/// - [`BlendMode`](crate::consts::BlendMode) — supported blend equations.
pub struct RaylibBlendMode<'a, T: 'a>(&'a mut T);
impl<'a, T: 'a> Drop for RaylibBlendMode<'a, T> {
    fn drop(&mut self) {
        unsafe { ffi::EndBlendMode() }
    }
}
impl<'a, T: 'a> std::ops::Deref for RaylibBlendMode<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.0
    }
}
impl<'a, T: 'a> std::ops::DerefMut for RaylibBlendMode<'a, T> {
    fn deref_mut(&mut self) -> &mut T {
        self.0
    }
}

/// Extension trait providing blend mode entry on draw handles.
///
/// Implemented for every [`RaylibDraw`] surface. The two methods are alternative forms:
/// [`begin_blend_mode`](Self::begin_blend_mode) returns a RAII guard;
/// [`draw_blend_mode`](Self::draw_blend_mode) takes a closure and ends the blend scope
/// automatically when the closure returns.
///
/// # Examples
///
/// Closure form:
///
/// ```no_run
/// use raylib::prelude::*;
///
/// let (mut rl, thread) = raylib::init().size(800, 600).title("demo").build();
/// while !rl.window_should_close() {
///     let mut d = rl.begin_drawing(&thread);
///     d.clear_background(Color::BLACK);
///     d.draw_blend_mode(BlendMode::BLEND_ADDITIVE, |mut b| {
///         // Additive blending: subsequent draws add to the framebuffer.
///         b.draw_circle(200, 200, 80.0, Color::new(64, 0, 0, 255));
///         b.draw_circle(240, 200, 80.0, Color::new(0, 64, 0, 255));
///     });
/// }
/// ```
///
/// See [`RaylibBlendMode`] for the equivalent guard-form example.
///
/// # See also
///
/// - [`RaylibBlendMode`] — RAII guard returned by `begin_blend_mode`.
/// - [`BlendMode`](crate::consts::BlendMode) — supported blend equations.
pub trait RaylibBlendModeExt
where
    Self: Sized,
{
    /// Begin blending mode (alpha, additive, multiplied, subtract, custom).
    /// Prefer using the closure version, [RaylibBlendModeExt::draw_blend_mode]. This version returns a handle that calls [raylib_sys::EndBlendMode] at the end of the scope and is provided as a fallback incase you run into issues with closures(such as lifetime or performance reasons)
    #[inline]
    #[must_use]
    fn begin_blend_mode(
        &mut self,
        blend_mode: crate::consts::BlendMode,
    ) -> RaylibBlendMode<'_, Self> {
        unsafe { ffi::BeginBlendMode((blend_mode as u32) as i32) }
        RaylibBlendMode(self)
    }

    /// Begin blending mode (alpha, additive, multiplied, subtract, custom).
    fn draw_blend_mode<'a>(
        &'a mut self,
        blend_mode: crate::consts::BlendMode,
        func: impl FnOnce(RaylibBlendMode<'a, Self>),
    ) {
        unsafe { ffi::BeginBlendMode((blend_mode as u32) as i32) }
        func(RaylibBlendMode(self));
        // Uncomment the following if RaylibBlendMode has been changed to no longer call EndBlendMode() in its drop implementation:
        // unsafe { ffi::EndBlendMode(); }
    }
}

impl<D: RaylibDraw> RaylibBlendModeExt for D {}
impl<'a, T: 'a> RaylibDraw for RaylibBlendMode<'a, T> {}
impl<'a, T: 'a> RaylibDraw3D for RaylibBlendMode<'a, T> {}

// Scissor Mode stuff

/// RAII draw handle for a scissor (clipping rectangle) mode — calls `EndScissorMode` on drop.
///
/// While alive, the GPU discards fragments outside the rectangle `(x, y, width, height)`
/// passed to the constructor — draws are clipped to that region. Drop runs `EndScissorMode`,
/// removing the scissor test so subsequent draws cover the full framebuffer again.
/// Implements [`RaylibDraw`] (and [`RaylibDraw3D`] when the parent surface does) via deref.
///
/// # Examples
///
/// ```rust
/// # #[cfg(feature = "software_renderer")] {
/// use raylib::prelude::*;
/// use raylib::test_harness::*;
///
/// with_headless(64, 64, |rl, thread| {
///     let img = render_frame(rl, thread, |d| {
///         d.clear_background(Color::WHITE);
///         {
///             // Clip subsequent draws to a 20×20 region at (10, 10).
///             let mut s = d.begin_scissor_mode(10, 10, 20, 20);
///             // Even though the rectangle is 64×64, only the clipped 20×20 paints RED.
///             s.draw_rectangle(0, 0, 64, 64, Color::RED);
///         }
///     });
///     // Inside the scissor region: RED.
///     assert_pixel(&img, 15, 15, Color::RED, 0);
///     // Outside the scissor region: untouched WHITE.
///     assert_pixel(&img, 5, 5, Color::WHITE, 0);
///     assert_pixel(&img, 40, 40, Color::WHITE, 0);
/// });
/// # }
/// ```
///
/// # See also
///
/// - [`RaylibScissorModeExt::begin_scissor_mode`] — constructor for this guard.
pub struct RaylibScissorMode<'a, T: 'a>(&'a mut T);
impl<'a, T: 'a> Drop for RaylibScissorMode<'a, T> {
    fn drop(&mut self) {
        unsafe { ffi::EndScissorMode() }
    }
}
impl<'a, T: 'a> std::ops::Deref for RaylibScissorMode<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.0
    }
}
impl<'a, T: 'a> std::ops::DerefMut for RaylibScissorMode<'a, T> {
    fn deref_mut(&mut self) -> &mut T {
        self.0
    }
}

/// Extension trait providing scissor mode entry on draw handles.
///
/// Implemented for every [`RaylibDraw`] surface. The two methods are alternative forms:
/// [`begin_scissor_mode`](Self::begin_scissor_mode) returns a RAII guard;
/// [`draw_scissor_mode`](Self::draw_scissor_mode) takes a closure and ends the scissor scope
/// automatically when the closure returns.
///
/// # Examples
///
/// Closure form:
///
/// ```no_run
/// use raylib::prelude::*;
///
/// let (mut rl, thread) = raylib::init().size(800, 600).title("demo").build();
/// while !rl.window_should_close() {
///     let mut d = rl.begin_drawing(&thread);
///     d.clear_background(Color::RAYWHITE);
///     d.draw_scissor_mode(100, 100, 200, 150, |mut s| {
///         // Only fragments inside (100, 100, 200, 150) survive the scissor test.
///         s.draw_rectangle(0, 0, 800, 600, Color::RED);
///     });
/// }
/// ```
///
/// See [`RaylibScissorMode`] for the equivalent guard-form example.
///
/// # See also
///
/// - [`RaylibScissorMode`] — RAII guard returned by `begin_scissor_mode`.
pub trait RaylibScissorModeExt
where
    Self: Sized,
{
    /// Begin scissor mode (define screen area for following drawing).
    /// Prefer using the closure version, [RaylibScissorModeExt::draw_scissor_mode]. This version returns a handle that calls [raylib_sys::EndScissorMode] at the end of the scope and is provided as a fallback incase you run into issues with closures(such as lifetime or performance reasons)
    #[must_use]
    #[inline]
    fn begin_scissor_mode(
        &mut self,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
    ) -> RaylibScissorMode<'_, Self> {
        unsafe { ffi::BeginScissorMode(x, y, width, height) }
        RaylibScissorMode(self)
    }

    /// Begin scissor mode (define screen area for following drawing).
    fn draw_scissor_mode<'a>(
        &'a mut self,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        func: impl FnOnce(RaylibScissorMode<'a, Self>),
    ) {
        unsafe { ffi::BeginScissorMode(x, y, width, height) }
        func(RaylibScissorMode(self));
        // Uncomment the following if RaylibScissorMode has been changed to no longer call EndScissorMode() in its drop implementation:
        // unsafe { ffi::EndScissorMode(); }
    }
}

impl<D: RaylibDraw> RaylibScissorModeExt for D {}
impl<'a, T: 'a> RaylibDraw for RaylibScissorMode<'a, T> {}
impl<'a, T: 'a + RaylibDraw3D> RaylibDraw3D for RaylibScissorMode<'a, T> {}

// Actual drawing functions

/// Drawing methods available within an active drawing or mode block (2D shapes, textures, text).
///
/// This trait is implemented on [`RaylibDrawHandle`] (the guard returned by
/// [`RaylibHandle::begin_drawing`]) and on every nested mode guard
/// ([`RaylibTextureMode`], [`RaylibMode2D`], [`RaylibMode3D`], etc.). Call methods on the guard
/// to issue GPU draw commands; all commands accumulate until the guard drops, at which point
/// `EndDrawing` is called and the frame is presented.
///
/// # Examples
///
/// ```no_run
/// use raylib::prelude::*;
///
/// let (mut rl, thread) = raylib::init().size(640, 480).title("demo").build();
///
/// while !rl.window_should_close() {
///     let mut d = rl.begin_drawing(&thread);
///     d.clear_background(Color::RAYWHITE);
///     d.draw_rectangle(10, 10, 100, 50, Color::RED);
///     d.draw_text("RaylibDraw", 10, 70, 20, Color::BLACK);
///     // `d` drops here → EndDrawing is called automatically
/// }
/// ```
pub trait RaylibDraw {
    /// Sets background color (framebuffer clear color.into()).
    #[inline]
    fn clear_background(&mut self, color: impl Into<ffi::Color>) {
        unsafe {
            ffi::ClearBackground(color.into());
        }
    }

    /// Get texture that is used for shapes drawing
    #[inline]
    #[must_use]
    fn get_shapes_texture(&self) -> Texture2D {
        Texture2D(unsafe { ffi::GetShapesTexture() })
    }

    /// Get texture source rectangle that is used for shapes drawing
    #[inline]
    #[must_use]
    fn get_shapes_texture_rectangle(&self) -> Rectangle {
        unsafe { ffi::GetShapesTextureRectangle() }
    }

    /// Define default texture used to draw shapes
    #[inline]
    fn set_shapes_texture(
        &mut self,
        texture: impl AsRef<ffi::Texture2D>,
        source: impl Into<ffi::Rectangle>,
    ) {
        unsafe { ffi::SetShapesTexture(*texture.as_ref(), source.into()) }
    }

    // // Draw gui widget
    // fn draw_gui<G: crate::rgui::GuiDraw>(&mut self, widget: G) -> crate::rgui::DrawResult {
    //     widget.draw()
    // }

    // SHAPES
    /// Draws a pixel.
    #[inline]
    fn draw_pixel(&mut self, x: i32, y: i32, color: impl Into<ffi::Color>) {
        unsafe {
            ffi::DrawPixel(x, y, color.into());
        }
    }

    /// Draws a pixel (Vector version).
    #[inline]
    fn draw_pixel_v(&mut self, position: impl Into<Vector2>, color: impl Into<ffi::Color>) {
        unsafe {
            ffi::DrawPixelV(position.into(), color.into());
        }
    }

    /// Draws a line.
    #[inline]
    fn draw_line(
        &mut self,
        start_pos_x: i32,
        start_pos_y: i32,
        end_pos_x: i32,
        end_pos_y: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawLine(start_pos_x, start_pos_y, end_pos_x, end_pos_y, color.into());
        }
    }

    /// Draws a line (Vector version).
    #[inline]
    fn draw_line_v(
        &mut self,
        start_pos: impl Into<Vector2>,
        end_pos: impl Into<Vector2>,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawLineV(start_pos.into(), end_pos.into(), color.into());
        }
    }

    /// Draws a line with thickness.
    #[inline]
    fn draw_line_ex(
        &mut self,
        start_pos: impl Into<Vector2>,
        end_pos: impl Into<Vector2>,
        thick: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawLineEx(start_pos.into(), end_pos.into(), thick, color.into());
        }
    }

    /// Draws a line using cubic-bezier curves in-out.
    #[inline]
    fn draw_line_bezier(
        &mut self,
        start_pos: impl Into<Vector2>,
        end_pos: impl Into<Vector2>,
        thick: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawLineBezier(start_pos.into(), end_pos.into(), thick, color.into());
        }
    }

    /// Draw a dashed line.
    ///
    /// `dash_size` is the length of each painted segment; `space_size` is the
    /// gap between segments. Both are in pixels.
    #[inline]
    fn draw_line_dashed(
        &mut self,
        start: impl Into<Vector2>,
        end: impl Into<Vector2>,
        dash_size: i32,
        space_size: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawLineDashed(
                start.into(),
                end.into(),
                dash_size,
                space_size,
                color.into(),
            );
        }
    }

    /// Draw lines sequence
    #[inline]
    fn draw_line_strip(&mut self, points: &[Vector2], color: impl Into<ffi::Color>) {
        unsafe {
            ffi::DrawLineStrip(
                points.as_ptr() as *mut ffi::Vector2,
                points.len() as i32,
                color.into(),
            );
        }
    }

    /// Draws a color-filled circle.
    #[inline]
    fn draw_circle(
        &mut self,
        center_x: i32,
        center_y: i32,
        radius: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCircle(center_x, center_y, radius, color.into());
        }
    }
    /// Draw a piece of a circle
    #[inline]
    fn draw_circle_sector(
        &mut self,
        center: impl Into<Vector2>,
        radius: f32,
        start_angle: f32,
        end_angle: f32,
        segments: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCircleSector(
                center.into(),
                radius,
                start_angle,
                end_angle,
                segments,
                color.into(),
            );
        }
    }

    /// Draw circle sector outline
    #[inline]
    fn draw_circle_sector_lines(
        &mut self,
        center: impl Into<Vector2>,
        radius: f32,
        start_angle: f32,
        end_angle: f32,
        segments: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCircleSectorLines(
                center.into(),
                radius,
                start_angle,
                end_angle,
                segments,
                color.into(),
            );
        }
    }

    /// Draws a gradient-filled circle.
    #[inline]
    fn draw_circle_gradient(
        &mut self,
        center_x: i32,
        center_y: i32,
        radius: f32,
        inner: impl Into<ffi::Color>,
        outer: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCircleGradient(
                Vector2 {
                    x: center_x as f32,
                    y: center_y as f32,
                },
                radius,
                inner.into(),
                outer.into(),
            );
        }
    }

    /// Draws a color-filled circle (Vector version).
    #[inline]
    fn draw_circle_v(
        &mut self,
        center: impl Into<Vector2>,
        radius: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCircleV(center.into(), radius, color.into());
        }
    }

    /// Draws circle outline.
    #[inline]
    fn draw_circle_lines(
        &mut self,
        center_x: i32,
        center_y: i32,
        radius: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCircleLines(center_x, center_y, radius, color.into());
        }
    }

    /// Draws circle outline. (Vector Version)
    #[inline]
    fn draw_circle_lines_v(
        &mut self,
        center: impl Into<Vector2>,
        radius: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCircleLinesV(center.into(), radius, color.into());
        }
    }

    /// Draws ellipse.
    #[inline]
    fn draw_ellipse(
        &mut self,
        center_x: i32,
        center_y: i32,
        radius_h: f32,
        radius_v: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawEllipse(center_x, center_y, radius_h, radius_v, color.into());
        }
    }

    /// Draws ellipse using a Vector2 center.
    #[inline]
    fn draw_ellipse_v(
        &mut self,
        center: impl Into<Vector2>,
        radius_h: f32,
        radius_v: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawEllipseV(center.into(), radius_h, radius_v, color.into());
        }
    }

    /// Draws ellipse.
    #[inline]
    fn draw_ellipse_lines(
        &mut self,
        center_x: i32,
        center_y: i32,
        radius_h: f32,
        radius_v: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawEllipseLines(center_x, center_y, radius_h, radius_v, color.into());
        }
    }

    /// Draws ellipse outline using a Vector2 center.
    #[inline]
    fn draw_ellipse_lines_v(
        &mut self,
        center: impl Into<Vector2>,
        radius_h: f32,
        radius_v: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawEllipseLinesV(center.into(), radius_h, radius_v, color.into());
        }
    }

    /// Draw ring
    #[inline]
    #[allow(clippy::too_many_arguments)]
    fn draw_ring(
        &mut self,
        center: impl Into<Vector2>,
        inner_radius: f32,
        outer_radius: f32,
        start_angle: f32,
        end_angle: f32,
        segments: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawRing(
                center.into(),
                inner_radius,
                outer_radius,
                start_angle,
                end_angle,
                segments,
                color.into(),
            );
        }
    }

    /// Draw ring lines
    #[inline]
    #[allow(clippy::too_many_arguments)]
    fn draw_ring_lines(
        &mut self,
        center: impl Into<Vector2>,
        inner_radius: f32,
        outer_radius: f32,
        start_angle: f32,
        end_angle: f32,
        segments: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawRingLines(
                center.into(),
                inner_radius,
                outer_radius,
                start_angle,
                end_angle,
                segments,
                color.into(),
            );
        }
    }

    /// Draws a color-filled rectangle.
    #[inline]
    fn draw_rectangle(
        &mut self,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawRectangle(x, y, width, height, color.into());
        }
    }

    /// Draws a color-filled rectangle (Vector version).
    #[inline]
    fn draw_rectangle_v(
        &mut self,
        position: impl Into<Vector2>,
        size: impl Into<Vector2>,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawRectangleV(position.into(), size.into(), color.into());
        }
    }

    /// Draws a color-filled rectangle from `rec`.
    #[inline]
    fn draw_rectangle_rec(&mut self, rec: impl Into<ffi::Rectangle>, color: impl Into<ffi::Color>) {
        unsafe {
            ffi::DrawRectangleRec(rec.into(), color.into());
        }
    }

    /// Draws a color-filled rectangle with pro parameters.
    #[inline]
    fn draw_rectangle_pro(
        &mut self,
        rec: impl Into<ffi::Rectangle>,
        origin: impl Into<Vector2>,
        rotation: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawRectanglePro(rec.into(), origin.into(), rotation, color.into());
        }
    }

    /// Draws a vertical-gradient-filled rectangle.
    ///
    /// **NOTE**: Gradient goes from bottom (`color1`) to top (`color2`).
    #[inline]
    fn draw_rectangle_gradient_v(
        &mut self,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        color1: impl Into<ffi::Color>,
        color2: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawRectangleGradientV(x, y, width, height, color1.into(), color2.into());
        }
    }

    /// Draws a horizontal-gradient-filled rectangle.
    ///
    /// **NOTE**: Gradient goes from bottom (`color1`) to top (`color2`).
    #[inline]
    fn draw_rectangle_gradient_h(
        &mut self,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        color1: impl Into<ffi::Color>,
        color2: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawRectangleGradientH(x, y, width, height, color1.into(), color2.into());
        }
    }

    /// Draws a gradient-filled rectangle with custom vertex colors.
    ///
    /// **NOTE**: Colors refer to corners, starting at top-left corner and going counter-clockwise.
    #[inline]
    fn draw_rectangle_gradient_ex(
        &mut self,
        rec: impl Into<ffi::Rectangle>,
        col1: impl Into<ffi::Color>,
        col2: impl Into<ffi::Color>,
        col3: impl Into<ffi::Color>,
        col4: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawRectangleGradientEx(
                rec.into(),
                col1.into(),
                col2.into(),
                col3.into(),
                col4.into(),
            );
        }
    }

    /// Draws rectangle outline.
    #[inline]
    fn draw_rectangle_lines(
        &mut self,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawRectangleLines(x, y, width, height, color.into());
        }
    }

    /// Draws rectangle outline with extended parameters.
    #[inline]
    fn draw_rectangle_lines_ex(
        &mut self,
        rec: impl Into<ffi::Rectangle>,
        line_thick: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawRectangleLinesEx(rec.into(), line_thick, color.into());
        }
    }
    /// Draws rectangle with rounded edges.
    #[inline]
    fn draw_rectangle_rounded(
        &mut self,
        rec: impl Into<ffi::Rectangle>,
        roundness: f32,
        segments: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawRectangleRounded(rec.into(), roundness, segments, color.into());
        }
    }

    /// Draws rectangle outline with rounded edges included.
    #[inline]
    fn draw_rectangle_rounded_lines(
        &mut self,
        rec: impl Into<ffi::Rectangle>,
        roundness: f32,
        segments: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawRectangleRoundedLines(rec.into(), roundness, segments, color.into());
        }
    }

    /// Draw rectangle with rounded edges outline
    #[inline]
    fn draw_rectangle_rounded_lines_ex(
        &mut self,
        rec: impl Into<ffi::Rectangle>,
        roundness: f32,
        segments: i32,
        line_thickness: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawRectangleRoundedLinesEx(
                rec.into(),
                roundness,
                segments,
                line_thickness,
                color.into(),
            )
        };
    }
    /// Draws a triangle.
    #[inline]
    fn draw_triangle(
        &mut self,
        v1: impl Into<Vector2>,
        v2: impl Into<Vector2>,
        v3: impl Into<Vector2>,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawTriangle(v1.into(), v2.into(), v3.into(), color.into());
        }
    }

    /// Draws a triangle using lines.
    #[inline]
    fn draw_triangle_lines(
        &mut self,
        v1: impl Into<Vector2>,
        v2: impl Into<Vector2>,
        v3: impl Into<Vector2>,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawTriangleLines(v1.into(), v2.into(), v3.into(), color.into());
        }
    }

    /// Draw a triangle fan defined by points.
    #[inline]
    fn draw_triangle_fan(&mut self, points: &[Vector2], color: impl Into<ffi::Color>) {
        unsafe {
            ffi::DrawTriangleFan(
                points.as_ptr() as *mut ffi::Vector2,
                points.len() as i32,
                color.into(),
            );
        }
    }

    /// Draw a triangle strip defined by points
    #[inline]
    fn draw_triangle_strip(&mut self, points: &[Vector2], color: impl Into<ffi::Color>) {
        unsafe {
            ffi::DrawTriangleStrip(
                points.as_ptr() as *mut ffi::Vector2,
                points.len() as i32,
                color.into(),
            );
        }
    }

    /// Draws a regular polygon of n sides (Vector version).
    #[inline]
    fn draw_poly(
        &mut self,
        center: impl Into<Vector2>,
        sides: i32,
        radius: f32,
        rotation: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawPoly(center.into(), sides, radius, rotation, color.into());
        }
    }

    /// Draws a regular polygon of n sides (Vector version).
    #[inline]
    fn draw_poly_lines(
        &mut self,
        center: impl Into<Vector2>,
        sides: i32,
        radius: f32,
        rotation: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawPolyLines(center.into(), sides, radius, rotation, color.into());
        }
    }

    /// Draws a `texture` using specified position and `tint` color.
    #[inline]
    fn draw_texture(
        &mut self,
        texture: impl AsRef<ffi::Texture2D>,
        x: i32,
        y: i32,
        tint: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawTexture(*texture.as_ref(), x, y, tint.into());
        }
    }

    /// Draws a `texture` using specified `position` vector and `tint` color.
    #[inline]
    fn draw_texture_v(
        &mut self,
        texture: impl AsRef<ffi::Texture2D>,
        position: impl Into<Vector2>,
        tint: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawTextureV(*texture.as_ref(), position.into(), tint.into());
        }
    }

    /// Draws a `texture` with extended parameters.
    #[inline]
    fn draw_texture_ex(
        &mut self,
        texture: impl AsRef<ffi::Texture2D>,
        position: impl Into<Vector2>,
        rotation: f32,
        scale: f32,
        tint: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawTextureEx(
                *texture.as_ref(),
                position.into(),
                rotation,
                scale,
                tint.into(),
            );
        }
    }

    /// Draws from a region of `texture` defined by the `source_rec` rectangle.
    #[inline]
    fn draw_texture_rec(
        &mut self,
        texture: impl AsRef<ffi::Texture2D>,
        source_rec: impl Into<ffi::Rectangle>,
        position: impl Into<Vector2>,
        tint: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawTextureRec(
                *texture.as_ref(),
                source_rec.into(),
                position.into(),
                tint.into(),
            );
        }
    }

    /// Draw from a region of `texture` defined by the `source_rec` rectangle with pro parameters.
    #[inline]
    fn draw_texture_pro(
        &mut self,
        texture: impl AsRef<ffi::Texture2D>,
        source_rec: impl Into<ffi::Rectangle>,
        dest_rec: impl Into<ffi::Rectangle>,
        origin: impl Into<Vector2>,
        rotation: f32,
        tint: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawTexturePro(
                *texture.as_ref(),
                source_rec.into(),
                dest_rec.into(),
                origin.into(),
                rotation,
                tint.into(),
            );
        }
    }

    /// Draws a texture (or part of it) that stretches or shrinks nicely
    #[inline]
    fn draw_texture_n_patch(
        &mut self,
        texture: impl AsRef<ffi::Texture2D>,
        n_patch_info: impl Into<ffi::NPatchInfo>,
        dest_rec: impl Into<ffi::Rectangle>,
        origin: impl Into<Vector2>,
        rotation: f32,
        tint: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawTextureNPatch(
                *texture.as_ref(),
                n_patch_info.into(),
                dest_rec.into(),
                origin.into(),
                rotation,
                tint.into(),
            );
        }
    }

    /// Shows current FPS.
    #[inline]
    fn draw_fps(&mut self, x: i32, y: i32) {
        unsafe {
            ffi::DrawFPS(x, y);
        }
    }

    /// Draws text (using default font).
    /// This does not support UTF-8. Use `[RaylibDrawHandle::draw_text_codepoints]` for that.
    #[inline]
    fn draw_text(
        &mut self,
        text: &str,
        x: i32,
        y: i32,
        font_size: i32,
        color: impl Into<ffi::Color>,
    ) {
        let c_text = CString::new(text).unwrap();

        unsafe {
            ffi::DrawText(c_text.as_ptr(), x, y, font_size, color.into());
        }
    }

    /// Draws text (using default font) with support for UTF-8.
    /// If you do not need UTF-8, use `[RaylibDrawHandle::draw_text]`.
    fn draw_text_codepoints(
        &mut self,
        font: impl AsRef<ffi::Font>,
        text: &str,
        position: impl Into<Vector2>,
        font_size: f32,
        spacing: f32,
        tint: impl Into<ffi::Color>,
    ) {
        unsafe {
            let c_text = CString::new(text).unwrap();

            let mut len = 0;
            let u = ffi::LoadCodepoints(c_text.as_ptr(), &mut len);

            ffi::DrawTextCodepoints(
                *font.as_ref(),
                u,
                text.len() as i32,
                position.into(),
                font_size,
                spacing,
                tint.into(),
            )
        }
    }
    /// Draws text using `font` and additional parameters.
    #[inline]
    fn draw_text_ex(
        &mut self,
        font: impl AsRef<ffi::Font>,
        text: &str,
        position: impl Into<Vector2>,
        font_size: f32,
        spacing: f32,
        tint: impl Into<ffi::Color>,
    ) {
        let c_text = CString::new(text).unwrap();
        unsafe {
            ffi::DrawTextEx(
                *font.as_ref(),
                c_text.as_ptr(),
                position.into(),
                font_size,
                spacing,
                tint.into(),
            );
        }
    }

    /// Draw text using Font and pro parameters (rotation)
    #[inline]
    #[allow(clippy::too_many_arguments)]
    fn draw_text_pro(
        &mut self,
        font: impl AsRef<ffi::Font>,
        text: &str,
        position: impl Into<Vector2>,
        origin: impl Into<Vector2>,
        rotation: f32,
        font_size: f32,
        spacing: f32,
        tint: impl Into<ffi::Color>,
    ) {
        let c_text = CString::new(text).unwrap();
        unsafe {
            ffi::DrawTextPro(
                *font.as_ref(),
                c_text.as_ptr(),
                position.into(),
                origin.into(),
                rotation,
                font_size,
                spacing,
                tint.into(),
            );
        }
    }

    /// Draw one character (codepoint)
    #[inline]
    fn draw_text_codepoint(
        &mut self,
        font: impl AsRef<ffi::Font>,
        codepoint: i32,
        position: impl Into<Vector2>,
        scale: f32,
        tint: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawTextCodepoint(
                *font.as_ref(),
                codepoint,
                position.into(),
                scale,
                tint.into(),
            );
        }
    }

    /// Enable waiting for events when the handle is dropped, no automatic event polling
    #[inline]
    fn enable_event_waiting(&self) {
        unsafe { ffi::EnableEventWaiting() }
    }

    /// Disable waiting for events when the handle is dropped, no automatic event polling
    #[inline]
    fn disable_event_waiting(&self) {
        unsafe { ffi::DisableEventWaiting() }
    }

    /// Draw a polygon outline of n sides with extended parameters
    #[inline]
    fn draw_poly_lines_ex(
        &mut self,
        center: impl Into<Vector2>,
        sides: i32,
        radius: f32,
        rotation: f32,
        line_thick: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawPolyLinesEx(
                center.into(),
                sides,
                radius,
                rotation,
                line_thick,
                color.into(),
            );
        }
    }
    /// Draw spline: Linear, minimum 2 points
    #[inline]
    fn draw_spline_linear(&mut self, points: &[Vector2], thick: f32, color: impl Into<ffi::Color>) {
        unsafe {
            ffi::DrawSplineLinear(
                points.as_ptr() as *mut ffi::Vector2,
                points.len() as i32,
                thick,
                color.into(),
            )
        }
    }
    /// Draw spline: B-Spline, minimum 4 points
    #[inline]
    fn draw_spline_basis(&mut self, points: &[Vector2], thick: f32, color: impl Into<ffi::Color>) {
        unsafe {
            ffi::DrawSplineBasis(
                points.as_ptr() as *mut ffi::Vector2,
                points.len() as i32,
                thick,
                color.into(),
            )
        }
    }
    /// Draw spline: Catmull-Rom, minimum 4 points
    #[inline]
    fn draw_spline_catmull_rom(
        &mut self,
        points: &[Vector2],
        thick: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawSplineCatmullRom(
                points.as_ptr() as *mut ffi::Vector2,
                points.len() as i32,
                thick,
                color.into(),
            )
        }
    }

    /// Draw spline: Quadratic Bezier, minimum 3 points (1 control point): [p1, c2, p3, c4...]
    #[inline]
    fn draw_spline_bezier_quadratic(
        &mut self,
        points: &[Vector2],
        thick: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawSplineBezierQuadratic(
                points.as_ptr() as *mut ffi::Vector2,
                points.len() as i32,
                thick,
                color.into(),
            )
        }
    }

    /// Draw spline: Cubic Bezier, minimum 4 points (2 control points): [p1, c2, c3, p4, c5, c6...]
    #[inline]
    fn draw_spline_bezier_cubic(
        &mut self,
        points: &[Vector2],
        thick: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawSplineBezierCubic(
                points.as_ptr() as *mut ffi::Vector2,
                points.len() as i32,
                thick,
                color.into(),
            )
        }
    }

    /// Draw spline segment: Linear, 2 points
    #[inline]
    fn draw_spline_segment_linear(
        &mut self,
        p1: impl Into<Vector2>,
        p2: impl Into<Vector2>,
        thick: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe { ffi::DrawSplineSegmentLinear(p1.into(), p2.into(), thick, color.into()) }
    }

    /// Draw spline segment: B-Spline, 4 points
    #[inline]
    fn draw_spline_segment_basis(
        &mut self,
        p1: impl Into<Vector2>,
        p2: impl Into<Vector2>,
        p3: impl Into<Vector2>,
        p4: impl Into<Vector2>,
        thick: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawSplineSegmentBasis(
                p1.into(),
                p2.into(),
                p3.into(),
                p4.into(),
                thick,
                color.into(),
            )
        }
    }

    /// Draw spline segment: Catmull-Rom, 4 points
    #[inline]
    fn draw_spline_segment_catmull_rom(
        &mut self,
        p1: impl Into<Vector2>,
        p2: impl Into<Vector2>,
        p3: impl Into<Vector2>,
        p4: impl Into<Vector2>,
        thick: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawSplineSegmentCatmullRom(
                p1.into(),
                p2.into(),
                p3.into(),
                p4.into(),
                thick,
                color.into(),
            )
        }
    }

    /// Draw spline segment: Quadratic Bezier, 2 points, 1 control point
    #[inline]
    fn draw_spline_segment_bezier_quadratic(
        &mut self,
        p1: impl Into<Vector2>,
        c2: impl Into<Vector2>,
        p3: impl Into<Vector2>,
        thick: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawSplineSegmentBezierQuadratic(
                p1.into(),
                c2.into(),
                p3.into(),
                thick,
                color.into(),
            )
        }
    }

    /// Draw spline segment: Cubic Bezier, 2 points, 2 control points
    #[inline]
    fn draw_spline_segment_bezier_cubic(
        &mut self,
        p1: impl Into<Vector2>,
        c2: impl Into<Vector2>,
        c3: impl Into<Vector2>,
        p4: impl Into<Vector2>,
        thick: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawSplineSegmentBezierCubic(
                p1.into(),
                c2.into(),
                c3.into(),
                p4.into(),
                thick,
                color.into(),
            )
        }
    }

    /// Get (evaluate) spline point: Linear
    #[inline]
    #[must_use]
    fn get_spline_point_linear(
        &mut self,
        start_pos: impl Into<Vector2>,
        end_pos: impl Into<Vector2>,
        t: f32,
    ) -> Vector2 {
        unsafe { ffi::GetSplinePointLinear(start_pos.into(), end_pos.into(), t) }
    }

    /// Get (evaluate) spline point: B-Spline
    #[inline]
    #[must_use]
    fn get_spline_point_basis(
        &mut self,
        p1: impl Into<Vector2>,
        p2: impl Into<Vector2>,
        p3: impl Into<Vector2>,
        p4: impl Into<Vector2>,
        t: f32,
    ) -> Vector2 {
        unsafe { ffi::GetSplinePointBasis(p1.into(), p2.into(), p3.into(), p4.into(), t) }
    }

    /// Get (evaluate) spline point: Catmull-Rom
    #[inline]
    #[must_use]
    fn get_spline_point_catmull_rom(
        &mut self,
        p1: impl Into<Vector2>,
        p2: impl Into<Vector2>,
        p3: impl Into<Vector2>,
        p4: impl Into<Vector2>,
        t: f32,
    ) -> Vector2 {
        unsafe { ffi::GetSplinePointCatmullRom(p1.into(), p2.into(), p3.into(), p4.into(), t) }
    }

    /// Get (evaluate) spline point: Quadratic Bezier
    #[inline]
    #[must_use]
    fn get_spline_point_bezier_quad(
        &mut self,
        p1: impl Into<Vector2>,
        c2: impl Into<Vector2>,
        p3: impl Into<Vector2>,
        t: f32,
    ) -> Vector2 {
        unsafe { ffi::GetSplinePointBezierQuad(p1.into(), c2.into(), p3.into(), t) }
    }

    /// Get (evaluate) spline point: Cubic Bezier
    #[inline]
    #[must_use]
    fn get_spline_point_bezier_cubic(
        &mut self,
        p1: impl Into<Vector2>,
        c2: impl Into<Vector2>,
        c3: impl Into<Vector2>,
        p4: impl Into<Vector2>,
        t: f32,
    ) -> Vector2 {
        unsafe { ffi::GetSplinePointBezierCubic(p1.into(), c2.into(), c3.into(), p4.into(), t) }
    }
}

/// Drawing methods available within an active 3D camera mode block.
///
/// Implemented on guards entered through a 3D camera ([`RaylibMode3D`]) and on nested mode
/// guards that wrap a 3D-capable parent ([`RaylibShaderMode`], [`RaylibBlendMode`],
/// [`RaylibScissorMode`]). The methods on this trait submit 3D primitives — points, lines,
/// triangles, cubes, spheres, cylinders, meshes — into the camera's projected world space.
/// 2D draw methods from [`RaylibDraw`] remain callable through the guard's deref; they are
/// projected onto the near plane and so usually render as overlays.
///
/// # Examples
///
/// ```no_run
/// use raylib::prelude::*;
///
/// let (mut rl, thread) = raylib::init().size(800, 600).title("demo").build();
/// let camera = Camera3D::perspective(
///     Vector3::new(4.0, 4.0, 4.0),
///     Vector3::new(0.0, 0.0, 0.0),
///     Vector3::new(0.0, 1.0, 0.0),
///     45.0,
/// );
/// while !rl.window_should_close() {
///     let mut d = rl.begin_drawing(&thread);
///     d.clear_background(Color::RAYWHITE);
///     let mut c = d.begin_mode3D(camera);
///     c.draw_cube(Vector3::zero(), 2.0, 2.0, 2.0, Color::RED);
///     c.draw_cube_wires(Vector3::zero(), 2.0, 2.0, 2.0, Color::MAROON);
///     c.draw_grid(10, 1.0);
/// }
/// ```
///
/// # See also
///
/// - [`RaylibMode3D`] — primary guard through which this trait is exposed.
/// - [`RaylibDraw`] — 2D drawing methods available on the same guard.
pub trait RaylibDraw3D {
    /// Draw a point in 3D space, actually a small line
    #[allow(non_snake_case)]
    #[inline]
    fn draw_point3D(&mut self, position: impl Into<Vector3>, color: impl Into<ffi::Color>) {
        unsafe {
            ffi::DrawPoint3D(position.into(), color.into());
        }
    }

    /// Draw a color-filled triangle (vertex in counter-clockwise order!)
    #[allow(non_snake_case)]
    #[inline]
    fn draw_triangle3D(
        &mut self,
        v1: impl Into<Vector3>,
        v2: impl Into<Vector3>,
        v3: impl Into<Vector3>,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawTriangle3D(v1.into(), v2.into(), v3.into(), color.into());
        }
    }

    /// Draw a triangle strip defined by points
    #[allow(non_snake_case)]
    #[inline]
    fn draw_triangle_strip3D(&mut self, points: &[Vector3], color: impl Into<ffi::Color>) {
        unsafe {
            ffi::DrawTriangleStrip3D(points.as_ptr() as *mut _, points.len() as i32, color.into());
        }
    }

    /// Draws a line in 3D world space.
    #[allow(non_snake_case)]
    #[inline]
    fn draw_line3D(
        &mut self,
        start_pos: impl Into<Vector3>,
        end_pos: impl Into<Vector3>,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawLine3D(start_pos.into(), end_pos.into(), color.into());
        }
    }

    /// Draws a circle in 3D world space.
    #[allow(non_snake_case)]
    #[inline]
    fn draw_circle3D(
        &mut self,
        center: impl Into<Vector3>,
        radius: f32,
        rotation_axis: impl Into<Vector3>,
        rotation_angle: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCircle3D(
                center.into(),
                radius,
                rotation_axis.into(),
                rotation_angle,
                color.into(),
            );
        }
    }

    /// Draws a cube.
    #[inline]
    fn draw_cube(
        &mut self,
        position: impl Into<Vector3>,
        width: f32,
        height: f32,
        length: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCube(position.into(), width, height, length, color.into());
        }
    }

    /// Draws a cube (Vector version).
    #[inline]
    fn draw_cube_v(
        &mut self,
        position: impl Into<Vector3>,
        size: impl Into<Vector3>,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCubeV(position.into(), size.into(), color.into());
        }
    }

    /// Draws a cube in wireframe.
    #[inline]
    fn draw_cube_wires(
        &mut self,
        position: impl Into<Vector3>,
        width: f32,
        height: f32,
        length: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCubeWires(position.into(), width, height, length, color.into());
        }
    }

    /// Draws a cube in wireframe. (Vector Version)
    #[inline]
    fn draw_cube_wires_v(
        &mut self,
        position: impl Into<Vector3>,
        size: impl Into<Vector3>,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCubeWiresV(position.into(), size.into(), color.into());
        }
    }

    /// Draw a 3d mesh with material and transform
    #[inline]
    fn draw_mesh(
        &mut self,
        mesh: impl AsRef<ffi::Mesh>,
        material: WeakMaterial,
        transform: impl Into<ffi::Matrix>,
    ) {
        unsafe { ffi::DrawMesh(*mesh.as_ref(), material.0, transform.into()) }
    }

    /// Draw multiple mesh instances with material and different transforms
    #[inline]
    fn draw_mesh_instanced(
        &mut self,
        mesh: impl AsRef<ffi::Mesh>,
        material: WeakMaterial,
        transforms: &[Matrix],
    ) {
        unsafe {
            ffi::DrawMeshInstanced(
                *mesh.as_ref(),
                material.0,
                transforms.as_ptr(),
                transforms.len() as i32,
            )
        }
    }

    /// Draws a sphere.
    #[inline]
    fn draw_sphere(
        &mut self,
        center_pos: impl Into<Vector3>,
        radius: f32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawSphere(center_pos.into(), radius, color.into());
        }
    }

    /// Draws a sphere with extended parameters.
    #[inline]
    fn draw_sphere_ex(
        &mut self,
        center_pos: impl Into<Vector3>,
        radius: f32,
        rings: i32,
        slices: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawSphereEx(center_pos.into(), radius, rings, slices, color.into());
        }
    }

    /// Draws a sphere in wireframe.
    #[inline]
    fn draw_sphere_wires(
        &mut self,
        center_pos: impl Into<Vector3>,
        radius: f32,
        rings: i32,
        slices: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawSphereWires(center_pos.into(), radius, rings, slices, color.into());
        }
    }

    /// Draws a cylinder.
    #[inline]
    fn draw_cylinder(
        &mut self,
        position: impl Into<Vector3>,
        radius_top: f32,
        radius_bottom: f32,
        height: f32,
        slices: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCylinder(
                position.into(),
                radius_top,
                radius_bottom,
                height,
                slices,
                color.into(),
            );
        }
    }

    /// Draws a cylinder with extended parameters.
    #[inline]
    fn draw_cylinder_ex(
        &mut self,
        start_position: impl Into<Vector3>,
        end_position: impl Into<Vector3>,
        radius_start: f32,
        radius_end: f32,
        slices: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCylinderEx(
                start_position.into(),
                end_position.into(),
                radius_start,
                radius_end,
                slices,
                color.into(),
            );
        }
    }

    /// Draws a cylinder in wireframe.
    #[inline]
    fn draw_cylinder_wires(
        &mut self,
        position: impl Into<Vector3>,
        radius_top: f32,
        radius_bottom: f32,
        height: f32,
        slices: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCylinderWires(
                position.into(),
                radius_top,
                radius_bottom,
                height,
                slices,
                color.into(),
            );
        }
    }

    /// Draws a cylinder in wireframe with extended parameters.
    #[inline]
    fn draw_cylinder_wires_ex(
        &mut self,
        start_position: impl Into<Vector3>,
        end_position: impl Into<Vector3>,
        radius_start: f32,
        radius_end: f32,
        slices: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCylinderWiresEx(
                start_position.into(),
                end_position.into(),
                radius_start,
                radius_end,
                slices,
                color.into(),
            );
        }
    }

    /// Draw capsule with the center of its sphere caps at startPos and endPos
    #[inline]
    fn draw_capsule(
        &mut self,
        start_pos: impl Into<Vector3>,
        end_pos: impl Into<Vector3>,
        radius: f32,
        slices: i32,
        rings: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCapsule(
                start_pos.into(),
                end_pos.into(),
                radius,
                slices,
                rings,
                color.into(),
            )
        }
    }

    ///Draw capsule wireframe with the center of its sphere caps at startPos and endPos
    #[inline]
    fn draw_capsule_wires(
        &mut self,
        start_pos: impl Into<Vector3>,
        end_pos: impl Into<Vector3>,
        radius: f32,
        slices: i32,
        rings: i32,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawCapsuleWires(
                start_pos.into(),
                end_pos.into(),
                radius,
                slices,
                rings,
                color.into(),
            )
        }
    }

    /// Draws an X/Z plane.
    #[inline]
    fn draw_plane(
        &mut self,
        center_pos: impl Into<Vector3>,
        size: impl Into<Vector2>,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawPlane(center_pos.into(), size.into(), color.into());
        }
    }

    /// Draws a ray line.
    #[inline]
    fn draw_ray(&mut self, ray: impl Into<ffi::Ray>, color: impl Into<ffi::Color>) {
        unsafe {
            ffi::DrawRay(ray.into(), color.into());
        }
    }

    /// Draws a grid (centered at (0, 0, 0)).
    #[inline]
    fn draw_grid(&mut self, slices: i32, spacing: f32) {
        unsafe {
            ffi::DrawGrid(slices, spacing);
        }
    }

    /// Draws a model (with texture if set).
    #[inline]
    fn draw_model(
        &mut self,
        model: impl AsRef<ffi::Model>,
        position: impl Into<Vector3>,
        scale: f32,
        tint: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawModel(*model.as_ref(), position.into(), scale, tint.into());
        }
    }

    /// Draws a model with extended parameters.
    #[inline]
    fn draw_model_ex(
        &mut self,
        model: impl AsRef<ffi::Model>,
        position: impl Into<Vector3>,
        rotation_axis: impl Into<Vector3>,
        rotation_angle: f32,
        scale: impl Into<Vector3>,
        tint: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawModelEx(
                *model.as_ref(),
                position.into(),
                rotation_axis.into(),
                rotation_angle,
                scale.into(),
                tint.into(),
            );
        }
    }

    /// Draws a model with wires (with texture if set).
    #[inline]
    fn draw_model_wires(
        &mut self,
        model: impl AsRef<ffi::Model>,
        position: impl Into<Vector3>,
        scale: f32,
        tint: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawModelWires(*model.as_ref(), position.into(), scale, tint.into());
        }
    }

    /// Draws a model with wires.
    #[inline]
    fn draw_model_wires_ex(
        &mut self,
        model: impl AsRef<ffi::Model>,
        position: impl Into<Vector3>,
        rotation_axis: impl Into<Vector3>,
        rotation_angle: f32,
        scale: impl Into<Vector3>,
        tint: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawModelWiresEx(
                *model.as_ref(),
                position.into(),
                rotation_axis.into(),
                rotation_angle,
                scale.into(),
                tint.into(),
            );
        }
    }

    /// Draws a bounding box (wires).
    #[inline]
    fn draw_bounding_box(
        &mut self,
        bbox: impl Into<ffi::BoundingBox>,
        color: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawBoundingBox(bbox.into(), color.into());
        }
    }

    /// Draws a billboard texture.
    #[inline]
    fn draw_billboard(
        &mut self,
        camera: impl Into<ffi::Camera3D>,
        texture: &Texture2D,
        center: impl Into<Vector3>,
        size: f32,
        tint: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawBillboard(camera.into(), texture.0, center.into(), size, tint.into());
        }
    }

    /// Draws a billboard texture defined by `source_rec`.
    #[inline]
    fn draw_billboard_rec(
        &mut self,
        camera: impl Into<ffi::Camera3D>,
        texture: &Texture2D,
        source_rec: impl Into<ffi::Rectangle>,
        center: impl Into<Vector3>,
        size: impl Into<Vector2>,
        tint: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawBillboardRec(
                camera.into(),
                texture.0,
                source_rec.into(),
                center.into(),
                size.into(),
                tint.into(),
            );
        }
    }

    /// Draw a billboard texture defined by source and rotation
    #[inline]
    #[allow(clippy::too_many_arguments)]
    fn draw_billboard_pro(
        &mut self,
        camera: impl Into<ffi::Camera>,
        texture: impl Into<ffi::Texture2D>,
        source: impl Into<ffi::Rectangle>,
        position: impl Into<Vector3>,
        up: impl Into<Vector3>,
        size: impl Into<Vector2>,
        origin: impl Into<Vector2>,
        rotation: f32,
        tint: impl Into<ffi::Color>,
    ) {
        unsafe {
            ffi::DrawBillboardPro(
                camera.into(),
                texture.into(),
                source.into(),
                position.into(),
                up.into(),
                size.into(),
                origin.into(),
                rotation,
                tint.into(),
            )
        }
    }
}