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 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011
|
#include <OpenDialog/open_dialog.h>
#include <SDL.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <ctype.h>
#include "utils.h"
#include "gui.h"
#include "font.h"
#include "audio/audio.h"
#ifdef _WIN32
#include <dwmapi.h>
#include "windows_associations.h"
#include <SDL_syswm.h>
#endif
static const SDL_Color gui_palette[4] = {{8, 24, 16,}, {57, 97, 57,}, {132, 165, 99}, {198, 222, 140}};
static uint32_t gui_palette_native[4];
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *texture = NULL;
SDL_PixelFormat *pixel_format = NULL;
enum pending_command pending_command;
unsigned command_parameter;
char *dropped_state_file = NULL;
static char **custom_palettes;
static unsigned n_custom_palettes;
#ifdef __APPLE__
#define MODIFIER_NAME " " CMD_STRING
#else
#define MODIFIER_NAME CTRL_STRING
#endif
shader_t shader;
static SDL_Rect rect;
static unsigned factor;
static SDL_Surface *converted_background = NULL;
bool screen_manually_resized = false;
void render_texture(void *pixels, void *previous)
{
if (renderer) {
if (pixels) {
SDL_UpdateTexture(texture, NULL, pixels, GB_get_screen_width(&gb) * sizeof (uint32_t));
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
else {
static void *_pixels = NULL;
if (pixels) {
_pixels = pixels;
}
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
GB_frame_blending_mode_t mode = configuration.blending_mode;
if (!previous) {
mode = GB_FRAME_BLENDING_MODE_DISABLED;
}
else if (mode == GB_FRAME_BLENDING_MODE_ACCURATE) {
if (GB_is_sgb(&gb)) {
mode = GB_FRAME_BLENDING_MODE_SIMPLE;
}
else {
mode = GB_is_odd_frame(&gb)? GB_FRAME_BLENDING_MODE_ACCURATE_ODD : GB_FRAME_BLENDING_MODE_ACCURATE_EVEN;
}
}
render_bitmap_with_shader(&shader, _pixels, previous,
GB_get_screen_width(&gb), GB_get_screen_height(&gb),
rect.x, rect.y, rect.w, rect.h,
mode);
SDL_GL_SwapWindow(window);
}
}
static const char *help[] = {
"Keyboard Shortcuts:\n"
" Open Menu: Escape\n"
" Open ROM: " MODIFIER_NAME "+O\n"
" Reset: " MODIFIER_NAME "+R\n"
" Pause: " MODIFIER_NAME "+P\n"
" Save state: " MODIFIER_NAME "+(0-9)\n"
" Load state: " MODIFIER_NAME "+" SHIFT_STRING "+(0-9)\n"
" Toggle Fullscreen " MODIFIER_NAME "+F\n"
#ifdef __APPLE__
" Mute/Unmute: " MODIFIER_NAME "+" SHIFT_STRING "+M\n"
#else
" Mute/Unmute: " MODIFIER_NAME "+M\n"
#endif
" Toggle channel: " ALT_STRING "+(1-4)\n"
" Break Debugger: " CTRL_STRING "+C",
"\n"
"SameBoy\n"
"Version " GB_VERSION "\n\n"
"Copyright " COPYRIGHT_STRING " 2015-" GB_COPYRIGHT_YEAR "\n"
"Lior Halphon\n\n"
"Licensed under the Expat\n"
"License, see LICENSE for\n"
"more details."
};
void update_viewport(void)
{
int win_width, win_height;
SDL_GL_GetDrawableSize(window, &win_width, &win_height);
int logical_width, logical_height;
SDL_GetWindowSize(window, &logical_width, &logical_height);
factor = win_width / logical_width;
double x_factor = win_width / (double) GB_get_screen_width(&gb);
double y_factor = win_height / (double) GB_get_screen_height(&gb);
if (configuration.scaling_mode == GB_SDL_SCALING_INTEGER_FACTOR) {
x_factor = (unsigned)(x_factor);
y_factor = (unsigned)(y_factor);
}
if (configuration.scaling_mode != GB_SDL_SCALING_ENTIRE_WINDOW) {
if (x_factor > y_factor) {
x_factor = y_factor;
}
else {
y_factor = x_factor;
}
}
unsigned new_width = x_factor * GB_get_screen_width(&gb);
unsigned new_height = y_factor * GB_get_screen_height(&gb);
rect = (SDL_Rect){(win_width - new_width) / 2, (win_height - new_height) /2,
new_width, new_height};
if (renderer) {
SDL_RenderSetViewport(renderer, &rect);
}
else {
glViewport(rect.x, rect.y, rect.w, rect.h);
}
}
static void rescale_window(void)
{
SDL_SetWindowSize(window, GB_get_screen_width(&gb) * configuration.default_scale, GB_get_screen_height(&gb) * configuration.default_scale);
}
static void draw_char(uint32_t *buffer, unsigned width, unsigned height, unsigned char ch, uint32_t color, uint32_t *mask_top, uint32_t *mask_bottom)
{
if (ch < ' ' || ch > font_max) {
ch = '?';
}
// Huge dirty hack
if ((signed char)ch == CHECKBOX_ON_STRING[0] || (signed char)ch == CHECKBOX_OFF_STRING[0]) {
if (color == gui_palette_native[3]) {
color = gui_palette_native[0];
}
else if (color == gui_palette_native[0]) {
color = gui_palette_native[3];
ch = CHECKBOX_OFF_STRING[0];
}
}
uint8_t *data = &font[(ch - ' ') * GLYPH_WIDTH * GLYPH_HEIGHT];
for (unsigned y = GLYPH_HEIGHT; y--;) {
for (unsigned x = GLYPH_WIDTH; x--;) {
if (*(data++) && buffer >= mask_top && buffer < mask_bottom) {
(*buffer) = color;
}
buffer++;
}
buffer += width - GLYPH_WIDTH;
}
}
static signed scroll = 0;
static void draw_unbordered_text(uint32_t *buffer, unsigned width, unsigned height, unsigned x, signed y, const char *string, uint32_t color, bool is_osd)
{
if (!is_osd) {
y -= scroll;
}
unsigned orig_x = x;
unsigned y_offset = is_osd? 0 : (GB_get_screen_height(&gb) - 144) / 2;
while (*string) {
if (*string == '\n') {
x = orig_x;
y += GLYPH_HEIGHT + 4;
string++;
continue;
}
if (x > width - GLYPH_WIDTH) {
break;
}
draw_char(&buffer[(signed)(x + width * y)], width, height, *string, color, &buffer[width * y_offset], &buffer[width * (is_osd? GB_get_screen_height(&gb) : y_offset + 144)]);
x += GLYPH_WIDTH;
string++;
}
}
void draw_text(uint32_t *buffer, unsigned width, unsigned height, unsigned x, signed y, const char *string, uint32_t color, uint32_t border, bool is_osd)
{
draw_unbordered_text(buffer, width, height, x - 1, y, string, border, is_osd);
draw_unbordered_text(buffer, width, height, x + 1, y, string, border, is_osd);
draw_unbordered_text(buffer, width, height, x, y - 1, string, border, is_osd);
draw_unbordered_text(buffer, width, height, x, y + 1, string, border, is_osd);
draw_unbordered_text(buffer, width, height, x, y, string, color, is_osd);
}
const char *osd_text = NULL;
unsigned osd_countdown = 0;
unsigned osd_text_lines = 1;
void show_osd_text(const char *text)
{
osd_text_lines = 1;
osd_text = text;
osd_countdown = 30;
while (*text++) {
if (*text == '\n') {
osd_text_lines++;
osd_countdown += 30;
}
}
}
enum style {
STYLE_LEFT,
STYLE_INDENT,
STYLE_CENTER,
STYLE_SELECTION,
STYLE_ARROWS,
};
static void draw_styled_text(uint32_t *buffer, unsigned width, unsigned height, unsigned y, const char *string, uint32_t color, uint32_t border, enum style style)
{
unsigned x = GLYPH_WIDTH * 2 + (width - 160) / 2;
if (style == STYLE_CENTER || style == STYLE_ARROWS) {
x = width / 2 - (unsigned) strlen(string) * GLYPH_WIDTH / 2;
}
else if (style == STYLE_LEFT) {
x = 6 + (width - 160) / 2;
}
draw_text(buffer, width, height, x, y, string, color, border, false);
switch (style) {
case STYLE_SELECTION:
draw_text(buffer, width, height, x - GLYPH_WIDTH, y, SELECTION_STRING, color, border, false);
break;
case STYLE_ARROWS:
draw_text(buffer, width, height, x - GLYPH_WIDTH, y, LEFT_ARROW_STRING, color, border, false);
draw_text(buffer, width, height, width - x, y, RIGHT_ARROW_STRING, color, border, false);
break;
default:
break;
}
}
struct menu_item {
const char *string;
void (*handler)(unsigned);
const char *(*value_getter)(unsigned);
void (*backwards_handler)(unsigned);
};
static const struct menu_item *current_menu = NULL;
static const struct menu_item *root_menu = NULL;
static unsigned menu_height;
static unsigned scrollbar_size;
static bool mouse_scroling = false;
static unsigned current_selection = 0;
static enum {
SHOWING_DROP_MESSAGE,
SHOWING_MENU,
SHOWING_HELP,
WAITING_FOR_KEY,
WAITING_FOR_JBUTTON,
TEXT_INPUT,
} gui_state;
static char text_input_title[26];
static char text_input_title2[26];
static char text_input[26];
static void (*text_input_callback)(char ch) = NULL;
static unsigned joypad_configuration_progress = 0;
static uint8_t joypad_axis_temp;
static void item_exit(unsigned index)
{
pending_command = GB_SDL_QUIT_COMMAND;
}
static unsigned current_help_page = 0;
static void item_help(unsigned index)
{
current_help_page = 0;
gui_state = SHOWING_HELP;
}
static void about(unsigned index)
{
current_help_page = 1;
gui_state = SHOWING_HELP;
}
static void enter_emulation_menu(unsigned index);
static void enter_graphics_menu(unsigned index);
static void enter_keyboard_menu(unsigned index);
static void enter_joypad_menu(unsigned index);
static void enter_audio_menu(unsigned index);
static void enter_controls_menu(unsigned index);
static void enter_help_menu(unsigned index);
static void enter_options_menu(unsigned index);
static void toggle_audio_recording(unsigned index);
extern void set_filename(const char *new_filename, typeof(free) *new_free_function);
static void nop(unsigned index){}
static void open_rom(unsigned index)
{
char *filename = do_open_rom_dialog();
if (filename) {
set_filename(filename, free);
pending_command = GB_SDL_NEW_FILE_COMMAND;
}
}
static void cart_swap(unsigned index)
{
char *filename = do_open_rom_dialog();
if (filename) {
set_filename(filename, free);
pending_command = GB_SDL_CART_SWAP_COMMAND;
}
}
static void recalculate_menu_height(void)
{
menu_height = 24;
scrollbar_size = 0;
if (gui_state == SHOWING_MENU) {
for (const struct menu_item *item = current_menu; item->string; item++) {
menu_height += 12;
if (item->backwards_handler) {
menu_height += 12;
}
}
}
if (menu_height > 144) {
scrollbar_size = 144 * 140 / menu_height;
}
}
#if SDL_COMPILEDVERSION < 2014
int SDL_OpenURL(const char *url)
{
char *string = NULL;
#ifdef __APPLE__
asprintf(&string, "open '%s'", url);
#else
#ifdef _WIN32
asprintf(&string, "explorer '%s'", url);
#else
asprintf(&string, "xdg-open '%s'", url);
#endif
#endif
int ret = system(string);
free(string);
return ret;
}
#endif
static char audio_recording_menu_item[] = "Start Audio Recording";
static void sponsor(unsigned index)
{
SDL_OpenURL("https://github.com/sponsors/LIJI32");
}
static void debugger_help(unsigned index)
{
SDL_OpenURL("https://sameboy.github.io/debugger/");
}
static void return_to_root_menu(unsigned index)
{
current_menu = root_menu;
current_selection = 0;
scroll = 0;
recalculate_menu_height();
}
#ifdef _WIN32
static void associate_rom_files(unsigned index);
#endif
static
#ifndef _WIN32
const
#endif
struct menu_item options_menu[] = {
{"Emulation Options", enter_emulation_menu},
{"Graphic Options", enter_graphics_menu},
{"Audio Options", enter_audio_menu},
{"Control Options", enter_controls_menu},
#ifdef _WIN32
{"Associate ROM Files", associate_rom_files},
#endif
{"Back", return_to_root_menu},
{NULL,}
};
#ifdef _WIN32
static void associate_rom_files(unsigned index)
{
if (GB_do_windows_association()) {
options_menu[index].string = "ROM Files Associated";
}
else {
options_menu[index].string = "Files Association Failed";
}
}
#endif
static void enter_options_menu(unsigned index)
{
current_menu = options_menu;
current_selection = 0;
scroll = 0;
recalculate_menu_height();
}
static const GB_cheat_t *current_cheat = NULL;
extern struct menu_item modify_cheat_menu[];
static void save_cheats(void)
{
extern char *filename;
size_t path_length = strlen(filename);
char cheat_path[path_length + 5];
replace_extension(filename, path_length, cheat_path, ".cht");
GB_save_cheats(&gb, cheat_path);
}
static void rename_callback(char ch)
{
if (ch == '\b' && text_input[0]) {
text_input[strlen(text_input) - 1] = 0;
return;
}
if (ch == '\n') {
GB_update_cheat(&gb,
current_cheat,
text_input,
current_cheat->address,
current_cheat->bank,
current_cheat->value,
current_cheat->old_value,
current_cheat->use_old_value,
current_cheat->enabled);
modify_cheat_menu[0].string = current_cheat->description;
gui_state = SHOWING_MENU;
save_cheats();
SDL_StopTextInput();
return;
}
if (ch < ' ') return;
size_t len = strlen(text_input);
if (len < 21) {
text_input[len] = ch;
text_input[len + 1] = 0;
}
}
static void rename_cheat(unsigned index)
{
strcpy(text_input_title, "Rename Cheat");
text_input_title2[0] = 0;
memcpy(text_input, current_cheat->description, 24);
text_input[24] = 0;
gui_state = TEXT_INPUT;
text_input_callback = rename_callback;
SDL_StartTextInput();
GB_update_cheat(&gb,
current_cheat,
current_cheat->description,
current_cheat->address,
current_cheat->bank,
current_cheat->value,
current_cheat->old_value,
current_cheat->use_old_value,
current_cheat->enabled);
save_cheats();
}
static void toggle_cheat(unsigned index)
{
GB_update_cheat(&gb,
current_cheat,
current_cheat->description,
current_cheat->address,
current_cheat->bank,
current_cheat->value,
current_cheat->old_value,
current_cheat->use_old_value,
!current_cheat->enabled);
save_cheats();
}
static const char *active_cheat_checkbox(unsigned index)
{
return current_cheat->enabled? CHECKBOX_ON_STRING : CHECKBOX_OFF_STRING;
}
static const char *get_cheat_address(unsigned index)
{
static char ret[12];
if (current_cheat->bank == GB_CHEAT_ANY_BANK) {
sprintf(ret, "$%04X", current_cheat->address);
}
else {
sprintf(ret, "$%02X:$%04X", current_cheat->bank, current_cheat->address);
}
return ret;
}
static void change_cheat_address_callback(char ch)
{
if (ch == '\b' && text_input[1]) {
size_t len = strlen(text_input);
text_input[len - 1] = 0;
if (text_input[len - 2] == ':') {
text_input[len - 2] = 0;
}
return;
}
if (ch == '\n') {
uint16_t bank = GB_CHEAT_ANY_BANK;
uint16_t address = 0;
const char *s = text_input + 1;
while (*s) {
if (*s == ':') {
bank = address;
address = 0;
}
else if (*s >= '0' && *s <= '9'){
address *= 0x10;
address += *s - '0';
}
else if (*s >= 'A' && *s <= 'F'){
address *= 0x10;
address += *s - 'A' + 10;
}
s++;
}
GB_update_cheat(&gb,
current_cheat,
current_cheat->description,
address,
bank,
current_cheat->value,
current_cheat->old_value,
current_cheat->use_old_value,
current_cheat->enabled);
save_cheats();
gui_state = SHOWING_MENU;
SDL_StopTextInput();
return;
}
size_t len = strlen(text_input);
if (len >= 12) return;
if (ch == ':' && (len >= 2) && !strchr(text_input, ':')) {
text_input[len] = ':';
text_input[len + 1] = '$';
text_input[len + 2] = 0;
return;
}
ch = toupper(ch);
if (!isxdigit(ch)) return;
unsigned digit_count = 0;
const char *s = text_input + 1;
while (*s) {
if (*s == ':') {
s += 2;
digit_count = 0;
}
else {
s++;
digit_count++;
}
}
if (digit_count == 4) return;
text_input[len] = ch;
text_input[len + 1] = 0;
}
static void change_cheat_address(unsigned index)
{
strcpy(text_input_title, "Enter Cheat Address");
text_input_title2[0] = 0;
strcpy(text_input, get_cheat_address(0));
gui_state = TEXT_INPUT;
text_input_callback = change_cheat_address_callback;
SDL_StartTextInput();
}
static const char *get_cheat_value(unsigned index)
{
static char ret[4];
sprintf(ret, "$%02X", current_cheat->value);
return ret;
}
static void change_cheat_value_callback(char ch)
{
if (ch == '\b' && text_input[1]) {
size_t len = strlen(text_input);
text_input[len - 1] = 0;
return;
}
if (ch == '\n') {
uint8_t value = 0;
const char *s = text_input + 1;
while (*s) {
if (*s >= '0' && *s <= '9'){
value *= 0x10;
value += *s - '0';
}
else if (*s >= 'A' && *s <= 'F'){
value *= 0x10;
value += *s - 'A' + 10;
}
s++;
}
GB_update_cheat(&gb,
current_cheat,
current_cheat->description,
current_cheat->address,
current_cheat->bank,
value,
current_cheat->old_value,
current_cheat->use_old_value,
current_cheat->enabled);
save_cheats();
gui_state = SHOWING_MENU;
SDL_StopTextInput();
return;
}
if (!isxdigit(ch)) return;
ch = toupper(ch);
size_t len = strlen(text_input);
if (len == 3) {
text_input[1] = text_input[2];
text_input[2] = ch;
return;
}
text_input[len] = ch;
text_input[len + 1] = 0;
}
static void change_cheat_value(unsigned index)
{
strcpy(text_input_title, "Enter Cheat Value");
text_input_title2[0] = 0;
strcpy(text_input, get_cheat_value(0));
gui_state = TEXT_INPUT;
text_input_callback = change_cheat_value_callback;
SDL_StartTextInput();
}
static const char *get_cheat_old_value(unsigned index)
{
if (!current_cheat->use_old_value) {
return "Any";
}
static char ret[4];
sprintf(ret, "$%02X", current_cheat->old_value);
return ret;
}
static void change_cheat_old_value_callback(char ch)
{
if (ch == '\b' && strcmp(text_input, "Any") != 0) {
size_t len = strlen(text_input);
if (len == 2) {
strcpy(text_input, "Any");
return;
}
text_input[len - 1] = 0;
return;
}
if (ch == '\n') {
uint8_t value = 0;
bool use_old_value = strcmp(text_input, "Any") != 0;
if (use_old_value) {
const char *s = text_input + 1;
while (*s) {
if (*s >= '0' && *s <= '9'){
value *= 0x10;
value += *s - '0';
}
else if (*s >= 'A' && *s <= 'F'){
value *= 0x10;
value += *s - 'A' + 10;
}
s++;
}
}
GB_update_cheat(&gb,
current_cheat,
current_cheat->description,
current_cheat->address,
current_cheat->bank,
current_cheat->value,
value,
use_old_value,
current_cheat->enabled);
save_cheats();
gui_state = SHOWING_MENU;
SDL_StopTextInput();
return;
}
if (!isxdigit(ch)) return;
ch = toupper(ch);
if (strcmp(text_input, "Any") == 0) {
strcpy(text_input, "$");
}
size_t len = strlen(text_input);
if (len == 3) {
text_input[1] = text_input[2];
text_input[2] = ch;
return;
}
text_input[len] = ch;
text_input[len + 1] = 0;
}
static void change_cheat_old_value(unsigned index)
{
strcpy(text_input_title, "Enter Cheat Old Value");
text_input_title2[0] = 0;
strcpy(text_input, get_cheat_old_value(0));
gui_state = TEXT_INPUT;
text_input_callback = change_cheat_old_value_callback;
SDL_StartTextInput();
}
static void enter_cheats_menu(unsigned index);
static void delete_cheat(unsigned index)
{
GB_remove_cheat(&gb, current_cheat);
save_cheats();
enter_cheats_menu(0);
}
struct menu_item modify_cheat_menu[] = {
{"", rename_cheat},
{"Enable", toggle_cheat, active_cheat_checkbox},
{"Address:", change_cheat_address, get_cheat_address},
{"Value:", change_cheat_value, get_cheat_value},
{"Old Value:", change_cheat_old_value, get_cheat_old_value},
{"Delete Cheat", delete_cheat},
{"Back", enter_cheats_menu},
{NULL,}
};
static void toggle_cheats(unsigned index)
{
GB_set_cheats_enabled(&gb, !GB_cheats_enabled(&gb));
}
static void add_cheat(unsigned index)
{
current_cheat = GB_add_cheat(&gb, "New Cheat", 0, 0, 0, 0, false, true);
modify_cheat_menu[0].string = current_cheat->description;
current_menu = modify_cheat_menu;
current_selection = 0;
scroll = 0;
save_cheats();
}
static void import_cheat_callback(char ch)
{
if (ch == '\b' && text_input[0]) {
size_t len = strlen(text_input);
text_input[len - 1] = 0;
return;
}
if (ch == '\n') {
if (!text_input[0]) {
gui_state = SHOWING_MENU;
SDL_StopTextInput();
return;
}
current_cheat = GB_import_cheat(&gb, text_input, "Imported Cheat", true);
if (current_cheat) {
gui_state = SHOWING_MENU;
modify_cheat_menu[0].string = current_cheat->description;
current_menu = modify_cheat_menu;
current_selection = 0;
scroll = 0;
save_cheats();
SDL_StopTextInput();
return;
}
strcpy(text_input_title, "Invalid Code.");
strcpy(text_input_title2, "Press Enter to Cancel");
text_input[0] = 0;
return;
}
if (ch != '-' && !isxdigit(ch)) return;
ch = toupper(ch);
size_t len = strlen(text_input);
if (len >= 12) {
return;
}
text_input[len] = ch;
text_input[len + 1] = 0;
if (text_input_title[0] != 'E') {
strcpy(text_input_title, "Enter a GameShark");
strcpy(text_input_title2, "or Game Genie Code");
}
}
static void import_cheat(unsigned index)
{
strcpy(text_input_title, "Enter a GameShark");
strcpy(text_input_title2, "or Game Genie Code");
text_input[0] = 0;
gui_state = TEXT_INPUT;
text_input_callback = import_cheat_callback;
save_cheats();
SDL_StartTextInput();
}
static void modify_cheat(unsigned index)
{
const GB_cheat_t *const *cheats = GB_get_cheats(&gb, NULL);
current_cheat = cheats[index - 3];
modify_cheat_menu[0].string = current_cheat->description;
current_menu = modify_cheat_menu;
current_selection = 0;
scroll = 0;
}
static const char *checkbox_for_cheat(unsigned index)
{
const GB_cheat_t *const *cheats = GB_get_cheats(&gb, NULL);
return cheats[index - 3]->enabled? CHECKBOX_ON_STRING : CHECKBOX_OFF_STRING;
}
static const char *cheats_global_checkbox(unsigned index)
{
return GB_cheats_enabled(&gb)? CHECKBOX_ON_STRING : CHECKBOX_OFF_STRING;
}
static void enter_cheats_menu(unsigned index)
{
struct menu_item *cheats_menu = NULL;
if (cheats_menu) {
free(cheats_menu);
}
size_t cheat_count;
const GB_cheat_t *const *cheats = GB_get_cheats(&gb, &cheat_count);
cheats_menu = calloc(cheat_count + 5, sizeof(struct menu_item));
cheats_menu[0] = (struct menu_item){"New Cheat", add_cheat};
cheats_menu[1] = (struct menu_item){"Import Cheat", import_cheat};
cheats_menu[2] = (struct menu_item){"Enable Cheats", toggle_cheats, cheats_global_checkbox};
for (size_t i = 0; i < cheat_count; i++) {
cheats_menu[i + 3] = (struct menu_item){cheats[i]->description, modify_cheat, checkbox_for_cheat};
}
cheats_menu[cheat_count + 3] = (struct menu_item){"Back", return_to_root_menu};
current_menu = cheats_menu;
current_selection = 0;
scroll = 0;
recalculate_menu_height();
}
static const struct menu_item paused_menu[] = {
{"Resume", NULL},
{"Open ROM", open_rom},
{"Hot Swap Cartridge", cart_swap},
{"Options", enter_options_menu},
{"Cheats", enter_cheats_menu},
{audio_recording_menu_item, toggle_audio_recording},
{"Help & About", enter_help_menu},
{"Sponsor SameBoy", sponsor},
{"Quit SameBoy", item_exit},
{NULL,}
};
static struct menu_item nonpaused_menu[sizeof(paused_menu) / sizeof(paused_menu[0]) - 3];
static void __attribute__((constructor)) build_nonpaused_menu(void)
{
const struct menu_item *in = paused_menu;
struct menu_item *out = nonpaused_menu;
while (in->string) {
if (in->handler == NULL || in->handler == cart_swap || in->handler == enter_cheats_menu) {
in++;
continue;
}
*out = *in;
out++;
in++;
}
}
static const struct menu_item help_menu[] = {
{"Shortcuts", item_help},
{"Debugger Help", debugger_help},
{"About SameBoy", about},
{"Back", return_to_root_menu},
{NULL,}
};
static void enter_help_menu(unsigned index)
{
current_menu = help_menu;
current_selection = 0;
scroll = 0;
recalculate_menu_height();
}
static void cycle_model(unsigned index)
{
switch (configuration.model) {
case MODEL_DMG: configuration.model = MODEL_MGB; break;
case MODEL_MGB: configuration.model = MODEL_SGB; break;
case MODEL_SGB: configuration.model = MODEL_CGB; break;
case MODEL_CGB: configuration.model = MODEL_AGB; break;
case MODEL_AGB: configuration.model = MODEL_AUTO; break;
case MODEL_AUTO: configuration.model = MODEL_DMG; break;
default: configuration.model = MODEL_AUTO;
}
pending_command = GB_SDL_RESET_COMMAND;
}
static void cycle_model_backwards(unsigned index)
{
switch (configuration.model) {
case MODEL_MGB: configuration.model = MODEL_DMG; break;
case MODEL_SGB: configuration.model = MODEL_MGB; break;
case MODEL_CGB: configuration.model = MODEL_SGB; break;
case MODEL_AGB: configuration.model = MODEL_CGB; break;
case MODEL_AUTO: configuration.model = MODEL_AGB; break;
case MODEL_DMG: configuration.model = MODEL_AUTO; break;
default: configuration.model = MODEL_AUTO;
}
pending_command = GB_SDL_RESET_COMMAND;
}
static const char *current_model_string(unsigned index)
{
return GB_inline_const(const char *[], {"Game Boy", "Game Boy Color", "Game Boy Advance", "Super Game Boy", "Game Boy Pocket", "Pick Automatically"})
[configuration.model];
}
static void cycle_cgb_revision(unsigned index)
{
if (configuration.cgb_revision == GB_MODEL_CGB_E - GB_MODEL_CGB_0) {
configuration.cgb_revision = 0;
}
else {
configuration.cgb_revision++;
}
pending_command = GB_SDL_RESET_COMMAND;
}
static void cycle_cgb_revision_backwards(unsigned index)
{
if (configuration.cgb_revision == 0) {
configuration.cgb_revision = GB_MODEL_CGB_E - GB_MODEL_CGB_0;
}
else {
configuration.cgb_revision--;
}
pending_command = GB_SDL_RESET_COMMAND;
}
static const char *current_cgb_revision_string(unsigned index)
{
return GB_inline_const(const char *[], {
"CPU CGB 0",
"CPU CGB A",
"CPU CGB B",
"CPU CGB C",
"CPU CGB D",
"CPU CGB E",
})
[configuration.cgb_revision];
}
static void cycle_sgb_revision(unsigned index)
{
configuration.sgb_revision++;
if (configuration.sgb_revision == SGB_MAX) {
configuration.sgb_revision = 0;
}
pending_command = GB_SDL_RESET_COMMAND;
}
static void cycle_sgb_revision_backwards(unsigned index)
{
if (configuration.sgb_revision == 0) {
configuration.sgb_revision = SGB_MAX;
}
configuration.sgb_revision--;
pending_command = GB_SDL_RESET_COMMAND;
}
static const char *current_sgb_revision_string(unsigned index)
{
return GB_inline_const(const char *[], {"Super Game Boy NTSC", "Super Game Boy PAL", "Super Game Boy 2"})
[configuration.sgb_revision];
}
static const uint32_t rewind_lengths[] = {0, 10, 30, 60, 60 * 2, 60 * 5, 60 * 10};
static const char *rewind_strings[] = {"Disabled",
"10 Seconds",
"30 Seconds",
"1 Minute",
"2 Minutes",
"5 Minutes",
"10 Minutes",
};
static void cycle_rewind(unsigned index)
{
for (unsigned i = 0; i < sizeof(rewind_lengths) / sizeof(rewind_lengths[0]) - 1; i++) {
if (configuration.rewind_length == rewind_lengths[i]) {
configuration.rewind_length = rewind_lengths[i + 1];
GB_set_rewind_length(&gb, configuration.rewind_length);
return;
}
}
configuration.rewind_length = rewind_lengths[0];
GB_set_rewind_length(&gb, configuration.rewind_length);
}
static void cycle_rewind_backwards(unsigned index)
{
for (unsigned i = 1; i < sizeof(rewind_lengths) / sizeof(rewind_lengths[0]); i++) {
if (configuration.rewind_length == rewind_lengths[i]) {
configuration.rewind_length = rewind_lengths[i - 1];
GB_set_rewind_length(&gb, configuration.rewind_length);
return;
}
}
configuration.rewind_length = rewind_lengths[sizeof(rewind_lengths) / sizeof(rewind_lengths[0]) - 1];
GB_set_rewind_length(&gb, configuration.rewind_length);
}
static const char *current_rewind_string(unsigned index)
{
for (unsigned i = 0; i < sizeof(rewind_lengths) / sizeof(rewind_lengths[0]); i++) {
if (configuration.rewind_length == rewind_lengths[i]) {
return rewind_strings[i];
}
}
return "Custom";
}
static const char *current_bootrom_string(unsigned index)
{
if (!configuration.bootrom_path[0]) {
return "Built-in Boot ROMs";
}
size_t length = strlen(configuration.bootrom_path);
static char ret[24] = {0,};
if (length <= 23) {
strcpy(ret, configuration.bootrom_path);
}
else {
memcpy(ret, configuration.bootrom_path, 11);
memcpy(ret + 12, configuration.bootrom_path + length - 11, 11);
}
for (unsigned i = 0; i < 24; i++) {
if (ret[i] < 0) {
ret[i] = MOJIBAKE_STRING[0];
}
}
if (length > 23) {
ret[11] = ELLIPSIS_STRING[0];
}
return ret;
}
static void toggle_bootrom(unsigned index)
{
if (configuration.bootrom_path[0]) {
configuration.bootrom_path[0] = 0;
}
else {
char *folder = do_open_folder_dialog();
if (!folder) return;
if (strlen(folder) < sizeof(configuration.bootrom_path) - 1) {
strcpy(configuration.bootrom_path, folder);
}
free(folder);
}
}
static void toggle_rtc_mode(unsigned index)
{
configuration.rtc_mode = !configuration.rtc_mode;
}
static const char *current_rtc_mode_string(unsigned index)
{
switch (configuration.rtc_mode) {
case GB_RTC_MODE_SYNC_TO_HOST: return "Sync to System Clock";
case GB_RTC_MODE_ACCURATE: return "Accurate";
}
return "";
}
static void cycle_agb_revision(unsigned index)
{
configuration.agb_revision ^= GB_MODEL_GBP_BIT;
pending_command = GB_SDL_RESET_COMMAND;
}
static const char *current_agb_revision_string(unsigned index)
{
if (configuration.agb_revision == GB_MODEL_GBP_A) {
return "CPU AGB A (GBP)";
}
return "CPU AGB A (AGB)";
}
static void cycle_turbo_cap(unsigned index)
{
if (configuration.turbo_cap >= 16) { // 400%
configuration.turbo_cap = 0; // uncapped
}
else if (configuration.turbo_cap == 0) { // uncapped
configuration.turbo_cap = 6; // 150%
}
else {
configuration.turbo_cap++;
}
}
static void cycle_turbo_cap_backwards(unsigned index)
{
if (configuration.turbo_cap == 0) { // uncapped
configuration.turbo_cap = 16; // 400%
}
else if (configuration.turbo_cap == 6) { // 150%
configuration.turbo_cap = 0; // uncapped
}
else {
configuration.turbo_cap--;
}
}
static const char *current_turbo_cap_string(unsigned index)
{
if (configuration.turbo_cap == 0) {
return "Uncapped";
}
static char ret[5];
snprintf(ret, sizeof(ret), "%d%%", configuration.turbo_cap * 25);
return ret;
}
static const struct menu_item emulation_menu[] = {
{"Emulated Model:", cycle_model, current_model_string, cycle_model_backwards},
{"SGB Revision:", cycle_sgb_revision, current_sgb_revision_string, cycle_sgb_revision_backwards},
{"GBC Revision:", cycle_cgb_revision, current_cgb_revision_string, cycle_cgb_revision_backwards},
{"GBA Revision:", cycle_agb_revision, current_agb_revision_string, cycle_agb_revision},
{"Boot ROMs Folder:", toggle_bootrom, current_bootrom_string, toggle_bootrom},
{"Rewind Length:", cycle_rewind, current_rewind_string, cycle_rewind_backwards},
{"Real Time Clock:", toggle_rtc_mode, current_rtc_mode_string, toggle_rtc_mode},
{"Turbo speed cap:", cycle_turbo_cap, current_turbo_cap_string, cycle_turbo_cap_backwards},
{"Back", enter_options_menu},
{NULL,}
};
static void enter_emulation_menu(unsigned index)
{
current_menu = emulation_menu;
current_selection = 0;
scroll = 0;
recalculate_menu_height();
}
static const char *current_scaling_mode(unsigned index)
{
return GB_inline_const(const char *[], {"Fill Entire Window", "Retain Aspect Ratio", "Retain Integer Factor"})
[configuration.scaling_mode];
}
static const char *current_default_scale(unsigned index)
{
return GB_inline_const(const char *[], {"1x", "2x", "3x", "4x", "5x", "6x", "7x", "8x"})
[configuration.default_scale - 1];
}
const char *current_color_correction_mode(unsigned index)
{
return GB_inline_const(const char *[], {"Disabled", "Correct Color Curves", "Modern - Balanced", "Modern - Boost Contrast", "Reduce Contrast", "Harsh Reality", "Modern - Accurate"})
[configuration.color_correction_mode];
}
const char *current_color_temperature(unsigned index)
{
static char ret[22];
strcpy(ret, SLIDER_STRING);
ret[configuration.color_temperature] = SELECTED_SLIDER_STRING[configuration.color_temperature];
return ret;
}
const char *current_palette(unsigned index)
{
if (configuration.dmg_palette == 4) {
return configuration.dmg_palette_name;
}
return GB_inline_const(const char *[], {"Greyscale", "Lime (Game Boy)", "Olive (Pocket)", "Teal (Light)"})
[configuration.dmg_palette];
}
const char *current_border_mode(unsigned index)
{
return GB_inline_const(const char *[], {"SGB Only", "Never", "Always"})
[configuration.border_mode];
}
static void cycle_scaling(unsigned index)
{
configuration.scaling_mode++;
if (configuration.scaling_mode == GB_SDL_SCALING_MAX) {
configuration.scaling_mode = 0;
}
update_viewport();
render_texture(NULL, NULL);
}
static void cycle_scaling_backwards(unsigned index)
{
if (configuration.scaling_mode == 0) {
configuration.scaling_mode = GB_SDL_SCALING_MAX - 1;
}
else {
configuration.scaling_mode--;
}
update_viewport();
render_texture(NULL, NULL);
screen_manually_resized = false;
}
static void cycle_default_scale(unsigned index)
{
if (configuration.default_scale == GB_SDL_DEFAULT_SCALE_MAX) {
configuration.default_scale = 1;
}
else {
configuration.default_scale++;
}
rescale_window();
update_viewport();
screen_manually_resized = false;
}
static void cycle_default_scale_backwards(unsigned index)
{
if (configuration.default_scale == 1) {
configuration.default_scale = GB_SDL_DEFAULT_SCALE_MAX;
}
else {
configuration.default_scale--;
}
rescale_window();
update_viewport();
screen_manually_resized = false;
}
static void cycle_color_correction(unsigned index)
{
if (configuration.color_correction_mode == GB_COLOR_CORRECTION_LOW_CONTRAST) {
configuration.color_correction_mode = GB_COLOR_CORRECTION_DISABLED;
}
else if (configuration.color_correction_mode == GB_COLOR_CORRECTION_MODERN_BALANCED) {
configuration.color_correction_mode = GB_COLOR_CORRECTION_MODERN_ACCURATE;
}
else if (configuration.color_correction_mode == GB_COLOR_CORRECTION_MODERN_ACCURATE) {
configuration.color_correction_mode = GB_COLOR_CORRECTION_MODERN_BOOST_CONTRAST;
}
else {
configuration.color_correction_mode++;
}
}
static void cycle_color_correction_backwards(unsigned index)
{
if (configuration.color_correction_mode == GB_COLOR_CORRECTION_DISABLED) {
configuration.color_correction_mode = GB_COLOR_CORRECTION_LOW_CONTRAST;
}
else if (configuration.color_correction_mode == GB_COLOR_CORRECTION_MODERN_ACCURATE) {
configuration.color_correction_mode = GB_COLOR_CORRECTION_MODERN_BALANCED;
}
else if (configuration.color_correction_mode == GB_COLOR_CORRECTION_MODERN_BOOST_CONTRAST) {
configuration.color_correction_mode = GB_COLOR_CORRECTION_MODERN_ACCURATE;
}
else {
configuration.color_correction_mode--;
}
}
static void decrease_color_temperature(unsigned index)
{
if (configuration.color_temperature < 20) {
configuration.color_temperature++;
}
}
static void increase_color_temperature(unsigned index)
{
if (configuration.color_temperature > 0) {
configuration.color_temperature--;
}
}
const GB_palette_t *current_dmg_palette(void)
{
typedef struct __attribute__ ((packed)) {
uint32_t magic;
uint8_t flags;
struct GB_color_s colors[5];
int32_t brightness_bias;
uint32_t hue_bias;
uint32_t hue_bias_strength;
} theme_t;
static theme_t theme;
if (configuration.dmg_palette == 4) {
char *path = resource_path("Palettes");
sprintf(path + strlen(path), "/%s.sbp", configuration.dmg_palette_name);
FILE *file = fopen(path, "rb");
if (!file) return &GB_PALETTE_GREY;
memset(&theme, 0, sizeof(theme));
fread(&theme, sizeof(theme), 1, file);
fclose(file);
#ifdef GB_BIG_ENDIAN
theme.magic = __builtin_bswap32(theme.magic);
#endif
if (theme.magic != 'SBPL') return &GB_PALETTE_GREY;
return (GB_palette_t *)&theme.colors;
}
switch (configuration.dmg_palette) {
case 1: return &GB_PALETTE_DMG;
case 2: return &GB_PALETTE_MGB;
case 3: return &GB_PALETTE_GBL;
default: return &GB_PALETTE_GREY;
}
}
static void update_gui_palette(void)
{
const GB_palette_t *palette = current_dmg_palette();
SDL_Color colors[4];
for (unsigned i = 4; i--; ) {
gui_palette_native[i] = SDL_MapRGB(pixel_format, palette->colors[i].r, palette->colors[i].g, palette->colors[i].b);
colors[i].r = palette->colors[i].r;
colors[i].g = palette->colors[i].g;
colors[i].b = palette->colors[i].b;
}
SDL_Surface *background = SDL_LoadBMP(resource_path("background.bmp"));
/* Create a blank background if background.bmp could not be loaded */
if (!background) {
background = SDL_CreateRGBSurface(0, 160, 144, 8, 0, 0, 0, 0);
}
SDL_SetPaletteColors(background->format->palette, colors, 0, 4);
converted_background = SDL_ConvertSurface(background, pixel_format, 0);
SDL_FreeSurface(background);
}
static void cycle_palette(unsigned index)
{
if (configuration.dmg_palette == 3) {
if (n_custom_palettes == 0) {
configuration.dmg_palette = 0;
}
else {
configuration.dmg_palette = 4;
strcpy(configuration.dmg_palette_name, custom_palettes[0]);
}
}
else if (configuration.dmg_palette == 4) {
for (unsigned i = 0; i < n_custom_palettes; i++) {
if (strcmp(custom_palettes[i], configuration.dmg_palette_name) == 0) {
if (i == n_custom_palettes - 1) {
configuration.dmg_palette = 0;
}
else {
strcpy(configuration.dmg_palette_name, custom_palettes[i + 1]);
}
break;
}
}
}
else {
configuration.dmg_palette++;
}
configuration.gui_palette_enabled = true;
update_gui_palette();
}
static void cycle_palette_backwards(unsigned index)
{
if (configuration.dmg_palette == 0) {
if (n_custom_palettes == 0) {
configuration.dmg_palette = 3;
}
else {
configuration.dmg_palette = 4;
strcpy(configuration.dmg_palette_name, custom_palettes[n_custom_palettes - 1]);
}
}
else if (configuration.dmg_palette == 4) {
for (unsigned i = 0; i < n_custom_palettes; i++) {
if (strcmp(custom_palettes[i], configuration.dmg_palette_name) == 0) {
if (i == 0) {
configuration.dmg_palette = 3;
}
else {
strcpy(configuration.dmg_palette_name, custom_palettes[i - 1]);
}
break;
}
}
}
else {
configuration.dmg_palette--;
}
configuration.gui_palette_enabled = true;
update_gui_palette();
}
static void cycle_border_mode(unsigned index)
{
if (configuration.border_mode == GB_BORDER_ALWAYS) {
configuration.border_mode = GB_BORDER_SGB;
}
else {
configuration.border_mode++;
}
}
static void cycle_border_mode_backwards(unsigned index)
{
if (configuration.border_mode == GB_BORDER_SGB) {
configuration.border_mode = GB_BORDER_ALWAYS;
}
else {
configuration.border_mode--;
}
}
extern bool uses_gl(void);
struct shader_name {
const char *file_name;
const char *display_name;
} shaders[] =
{
{"NearestNeighbor", "Nearest Neighbor"},
{"Bilinear", "Bilinear"},
{"SmoothBilinear", "Smooth Bilinear"},
{"MonoLCD", "Monochrome LCD"},
{"LCD", "LCD Display"},
{"CRT", "CRT Display"},
{"FlatCRT", "Flat CRT Display"},
{"Scale2x", "Scale2x"},
{"Scale4x", "Scale4x"},
{"AAScale2x", "Anti-aliased Scale2x"},
{"AAScale4x", "Anti-aliased Scale4x"},
{"HQ2x", "HQ2x"},
{"OmniScale", "OmniScale"},
{"OmniScaleLegacy", "OmniScale Legacy"},
{"AAOmniScaleLegacy", "AA OmniScale Legacy"},
};
static void cycle_filter(unsigned index)
{
if (!uses_gl()) return;
unsigned i = 0;
for (; i < sizeof(shaders) / sizeof(shaders[0]); i++) {
if (strcmp(shaders[i].file_name, configuration.filter) == 0) {
break;
}
}
i += 1;
if (i >= sizeof(shaders) / sizeof(shaders[0])) {
i -= sizeof(shaders) / sizeof(shaders[0]);
}
strcpy(configuration.filter, shaders[i].file_name);
free_shader(&shader);
if (!init_shader_with_name(&shader, configuration.filter)) {
init_shader_with_name(&shader, "NearestNeighbor");
}
}
static void cycle_filter_backwards(unsigned index)
{
if (!uses_gl()) return;
unsigned i = 0;
for (; i < sizeof(shaders) / sizeof(shaders[0]); i++) {
if (strcmp(shaders[i].file_name, configuration.filter) == 0) {
break;
}
}
i -= 1;
if (i >= sizeof(shaders) / sizeof(shaders[0])) {
i = sizeof(shaders) / sizeof(shaders[0]) - 1;
}
strcpy(configuration.filter, shaders[i].file_name);
free_shader(&shader);
if (!init_shader_with_name(&shader, configuration.filter)) {
init_shader_with_name(&shader, "NearestNeighbor");
}
}
static const char *current_filter_name(unsigned index)
{
if (!uses_gl()) return "Requires OpenGL 3.2+";
unsigned i = 0;
for (; i < sizeof(shaders) / sizeof(shaders[0]); i++) {
if (strcmp(shaders[i].file_name, configuration.filter) == 0) {
break;
}
}
if (i == sizeof(shaders) / sizeof(shaders[0])) {
i = 0;
}
return shaders[i].display_name;
}
static void cycle_blending_mode(unsigned index)
{
if (!uses_gl()) return;
if (configuration.blending_mode == GB_FRAME_BLENDING_MODE_ACCURATE) {
configuration.blending_mode = GB_FRAME_BLENDING_MODE_DISABLED;
}
else {
configuration.blending_mode++;
}
}
static void cycle_blending_mode_backwards(unsigned index)
{
if (!uses_gl()) return;
if (configuration.blending_mode == GB_FRAME_BLENDING_MODE_DISABLED) {
configuration.blending_mode = GB_FRAME_BLENDING_MODE_ACCURATE;
}
else {
configuration.blending_mode--;
}
}
static const char *blending_mode_string(unsigned index)
{
if (!uses_gl()) return "Requires OpenGL 3.2+";
return GB_inline_const(const char *[], {"Disabled", "Simple", "Accurate"})
[configuration.blending_mode];
}
static void toggle_osd(unsigned index)
{
osd_countdown = 0;
configuration.osd = !configuration.osd;
}
static const char *current_osd_mode(unsigned index)
{
return configuration.osd? "Enabled" : "Disabled";
}
static const char *current_vsync_mode(unsigned index)
{
switch (configuration.vsync_mode) {
default:
case 0: return "Disabled";
case 1: return "Enabled";
case -1: return "Adaptive";
}
}
static void cycle_vsync(unsigned index)
{
retry:
configuration.vsync_mode++;
if (configuration.vsync_mode == 2) {
configuration.vsync_mode = -1;
}
if (SDL_GL_SetSwapInterval(configuration.vsync_mode) && configuration.vsync_mode != 0) {
goto retry;
}
}
static void cycle_vsync_backwards(unsigned index)
{
retry:
configuration.vsync_mode--;
if (configuration.vsync_mode == -2) {
configuration.vsync_mode = 1;
}
if (SDL_GL_SetSwapInterval(configuration.vsync_mode) && configuration.vsync_mode != 0) {
goto retry;
}
}
#ifdef _WIN32
// Don't use the standard header definitions because we might not have the newest headers
typedef enum {
DWM_CORNER_DEFAULT = 0,
DWM_CORNER_SQUARE = 1,
DWM_CORNER_ROUND = 2,
DWM_CORNER_ROUNDSMALL = 3
} DMW_corner_settings_t;
#define DWM_CORNER_PREFERENCE 33
void configure_window_corners(void)
{
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(window, &wmInfo);
HWND hwnd = wmInfo.info.win.window;
DMW_corner_settings_t pref = configuration.disable_rounded_corners? DWM_CORNER_SQUARE : DWM_CORNER_DEFAULT;
DwmSetWindowAttribute(hwnd, DWM_CORNER_PREFERENCE, &pref, sizeof(pref));
}
static void toggle_corners(unsigned index)
{
configuration.disable_rounded_corners = !configuration.disable_rounded_corners;
configure_window_corners();
}
static const char *current_corner_mode(unsigned index)
{
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(window, &wmInfo);
HWND hwnd = wmInfo.info.win.window;
DMW_corner_settings_t pref;
if (DwmGetWindowAttribute(hwnd, DWM_CORNER_PREFERENCE, &pref, sizeof(pref)) ||
pref == DWM_CORNER_SQUARE) {
return "Square";
}
return "Rounded";
}
#endif
static const struct menu_item graphics_menu[] = {
{"Scaling Mode:", cycle_scaling, current_scaling_mode, cycle_scaling_backwards},
{"Default Window Scale:", cycle_default_scale, current_default_scale, cycle_default_scale_backwards},
{"Scaling Filter:", cycle_filter, current_filter_name, cycle_filter_backwards},
{"Color Correction:", cycle_color_correction, current_color_correction_mode, cycle_color_correction_backwards},
{"Ambient Light Temp.:", decrease_color_temperature, current_color_temperature, increase_color_temperature},
{"Frame Blending:", cycle_blending_mode, blending_mode_string, cycle_blending_mode_backwards},
{"Mono Palette:", cycle_palette, current_palette, cycle_palette_backwards},
{"Display Border:", cycle_border_mode, current_border_mode, cycle_border_mode_backwards},
{"On-Screen Display:", toggle_osd, current_osd_mode, toggle_osd},
{"Vsync Mode:", cycle_vsync, current_vsync_mode, cycle_vsync_backwards},
#ifdef _WIN32
{"Window Corners:", toggle_corners, current_corner_mode, toggle_corners},
#endif
{"Back", enter_options_menu},
{NULL,}
};
static void enter_graphics_menu(unsigned index)
{
current_menu = graphics_menu;
current_selection = 0;
scroll = 0;
recalculate_menu_height();
}
static const char *highpass_filter_string(unsigned index)
{
return GB_inline_const(const char *[], {"None (Keep DC Offset)", "Accurate", "Preserve Waveform"})
[configuration.highpass_mode];
}
static void cycle_highpass_filter(unsigned index)
{
configuration.highpass_mode++;
if (configuration.highpass_mode == GB_HIGHPASS_MAX) {
configuration.highpass_mode = 0;
}
}
static void cycle_highpass_filter_backwards(unsigned index)
{
if (configuration.highpass_mode == 0) {
configuration.highpass_mode = GB_HIGHPASS_MAX - 1;
}
else {
configuration.highpass_mode--;
}
}
static const char *volume_string(unsigned index)
{
static char ret[5];
sprintf(ret, "%d%%", configuration.volume);
return ret;
}
static void increase_volume(unsigned index)
{
configuration.volume += 5;
if (configuration.volume > 100) {
configuration.volume = 100;
}
}
static void decrease_volume(unsigned index)
{
configuration.volume -= 5;
if (configuration.volume > 100) {
configuration.volume = 0;
}
}
static const char *interference_volume_string(unsigned index)
{
static char ret[5];
sprintf(ret, "%d%%", configuration.interference_volume);
return ret;
}
static void increase_interference_volume(unsigned index)
{
configuration.interference_volume += 5;
if (configuration.interference_volume > 100) {
configuration.interference_volume = 100;
}
}
static void decrease_interference_volume(unsigned index)
{
configuration.interference_volume -= 5;
if (configuration.interference_volume > 100) {
configuration.interference_volume = 0;
}
}
static const char *audio_driver_string(unsigned index)
{
return GB_audio_driver_name();
}
static const char *preferred_audio_driver_string(unsigned index)
{
if (configuration.audio_driver[0] == 0) {
return "Auto";
}
return configuration.audio_driver;
}
static void audio_driver_changed(void);
static void cycle_prefrered_audio_driver(unsigned index)
{
audio_driver_changed();
if (configuration.audio_driver[0] == 0) {
strcpy(configuration.audio_driver, GB_audio_driver_name_at_index(0));
return;
}
unsigned i = 0;
while (true) {
const char *name = GB_audio_driver_name_at_index(i);
if (name[0] == 0) { // Not a supported driver? Switch to auto
configuration.audio_driver[0] = 0;
return;
}
if (strcmp(configuration.audio_driver, name) == 0) {
strcpy(configuration.audio_driver, GB_audio_driver_name_at_index(i + 1));
return;
}
i++;
}
}
static void cycle_preferred_audio_driver_backwards(unsigned index)
{
audio_driver_changed();
if (configuration.audio_driver[0] == 0) {
unsigned i = 0;
while (true) {
const char *name = GB_audio_driver_name_at_index(i);
if (name[0] == 0) {
strcpy(configuration.audio_driver, GB_audio_driver_name_at_index(i - 1));
return;
}
i++;
}
return;
}
unsigned i = 0;
while (true) {
const char *name = GB_audio_driver_name_at_index(i);
if (name[0] == 0) { // Not a supported driver? Switch to auto
configuration.audio_driver[0] = 0;
return;
}
if (strcmp(configuration.audio_driver, name) == 0) {
strcpy(configuration.audio_driver, GB_audio_driver_name_at_index(i - 1));
return;
}
i++;
}
}
static struct menu_item audio_menu[] = {
{"Highpass Filter:", cycle_highpass_filter, highpass_filter_string, cycle_highpass_filter_backwards},
{"Volume:", increase_volume, volume_string, decrease_volume},
{"Interference Volume:", increase_interference_volume, interference_volume_string, decrease_interference_volume},
{"Preferred Audio Driver:", cycle_prefrered_audio_driver, preferred_audio_driver_string, cycle_preferred_audio_driver_backwards},
{"Active Driver:", nop, audio_driver_string},
{"Back", enter_options_menu},
{NULL,}
};
static void audio_driver_changed(void)
{
audio_menu[4].value_getter = NULL;
audio_menu[4].string = "Relaunch to apply";
}
static void enter_audio_menu(unsigned index)
{
current_menu = audio_menu;
current_selection = 0;
scroll = 0;
recalculate_menu_height();
}
static void modify_key(unsigned index)
{
gui_state = WAITING_FOR_KEY;
}
static const char *key_name(unsigned index);
static const struct menu_item keyboard_menu[] = {
{"Right:", modify_key, key_name,},
{"Left:", modify_key, key_name,},
{"Up:", modify_key, key_name,},
{"Down:", modify_key, key_name,},
{"A:", modify_key, key_name,},
{"B:", modify_key, key_name,},
{"Select:", modify_key, key_name,},
{"Start:", modify_key, key_name,},
{"Turbo:", modify_key, key_name,},
{"Rewind:", modify_key, key_name,},
{"Slow-Motion:", modify_key, key_name,},
{"Rapid A:", modify_key, key_name,},
{"Rapid B:", modify_key, key_name,},
{"Back", enter_controls_menu},
{NULL,}
};
static const char *key_name(unsigned index)
{
SDL_Scancode code = index >= GB_CONF_KEYS_COUNT? configuration.keys_2[index - GB_CONF_KEYS_COUNT] : configuration.keys[index];
if (!code) return "Not Set";
return SDL_GetScancodeName(code);
}
static void enter_keyboard_menu(unsigned index)
{
current_menu = keyboard_menu;
current_selection = 0;
scroll = 0;
recalculate_menu_height();
}
static unsigned joypad_index = 0;
static SDL_GameController *controller = NULL;
SDL_Haptic *haptic = NULL;
SDL_Joystick *joystick = NULL;
static const char *current_joypad_name(unsigned index)
{
static char name[23] = {0,};
const char *orig_name = joystick? SDL_JoystickName(joystick) : NULL;
if (!orig_name) return "Not Found";
unsigned i = 0;
// SDL returns a name with repeated and trailing spaces
while (*orig_name && i < sizeof(name) - 2) {
if (orig_name[0] != ' ' || orig_name[1] != ' ') {
name[i++] = *orig_name > 0? *orig_name : MOJIBAKE_STRING[0];
}
orig_name++;
}
if (i && name[i - 1] == ' ') {
i--;
}
name[i] = 0;
return name;
}
static void cycle_joypads(unsigned index)
{
joypad_index++;
if (joypad_index >= SDL_NumJoysticks()) {
joypad_index = 0;
}
if (haptic) {
SDL_HapticClose(haptic);
haptic = NULL;
}
if (controller) {
SDL_GameControllerClose(controller);
controller = NULL;
}
else if (joystick) {
SDL_JoystickClose(joystick);
joystick = NULL;
}
if ((controller = SDL_GameControllerOpen(joypad_index))) {
joystick = SDL_GameControllerGetJoystick(controller);
}
else {
joystick = SDL_JoystickOpen(joypad_index);
}
if (joystick) {
haptic = SDL_HapticOpenFromJoystick(joystick);
}
}
static void cycle_joypads_backwards(unsigned index)
{
joypad_index--;
if (joypad_index >= SDL_NumJoysticks()) {
joypad_index = SDL_NumJoysticks() - 1;
}
if (haptic) {
SDL_HapticClose(haptic);
haptic = NULL;
}
if (controller) {
SDL_GameControllerClose(controller);
controller = NULL;
}
else if (joystick) {
SDL_JoystickClose(joystick);
joystick = NULL;
}
if ((controller = SDL_GameControllerOpen(joypad_index))) {
joystick = SDL_GameControllerGetJoystick(controller);
}
else {
joystick = SDL_JoystickOpen(joypad_index);
}
if (joystick) {
haptic = SDL_HapticOpenFromJoystick(joystick);
}}
static void detect_joypad_layout(unsigned index)
{
gui_state = WAITING_FOR_JBUTTON;
joypad_configuration_progress = 0;
joypad_axis_temp = -1;
}
static void cycle_rumble_mode(unsigned index)
{
if (configuration.rumble_mode == GB_RUMBLE_ALL_GAMES) {
configuration.rumble_mode = GB_RUMBLE_DISABLED;
}
else {
configuration.rumble_mode++;
}
}
static void cycle_rumble_mode_backwards(unsigned index)
{
if (configuration.rumble_mode == GB_RUMBLE_DISABLED) {
configuration.rumble_mode = GB_RUMBLE_ALL_GAMES;
}
else {
configuration.rumble_mode--;
}
}
static const char *current_rumble_mode(unsigned index)
{
return GB_inline_const(const char *[], {"Disabled", "Rumble Game Paks Only", "All Games"})
[configuration.rumble_mode];
}
static void toggle_use_faux_analog_inputs(unsigned index)
{
configuration.use_faux_analog_inputs ^= true;
}
static const char *current_faux_analog_inputs(unsigned index)
{
return configuration.use_faux_analog_inputs? "Faux Analog" : "Digital";
}
static void toggle_allow_background_controllers(unsigned index)
{
configuration.allow_background_controllers ^= true;
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
configuration.allow_background_controllers? "1" : "0");
}
static const char *current_background_control_mode(unsigned index)
{
return configuration.allow_background_controllers? "Always" : "During Window Focus Only";
}
static void cycle_hotkey(unsigned index)
{
if (configuration.hotkey_actions[index - 2] == HOTKEY_MAX) {
configuration.hotkey_actions[index - 2] = 0;
}
else {
configuration.hotkey_actions[index - 2]++;
}
}
static void cycle_hotkey_backwards(unsigned index)
{
if (configuration.hotkey_actions[index - 2] == 0) {
configuration.hotkey_actions[index - 2] = HOTKEY_MAX;
}
else {
configuration.hotkey_actions[index - 2]--;
}
}
static const char *current_hotkey(unsigned index)
{
return GB_inline_const(const char *[], {
"None",
"Toggle Pause",
"Toggle Mute",
"Reset",
"Quit SameBoy",
"Save State Slot 1",
"Load State Slot 1",
"Save State Slot 2",
"Load State Slot 2",
"Save State Slot 3",
"Load State Slot 3",
"Save State Slot 4",
"Load State Slot 4",
"Save State Slot 5",
"Load State Slot 5",
"Save State Slot 6",
"Load State Slot 6",
"Save State Slot 7",
"Load State Slot 7",
"Save State Slot 8",
"Load State Slot 8",
"Save State Slot 9",
"Load State Slot 9",
"Save State Slot 10",
"Load State Slot 10",
}) [configuration.hotkey_actions[index - 2]];
}
static const struct menu_item joypad_menu[] = {
{"Joypad:", cycle_joypads, current_joypad_name, cycle_joypads_backwards},
{"Configure layout", detect_joypad_layout},
{"Hotkey 1 Action:", cycle_hotkey, current_hotkey, cycle_hotkey_backwards},
{"Hotkey 2 Action:", cycle_hotkey, current_hotkey, cycle_hotkey_backwards},
{"Rumble Mode:", cycle_rumble_mode, current_rumble_mode, cycle_rumble_mode_backwards},
{"Analog Stick Behavior:", toggle_use_faux_analog_inputs, current_faux_analog_inputs, toggle_use_faux_analog_inputs},
{"Enable Control:", toggle_allow_background_controllers, current_background_control_mode, toggle_allow_background_controllers},
{"Back", enter_controls_menu},
{NULL,}
};
static void enter_joypad_menu(unsigned index)
{
current_menu = joypad_menu;
current_selection = 0;
scroll = 0;
recalculate_menu_height();
}
joypad_button_t get_joypad_button(uint8_t physical_button)
{
for (unsigned i = 0; i < JOYPAD_BUTTONS_MAX; i++) {
if (configuration.joypad_configuration[i] == physical_button) {
return i;
}
}
return JOYPAD_BUTTONS_MAX;
}
joypad_axis_t get_joypad_axis(uint8_t physical_axis)
{
for (unsigned i = 0; i < JOYPAD_AXISES_MAX; i++) {
if (configuration.joypad_axises[i] == physical_axis) {
return i;
}
}
return JOYPAD_AXISES_MAX;
}
void connect_joypad(void)
{
if (joystick && !SDL_NumJoysticks()) {
if (controller) {
SDL_GameControllerClose(controller);
controller = NULL;
joystick = NULL;
}
else {
SDL_JoystickClose(joystick);
joystick = NULL;
}
}
else if (!joystick && SDL_NumJoysticks()) {
if ((controller = SDL_GameControllerOpen(0))) {
joystick = SDL_GameControllerGetJoystick(controller);
}
else {
joystick = SDL_JoystickOpen(0);
}
}
if (joystick) {
haptic = SDL_HapticOpenFromJoystick(joystick);
}
}
static void toggle_mouse_control(unsigned index)
{
configuration.allow_mouse_controls = !configuration.allow_mouse_controls;
}
static const char *mouse_control_string(unsigned index)
{
return configuration.allow_mouse_controls? "Allow mouse control" : "Disallow mouse control";
}
static const struct menu_item controls_menu[] = {
{"Keyboard Options", enter_keyboard_menu},
{"Joypad Options", enter_joypad_menu},
{"Motion-controlled games:", toggle_mouse_control, mouse_control_string, toggle_mouse_control},
{"Back", enter_options_menu},
{NULL,}
};
static void enter_controls_menu(unsigned index)
{
current_menu = controls_menu;
current_selection = 0;
scroll = 0;
recalculate_menu_height();
}
static void toggle_audio_recording(unsigned index)
{
if (!GB_is_inited(&gb)) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Cannot start audio recording, open a ROM file first.", window);
return;
}
static bool is_recording = false;
if (is_recording) {
is_recording = false;
show_osd_text("Audio recording ended");
int error = GB_stop_audio_recording(&gb);
if (error) {
char *message = NULL;
asprintf(&message, "Could not finalize recording: %s", strerror(error));
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", message, window);
free(message);
}
static const char item_string[] = "Start Audio Recording";
memcpy(audio_recording_menu_item, item_string, sizeof(item_string));
return;
}
char *filename = do_save_recording_dialog(GB_get_sample_rate(&gb));
/* Drop events as it SDL seems to catch several in-dialog events */
SDL_Event event;
while (SDL_PollEvent(&event));
if (filename) {
GB_audio_format_t format = GB_AUDIO_FORMAT_RAW;
size_t length = strlen(filename);
if (length >= 5) {
if (strcasecmp(".aiff", filename + length - 5) == 0) {
format = GB_AUDIO_FORMAT_AIFF;
}
else if (strcasecmp(".aifc", filename + length - 5) == 0) {
format = GB_AUDIO_FORMAT_AIFF;
}
else if (length >= 4) {
if (strcasecmp(".aif", filename + length - 4) == 0) {
format = GB_AUDIO_FORMAT_AIFF;
}
else if (strcasecmp(".wav", filename + length - 4) == 0) {
format = GB_AUDIO_FORMAT_WAV;
}
}
}
int error = GB_start_audio_recording(&gb, filename, format);
free(filename);
if (error) {
char *message = NULL;
asprintf(&message, "Could not finalize recording: %s", strerror(error));
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", message, window);
free(message);
return;
}
is_recording = true;
static const char item_string[] = "Stop Audio Recording";
memcpy(audio_recording_menu_item, item_string, sizeof(item_string));
show_osd_text("Audio recording started");
}
}
void convert_mouse_coordinates(signed *x, signed *y)
{
signed width = GB_get_screen_width(&gb);
signed height = GB_get_screen_height(&gb);
signed x_offset = (width - 160) / 2;
signed y_offset = (height - 144) / 2;
*x = (signed)(*x - rect.x / factor) * width / (signed)(rect.w / factor) - x_offset;
*y = (signed)(*y - rect.y / factor) * height / (signed)(rect.h / factor) - y_offset;
if (strcmp("CRT", configuration.filter) == 0) {
*y = *y * 8 / 7;
*y -= 144 / 16;
}
}
void update_swap_interval(void)
{
SDL_DisplayMode mode;
SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(window), &mode);
if (mode.refresh_rate >= 60) {
if (SDL_GL_SetSwapInterval(1)) {
SDL_GL_SetSwapInterval(0);
}
}
else {
SDL_GL_SetSwapInterval(0);
}
}
void run_gui(bool is_running)
{
SDL_ShowCursor(SDL_ENABLE);
connect_joypad();
/* Draw the background screen */
if (!converted_background) {
if (configuration.gui_palette_enabled) {
update_gui_palette();
}
else {
SDL_Surface *background = SDL_LoadBMP(resource_path("background.bmp"));
/* Create a blank background if background.bmp could not be loaded */
if (!background) {
background = SDL_CreateRGBSurface(0, 160, 144, 8, 0, 0, 0, 0);
}
SDL_SetPaletteColors(background->format->palette, gui_palette, 0, 4);
converted_background = SDL_ConvertSurface(background, pixel_format, 0);
SDL_FreeSurface(background);
for (unsigned i = 4; i--; ) {
gui_palette_native[i] = SDL_MapRGB(pixel_format, gui_palette[i].r, gui_palette[i].g, gui_palette[i].b);
}
}
}
unsigned width = GB_get_screen_width(&gb);
unsigned height = GB_get_screen_height(&gb);
unsigned x_offset = (width - 160) / 2;
unsigned y_offset = (height - 144) / 2;
uint32_t pixels[width * height];
if (width != 160 || height != 144) {
for (unsigned i = 0; i < width * height; i++) {
pixels[i] = gui_palette_native[0];
}
}
SDL_Event event = {0,};
gui_state = is_running? SHOWING_MENU : SHOWING_DROP_MESSAGE;
bool should_render = true;
current_menu = root_menu = is_running? paused_menu : nonpaused_menu;
recalculate_menu_height();
current_selection = 0;
scroll = 0;
bool scrollbar_drag = false;
signed scroll_mouse_start = 0;
signed scroll_start = 0;
while (true) {
SDL_WaitEvent(&event);
/* Convert Joypad and mouse events (We only generate down events) */
if (gui_state != WAITING_FOR_KEY && gui_state != WAITING_FOR_JBUTTON && gui_state != TEXT_INPUT) {
switch (event.type) {
case SDL_KEYDOWN:
if (gui_state == WAITING_FOR_KEY) break;
if (event.key.keysym.mod != 0) break;
switch (event.key.keysym.scancode) {
// Do not remap these keys to prevent deadlocking
case SDL_SCANCODE_ESCAPE:
case SDL_SCANCODE_RETURN:
case SDL_SCANCODE_RIGHT:
case SDL_SCANCODE_LEFT:
case SDL_SCANCODE_UP:
case SDL_SCANCODE_DOWN:
case SDL_SCANCODE_H:
case SDL_SCANCODE_J:
case SDL_SCANCODE_K:
case SDL_SCANCODE_L:
break;
default:
if (event.key.keysym.scancode == configuration.keys[GB_KEY_RIGHT]) event.key.keysym.scancode = SDL_SCANCODE_RIGHT;
else if (event.key.keysym.scancode == configuration.keys[GB_KEY_LEFT]) event.key.keysym.scancode = SDL_SCANCODE_LEFT;
else if (event.key.keysym.scancode == configuration.keys[GB_KEY_UP]) event.key.keysym.scancode = SDL_SCANCODE_UP;
else if (event.key.keysym.scancode == configuration.keys[GB_KEY_DOWN]) event.key.keysym.scancode = SDL_SCANCODE_DOWN;
else if (event.key.keysym.scancode == configuration.keys[GB_KEY_A]) event.key.keysym.scancode = SDL_SCANCODE_RETURN;
else if (event.key.keysym.scancode == configuration.keys[GB_KEY_START]) event.key.keysym.scancode = SDL_SCANCODE_RETURN;
else if (event.key.keysym.scancode == configuration.keys[GB_KEY_B]) event.key.keysym.scancode = SDL_SCANCODE_ESCAPE;
break;
}
break;
case SDL_WINDOWEVENT:
should_render = true;
break;
case SDL_MOUSEBUTTONUP:
scrollbar_drag = false;
break;
case SDL_MOUSEBUTTONDOWN:
if (gui_state == SHOWING_HELP) {
event.type = SDL_KEYDOWN;
event.key.keysym.scancode = SDL_SCANCODE_RETURN;
}
else if (gui_state == SHOWING_DROP_MESSAGE) {
event.type = SDL_KEYDOWN;
event.key.keysym.scancode = SDL_SCANCODE_ESCAPE;
}
else if (gui_state == SHOWING_MENU) {
signed x = event.button.x;
signed y = event.button.y;
convert_mouse_coordinates(&x, &y);
if (x >= 160 - 6 && x < 160 && menu_height > 144) {
unsigned scrollbar_offset = (140 - scrollbar_size) * scroll / (menu_height - 144);
if (scrollbar_offset + scrollbar_size > 140) {
scrollbar_offset = 140 - scrollbar_size;
}
if (y < scrollbar_offset || y > scrollbar_offset + scrollbar_size) {
scroll = (menu_height - 144) * y / 143;
should_render = true;
}
scrollbar_drag = true;
mouse_scroling = true;
scroll_mouse_start = y;
scroll_start = scroll;
break;
}
y += scroll;
if (x < 0 || x >= 160 || y < 24) {
continue;
}
unsigned item_y = 24;
unsigned index = 0;
for (const struct menu_item *item = current_menu; item->string; item++, index++) {
if (!item->backwards_handler) {
if (y >= item_y && y < item_y + 12) {
break;
}
item_y += 12;
}
else {
if (y >= item_y && y < item_y + 24) {
break;
}
item_y += 24;
}
}
if (!current_menu[index].string) continue;
current_selection = index;
event.type = SDL_KEYDOWN;
if (current_menu[index].backwards_handler) {
event.key.keysym.scancode = x < 80? SDL_SCANCODE_LEFT : SDL_SCANCODE_RIGHT;
}
else {
event.key.keysym.scancode = SDL_SCANCODE_RETURN;
}
}
break;
case SDL_JOYBUTTONDOWN:
event.type = SDL_KEYDOWN;
joypad_button_t button = get_joypad_button(event.jbutton.button);
if (button == JOYPAD_BUTTON_A) {
event.key.keysym.scancode = SDL_SCANCODE_RETURN;
}
else if (button == JOYPAD_BUTTON_MENU || button == JOYPAD_BUTTON_B) {
event.key.keysym.scancode = SDL_SCANCODE_ESCAPE;
}
else if (button == JOYPAD_BUTTON_UP) event.key.keysym.scancode = SDL_SCANCODE_UP;
else if (button == JOYPAD_BUTTON_DOWN) event.key.keysym.scancode = SDL_SCANCODE_DOWN;
else if (button == JOYPAD_BUTTON_LEFT) event.key.keysym.scancode = SDL_SCANCODE_LEFT;
else if (button == JOYPAD_BUTTON_RIGHT) event.key.keysym.scancode = SDL_SCANCODE_RIGHT;
break;
case SDL_JOYHATMOTION: {
uint8_t value = event.jhat.value;
if (value != 0) {
uint32_t scancode =
value == SDL_HAT_UP ? SDL_SCANCODE_UP
: value == SDL_HAT_DOWN ? SDL_SCANCODE_DOWN
: value == SDL_HAT_LEFT ? SDL_SCANCODE_LEFT
: value == SDL_HAT_RIGHT ? SDL_SCANCODE_RIGHT
: 0;
if (scancode != 0) {
event.type = SDL_KEYDOWN;
event.key.keysym.scancode = scancode;
}
}
break;
}
case SDL_JOYAXISMOTION: {
static bool axis_active[2] = {false, false};
joypad_axis_t axis = get_joypad_axis(event.jaxis.axis);
if (axis == JOYPAD_AXISES_X) {
if (!axis_active[0] && event.jaxis.value > JOYSTICK_HIGH) {
axis_active[0] = true;
event.type = SDL_KEYDOWN;
event.key.keysym.scancode = SDL_SCANCODE_RIGHT;
}
else if (!axis_active[0] && event.jaxis.value < -JOYSTICK_HIGH) {
axis_active[0] = true;
event.type = SDL_KEYDOWN;
event.key.keysym.scancode = SDL_SCANCODE_LEFT;
}
else if (axis_active[0] && event.jaxis.value < JOYSTICK_LOW && event.jaxis.value > -JOYSTICK_LOW) {
axis_active[0] = false;
}
}
else if (axis == JOYPAD_AXISES_Y) {
if (!axis_active[1] && event.jaxis.value > JOYSTICK_HIGH) {
axis_active[1] = true;
event.type = SDL_KEYDOWN;
event.key.keysym.scancode = SDL_SCANCODE_DOWN;
}
else if (!axis_active[1] && event.jaxis.value < -JOYSTICK_HIGH) {
axis_active[1] = true;
event.type = SDL_KEYDOWN;
event.key.keysym.scancode = SDL_SCANCODE_UP;
}
else if (axis_active[1] && event.jaxis.value < JOYSTICK_LOW && event.jaxis.value > -JOYSTICK_LOW) {
axis_active[1] = false;
}
}
}
}
}
switch (event.type) {
case SDL_DISPLAYEVENT:
update_swap_interval();
break;
case SDL_QUIT: {
if (!is_running) {
exit(0);
}
else {
pending_command = GB_SDL_QUIT_COMMAND;
return;
}
}
case SDL_WINDOWEVENT: {
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
update_viewport();
render_texture(NULL, NULL);
screen_manually_resized = true;
}
if (event.window.type == SDL_WINDOWEVENT_MOVED
#if SDL_COMPILEDVERSION > 2018
|| event.window.type == SDL_WINDOWEVENT_DISPLAY_CHANGED
#endif
) {
update_swap_interval();
}
break;
}
case SDL_DROPFILE: {
if (GB_is_save_state(event.drop.file)) {
if (GB_is_inited(&gb)) {
dropped_state_file = event.drop.file;
pending_command = GB_SDL_LOAD_STATE_FROM_FILE_COMMAND;
}
else {
SDL_free(event.drop.file);
}
break;
}
else {
set_filename(event.drop.file, SDL_free);
pending_command = GB_SDL_NEW_FILE_COMMAND;
return;
}
}
case SDL_JOYBUTTONDOWN: {
if (gui_state == WAITING_FOR_JBUTTON && joypad_configuration_progress != JOYPAD_BUTTONS_MAX) {
should_render = true;
configuration.joypad_configuration[joypad_configuration_progress++] = event.jbutton.button;
}
break;
}
case SDL_JOYHATMOTION: {
if (gui_state == WAITING_FOR_JBUTTON && joypad_configuration_progress == JOYPAD_BUTTON_RIGHT) {
should_render = true;
configuration.joypad_configuration[joypad_configuration_progress++] = -1;
configuration.joypad_configuration[joypad_configuration_progress++] = -1;
configuration.joypad_configuration[joypad_configuration_progress++] = -1;
configuration.joypad_configuration[joypad_configuration_progress++] = -1;
}
break;
}
case SDL_JOYAXISMOTION: {
if (gui_state == WAITING_FOR_JBUTTON &&
joypad_configuration_progress == JOYPAD_BUTTONS_MAX &&
abs(event.jaxis.value) >= 0x4000) {
if (joypad_axis_temp == (uint8_t)-1) {
joypad_axis_temp = event.jaxis.axis;
}
else if (joypad_axis_temp != event.jaxis.axis) {
if (joypad_axis_temp < event.jaxis.axis) {
configuration.joypad_axises[JOYPAD_AXISES_X] = joypad_axis_temp;
configuration.joypad_axises[JOYPAD_AXISES_Y] = event.jaxis.axis;
}
else {
configuration.joypad_axises[JOYPAD_AXISES_Y] = joypad_axis_temp;
configuration.joypad_axises[JOYPAD_AXISES_X] = event.jaxis.axis;
}
gui_state = SHOWING_MENU;
should_render = true;
}
}
break;
}
case SDL_MOUSEWHEEL: {
if (menu_height > 144) {
scroll -= event.wheel.y;
if (scroll < 0) {
scroll = 0;
}
if (scroll >= menu_height - 144) {
scroll = menu_height - 144;
}
mouse_scroling = true;
should_render = true;
}
break;
}
case SDL_MOUSEMOTION: {
if (scrollbar_drag && scrollbar_size < 140 && scrollbar_size > 0) {
signed x = event.motion.x;
signed y = event.motion.y;
convert_mouse_coordinates(&x, &y);
signed delta = scroll_mouse_start - y;
scroll = scroll_start - delta * (signed)(menu_height - 144) / (signed)(140 - scrollbar_size);
if (scroll < 0) {
scroll = 0;
}
if (scroll >= menu_height - 144) {
scroll = menu_height - 144;
}
should_render = true;
}
break;
}
case SDL_TEXTINPUT:
if (gui_state == TEXT_INPUT) {
char *s = event.text.text;
while (*s) {
text_input_callback(*(s++));
}
should_render = true;
}
break;
case SDL_KEYDOWN:
scrollbar_drag = false;
if (gui_state == TEXT_INPUT) {
if (event.key.keysym.sym == SDLK_v && (event.key.keysym.mod & MODIFIER)) {
char *s = SDL_GetClipboardText();
while (*s) {
text_input_callback(*(s++));
}
should_render = true;
}
else if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE) {
text_input_callback('\b');
should_render = true;
}
else if (event.key.keysym.scancode == SDL_SCANCODE_RETURN ||
event.key.keysym.scancode == SDL_SCANCODE_RETURN2 ||
event.key.keysym.scancode == SDL_SCANCODE_KP_ENTER) {
text_input_callback('\n');
should_render = true;
}
}
else if (gui_state == WAITING_FOR_KEY) {
if (current_selection > 8) {
configuration.keys_2[current_selection - GB_CONF_KEYS_COUNT] = event.key.keysym.scancode;
}
else {
configuration.keys[current_selection] = event.key.keysym.scancode;
}
gui_state = SHOWING_MENU;
should_render = true;
}
else if (event_hotkey_code(&event) == SDL_SCANCODE_F && event.key.keysym.mod & MODIFIER) {
if ((SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN_DESKTOP) == false) {
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
}
else {
SDL_SetWindowFullscreen(window, 0);
}
update_swap_interval();
update_viewport();
screen_manually_resized = true;
}
else if (event_hotkey_code(&event) == SDL_SCANCODE_O) {
if (event.key.keysym.mod & MODIFIER) {
char *filename = do_open_rom_dialog();
if (filename) {
set_filename(filename, free);
pending_command = GB_SDL_NEW_FILE_COMMAND;
return;
}
}
}
else if (event.key.keysym.scancode == SDL_SCANCODE_RETURN && gui_state == WAITING_FOR_JBUTTON) {
should_render = true;
if (joypad_configuration_progress != JOYPAD_BUTTONS_MAX) {
configuration.joypad_configuration[joypad_configuration_progress] = -1;
}
else {
configuration.joypad_axises[0] = -1;
configuration.joypad_axises[1] = -1;
}
joypad_configuration_progress++;
if (joypad_configuration_progress > JOYPAD_BUTTONS_MAX) {
gui_state = SHOWING_MENU;
}
}
else if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
if (gui_state == SHOWING_MENU && current_menu != root_menu) {
for (const struct menu_item *item = current_menu; item->string; item++) {
if (strcmp(item->string, "Back") == 0) {
item->handler(0);
goto handle_pending;
break;
}
}
should_render = true;
}
else if (is_running) {
SDL_StopTextInput();
return;
}
else {
if (gui_state == SHOWING_DROP_MESSAGE) {
gui_state = SHOWING_MENU;
}
else if (gui_state == SHOWING_MENU) {
gui_state = SHOWING_DROP_MESSAGE;
}
current_selection = 0;
mouse_scroling = false;
scroll = 0;
current_menu = root_menu;
recalculate_menu_height();
should_render = true;
}
}
else if (gui_state == SHOWING_MENU) {
if ((event.key.keysym.scancode == SDL_SCANCODE_DOWN ||
event.key.keysym.scancode == SDL_SCANCODE_J) &&
current_menu[current_selection + 1].string) {
current_selection++;
mouse_scroling = false;
should_render = true;
}
else if ((event.key.keysym.scancode == SDL_SCANCODE_UP ||
event.key.keysym.scancode == SDL_SCANCODE_K) &&
current_selection) {
current_selection--;
mouse_scroling = false;
should_render = true;
}
else if (event.key.keysym.scancode == SDL_SCANCODE_RETURN && !current_menu[current_selection].backwards_handler) {
if (current_menu[current_selection].handler) {
current_menu[current_selection].handler(current_selection);
handle_pending:
if (pending_command == GB_SDL_RESET_COMMAND && !is_running) {
pending_command = GB_SDL_NO_COMMAND;
}
if (pending_command) {
if (!is_running && pending_command == GB_SDL_QUIT_COMMAND) {
exit(0);
}
return;
}
should_render = true;
}
else {
return;
}
}
else if ((event.key.keysym.scancode == SDL_SCANCODE_RIGHT ||
event.key.keysym.scancode == SDL_SCANCODE_L) &&
current_menu[current_selection].backwards_handler) {
current_menu[current_selection].handler(current_selection);
should_render = true;
}
else if ((event.key.keysym.scancode == SDL_SCANCODE_LEFT ||
event.key.keysym.scancode == SDL_SCANCODE_H) &&
current_menu[current_selection].backwards_handler) {
current_menu[current_selection].backwards_handler(current_selection);
should_render = true;
}
}
else if (gui_state == SHOWING_HELP) {
gui_state = SHOWING_MENU;
should_render = true;
}
break;
}
if (should_render) {
should_render = false;
rerender:
SDL_LockSurface(converted_background);
if (width == 160 && height == 144) {
memcpy(pixels, converted_background->pixels, sizeof(pixels));
}
else {
for (unsigned i = 0; i < width * height; i++) {
pixels[i] = gui_palette_native[0];
}
for (unsigned y = 0; y < 144; y++) {
memcpy(pixels + x_offset + width * (y + y_offset), ((uint32_t *)converted_background->pixels) + 160 * y, 160 * 4);
}
}
SDL_UnlockSurface(converted_background);
switch (gui_state) {
case SHOWING_DROP_MESSAGE:
draw_styled_text(pixels, width, height, 8 + y_offset, "Press ESC for menu", gui_palette_native[3], gui_palette_native[0], STYLE_CENTER);
draw_styled_text(pixels, width, height, 116 + y_offset, "Drop a GB or GBC", gui_palette_native[3], gui_palette_native[0], STYLE_CENTER);
draw_styled_text(pixels, width, height, 128 + y_offset, "file to play", gui_palette_native[3], gui_palette_native[0], STYLE_CENTER);
break;
case SHOWING_MENU:
draw_styled_text(pixels, width, height, 8 + y_offset, "SameBoy", gui_palette_native[3], gui_palette_native[0], STYLE_LEFT);
unsigned i = 0, y = 24;
for (const struct menu_item *item = current_menu; item->string; item++, i++) {
if (i == current_selection && !mouse_scroling) {
if (i == 0) {
if (y < scroll) {
scroll = (y - 4) / 12 * 12;
goto rerender;
}
}
else {
if (y < scroll + 24) {
scroll = (y - 24) / 12 * 12;
goto rerender;
}
}
}
if (i == current_selection && i == 0 && scroll != 0 && !mouse_scroling) {
scroll = 0;
goto rerender;
}
if (item->value_getter && !item->backwards_handler) {
char line[25];
snprintf(line, sizeof(line), "%s%*s", item->string, 23 - (unsigned)strlen(item->string), item->value_getter(i));
draw_styled_text(pixels, width, height, y + y_offset, line, gui_palette_native[3], gui_palette_native[0],
i == current_selection ? STYLE_SELECTION : STYLE_INDENT);
y += 12;
}
else {
if (item->value_getter) {
draw_styled_text(pixels, width, height, y + y_offset, item->string, gui_palette_native[3], gui_palette_native[0],
STYLE_LEFT);
}
else {
draw_styled_text(pixels, width, height, y + y_offset, item->string, gui_palette_native[3], gui_palette_native[0],
i == current_selection ? STYLE_SELECTION : STYLE_INDENT);
}
y += 12;
if (item->value_getter) {
draw_styled_text(pixels, width, height, y + y_offset - 1, item->value_getter(i), gui_palette_native[3], gui_palette_native[0],
i == current_selection ? STYLE_ARROWS : STYLE_CENTER);
y += 12;
}
}
if (i == current_selection && !mouse_scroling) {
if (y > scroll + 144) {
scroll = (y - 144) / 12 * 12;
if (scroll > menu_height - 144) {
scroll = menu_height - 144;
}
goto rerender;
}
}
}
if (scrollbar_size) {
unsigned scrollbar_offset = (140 - scrollbar_size) * scroll / (menu_height - 144);
if (scrollbar_offset + scrollbar_size > 140) {
scrollbar_offset = 140 - scrollbar_size;
}
for (unsigned y = 0; y < 140; y++) {
uint32_t *pixel = pixels + x_offset + 156 + width * (y + y_offset + 2);
if (y >= scrollbar_offset && y < scrollbar_offset + scrollbar_size) {
pixel[0] = pixel[1] = gui_palette_native[2];
}
else {
pixel[0] = pixel[1] = gui_palette_native[1];
}
}
}
break;
case SHOWING_HELP:
draw_text(pixels, width, height, 2 + x_offset, 2 + y_offset, help[current_help_page], gui_palette_native[3], gui_palette_native[0], false);
break;
case WAITING_FOR_KEY:
draw_styled_text(pixels, width, height, 68 + y_offset, "Press a Key", gui_palette_native[3], gui_palette_native[0], STYLE_CENTER);
break;
case WAITING_FOR_JBUTTON:
draw_styled_text(pixels, width, height, 68 + y_offset,
joypad_configuration_progress != JOYPAD_BUTTONS_MAX ? "Press button for" : "Move the Analog Stick",
gui_palette_native[3], gui_palette_native[0], STYLE_CENTER);
draw_styled_text(pixels, width, height, 80 + y_offset,
GB_inline_const(const char *[], {
"Right",
"Left",
"Up",
"Down",
"A",
"B",
"Select",
"Start",
"Open Menu",
"Turbo",
"Rewind",
"Slow-Motion",
"Hotkey 1",
"Hotkey 2",
"Rapid A",
"Rapid B",
"",
}) [joypad_configuration_progress],
gui_palette_native[3], gui_palette_native[0], STYLE_CENTER);
draw_styled_text(pixels, width, height, 104 + y_offset, "Press Enter to skip", gui_palette_native[3], gui_palette_native[0], STYLE_CENTER);
break;
case TEXT_INPUT:
draw_styled_text(pixels, width, height, 32 + y_offset, text_input_title, gui_palette_native[3], gui_palette_native[0], STYLE_CENTER);
draw_styled_text(pixels, width, height, 44 + y_offset, text_input_title2, gui_palette_native[3], gui_palette_native[0], STYLE_CENTER);
draw_styled_text(pixels, width, height, 64 + y_offset, text_input, gui_palette_native[3], gui_palette_native[0], STYLE_CENTER);
break;
}
render_texture(pixels, NULL);
#ifdef _WIN32
/* Required for some Windows 10 machines, god knows why */
render_texture(pixels, NULL);
#endif
}
}
}
static void __attribute__ ((constructor)) list_custom_palettes(void)
{
char *path = resource_path("Palettes");
if (!path) return;
if (strlen(path) > 1024 - 30) {
// path too long to safely concat filenames
return;
}
DIR *dir = opendir(path);
if (!dir) return;
struct dirent *ent;
while ((ent = readdir(dir))) {
unsigned length = strlen(ent->d_name);
if (length < 5 || length > 28) {
continue;
}
if (strcmp(ent->d_name + length - 4, ".sbp")) continue;
ent->d_name[length - 4] = 0;
custom_palettes = realloc(custom_palettes,
sizeof(custom_palettes[0]) * (n_custom_palettes + 1));
custom_palettes[n_custom_palettes++] = strdup(ent->d_name);
}
closedir(dir);
}
|