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 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379
|
/*
* PROPRIETARY INFORMATION. This software is proprietary to POWDER
* Development, and is not to be reproduced, transmitted, or disclosed
* in any way without written permission.
*
* Produced by: Jeff Lait
*
* POWDER Development
*
* NAME: gfxengine.cpp ( POWDER Library, C++ )
*
* COMMENTS:
* This simple set of routines allows a somewhat platform independent
* way of drawing to the GBA.
*/
#include "gfxengine.h"
#include "gfxengine.h"
#include "mygba.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "gfx/all_bitmaps.h"
#include "map.h"
#include "creature.h"
#include "glbdef.h"
#include "assert.h"
#include "item.h"
#include "control.h"
#include "msg.h"
#include "stylus.h"
#include "victory.h"
// The number of 8x8 tiles to have. The maximum for 256 colours
// on the GBA is 512. SDL is theoritically infinite, except the
// 1024 and 2048 bits are used for flip flags.
#if defined(USING_SDL)
#define TILEARRAY 1023
#else
#define TILEARRAY 512
#endif
// This is used for the maximum number the bitmap routines can grab.
// We can safely bump this up in the SDL case.
#if defined(USING_SDL)
// Since it is a 16x16 map, this is sufficient.
#define TILESTASH 256
#else
#define TILESTASH 50
#endif
// Gets the number of bytes of a tile in our current tileset
#ifdef USING_SDL
#define BYTEPERTILE (glb_tilesets[glb_tileset].tilewidth*glb_tilesets[glb_tileset].tilewidth)
#define WORDPERTILE (glb_tilesets[glb_tileset].tilewidth*glb_tilesets[glb_tileset].tilewidth / 2)
#define TILEWIDTH (glb_tilesets[glb_tileset].tilewidth)
#define TILEHEIGHT (glb_tilesets[glb_tileset].tilewidth)
#else
#define BYTEPERTILE 8*8
#define WORDPERTILE 4*8
#define TILEWIDTH 8
#define TILEHEIGHT 8
#endif
#ifdef USING_DS
#define RAW_SCREEN_WIDTH 256
#define RAW_SCREEN_HEIGHT 192
#define PLATE_WIDTH 256
#define PLATE_HEIGHT 192
#include <fat.h>
#else
#ifdef USING_SDL
#define RAW_SCREEN_WIDTH (TILEWIDTH * 32)
#define RAW_SCREEN_HEIGHT (TILEWIDTH * 24)
#define PLATE_WIDTH 256
#define PLATE_HEIGHT 192
#else
#define RAW_SCREEN_WIDTH 240
#define RAW_SCREEN_HEIGHT 160
#define PLATE_WIDTH 240
#define PLATE_HEIGHT 160
#endif
#endif
// Global variables
int glb_newframe = 0;
volatile int glb_framecount = 0;
int glb_gfxmode = -1;
// Tileset defaults to Akoi Meexx.
#ifndef iPOWDER
int glb_tileset = 4;
#else
int glb_tileset = 2;
int glb_modetilesets[2] = { 2, 2 };
int glb_tilesetmode = 0;
#endif
// Fonts default to shadowed.
int glb_currentfont = 2;
// This is our current tile reference array.
// glb_tileidx[TILE_NAME] gives the gba tile index.
// This has already been multiplied by 4!
u16 glb_tileidx[NUM_TILES];
// This is the gba tile that ties to the given character.
u16 glb_charidx[256];
#define MAX_COLOUR_CHAR 10
u8 *glb_colourchardata[MAX_COLOUR_CHAR];
// glb_idxcount[gbaidx] gives the number of times that tile
// is currently used.
u16 glb_idxcount[TILEARRAY];
// This is a map of our current 64x64 tile layout.
u16 *glb_tilemap, *glb_shadowmap, *glb_mobmap;
u16 *glb_abstilemap, *glb_absmobmap, *glb_absshadowmap;
// This is 32x24 and matches the currently displayed characters.
u8 *glb_charmap;
// Used in our compositing engine for building the paper doll
u8 *glb_comptiledata = 0;
int glb_lastdesttile;
// Used in our greyscale sprite engine, must be global so we
// can resize this
u8 *glb_greyscaletiledata = 0;
// Our one lone sprite.
u16 glb_sprite = SPRITE_CURSOR;
// This is the current scroll offset of where our tile layout matches
// with the tilemap.
int glb_scrollx, glb_scrolly;
// This is the tile offset of the center tile. We use this for inventory.
int glb_offx, glb_offy;
// This is the pixel level scroll to nudge all the scrolling planes.
s8 glb_nudgex = 0, glb_nudgey = 0;
// Current inventory position.. Defaults to where the first added
// item will go.
int glb_invx = 1, glb_invy = 0;
MOB *glb_invmob = 0;
// Are we in sprite or dungeon mode?
bool glb_usesprites = true;
// bg2 tiles. This maps to the screen with 240/8 = 30 to 160/8 = 20
// ratio. Thus, there are are 600 tiles.
tile_info_ptr glb_bg2tiles;
tile_info_ptr glb_bg1tiles;
// This has the same format as tiles! It is XxY for 8x8 blocks,
// of which there are 30x20.
char *glb_bg2tiledata;
// This the palette entry for each of the standard colours.
char glb_stdcolor[NUM_COLOURS];
// This is the mapping for each colour in the sprite palette into
// the greyscale equivalent.
u8 *glb_spritegreyscale = 0;
// Text look up tables...
const char *glb_texttable[] = {
"abcdefghij",
"klmnopqrst",
"uvwxyz" SYMBOLSTRING_HEART SYMBOLSTRING_MAGIC SYMBOLSTRING_NEXT SYMBOLSTRING_DLEVEL,
"ABCDEFGHIJ",
"KLMNOPQRST",
"UVWXYZ" SYMBOLSTRING_LEFT SYMBOLSTRING_RIGHT SYMBOLSTRING_UP SYMBOLSTRING_DOWN,
"0123456789",
"!@#$%^&*()",
"=-\\',./+_|",
"\"<>?[]{}`~",
",.<>;:" SYMBOLSTRING_AC SYMBOLSTRING_TRIDUDE SYMBOLSTRING_CURSOR " ",
SYMBOLSTRING_UNIQUE " ",
0
};
u8
rgbtogrey(u8 cr, u8 cg, u8 cb)
{
// Roughly a 5:6:1 weighting, but scaled up so we
// get to divide by 16 for some meaningless efficiency.
int grey = cg * 8 + cr * 6 + cb * 2;
grey += 7;
grey /= 16;
if (grey > 255)
grey = 255;
return grey;
}
#ifdef USING_SDL
static int
rawtoplate_x(int x)
{
int sx = (int) ((x * PLATE_WIDTH) / ((float)(RAW_SCREEN_WIDTH)) + 0.5);
if (sx >= PLATE_WIDTH)
sx = PLATE_WIDTH-1;
return sx;
}
static int
rawtoplate_y(int y)
{
int sy = (int) ((y * PLATE_HEIGHT) / ((float)(RAW_SCREEN_HEIGHT)) + 0.5);
if (sy >= PLATE_HEIGHT)
sy = PLATE_HEIGHT-1;
return sy;
}
#else
static int
rawtoplate_x(int x)
{
return x;
}
static int
rawtoplate_y(int y)
{
return y;
}
#endif
int
gfx_getchartile(char c)
{
int textline;
const char *text;
int pos;
// Special cases...
switch (c)
{
case '\b':
c = SYMBOL_LEFT;
break;
case '\n':
case '\r':
c = SYMBOL_NEXT;
break;
case '\t':
c = SYMBOL_UP;
break;
}
pos = 0;
textline = 0;
while (glb_texttable[textline])
{
text = glb_texttable[textline];
while (*text)
{
if (*text == c)
return pos;
pos++;
text++;
}
textline++;
}
// Failure, lower case a.
return 1;
}
u16
gfx_lockColourCharTile(u8 c, COLOUR_NAMES colour, u8 *result)
{
u8 virtualtile, charnum;
int idx, sourceidx;
// Find a free colour tile in our desired range.
for (virtualtile = COLOURED_SYMBOLS; virtualtile < COLOURED_SYMBOLS + MAX_COLOUR_CHAR; virtualtile++)
{
if (glb_charidx[virtualtile] == INVALID_TILEIDX)
{
// This is a free index!
break;
}
}
// Check to see if we failed, if so, return the no tile.
// Flag that we shouldn't unlock this tile.
if (virtualtile == COLOURED_SYMBOLS + MAX_COLOUR_CHAR)
{
*result = 0xff;
return gfx_lookupTile(TILE_NOTILE);
}
// Now try and allocate a tile.
idx = gfx_findFreeTile(1);
if (idx == INVALID_TILEIDX)
{
// Bah, ran out of tiles!
*result = 0xff;
return gfx_lookupTile(TILE_NOTILE);
}
// Now find what origin tile we want to map from.
sourceidx = gfx_getchartile(c);
charnum = virtualtile - COLOURED_SYMBOLS;
// Check to see if our tiledata is available.
if (!glb_colourchardata[charnum])
glb_colourchardata[charnum] = (u8 *) new u16[WORDPERTILE];
u8 *dst;
const u8 *src;
u16 *palette = (u16 *) glb_tilesets[glb_tileset].palette;
int i;
dst = glb_colourchardata[charnum];
src = (const u8*)&glb_tilesets[glb_tileset].alphabet[glb_currentfont][sourceidx*BYTEPERTILE];
for (i = 0; i < BYTEPERTILE; i++)
{
// Anything not black or transparent is turned into
// our desired colour.
if (*src == 0 || *src == glb_stdcolor[COLOUR_BLACK] || !palette[*src] || palette[*src] == 0x842)
*dst = *src;
else
*dst = glb_stdcolor[colour];
dst++;
src++;
}
// And send the resulting tile to VRAM.
ham_ReloadTileGfx(glb_bg1tiles,
(u16*) glb_colourchardata[charnum],
idx,
1);
glb_idxcount[idx]++;
glb_charidx[virtualtile] = idx;
// When we unlock, this is the tile we'll actually unlock!
*result = virtualtile;
return idx;
}
u16
gfx_lockCharTile(u8 c, u8 *result)
{
int idx;
int sourceidx;
// Check our character tile array to see if it is already done.
*result = c;
idx = glb_charidx[c];
if (idx != INVALID_TILEIDX)
{
// Great, already allocated, inc and return.
glb_idxcount[idx]++;
return idx;
}
// Must allocate it...
idx = gfx_findFreeTile(1);
if (idx != INVALID_TILEIDX)
{
sourceidx = gfx_getchartile(c);
ham_ReloadTileGfx(glb_bg1tiles,
(u16*)(&glb_tilesets[glb_tileset].alphabet[glb_currentfont][sourceidx * BYTEPERTILE]),
idx,
1);
glb_idxcount[idx]++;
glb_charidx[c] = idx;
return idx;
}
// Damn, failure to allocate!
UT_ASSERT(!"Failure to allocate char tile");
// Flag that we shouldn't unlock this tile.
*result = 0xff;
return gfx_lookupTile(TILE_NOTILE);
}
void
gfx_unlockCharTile(u8 c)
{
u16 idx;
if (c == 0xff)
return; // Blank dude.
idx = glb_charidx[c];
UT_ASSERT(idx != INVALID_TILEIDX);
if (idx == INVALID_TILEIDX)
return;
UT_ASSERT(glb_idxcount[idx]);
if (glb_idxcount[idx])
glb_idxcount[idx]--;
if (!glb_idxcount[idx])
{
// TIle is now free.
glb_charidx[c] = INVALID_TILEIDX;
}
}
u16
gfx_findFreeTile(int size)
{
int tile, j;
for (tile = 0; tile <= TILEARRAY - size; tile++)
{
if (glb_idxcount[tile])
continue;
// We have a zero, check if next size are zero..
for (j = 0; j < size; j++)
{
if (glb_idxcount[tile+j])
{
tile = tile+j;
break;
}
}
// Making it here implies that we found a free tile.
if (j == size)
return tile;
}
// Failure to find a free one.
return INVALID_TILEIDX;
}
u16
gfx_lookupTile(TILE_NAMES tile)
{
if (tile == INVALID_TILEIDX)
return INVALID_TILEIDX;
return glb_tileidx[tile];
}
u16
gfx_lockTile(TILE_NAMES tile, TILE_NAMES *result)
{
u16 idx;
if (result)
*result = tile;
if (tile == INVALID_TILEIDX)
return INVALID_TILEIDX;
idx = glb_tileidx[tile];
if (idx != INVALID_TILEIDX)
{
if (tile == TILE_VOID || tile == TILE_NOTILE)
{
// We only lock these tiles once ever.
return idx;
}
glb_idxcount[idx]++;
glb_idxcount[idx+1]++;
glb_idxcount[idx+2]++;
glb_idxcount[idx+3]++;
return idx;
}
idx = gfx_findFreeTile(4);
if (idx != INVALID_TILEIDX)
{
// A free index! Return it.
ham_ReloadTileGfx(glb_bg1tiles,
(u16*)(&glb_tilesets[glb_tileset].dungeon[tile * 4 * BYTEPERTILE]),
idx,
4);
glb_idxcount[idx]++;
glb_idxcount[idx+1]++;
glb_idxcount[idx+2]++;
glb_idxcount[idx+3]++;
glb_tileidx[tile] = idx;
return idx;
}
// Failed to find free...
UT_ASSERT(!"FAILED TO ALLOC TILE!");
if (result)
*result = (TILE_NAMES) INVALID_TILEIDX;
return gfx_lookupTile(TILE_NOTILE);
}
void
gfx_forceLockTile(TILE_NAMES tile, int mincount)
{
int idx, j;
// See if our tile is already locked.
// Problem: If we have a
idx = gfx_lookupTile(tile);
if (idx == INVALID_TILEIDX)
{
// Tile not yet locked, lock it!
idx = gfx_lockTile(tile, 0);
}
// Verify we have the minimum count.
for (j = 0; j < 4; j++)
{
if (glb_idxcount[idx+j] < mincount)
glb_idxcount[idx+j] = mincount;
}
}
void
gfx_unlockTile(TILE_NAMES tile)
{
u16 idx;
if (tile == INVALID_TILEIDX)
return;
if (tile == TILE_NOTILE || tile == TILE_VOID)
{
// Never unlock these special tiles.
// We need to be able to always procur them
return;
}
idx = glb_tileidx[tile];
UT_ASSERT(idx != INVALID_TILEIDX);
if (idx == INVALID_TILEIDX)
return;
// Must have already locked it...
UT_ASSERT(glb_idxcount[idx]);
UT_ASSERT(glb_idxcount[idx+1]);
UT_ASSERT(glb_idxcount[idx+2]);
UT_ASSERT(glb_idxcount[idx+3]);
glb_idxcount[idx]--;
glb_idxcount[idx+1]--;
glb_idxcount[idx+2]--;
glb_idxcount[idx+3]--;
if (!glb_idxcount[idx])
{
// Tile is now free...
glb_tileidx[tile] = INVALID_TILEIDX;
}
}
int
gfx_getNumFreeTiles()
{
int idx, num;
num = 0;
for (idx = 0; idx < TILEARRAY; idx++)
{
if (glb_idxcount[idx] == 0)
num++;
}
return num;
}
//
// VBL func
//
void vblFunc()
{
glb_newframe = 1;
glb_framecount++;
}
void
gfx_buildstdcolors()
{
COLOUR_NAMES colour;
FOREACH_COLOUR(colour)
{
glb_stdcolor[colour] = gfx_lookupcolor(glb_colourdefs[colour].red,
glb_colourdefs[colour].green,
glb_colourdefs[colour].blue);
}
// Build the standard colours...
glb_stdcolor[COLOUR_INVISIBLE] = 0; // Always true.
// This is also a good time to update our grey table if we have it.
gfx_updatespritegreytable();
}
void
gfx_init()
{
// Initialize everything...
ham_Init();
// Install our interrupt handler
#if defined(USING_SDL) || defined(USING_DS) || defined(NO_HAM)
ham_StartIntHandler(INT_TYPE_VBL, &vblFunc);
#else
ham_StartIntHandler(INT_TYPE_VBL, (void *)&vblFunc);
#endif
// These maps store the TILE_NAMES currently at each map position.
// The absolute version do the same for absolute overlays.
// This allows us to avoid double writes, and load tiles into
// VRAM as necessary.
// 0xff is used as INVALID_TILEIDX is 65535.
glb_tilemap = new u16[32*32];
glb_shadowmap = new u16[32*32];
glb_mobmap = new u16[32*32];
glb_abstilemap = new u16[17*12];
glb_absmobmap = new u16[17*12];
glb_absshadowmap = new u16[17*12];
glb_charmap = new u8[32*24];
memset(glb_tilemap, 0xff, 32*32 * sizeof(u16));
memset(glb_shadowmap, 0xff, 32*32 * sizeof(u16));
memset(glb_mobmap, 0xff, 32*32 * sizeof(u16));
memset(glb_abstilemap, 0xff, 17*12*sizeof(u16));
memset(glb_absmobmap, 0xff, 17*12*sizeof(u16));
memset(glb_absshadowmap, 0xff, 17*12*sizeof(u16));
memset(glb_charmap, 0xff, 32*24);
}
void
gfx_reblitslugandblood()
{
u16 *screen;
int x, y;
screen = hamfake_lockScreen();
#if defined(USING_DS) || defined(USING_SDL)
// Scroll back the screen since it was in GBA coords.
screen -= TILEWIDTH + 2*TILEHEIGHT * RAW_SCREEN_WIDTH;
if (RAW_SCREEN_WIDTH != PLATE_WIDTH ||
RAW_SCREEN_HEIGHT != PLATE_HEIGHT)
{
for (y = 0; y < RAW_SCREEN_HEIGHT; y++)
{
int sy = rawtoplate_y(y);
for (x = 0; x < RAW_SCREEN_WIDTH; x++)
{
int sx = rawtoplate_x(x);
screen[y*RAW_SCREEN_WIDTH+x] = bmp_slug_and_blood[sy*PLATE_WIDTH+sx];
}
}
}
else
{
for (y = 0; y < PLATE_HEIGHT; y++)
{
for (x = 0; x < PLATE_WIDTH; x++)
{
screen[y*RAW_SCREEN_WIDTH+x] = bmp_slug_and_blood[y*PLATE_WIDTH+x];
}
}
}
#else
for (y = 0; y < PLATE_WIDTH*PLATE_HEIGHT; y++)
{
screen[y] = bmp_slug_and_blood[y];
}
#endif
hamfake_unlockScreen(screen);
}
void
gfx_setmode(int mode)
{
int x, y;
map_info_ptr bg2mapset;
// A no-op.
if (mode == glb_gfxmode)
return;
glb_gfxmode = mode;
if (mode == 3)
{
// Intro screen...
ham_SetBgMode(3);
gfx_reblitslugandblood();
// Ensure we have current sprite date.
hamfake_LoadSpritePal((void *)glb_tilesets[glb_tileset].spritepalette, 512);
hamfake_ReloadSpriteGfx((u16*)(&glb_tilesets[glb_tileset].sprite[glb_sprite*4*BYTEPERTILE]),
0, 4);
// NOTE: This actually copies incorrect data! Likely due to it
// using 32bit copies or ignoring waitstates.
// memcpy( (unsigned short *) 0x06000000,
// bmp_slug_and_blood,
// 38400*2 );
return;
}
// Must be tiled mode.
UT_ASSERT(mode == 0);
// This is all four bgs, no zooming.
ham_SetBgMode(0);
// Wipe any charmap left over from raw mode.
memset(glb_charmap, 0xff, 32*24);
ham_LoadBGPal((void *)glb_tilesets[glb_tileset].palette, 512);
hamfake_LoadSpritePal((void *)glb_tilesets[glb_tileset].spritepalette, 512);
// So, what are our layers?
// 0: 32x32: Text and minimap.
// 1: 64x64: Terrain tiles
// 2: 64x64: Monters/Item tiles
// 3: 64x64: Shadow tiles
#if 1
// We want a 32x32 as we only will use one screen worth.
ham_bg[0].mi = ham_InitMapEmptySet(0, 0);
// We want a 64x64 screen.
ham_bg[1].mi = ham_InitMapEmptySet(3, 0);
// 64x64 map
bg2mapset = ham_InitMapEmptySet(3, 0);
// We want a 64x64 screen.
ham_bg[3].mi = ham_InitMapEmptySet(3, 0);
#else
// We want a 32x32 as we only will use one screen worth.
ham_bg[0].mi = ham_InitMapEmptySet(0, 0);
// We want a 64x64 screen.
ham_bg[1].mi = ham_bg[0].mi;
// 32 x 32 map.
bg2mapset = ham_bg[0].mi;
// We want a 64x64 screen.
ham_bg[3].mi = ham_bg[0].mi;
#endif
ham_bg[1].ti = ham_InitTileEmptySet(TILEARRAY, 1, 1);
// Init the dungeon tile set...
// ** EXCESSIVE PROFANITY DETECTED **
// ** DELETING OFFENDING LINES **
// ** RESUMING COMMENT **
// to respond to so FOAD, HTH, HAND.
// Thus, we now create a single tile set of TILEARRAY size.
// NOTE: We now share this with the bitmap cache, so that
// code currently assumes it is contiguous and uses 8x8 rather
// than 16x16, so for now we'll keep them the same.
//ham_bg[1].ti = ham_InitTileSet((void *)&DUNGEONTILES,
// (NUM_TILES-3) * 4 * WORDPERTILE, 1, 1);
glb_bg1tiles = ham_bg[1].ti;
ham_bg[0].ti = ham_bg[1].ti;
// Initialize our arrays to be all empty...
memset(glb_tileidx, 0xff, sizeof(u16) * NUM_TILES);
memset(glb_charidx, 0xff, sizeof(u16) * 256);
memset(glb_idxcount, 0, sizeof(u16) * TILEARRAY);
// Create the level 2 bg, where we do all our crazy pixel level
// stuff.
// You would think this size would have to be 600. That crashes
// due to out of vram, yet I seem to be able to write to it just
// fine? WTF?
// Calculations show that we can't fit, even in an ideal case,
// this much data in the 64k available in the tile/map data.
// One option is to use the object data set (which is 32k), except
// the actual screen required is 37.5k. THus, we instead create
// a tilestash which we can read from & deal with that way.
// glb_bg2tiles = ham_InitTileEmptySet(TILESTASH, 1, 0);
glb_bg2tiles = glb_bg1tiles;
// Highest priority as it will sit on top.
ham_InitBg(0, 1, 0, 0);
ham_InitBg(1, 1, 3, 0);
// Create our level 3 background, where we have our lighting overlay
// and put blood sprites, etc.
ham_bg[3].ti = ham_bg[1].ti;
// Above the lower map, but below the text.
ham_InitBg(3, 1, 1, 0);
memset(glb_tilemap, 0xff, 32*32 * sizeof(u16));
memset(glb_shadowmap, 0xff, 32*32 * sizeof(u16));
memset(glb_mobmap, 0xff, 32*32 * sizeof(u16));
memset(glb_abstilemap, 0xff, 17*12*sizeof(u16));
memset(glb_absmobmap, 0xff, 17*12*sizeof(u16));
memset(glb_absshadowmap, 0xff, 17*12*sizeof(u16));
memset(glb_charmap, 0xff, 32*24);
ham_bg[2].ti = glb_bg2tiles;
ham_bg[2].mi = bg2mapset;
// We allocate a half sized block as u16 and cast back to char
// to ensure we are aligned to u16.
glb_bg2tiledata = (char *) new u16[WORDPERTILE*TILESTASH];
memset(glb_bg2tiledata, 0, BYTEPERTILE*TILESTASH);
// No rot. Above the ground but below the shadow.
ham_InitBg(2, 1, 2, 0);
gfx_buildstdcolors();
// We always want the no tile locked so we can show it when
// things go bad...
// We want to lock VOID first as it will be tile 0, and thus
// our default screen will be sweet, sweet, black.
// Actaully, there is apparently no guarantee that the initial
// tile values are 0. Thus, we must manually set all of our
// tile planes to void here.
int voidtileidx;
// We know these locks must always succeed so we don't care to stash
// the result. In any case, we have engineered it that they are
// never freed.
voidtileidx = gfx_lockTile(TILE_VOID, 0);
gfx_lockTile(TILE_NOTILE, 0);
for (y = 0; y < 64; y++)
{
for (x = 0; x < 64; x++)
{
if (x < 32 && y < 32)
{
ham_SetMapTile(0, x, y, voidtileidx);
}
ham_SetMapTile(1, x, y, voidtileidx);
ham_SetMapTile(2, x, y, voidtileidx);
ham_SetMapTile(3, x, y, voidtileidx);
}
}
// Load the sprite.
hamfake_ReloadSpriteGfx((u16*)(&glb_tilesets[glb_tileset].sprite[glb_sprite*4*BYTEPERTILE]),
0, 4);
}
//
// This forces a rebuilding of all the tiles from our cache to the
// ham space.
//
void
gfx_refreshtiles()
{
int x, y, tile, tileidx;
// Ignore this if we are not in a graphics mode.
if (glb_gfxmode)
return;
// A refresh will also clear anything in the abs maps, so we
// first unlock those tiles.
for (y = 0; y < 12; y++)
{
for (x = 0; x < 17; x++)
{
tile = glb_abstilemap[y * 17 + x];
if (tile != INVALID_TILEIDX)
{
gfx_unlockTile((TILE_NAMES)tile);
glb_abstilemap[y * 17 + x] = INVALID_TILEIDX;
}
tile = glb_absshadowmap[y * 17 + x];
if (tile != INVALID_TILEIDX)
{
gfx_unlockTile((TILE_NAMES)tile);
glb_absshadowmap[y * 17 + x] = INVALID_TILEIDX;
}
tile = glb_absmobmap[y * 17 + x];
if (tile != INVALID_TILEIDX)
{
gfx_unlockTile((TILE_NAMES)tile);
glb_absmobmap[y * 17 + x] = INVALID_TILEIDX;
}
}
}
// TODO: Insert map might prove a tad bit more efficient, though
// would need a full size scratch space.
for (y = 0; y < 32; y++)
{
for (x = 0; x < 32; x++)
{
tile = glb_tilemap[(y + glb_scrolly) * 32 + x + glb_scrollx];
tileidx = gfx_lockTile((TILE_NAMES)tile, (TILE_NAMES *) &tile);
ham_SetMapTile(1, x*2, y*2, tileidx);
ham_SetMapTile(1, x*2+1, y*2, tileidx+1);
ham_SetMapTile(1, x*2, y*2+1, tileidx+2);
ham_SetMapTile(1, x*2+1, y*2+1, tileidx+3);
// The above lock should be a double count, so we unlock
// afterwards. Any write to glb_tilemap must have done
// a lock!
gfx_unlockTile((TILE_NAMES)tile);
}
}
// Update overlay buffer...
for (y = 0; y < 32; y++)
{
for (x = 0; x < 32; x++)
{
tile = glb_shadowmap[(y + glb_scrolly) * 32 + x + glb_scrollx];
tileidx = gfx_lockTile((TILE_NAMES)tile, (TILE_NAMES *) &tile);
ham_SetMapTile(3, x*2, y*2, tileidx);
ham_SetMapTile(3, x*2+1, y*2, tileidx+1);
ham_SetMapTile(3, x*2, y*2+1, tileidx+2);
ham_SetMapTile(3, x*2+1, y*2+1, tileidx+3);
// See comment above why this is needed.
gfx_unlockTile((TILE_NAMES)tile);
}
}
// Update mob buffer...
for (y = 0; y < 32; y++)
{
for (x = 0; x < 32; x++)
{
tile = glb_mobmap[(y + glb_scrolly) * 32 + x + glb_scrollx];
tileidx = gfx_lockTile((TILE_NAMES)tile, (TILE_NAMES *) &tile);
ham_SetMapTile(2, x*2, y*2, tileidx);
ham_SetMapTile(2, x*2+1, y*2, tileidx+1);
ham_SetMapTile(2, x*2, y*2+1, tileidx+2);
ham_SetMapTile(2, x*2+1, y*2+1, tileidx+3);
// See comment above why this is needed.
gfx_unlockTile((TILE_NAMES)tile);
}
}
#if 0
BUF buf;
buf.sprintf("%d, %d, %d, %d...%d, %d, %d, %d...",
(int)ham_bg[0].ti->first_block,
(int)ham_bg[1].ti->first_block,
(int)ham_bg[2].ti->first_block,
(int)ham_bg[3].ti->first_block,
(int)ham_bg[0].mi->first_block,
(int)ham_bg[1].mi->first_block,
(int)ham_bg[2].mi->first_block,
(int)ham_bg[3].mi->first_block);
msg_report(buf);
#endif
}
void
gfx_settile(int tx, int ty, int tileno)
{
int hamx, hamy, tileidx;
hamx = tx - glb_scrollx;
hamy = ty - glb_scrolly;
// Only send onto ham if on screen and is a real change.
if (glb_tilemap[ty * 32 + tx] != tileno)
// && hamx < 32 && hamy < 32)
{
gfx_unlockTile((TILE_NAMES)glb_tilemap[ty * 32 + tx]);
tileidx = gfx_lockTile((TILE_NAMES)tileno, (TILE_NAMES *) &tileno);
ham_SetMapTile(1, tx*2, ty*2, tileidx);
ham_SetMapTile(1, tx*2+1, ty*2, tileidx+1);
ham_SetMapTile(1, tx*2, ty*2+1, tileidx+2);
ham_SetMapTile(1, tx*2+1, ty*2+1, tileidx+3);
// UPdate our local map.
glb_tilemap[ty * 32 + tx] = tileno;
}
}
void
gfx_setabstile(int tx, int ty, int tileno)
{
// Handle ability to write to -1 column
tx++;
UT_ASSERT(tx >= 0);
UT_ASSERT(tx < 17);
UT_ASSERT(ty >= 0);
UT_ASSERT(ty < 12);
int tx1, ty1, tileidx, abstile;
// Update the abs map...
abstile = glb_abstilemap[ty * 17 + tx];
if (abstile == tileno)
return;
gfx_unlockTile((TILE_NAMES)abstile);
tileidx = gfx_lockTile((TILE_NAMES)tileno, (TILE_NAMES *)&tileno);
glb_abstilemap[ty * 17 + tx] = tileno;
// Extra -1 is for our offset.
tx += glb_offx - 7 - 1;
ty += glb_offy - 5;
tx *= 2;
ty *= 2;
ty += 1; // Correct for the off by one nature of glb_offx
tx1 = tx + 1;
ty1 = ty + 1;
tx &= 63;
ty &= 63;
tx1 &= 63;
ty1 &= 63;
ham_SetMapTile(1, tx, ty, tileidx);
ham_SetMapTile(1, tx1, ty, tileidx+1);
ham_SetMapTile(1, tx, ty1, tileidx+2);
ham_SetMapTile(1, tx1, ty1, tileidx+3);
}
void
gfx_setoverlay(int tx, int ty, int tileno)
{
int hamx, hamy, tileidx;
hamx = tx - glb_scrollx;
hamy = ty - glb_scrolly;
// Only send onto ham if on screen and is a real change.
if (glb_shadowmap[ty * 32 + tx] != tileno)
// && hamx < 32 && hamy < 32)
{
gfx_unlockTile((TILE_NAMES)glb_shadowmap[ty * 32 + tx]);
tileidx = gfx_lockTile((TILE_NAMES)tileno, (TILE_NAMES *)&tileno);
ham_SetMapTile(3, tx*2, ty*2, tileidx);
ham_SetMapTile(3, tx*2+1, ty*2, tileidx+1);
ham_SetMapTile(3, tx*2, ty*2+1, tileidx+2);
ham_SetMapTile(3, tx*2+1, ty*2+1, tileidx+3);
// UPdate our local map.
glb_shadowmap[ty * 32 + tx] = tileno;
}
}
int
gfx_getoverlay(int tx, int ty)
{
return glb_shadowmap[ty * 32 + tx];
}
void
gfx_setabsoverlay(int tx, int ty, int tileno)
{
// Correct for ability to write to -1 column
tx++;
UT_ASSERT(tx >= 0);
UT_ASSERT(tx < 17);
UT_ASSERT(ty >= 0);
UT_ASSERT(ty < 12);
// Ignore this if we are not in a valid mode.
if (glb_gfxmode)
return;
int tx1, ty1, tileidx, abstile;
// Update the abs map...
abstile = glb_absshadowmap[ty * 17 + tx];
if (abstile == tileno)
return;
gfx_unlockTile((TILE_NAMES)abstile);
tileidx = gfx_lockTile((TILE_NAMES)tileno, (TILE_NAMES *)&tileno);
glb_absshadowmap[ty * 17 + tx] = tileno;
tx += glb_offx - 7 - 1; // Extra -1 is for writing to -1 column
ty += glb_offy - 5;
tx *= 2;
ty *= 2;
ty += 1; // Correct for the off by one nature of glb_offx
tx1 = tx + 1;
ty1 = ty + 1;
tx &= 63;
ty &= 63;
tx1 &= 63;
ty1 &= 63;
ham_SetMapTile(3, tx, ty, tileidx);
ham_SetMapTile(3, tx1, ty, tileidx+1);
ham_SetMapTile(3, tx, ty1, tileidx+2);
ham_SetMapTile(3, tx1, ty1, tileidx+3);
}
void
gfx_setmoblayer(int tx, int ty, int tileno)
{
int hamx, hamy, tileidx;
hamx = tx - glb_scrollx;
hamy = ty - glb_scrolly;
// Only send onto ham if on screen and is a real change.
if (glb_mobmap[ty * 32 + tx] != tileno)
// && hamx < 32 && hamy < 32)
{
gfx_unlockTile((TILE_NAMES)glb_mobmap[ty * 32 + tx]);
tileidx = gfx_lockTile((TILE_NAMES)tileno, (TILE_NAMES *)&tileno);
ham_SetMapTile(2, tx*2, ty*2, tileidx);
ham_SetMapTile(2, tx*2+1, ty*2, tileidx+1);
ham_SetMapTile(2, tx*2, ty*2+1, tileidx+2);
ham_SetMapTile(2, tx*2+1, ty*2+1, tileidx+3);
// UPdate our local map.
glb_mobmap[ty * 32 + tx] = tileno;
}
}
void
gfx_setabsmob(int tx, int ty, int tileno)
{
// Handle ability to write to -1 column
tx++;
UT_ASSERT(tx >= 0);
UT_ASSERT(tx < 17);
UT_ASSERT(ty >= 0);
UT_ASSERT(ty < 12);
int tx1, ty1, tileidx, abstile;
// Update the abs map...
abstile = glb_absmobmap[ty * 17 + tx];
if (abstile == tileno)
return;
gfx_unlockTile((TILE_NAMES)abstile);
tileidx = gfx_lockTile((TILE_NAMES)tileno, (TILE_NAMES *)&tileno);
glb_absmobmap[ty * 17 + tx] = tileno;
// Extra -1 is for our offset.
tx += glb_offx - 7 - 1;
ty += glb_offy - 5;
tx *= 2;
ty *= 2;
ty += 1; // Correct for the off by one nature of glb_offx
tx1 = tx + 1;
ty1 = ty + 1;
tx &= 63;
ty &= 63;
tx1 &= 63;
ty1 &= 63;
ham_SetMapTile(2, tx, ty, tileidx);
ham_SetMapTile(2, tx1, ty, tileidx+1);
ham_SetMapTile(2, tx, ty1, tileidx+2);
ham_SetMapTile(2, tx1, ty1, tileidx+3);
}
int
gfx_getmoblayer(int tx, int ty)
{
return glb_mobmap[ty * 32 + tx];
}
void
gfx_printtext(int x, int y, const char *text)
{
// We silently clip at 30.
while (x < 30 && *text)
{
gfx_printchar(x++, y, *text++);
}
}
void
gfx_printcharraw(int x, int y, char c)
{
int idx, offx, offy;
u16 *screen = hamfake_lockScreen();
u8 *chardata;
u16 *palette;
u16 *backplate;
idx = gfx_getchartile(c);
glb_charmap[(y/TILEHEIGHT)*32 + (x/TILEWIDTH)] = c;
chardata = (u8 *) (&glb_tilesets[glb_tileset].alphabet[glb_currentfont][idx * BYTEPERTILE]);
palette = (u16 *) glb_tilesets[glb_tileset].palette;
backplate = (u16 *) bmp_slug_and_blood;
// Find offset for x & y.
// We have to update the backplate location as we were given
// GBA coords and backplate is in DS coords.
#if defined(USING_DS) || defined(USING_SDL)
// This is not tile specific as we don't scale our plate (yet)
backplate += 8 + 16 * PLATE_WIDTH;
#endif
int backx = x;
int backoff = 0;
x += y * RAW_SCREEN_WIDTH;
screen += x;
for (offy = 0; offy < TILEHEIGHT; offy++)
{
backoff = rawtoplate_y(y+offy) * PLATE_WIDTH;
for (offx = 0; offx < TILEWIDTH; offx++)
{
if (*chardata)
{
*screen = palette[*chardata];
}
else // Transparent.
*screen = backplate[backoff + rawtoplate_x(backx+offx)];
screen++;
chardata++;
}
screen += RAW_SCREEN_WIDTH - TILEWIDTH;
}
hamfake_unlockScreen(screen);
}
void
gfx_printcolourcharraw(int x, int y, char c, COLOUR_NAMES colour)
{
int idx, offx, offy;
u16 *screen = hamfake_lockScreen();
u8 *chardata;
u16 *palette;
u16 *backplate;
u16 charcolour, desiredcolour;
desiredcolour = (glb_colourdefs[colour].blue >> 3) << 10;
desiredcolour |= (glb_colourdefs[colour].green >> 3) << 5;
desiredcolour |= (glb_colourdefs[colour].red >> 3);
idx = gfx_getchartile(c);
glb_charmap[(y/TILEHEIGHT)*32 + (x/TILEWIDTH)] = c;
chardata = (u8 *) (&glb_tilesets[glb_tileset].alphabet[glb_currentfont][idx * BYTEPERTILE]);
palette = (u16 *) glb_tilesets[glb_tileset].palette;
backplate = (u16 *) bmp_slug_and_blood;
// Find offset for x & y.
// We have to update the backplate location as we were given
// GBA coords and backplate is in DS coords.
#if defined(USING_DS) || defined(USING_SDL)
backplate += 8 + 16 * PLATE_WIDTH;
#endif
int backx = x;
int backoff = 0;
x += y * RAW_SCREEN_WIDTH;
screen += x;
for (offy = 0; offy < TILEHEIGHT; offy++)
{
backoff = rawtoplate_y(y+offy) * PLATE_WIDTH;
for (offx = 0; offx < TILEWIDTH; offx++)
{
if (*chardata)
{
// If the palette is black, we pass it through.
// Otherwise we use our desired colour.
charcolour = palette[*chardata];
if (!charcolour || charcolour == 0x842)
*screen = charcolour;
else
*screen = desiredcolour;
}
else
*screen = backplate[backoff + rawtoplate_x(backx+offx)];
screen++;
chardata++;
}
screen += RAW_SCREEN_WIDTH - TILEWIDTH;
}
hamfake_unlockScreen(screen);
}
void
gfx_printchar(int x, int y, char c)
{
int idx;
if (glb_gfxmode == 3)
{
gfx_printcharraw(x * TILEWIDTH, y * TILEHEIGHT, c);
return;
}
// If there is no change, don't do anything...
if (glb_charmap[y * 32 + x] == c)
return;
// Locking error:
// 1) Attempt lock on 'a'. Fail and get notile
// 2) Update charmap with 'a'
// 3) Attempt lock on 'a', succeed.
// 4) Unlock this charmap and free the tile despite extra count.
// Thus, we request the actual number to record in our charmap
// that is set to INVALID if lock fails.
// Unlock the old tile...
gfx_unlockCharTile(glb_charmap[y*32 + x]);
idx = gfx_lockCharTile(c, (u8 *) &c);
glb_charmap[y*32 + x] = c;
ham_SetMapTile(0, x, y, idx);
}
void
gfx_printcolourchar(int x, int y, char c, COLOUR_NAMES colour)
{
int idx;
if (glb_gfxmode == 3)
{
gfx_printcolourcharraw(x * TILEWIDTH, y * TILEHEIGHT, c, colour);
return;
}
// There is no such thing as no change in the coloured char world.
// Locking error:
// 1) Attempt lock on 'a'. Fail and get notile
// 2) Update charmap with 'a'
// 3) Attempt lock on 'a', succeed.
// 4) Unlock this charmap and free the tile despite extra count.
// Thus, we request the actual number to record in our charmap
// that is set to INVALID if lock fails.
// Unlock the old tile...
gfx_unlockCharTile(glb_charmap[y*32 + x]);
idx = gfx_lockColourCharTile(c, colour, (u8 *) &c);
glb_charmap[y*32 + x] = c;
ham_SetMapTile(0, x, y, idx);
}
void
gfx_cleartextline(int y, int startx)
{
int x;
for (x = startx; x < 30; x++)
gfx_printchar(x, y, ' ');
}
void
gfx_nudgecenter(int px, int py)
{
int tx, ty;
glb_nudgex = px;
glb_nudgey = py;
gfx_getscrollcenter(tx, ty);
gfx_scrollcenter(tx, ty);
}
void
gfx_getscrollcenter(int &tx, int &ty)
{
tx = glb_offx;
ty = glb_offy;
}
void gfx_scrollcenter(int tx, int ty)
{
// int dorebuild = 0;
// Clamp to the size of the map...
#if 0
if (tx - 8 < 0)
tx = 8;
if (tx + 8 >= MAP_WIDTH)
tx = MAP_WIDTH - 8;
if (ty - 5 < 0)
ty = 5;
if (ty + 5 >= MAP_HEIGHT)
ty = MAP_HEIGHT - 5;
#endif
// We want the specified tx/ty to become the center of our tile map.
// The problem is if it doesn't fit on our current screen, we must
// scroll & rebuild.
// Our screen width is 15 tiles and height 10 tiles.
// This code allows us to have a real screen larger than 32x32
// by scrolling the area. Sadly, my scroll code sucks and costs
// way too much to do, so results in horrible flickering etc,
// thus I decided to go back to 32x32.
#if 0
if (tx + 8 - glb_scrollx > 16)
{
glb_scrollx += 16;
dorebuild = 1;
}
if (tx - 8 < glb_scrollx)
{
glb_scrollx -= 16;
dorebuild = 1;
}
if (ty + 5 - glb_scrolly > 16)
{
glb_scrolly += 16;
dorebuild = 1;
}
if (ty - 5 < glb_scrolly)
{
glb_scrolly -= 16;
dorebuild = 1;
}
if (dorebuild)
{
gfx_refreshtiles();
}
#endif
// If we are running on the DS we need slightly different magic constants.
// Specifically, we have 256 rather than 240 wide, so need an extra
// 8 offset horizontally. Likewise, we have 192 rather than 160 vertically
// so need an extra 16 vertically. Ideally we will eventually use this
// extra space.
#ifdef iPOWDER
M_BG1SCRLX_SET((tx-glb_scrollx)*TILEWIDTH*2 - 14*TILEWIDTH - TILEWIDTH + glb_nudgex);
M_BG1SCRLY_SET((ty-glb_scrolly)*TILEHEIGHT*2 - 9*TILEHEIGHT - 3*TILEHEIGHT + glb_nudgey);
M_BG2SCRLX_SET((tx-glb_scrollx)*TILEWIDTH*2 - 14*TILEWIDTH - TILEWIDTH + glb_nudgex);
M_BG2SCRLY_SET((ty-glb_scrolly)*TILEHEIGHT*2 - 9*TILEHEIGHT - 3*TILEHEIGHT + glb_nudgey);
M_BG3SCRLX_SET((tx-glb_scrollx)*TILEWIDTH*2 - 14*TILEWIDTH - TILEWIDTH + glb_nudgex);
M_BG3SCRLY_SET((ty-glb_scrolly)*TILEHEIGHT*2 - 9*TILEHEIGHT - 3*TILEHEIGHT + glb_nudgey);
// We also need to set the text port
M_BG0SCRLX_SET(-TILEWIDTH);
M_BG0SCRLY_SET(-TILEHEIGHT*3);
#elif defined(USING_DS) || defined(USING_SDL)
M_BG1SCRLX_SET((tx-glb_scrollx)*TILEWIDTH*2 - 14*TILEWIDTH - TILEWIDTH + glb_nudgex);
M_BG1SCRLY_SET((ty-glb_scrolly)*TILEHEIGHT*2 - 9*TILEHEIGHT - 2*TILEHEIGHT + glb_nudgey);
M_BG2SCRLX_SET((tx-glb_scrollx)*TILEWIDTH*2 - 14*TILEWIDTH - TILEWIDTH + glb_nudgex);
M_BG2SCRLY_SET((ty-glb_scrolly)*TILEHEIGHT*2 - 9*TILEHEIGHT - 2*TILEHEIGHT + glb_nudgey);
M_BG3SCRLX_SET((tx-glb_scrollx)*TILEWIDTH*2 - 14*TILEWIDTH - TILEWIDTH + glb_nudgex);
M_BG3SCRLY_SET((ty-glb_scrolly)*TILEHEIGHT*2 - 9*TILEHEIGHT - 2*TILEHEIGHT + glb_nudgey);
// We also need to set the text port
M_BG0SCRLX_SET(-TILEWIDTH);
M_BG0SCRLY_SET(-TILEHEIGHT*2);
#else
M_BG1SCRLX_SET((tx-glb_scrollx)*TILEWIDTH*2 - 14*TILEWIDTH + glb_nudgex);
M_BG1SCRLY_SET((ty-glb_scrolly)*TILEHEIGHT*2 - 9*TILEHEIGHT + glb_nudgey);
M_BG2SCRLX_SET((tx-glb_scrollx)*TILEWIDTH*2 - 14*TILEWIDTH + glb_nudgex);
M_BG2SCRLY_SET((ty-glb_scrolly)*TILEHEIGHT*2 - 9*TILEHEIGHT + glb_nudgey);
M_BG3SCRLX_SET((tx-glb_scrollx)*TILEWIDTH*2 - 14*TILEWIDTH + glb_nudgex);
M_BG3SCRLY_SET((ty-glb_scrolly)*TILEHEIGHT*2 - 9*TILEHEIGHT + glb_nudgey);
// No need to adjust the textport in raw GBA mode.
#endif
glb_offx = tx;
glb_offy = ty;
}
int
gfx_isnewframe()
{
int result;
hamfake_rebuildScreen();
hamfake_awaitEvent();
// The event could be a new frame...
result = glb_newframe;
glb_newframe = 0;
return result;
}
int
gfx_getframecount()
{
return glb_framecount;
}
u16 glb_tilestashtiles[TILESTASH];
char glb_tilestashdata[TILESTASH][4];
int glb_tilestashsize = 0;
void
gfx_updatebitmap()
{
int i;
// Write out bg2tiledata to the bg2tiles.
//ham_ReloadTileGfx(glb_bg2tiles, (u16 *)glb_bg2tiledata, 0, 600);
//ham_ReloadTileGfx(glb_bg2tiles, (u16 *)glb_bg2tiledata, 0, glb_tilestashsize);
for (i = 0; i < glb_tilestashsize; i++)
{
ham_ReloadTileGfx(glb_bg1tiles,
&((u16 *) glb_bg2tiledata)[WORDPERTILE * i],
glb_tilestashtiles[i], 1);
}
}
void
gfx_putpixel(char c, int x, int y)
{
int tx, ty;
UT_ASSERT(x >= 0 && y >= 0 && x < 240 && y < 160);
tx = x >> 3;
ty = y >> 3;
x = x & 7;
y = y & 7;
glb_bg2tiledata[tx * BYTEPERTILE + ty * BYTEPERTILE * 30 + y * TILEWIDTH + x] = c;
}
void
gfx_drawverline(char c, int x, int ly, int hy)
{
while (ly <= hy)
gfx_putpixel(c, x, ly++);
}
void
gfx_drawhorline(char c, int lx, int y, int hx)
{
while (lx <= hx)
gfx_putpixel(c, lx++, y);
}
void
gfx_drawbox(char c, int lx, int ly, int hx, int hy, int solid)
{
if (solid)
{
while (ly <= hy)
gfx_drawhorline(c, lx, ly++, hx);
}
else
{
gfx_drawhorline(c, lx, ly, hx);
gfx_drawhorline(c, lx, hy, hx);
gfx_drawverline(c, lx, ly+1, hy-1);
gfx_drawverline(c, hx, ly+1, hy-1);
}
}
// Given a SQUARE number give us the minimap colour.
int
gfx_getcolfromtile(int tile, int mapped)
{
COLOUR_NAMES c;
if (mapped)
{
c = (COLOUR_NAMES) glb_squaredefs[tile].minicolour;
}
else
{
c = COLOUR_LIGHTBLACK;
}
return gfx_lookupcolor(c);
}
static void
gfx_resettilestash()
{
int i;
for (i = 0; i < glb_tilestashsize; i++)
{
// Mark it as freed...
glb_idxcount[glb_tilestashtiles[i]] = 0;
}
glb_tilestashsize = 0;
}
#ifdef USING_SDL
void
gfx_fillminimapblock(char *dst, char c[4])
{
int sy, sx;
for (sy = 0; sy < TILEHEIGHT/2; sy++)
{
for (sx = 0; sx < TILEWIDTH/2; sx++)
*dst++ = c[0];
for (; sx < TILEWIDTH; sx++)
*dst++ = c[1];
}
for (; sy < TILEHEIGHT; sy++)
{
for (sx = 0; sx < TILEWIDTH/2; sx++)
*dst++ = c[2];
for (; sx < TILEWIDTH; sx++)
*dst++ = c[3];
}
}
#else
void
gfx_fillminimapblock(char *dst, char c[4])
{
int sy;
for (sy = 0; sy < 4; sy++)
{
*dst++ = c[0];
*dst++ = c[0];
*dst++ = c[0];
*dst++ = c[0];
*dst++ = c[1];
*dst++ = c[1];
*dst++ = c[1];
*dst++ = c[1];
}
for (sy = 0; sy < 4; sy++)
{
*dst++ = c[2];
*dst++ = c[2];
*dst++ = c[2];
*dst++ = c[2];
*dst++ = c[3];
*dst++ = c[3];
*dst++ = c[3];
*dst++ = c[3];
}
}
#endif
int
gfx_findminimaptilenumber(char c[4])
{
int i;
u16 tile;
// Search the tile stash to see if we are already in it...
for (i = 0; i < glb_tilestashsize; i++)
{
tile = glb_tilestashtiles[i];
// We can flip horizontally, vertically, or both.
if (c[0] == glb_tilestashdata[i][0])
{
// No flip.
if (c[1] == glb_tilestashdata[i][1] &&
c[2] == glb_tilestashdata[i][2] &&
c[3] == glb_tilestashdata[i][3])
{
return tile;
}
}
if (c[0] == glb_tilestashdata[i][1])
{
// Flip horizontally
if (c[1] == glb_tilestashdata[i][0] &&
c[2] == glb_tilestashdata[i][3] &&
c[3] == glb_tilestashdata[i][2])
{
return tile | 1024;
}
}
if (c[0] == glb_tilestashdata[i][2])
{
// Flip vertically
if (c[1] == glb_tilestashdata[i][3] &&
c[2] == glb_tilestashdata[i][0] &&
c[3] == glb_tilestashdata[i][1])
{
return tile | 2048;
}
}
if (c[0] == glb_tilestashdata[i][3])
{
// Flip both.
if (c[1] == glb_tilestashdata[i][2] &&
c[2] == glb_tilestashdata[i][1] &&
c[3] == glb_tilestashdata[i][0])
{
return tile | 1024 | 2048;
}
}
}
// We didn't find the tile.
tile = gfx_findFreeTile(1);
if (tile == INVALID_TILEIDX)
return gfx_lookupTile(TILE_NOTILE);
// Ignore for now as we know it occrus on Quizar's map.
// Seems rather mean to keep this limit since we know we have
// a hard limit with gfx_findFreeTile(1).
// Why not allow stash to be bigger and just make sure
// we free the tile's afterwards?
// UT_ASSERT(i < TILESTASH);
if (i >= TILESTASH)
{
return gfx_lookupTile(TILE_NOTILE);
}
i = glb_tilestashsize++;
glb_tilestashtiles[i] = tile;
glb_idxcount[tile]++;
// Build the tile.
memcpy(glb_tilestashdata[i], c, 4);
gfx_fillminimapblock(&glb_bg2tiledata[i * BYTEPERTILE], c);
return tile;
}
// Fill in an 8x8 block given the colour array. Colours are
// (0,0), (1,0), (0,1), (1,1).
void
gfx_drawminimapblock(int bx, int by, char c[4])
{
char *dst;
dst = &glb_bg2tiledata[by * BYTEPERTILE * 30 + bx * BYTEPERTILE];
gfx_fillminimapblock(dst, c);
}
void
gfx_displayminimap(MAP *level)
{
gfx_resettilestash();
// gfx_drawbox(glb_stdcolor[COLOUR_RED], 51, 11, 189, 149, 0);
char c[4];
int ax, ay; // AVatar position.
if (MOB::getAvatar())
{
ax = MOB::getAvatar()->getX();
ay = MOB::getAvatar()->getY();
}
else
{
ax = -1;
ay = -1;
}
// The map is 32x32.
// With 4 pixel size elements, we have a 128x128 map.
// We draw it from 56 to 184 and 16 to 144
// We have a 10 pixel border on all sides.
int x, y, bx, by, tile;
// For efficiency, we write to one tile at a time. This greatly
// improves the efficiency...
y = 0;
for (by = 0; by < 16; by++)
{
x = 0;
for (bx = 0; bx < 16; bx++)
{
c[0] = gfx_getcolfromtile(level->getTile(x, y),
level->getFlag(x, y, SQUAREFLAG_MAPPED));
c[1] = gfx_getcolfromtile(level->getTile(x+1, y),
level->getFlag(x+1, y, SQUAREFLAG_MAPPED));
c[2] = gfx_getcolfromtile(level->getTile(x, y+1),
level->getFlag(x, y+1, SQUAREFLAG_MAPPED));
c[3] = gfx_getcolfromtile(level->getTile(x+1, y+1),
level->getFlag(x+1, y+1, SQUAREFLAG_MAPPED));
if ((ax >> 1) == bx && (ay >> 1) == by)
{
c[(ax & 1) + ((ay &1) << 1)] = glb_stdcolor[COLOUR_WHITE];
}
tile = gfx_findminimaptilenumber(c);
ham_SetMapTile(0, bx+7, by+2, tile);
// gfx_drawminimapblock(bx + 7, by + 2, c);
x += 2;
}
y += 2;
}
#if 0
for (item = level->getItemHead(); item; item = item->getNext())
{
gfx_drawbox(glb_stdcolor[COLOUR_GREEN],
56 + item->getX() * 4 + 1, 16 + item->getY() * 4 + 1,
56 + item->getX() * 4 + 2, 16 + item->getY() * 4 + 2,
1);
}
for (mob = level->getMobHead(); mob; mob = mob->getNext())
{
// Turn this #if off in order to get clarivoyance all the
// time.
#if 1
if (level->hasLOS(MOB::getAvatar()->getX(), MOB::getAvatar()->getY(),
mob->getX(), mob->getY()))
#endif
{
gfx_drawbox(glb_stdcolor[COLOUR_WHITE],
56 + mob->getX() * 4 + 1, 16 + mob->getY() * 4 + 1,
56 + mob->getX() * 4 + 2, 16 + mob->getY() * 4 + 2,
1);
}
}
#endif
gfx_updatebitmap();
#if 0
{
BUF buf;
buf.sprintf("%d tiles used", glb_tilestashsize);
msg_report(buf);
}
#endif
}
void
gfx_hideminimap()
{
// Free up tiles.
gfx_resettilestash();
// Erase the tile list.
int tile, bx, by;
tile = gfx_lookupTile(TILE_VOID);
for (by = 0; by < 16; by++)
{
// We corrupted the text line so just write spaces over it.
gfx_cleartextline(by + 2);
// The text clear may be a no-op if the tile was already a space
// we thus explicitly set the tile to void
for (bx = 0; bx < 16; bx++)
ham_SetMapTile(0, bx+7, by+2, tile);
}
}
int
gfx_lookupcolor(int r, int g, int b)
{
return gfx_lookupcolor(r, g, b, glb_tilesets[glb_tileset].palette);
}
int
gfx_lookupcolor(int r, int g, int b, const u16 *palette)
{
int i;
int minerror, closematch, cr, cg, cb;
// Larger than any possible match.
minerror = 255*255*3 + 1;
closematch = 0;
// We skip colour 0 as that is invisible.
for (i = 1; i < (int)512 / 2; i++)
{
// Unpack the given colour from 5:5:5
cb = (palette[i] >> 10) << 3;
cg = ((palette[i] >> 5) & 31) << 3;
cr = (palette[i] & 31) << 3;
// Find the difference squared.
cr -= r;
cg -= g;
cb -= b;
cr *= cr;
cg *= cg;
cb *= cb;
cr += cg;
cr += cb;
// cr is now the squared difference. If this is a new low, keep.
if (cr < minerror)
{
minerror = cr;
closematch = i;
}
}
UT_ASSERT(closematch);
// Result is closematch.
return closematch;
}
int
gfx_lookupcolor(COLOUR_NAMES color)
{
UT_ASSERT(color < NUM_COLOURS);
return glb_stdcolor[color];
}
void
gfx_sleep(int vcycles)
{
if (glbStressTest)
return;
int start = glb_framecount;
while (glb_framecount < start + vcycles)
{
hamfake_rebuildScreen();
hamfake_awaitEvent();
}
}
void
gfx_updateinventoryline(MOB *mob)
{
ITEM *item;
gfx_cleartextline(18);
if (!mob)
{
gfx_printtext(0, 18, "No item slot");
return;
}
item = mob->getItem(glb_invx, glb_invy);
if (item)
gfx_printtext(0, 18,
gram_capitalize(item->getName(false, false, true)));
else if (glb_invx == 0)
{
ITEMSLOT_NAMES slot = (ITEMSLOT_NAMES) glb_invy;
if (mob->hasSlot(slot))
gfx_printtext(0, 18,
gram_capitalize(mob->getSlotName(slot)));
else
gfx_printtext(0, 18, "No item slot");
}
else
gfx_printtext(0, 18, "Empty slot");
}
void
gfx_showinventory(MOB *mob)
{
int tx, ty, ix, iy;
ITEM *item;
BUF buf;
glb_invmob = mob;
// Clear out all the lines...
for (ty = 2; ty < 19; ty++)
gfx_cleartextline(ty);
// Write out the descriptions...
for (iy = 0; iy < NUM_ITEMSLOTS; iy++)
{
gfx_printtext(0, iy * 2 + 2, glb_itemslotdefs[iy].desc1);
gfx_printtext(0, iy * 2 + 3, glb_itemslotdefs[iy].desc2);
}
// Width 12, height 8.
// Our complete size 15 x 10
for (iy = 0; iy < MOBINV_HEIGHT; iy++)
{
ty = iy + 1;
for (ix = 0; ix < MOBINV_WIDTH; ix++)
{
tx = ix + 2 + (ix!=0);
// Draw the inventory item with pass thru and clear out
// the overlay..
item = mob->getItem(ix, iy);
if (!item)
gfx_setabstile(tx, ty, TILE_NORMALSLOT);
else if (item->isFavorite())
{
gfx_setabstile(tx, ty, TILE_FAVORITESLOT);
}
else
{
if (item->isKnownCursed())
{
if (item->isBlessed())
gfx_setabstile(tx, ty, TILE_HOLYSLOT);
else if (item->isCursed())
gfx_setabstile(tx, ty, TILE_CURSEDSLOT);
else
gfx_setabstile(tx, ty, TILE_NORMALSLOT);
}
else
{
if (item->isKnownNotCursed())
gfx_setabstile(tx, ty, TILE_NORMALSLOT);
else
gfx_setabstile(tx, ty, TILE_EMPTYSLOT);
}
}
if (!ix)
{
if (!mob->hasSlot((ITEMSLOT_NAMES) iy))
{
gfx_setabsmob(tx, ty, TILE_INVALIDSLOT);
}
else
gfx_setabsmob(tx, ty,
(item ? item->getTile() : TILE_VOID));
}
else
gfx_setabsmob(tx, ty,
(item ? item->getTile() : TILE_VOID));
if (ix == glb_invx && iy == glb_invy)
gfx_setabsoverlay(tx, ty, TILE_CURSOR);
else
gfx_setabsoverlay(tx, ty, TILE_VOID);
// If the item is quivered, add the q flag.
if (item && item->isQuivered())
{
gfx_printtext(tx * 2 + 1, ty * 2, "q");
}
// Flag artifacts.
if (item && item->isArtifact())
{
gfx_printtext(tx * 2, ty * 2, SYMBOLSTRING_UNIQUE);
}
// If the stack count is non-unit, we add the number.
if (item && item->getStackCount() != 1)
{
buf.sprintf("%2d", item->getStackCount());
gfx_printtext(tx * 2, ty * 2 + 1, buf);
}
}
}
// Update the current inventory item...
gfx_updateinventoryline(mob);
}
void
gfx_hideinventory()
{
int ty;
// Clear out all the lines...
for (ty = 2; ty < 19; ty++)
gfx_cleartextline(ty);
gfx_refreshtiles();
glb_invmob = 0;
}
void
gfx_getinvcursor(int &ix, int &iy)
{
ix = glb_invx;
iy = glb_invy;
}
void
gfx_setinvcursor(int ix, int iy, bool onlyslots)
{
int tx, ty;
ITEM *item;
// Erase from old pos...
tx = glb_invx + 2 + (glb_invx != 0);
ty = glb_invy + 1;
// but only if inventory currently up
if (glb_invmob)
gfx_setabsoverlay(tx, ty, TILE_VOID);
// Scroll the cursor as you hit the top of the inventory
// If we are on the equipment column, we always stay on the
// equipment column.
// If we are in the inventory area, we loop only in there.
// Horizontal movement never adjusts y value.
if (iy < 0)
{
if (ix)
{
ix--;
if (!ix)
ix = MOBINV_WIDTH-1;
}
iy = MOBINV_HEIGHT-1;
}
else if (iy >= MOBINV_HEIGHT)
{
if (ix)
{
ix++;
if (ix == MOBINV_WIDTH)
ix = 1;
}
iy = 0;
}
else if (ix < 0)
{
ix = MOBINV_WIDTH-1;
}
else if (ix >= MOBINV_WIDTH)
{
ix = 0;
}
// Ensure we never leave the itemslot list...
if (onlyslots)
ix = 0;
// ix/iy might still be out of range, so it makes sense
// to reclamp here.
// Update new position...
glb_invx = ix % MOBINV_WIDTH;
if (glb_invx < 0) glb_invx += MOBINV_WIDTH;
glb_invy = iy % MOBINV_HEIGHT;
if (glb_invy < 0) glb_invy += MOBINV_HEIGHT;
tx = glb_invx + 2 + (glb_invx != 0);
ty = glb_invy + 1;
// Rest of this is only needed if inventory is displayed.
if (!glb_invmob)
return;
gfx_setabsoverlay(tx, ty, TILE_CURSOR);
// Update the current inventory item...
gfx_updateinventoryline(glb_invmob);
}
void
gfx_displaylist(int x, int y, int ylen, const char **list, int select, int &startoflist, int &endoflist)
{
int i, j, n, displaylen;
int scroll;
const char *entry = "You should stop reading the binary!";
// Calculate list size.
for (n = 0; list[n]; n++);
// Determine the scroll offset required for select to be centered.
displaylen = ylen-y;
if (n < displaylen)
scroll = 0; // No scrolling necessary.
else
{
scroll = select - displaylen/2 + 1;
// Do not scroll down past end.
if (scroll + displaylen > n)
{
scroll = n - displaylen+1;
}
// Do not scroll up past start.
// Note that a scroll of 1 merely hides the first entry, so
// isn't at all useful.
if (scroll <= 1)
scroll = 0;
}
// First, chew up the scrolled entries.
for (i = 0; i < scroll; i++)
{
entry = list[i];
if (!entry) break;
}
startoflist = scroll;
// Now, display & clear the rest of the text.
j = y;
if (scroll)
{
// Print the scroll up message
gfx_cleartextline(j, x-1);
gfx_printtext(x, j, SYMBOLSTRING_UP SYMBOLSTRING_UP SYMBOLSTRING_UP);
j++;
}
for (; j < ylen; j++)
{
gfx_cleartextline(j, x-1);
if (entry)
{
entry = list[i];
if (entry)
{
// Last line may be scroll down. This is true so long as
// there is extra entries.
if (list[i+1] && j == (ylen-1))
{
gfx_printtext(x, j,
SYMBOLSTRING_DOWN SYMBOLSTRING_DOWN SYMBOLSTRING_DOWN);
}
else
{
gfx_printtext(x, j, entry);
endoflist = i;
}
// Highlight current selection.
if (i == select)
{
gfx_printtext(x-1, j, SYMBOLSTRING_RIGHT);
gfx_printtext(x+strlen(entry), j, SYMBOLSTRING_LEFT);
}
}
i++;
}
}
}
// Evil!
void writeGlobalActionBar(bool useemptyslot);
// Returns selected menu entry...
// x & y is where top left of menu will be. Default is menu[0].
// -1 means the selection was cancelled.
int
gfx_selectmenu(int x, int y, const char **menu, int &aorb, int def,
bool anykey, bool disablepaging,
const u8 *menuactions,
u8 *actionstrip)
{
int i, num, select, dx, dy;
const char *entry;
aorb = -1;
num = 0;
for (i = 0; (entry = menu[i]); i++)
{
num++;
}
// If there are no items to pick, cancel immediately.
if (!num)
return -1;
// Now, we can treat menu as a 0..num-1 array.
select = def;
if (select >= num)
select = 0; // Illegal default given.
if (select < 0)
select = 0; // Less than zero is also illegal.
STYLUSLOCK styluslock((!menuactions) ? REGION_MENU
: (REGION_MENU | REGION_BOTTOMBUTTON | REGION_SIDEBUTTON));
int startoflist = 0, endoflist = 0;
// If we are in drag mode, we need to free up the borders so they
// can be selected.
if (menuactions)
styluslock.setRange(x, 0, 29, 20);
else
styluslock.setRange(x, -1, -1, -1);
// Display the menu.
gfx_displaylist(x, y, 18, menu, select, startoflist, endoflist);
// Wait for all buttons to go up to start...
// We only check for buttons that we care about. We don't want to
// abort key repeat on arrow keys in case someone cancels a popup
// window.
#ifndef HAS_KEYBOARD
while ( (ctrl_rawpressed(BUTTON_A) || ctrl_rawpressed(BUTTON_B))
&& !hamfake_forceQuit())
{
if (!gfx_isnewframe())
continue;
}
#endif
// Wipe out all of our hit states.
hamfake_clearKeyboardBuffer();
if (menuactions) writeGlobalActionBar(true);
while (1)
{
if (!gfx_isnewframe())
continue;
if (hamfake_forceQuit())
return -1;
#ifdef HAS_KEYBOARD
int key;
key = hamfake_getKeyPress(false);
dx = 0;
dy = 0;
switch (key)
{
case GFX_KEYF1:
case GFX_KEYF2:
case GFX_KEYF3:
case GFX_KEYF4:
case GFX_KEYF5:
case GFX_KEYF6:
case GFX_KEYF7:
case GFX_KEYF8:
case GFX_KEYF9:
case GFX_KEYF10:
case GFX_KEYF11:
case GFX_KEYF12:
case GFX_KEYF13:
case GFX_KEYF14:
case GFX_KEYF15:
if (menuactions)
{
// Bind the first 15 menu actions to our key
int index = key - GFX_KEYF1;
actionstrip[index] = menuactions[select];
// Rebuild the list.
writeGlobalActionBar(true);
}
break;
case 'k':
case GFX_KEYUP:
case '8':
dy = -1;
break;
case 'j':
case GFX_KEYDOWN:
case '2':
dy = 1;
break;
case GFX_KEYPGUP:
dy = MAX(-10, -select);
break;
case GFX_KEYPGDOWN:
dy = MIN(10, num - select - 1);
break;
case 'l':
case 'h':
case GFX_KEYLEFT:
case GFX_KEYRIGHT:
case '4':
case '6':
dy = 0;
break;
// Press a.
case ' ':
case '\n':
case '\r':
case '5':
aorb = 0;
if (anykey)
aorb = BUTTON_A;
if (menuactions) writeGlobalActionBar(false);
return select;
// Press b.
case '0':
case '\x1b':
aorb = 1;
if (anykey)
aorb = BUTTON_B;
if (menuactions) writeGlobalActionBar(false);
return select;
// Request for more information, a select key!
case 'i':
case '?':
if (anykey)
{
aorb = BUTTON_SELECT;
if (menuactions) writeGlobalActionBar(false);
return select;
}
default:
dx = 0;
dy = 0;
break;
}
#else
dx = dy = 0;
if (ctrl_hit(BUTTON_START))
{
// Cancel
if (menuactions) writeGlobalActionBar(false);
return -1;
}
if (ctrl_hit(BUTTON_A))
{
// Accept....
aorb = 0;
if (anykey)
aorb = BUTTON_A;
if (menuactions) writeGlobalActionBar(false);
return select;
}
if (ctrl_hit(BUTTON_B))
{
aorb = 1;
if (anykey)
aorb = BUTTON_B;
if (menuactions) writeGlobalActionBar(false);
return select;
}
if (anykey)
{
if (ctrl_hit(BUTTON_SELECT))
{
aorb = BUTTON_SELECT;
if (menuactions) writeGlobalActionBar(false);
return select;
}
if (ctrl_hit(BUTTON_X))
{
aorb = BUTTON_X;
if (menuactions) writeGlobalActionBar(false);
return select;
}
if (ctrl_hit(BUTTON_Y))
{
aorb = BUTTON_Y;
if (menuactions) writeGlobalActionBar(false);
return select;
}
}
if (anykey && disablepaging)
{
if (ctrl_hit(BUTTON_L))
{
aorb = BUTTON_L;
if (menuactions) writeGlobalActionBar(false);
return select;
}
if (ctrl_hit(BUTTON_R))
{
aorb = BUTTON_R;
if (menuactions) writeGlobalActionBar(false);
return select;
}
}
else
{
// We clamp page up/down to the limits
if (ctrl_hit(BUTTON_L))
dy = MAX(-10, -select);
if (ctrl_hit(BUTTON_R))
dy = MIN(10, num - select - 1);
}
// Change our position if dy not zero.
// Only test arrow keys if r or l not pressed
if (!dx && !dy)
ctrl_getdir(dx, dy);
#endif
// Process any drag requests.
if (menuactions)
{
if (styluslock.performDrags(y*TILEHEIGHT, startoflist, endoflist, menuactions, actionstrip))
{
// Update...
if (menuactions) writeGlobalActionBar(true);
}
}
int styluschoice;
bool inbounds = false;
if (styluslock.selectmenu(styluschoice, x*TILEWIDTH, y*TILEHEIGHT, inbounds))
{
if (!inbounds)
{
// User clicked outside of the menu, consider
// a cancel.
aorb = 1;
if (anykey)
aorb = BUTTON_B;
if (menuactions) writeGlobalActionBar(false);
return select;
}
else
{
// A real selection if this was valid.
// Check for the case of up arrows.
if (startoflist)
styluschoice--;
if (styluschoice < 0)
{
// Scroll up.
dy = MAX(-10, -select);
}
else if (startoflist + styluschoice > endoflist)
{
// Scroll down.
dy = MIN(10, num - select - 1);
}
else
{
aorb = 0;
if (anykey)
aorb = BUTTON_A;
if (menuactions) writeGlobalActionBar(false);
return startoflist + styluschoice;
}
}
}
if (!dx && !dy)
{
// Nothing to do.
continue;
}
else if (dx && !dy)
{
// Fast paging with left/right for cases where L/R
// are already overload.
if (ctrl_hit(BUTTON_L))
dy = MAX(-10, -select);
if (ctrl_hit(BUTTON_R))
dy = MIN(10, num - select - 1);
}
if (dy)
{
// Adjust selection...
select += dy;
// Note: On the libdns system this triggers a compiler bug
// of some sort with -O2. Basically, if dy < 0 we take the
// following branch as if select == dy.
if (select < 0)
select = num-1;
if (select >= num) // Not else so zero length lists yield 0
select = 0;
gfx_displaylist(x, y, 18, menu, select, startoflist, endoflist);
}
}
if (menuactions) writeGlobalActionBar(false);
return -1;
}
bool
gfx_yesnomenu(const char *prompt, bool defchoice)
{
int aorb;
int choice, width;
// Prompt...
const char *menu[4] =
{
"Yes",
"No",
0
};
width = strlen(prompt);
// Build an overlay for our question...
{
// Erase all tiles.
int sx, sy;
TILE_NAMES tile;
for (sy = 2; sy < 6; sy++)
{
if (sy == 2)
tile = TILE_SCROLL_TOP;
else if (sy == 5)
tile = TILE_SCROLL_BOTTOM;
else
tile = TILE_SCROLL_BACK;
for (sx = 2; sx < 2 + (width+1)/2; sx++)
{
// We don't want to write past the end
// even for a large prompt.
if (sx >= 16)
break;
gfx_setabsoverlay(sx, sy, tile);
}
}
}
// Write out the query.
// This really should be made a horizontal
// yes/no box!
gfx_printtext(4, 6, prompt);
choice = gfx_selectmenu(8, 8, menu, aorb, !defchoice);
if (aorb)
choice = -1;
if (choice >= 1)
choice = -1;
// Clear
{
int y;
for (y = 3; y < 19; y++)
gfx_cleartextline(y);
}
// This is because of our overlay options...
gfx_refreshtiles();
if (choice == 0)
return true;
return false;
}
void
gfx_displayinfoblock(const char *text, bool prevline)
{
int x, y;
gfx_cleartextline(3);
gfx_cleartextline(18);
if (prevline)
{
gfx_printchar(0, 3, SYMBOL_UP);
gfx_printchar(14, 3, SYMBOL_UP);
gfx_printchar(29, 3, SYMBOL_UP);
}
for (y = 4; y < 18; y++)
{
for (x = 0; x < 30; x++)
{
if (!*text)
{
gfx_printchar(x, y, ' ');
}
else
{
gfx_printchar(x, y, *text);
text++;
}
}
}
// If there is more text (which there should be if we get here)
// prompt the fact.
if (*text)
{
gfx_printchar(0, 18, SYMBOL_DOWN);
gfx_printchar(14, 18, SYMBOL_DOWN);
gfx_printchar(29, 18, SYMBOL_DOWN);
}
}
void
gfx_displayinfotext(const char *text)
{
const char *curtext;
int dx, dy, y;
STYLUSLOCK styluslock(REGION_DISPLAYTEXT);
gfx_displayinfoblock(text, false);
curtext = text;
while (1)
{
if (!gfx_isnewframe())
continue;
if (hamfake_forceQuit())
return;
#ifdef HAS_KEYBOARD
int key;
bool stop = false;
key = hamfake_getKeyPress(false);
dy = 0;
switch (key)
{
case 'k':
case GFX_KEYUP:
case '8':
dy = -1;
break;
case 'j':
case GFX_KEYDOWN:
case '2':
dy = 1;
break;
case GFX_KEYPGUP:
dy = -10;
break;
case GFX_KEYPGDOWN:
dy = 10;
break;
case 'l':
case 'h':
case '4':
case '6':
case GFX_KEYLEFT:
case GFX_KEYRIGHT:
dy = 0;
break;
case ' ':
case '5':
case '0':
case '\r':
case '\x1b':
stop = true;
break;
default:
dy = 0;
break;
}
if (stop)
break;
#else
if (ctrl_hit(BUTTON_A))
break;
if (ctrl_hit(BUTTON_B))
break;
if (ctrl_hit(BUTTON_R))
{
dy = 10;
}
else if (ctrl_hit(BUTTON_L))
{
dy = -10;
}
else
{
ctrl_getdir(dx, dy);
}
hamfake_clearKeyboardBuffer();
#endif
if (styluslock.getdisplaytext(dy))
{
dy = dy;
if (!dy)
break;
}
if (!dy)
continue;
while (dy)
{
if (dy < 0)
{
dy++;
// User wants to scroll up.
if (curtext != text)
{
curtext -= 30;
}
else
dy = 0;
}
if (dy > 0)
{
dy--;
// User wants scroll down.
if (strlen(curtext) >= 30 * (18 - 4))
{
curtext += 30;
}
else
dy = 0;
}
}
gfx_displayinfoblock(curtext, curtext != text);
}
for (y = 3; y < 19; y++)
gfx_cleartextline(y);
}
char **glb_pager = 0;
int glb_pager_curline = -1, glb_pager_size = 0;
int glb_pager_width = 30;
void
gfx_pager_setwidth(int width)
{
glb_pager_width = width;
}
void
gfx_pager_addtext(BUF buf)
{
gfx_pager_addtext(buf.buffer());
}
void
gfx_pager_addtext(const char *text)
{
// Make sure we have a line...
if (!glb_pager)
{
glb_pager_curline = -1;
glb_pager_size = 0;
gfx_pager_newline();
}
const char *src;
char *dst, *origdst;
int dstlen, dstmaxlen;
// Point to the next write location...
src = text;
origdst = dst = glb_pager[glb_pager_curline];
dstmaxlen = glb_pager_width;
dstlen = strlen(dst);
dst += dstlen;
while (*src && dstlen < dstmaxlen)
{
*dst++ = *src++;
dstlen++;
}
// If we ran out of source material, done.
if (!*src)
{
*dst++ = '\0';
return;
}
// We have to wrap :< If src is a space, wrapping is trivial.
// Otherwise, we backup src until it is a space...
while (!isspace(*src) && src > text)
{
src--;
dst--;
dstlen--;
}
// Now, move forward until we ate all the spaces (this seems odd
// except if *src was a space going into the last loop)
while (*src && isspace(*src))
{
src++;
}
// It could be trailing spaces sent us over the bounds. We will
// thus eat these and ignore them.
if (!*src)
{
*dst++ = '\0';
return;
}
// Avoid throwing this on the stack as we recurse and GBA sucks.
char *prefix = new char[30+1];
prefix[0] = '\0';
// Special case: If src == str now, yet our dstlen == 0, we have a
// single word over SCREEN_WIDTH. We resolve this by dumping
// dstmaxlen - 1 chars, a hyphen, and then continuing as if we
// found a space.
if (src == text && !dstlen)
{
while (*src && dstlen < dstmaxlen-1)
{
*dst++ = *src++;
dstlen++;
}
// Append a hyphen...
*dst++ = '-';
}
// Special case: If src == str, dstlen != 0, we are tacking onto
// the end of an existing word. If that word ends with a space,
// great, we wrap as expected. However, we often build up
// a string with a series of writes, such as "foo" followed by ", bar".
// We want to wrap the last word on dst. If dst doesn't have a sub
// word, obviously we avoid this.
// Of course, this isn't the full story. It could be that the
// previous line got to exactly 30 columns. Any spaces would be
// silently eaten so it would look like there are no spaces.
// To solve this, we look for special non-breaking characters that
// we feel should be enjoined. Specifically, a punctuation on the
// LHS means we should break, and a punctuation on the RHS means
// we should not.
else if (src == text)
{
char rhs, lhs;
rhs = src[0];
lhs = origdst[dstlen-1];
// Check if LHS is breaking, in which case we always break
// so ignore this code path.
if (!isspace(lhs) && lhs != '.' && lhs != ',' && lhs != '!' && lhs != '?')
{
// Find the start of the word in dst...
int wordstart = dstlen - 1;
while (!isspace(origdst[wordstart]) && wordstart > 0)
{
wordstart--;
}
// If wordstart is now zero, our previuos entry was the
// full length. We might hyphenate here, but I think it
// is simpler just to let the break lie where we broke
// it before.
if (wordstart)
{
// Since wordstart points to a space, we can safely
// mark wordstart as null & strcpy out wordstart+1
strcpy(prefix, &origdst[wordstart+1]);
origdst[wordstart] = '\0';
}
}
}
// Wrap to the next line. Null terminate dst and build
// a new line.
*dst++ = '\0';
gfx_pager_newline();
// And, because we are too lazy to make this a loop, recurse.
gfx_pager_addtext(prefix);
delete [] prefix;
gfx_pager_addtext(src);
}
void
gfx_pager_addsingleline(const char *text)
{
// Make sure we have a line...
if (!glb_pager)
{
glb_pager_curline = -1;
glb_pager_size = 0;
gfx_pager_newline();
}
const char *src;
char *dst;
int dstlen, dstmaxlen;
// Point to the next write location...
src = text;
dst = glb_pager[glb_pager_curline];
dstmaxlen = glb_pager_width;
dstlen = strlen(dst);
if (dstlen > 0)
gfx_pager_newline();
dst = glb_pager[glb_pager_curline];
dstmaxlen = glb_pager_width;
dstlen = strlen(dst);
dst += dstlen;
while (*src && dstlen < dstmaxlen)
{
*dst++ = *src++;
dstlen++;
}
// Even if we didn't run out of source, we truncate and return.
*dst++ = '\0';
// Ensure next line starts fresh.
gfx_pager_newline();
}
void
gfx_pager_newline()
{
// Move to the next line.
glb_pager_curline++;
// Check for over flow.
if (glb_pager_curline >= glb_pager_size)
{
// Reallocate.
char **newpager;
newpager = new char *[glb_pager_size + 32];
if (glb_pager)
memcpy(newpager, glb_pager, sizeof(char *) * glb_pager_size);
glb_pager_size += 32;
delete [] glb_pager;
glb_pager = newpager;
}
// And ensure the new buffer is allocated.
glb_pager[glb_pager_curline] = new char[glb_pager_width+2];
glb_pager[glb_pager_curline][0] = '\0';
}
void
gfx_pager_separator()
{
// This is so long so 80 column print outs get a full separator.
gfx_pager_addsingleline("-------------------------------------------------------------------------------");
}
void
gfx_pager_display_block(int y)
{
int sx, sy;
const char *text;
gfx_cleartextline(3);
gfx_cleartextline(18);
if (y)
{
gfx_printchar(0, 3, SYMBOL_UP);
gfx_printchar(14, 3, SYMBOL_UP);
gfx_printchar(29, 3, SYMBOL_UP);
}
for (sy = 4; sy < 18; sy++, y++)
{
if (y > glb_pager_curline)
gfx_cleartextline(sy);
else
{
text = glb_pager[y];
for (sx = 0; sx < 30; sx++)
{
if (!*text)
{
gfx_printchar(sx, sy, ' ');
}
else
{
gfx_printchar(sx, sy, *text);
text++;
}
}
}
}
// If we still have rows left, display down arrows.
if (y < glb_pager_curline)
{
gfx_printchar(0, 18, SYMBOL_DOWN);
gfx_printchar(14, 18, SYMBOL_DOWN);
gfx_printchar(29, 18, SYMBOL_DOWN);
}
}
void
gfx_pager_savetofile(const char *name)
{
#ifdef HAS_DISKIO
if (!hamfake_fatvalid())
{
gfx_pager_reset();
return;
}
#ifdef iPOWDER
if (hamfake_cansendemail())
{
for (int y = 3; y < 19; y++)
gfx_cleartextline(y);
if (gfx_yesnomenu("Email Game Summary?", false))
{
// Send an email.
BUF body;
for (int i = 0; i <= glb_pager_curline; i++)
{
body.strcat(glb_pager[i]);
body.strcat("\n\r");
}
hamfake_sendemail("", name, body.buffer());
}
}
gfx_pager_reset();
return;
#endif
FILE *fp;
fp = hamfake_fopen(name, "wt");
if (!fp)
{
printf("OPEN %s FAILED!\n", name);
return;
}
int i;
for (i = 0; i <= glb_pager_curline; i++)
{
#if defined(LINUX) || defined(WIN32)
fprintf(fp, "%s\n", glb_pager[i]);
#else
// For embedded systems always use line feeds so
// the windows users can parse it easier.
// Linux users are more used to crappy line feeds.
fprintf(fp, "%s\r\n", glb_pager[i]);
#endif
}
fclose(fp);
#endif
gfx_pager_reset();
}
void hideSideActionBar();
void
gfx_pager_display(int y)
{
if (!glb_pager) return;
int dx, dy;
int tile;
// Make y the center.
y -= (18 - 4) / 2;
if (y >= glb_pager_curline - (18 - 4))
y = glb_pager_curline - (18 - 4);
// Hide the side buttons.
hideSideActionBar();
// Clip the requested y position.
if (y < 0)
y = 0;
// Erase all tiles.
int sx, sy;
for (sy = 1; sy < 10; sy++)
{
if (sy == 1)
tile = TILE_SCROLL_TOP;
else if (sy == 9)
tile = TILE_SCROLL_BOTTOM;
else
tile = TILE_SCROLL_BACK;
for (sx = -1; sx < 16; sx++)
{
gfx_setabsoverlay(sx, sy, tile);
}
}
gfx_pager_display_block(y);
#ifndef HAS_KEYBOARD
// Wait for no button to be hit.
while (ctrl_anyrawpressed() && !hamfake_forceQuit())
{
hamfake_awaitEvent();
}
#endif
// Clear the repeat queue of SDL
hamfake_clearKeyboardBuffer();
STYLUSLOCK styluslock(REGION_DISPLAYTEXT);
while (1)
{
if (!gfx_isnewframe())
continue;
if (hamfake_forceQuit())
return;
// Quit immediately in stress test
if (glbStressTest)
break;
#ifdef HAS_KEYBOARD
int key;
bool stop = false;
key = hamfake_getKeyPress(false);
dy = 0;
switch (key)
{
case 'k':
case GFX_KEYUP:
case '8':
dy = -1;
break;
case 'j':
case GFX_KEYDOWN:
case '2':
dy = 1;
break;
case GFX_KEYPGUP:
dy = -10;
break;
case GFX_KEYPGDOWN:
dy = 10;
break;
case 'l':
case 'h':
case GFX_KEYLEFT:
case GFX_KEYRIGHT:
case '4':
case '6':
dy = 0;
break;
case ' ':
case '\n':
case '\r':
case '5':
case '0':
case '\x1b':
stop = true;
break;
default:
dy = 0;
break;
}
if (stop)
break;
#else
if (ctrl_hit(BUTTON_A))
break;
if (ctrl_hit(BUTTON_B))
break;
if (ctrl_hit(BUTTON_R))
{
dy = 10;
}
else if (ctrl_hit(BUTTON_L))
{
dy = -10;
}
else
{
ctrl_getdir(dx, dy);
}
hamfake_clearKeyboardBuffer();
#endif
if (styluslock.getdisplaytext(dy))
{
dy = dy;
if (!dy)
break;
}
if (dy < 0)
{
// User wants to scroll up.
if (y)
{
y += dy;
if (y < 0)
y = 0;
gfx_pager_display_block(y);
}
}
if (dy > 0)
{
if (y < glb_pager_curline - (18 - 4))
{
y += dy;
if (y > glb_pager_curline - (18 - 4))
{
y = glb_pager_curline - (18 - 4);
}
gfx_pager_display_block(y);
}
}
}
for (y = 3; y < 19; y++)
gfx_cleartextline(y);
gfx_pager_reset();
// Because we did absolute sets, this will restore.
gfx_refreshtiles();
// Clear the repeat queue of SDL
hamfake_clearKeyboardBuffer();
}
void
gfx_pager_reset()
{
int y;
// Clear out our pager.
for (y = 0; y <= glb_pager_curline; y++)
{
delete [] glb_pager[y];
}
delete [] glb_pager;
glb_pager = 0;
glb_pager_size = 0;
glb_pager_curline = -1;
}
bool
gfx_selecttile(int &tx, int &ty, bool quickselect, int *quickold, int *aorb,
bool *stylus)
{
int dx, dy;
int oldoverlay;
bool cancel;
STYLUSLOCK styluslock(REGION_MAPTILES);
if (stylus)
*stylus = false;
// If we are quickselecting, our old overlay may be the cursor
// itself - we require the caller to maintain its value.
oldoverlay = gfx_getoverlay(tx, ty);
if (quickselect)
oldoverlay = quickold ? *quickold : TILE_VOID;
if (aorb)
*aorb = 0;
gfx_setoverlay(tx, ty, TILE_CURSOR);
gfx_scrollcenter(tx, ty);
while (1)
{
if (!gfx_isnewframe())
continue;
if (hamfake_forceQuit())
return false;
if (ctrl_hit(BUTTON_A))
{
// Accept....
gfx_scrollcenter(MOB::getAvatar()->getX(), MOB::getAvatar()->getY());
gfx_setoverlay(tx, ty, oldoverlay);
if (aorb)
*aorb = 0;
hamfake_clearKeyboardBuffer();
return true;
}
if (ctrl_hit(BUTTON_B))
{
// Cancel
gfx_scrollcenter(MOB::getAvatar()->getX(), MOB::getAvatar()->getY());
gfx_setoverlay(tx, ty, oldoverlay);
if (aorb)
*aorb = 1;
hamfake_clearKeyboardBuffer();
if (quickselect)
return true;
else
return false;
}
if (styluslock.getmaptile(dx, dy, cancel))
{
// User pressed on a tile successfully.
gfx_scrollcenter(MOB::getAvatar()->getX(), MOB::getAvatar()->getY());
gfx_setoverlay(tx, ty, oldoverlay);
// Set the new location
tx = dx;
ty = dy;
// Determine the overlay value of the new tile that we
// are moving the curosr to.
oldoverlay = gfx_getoverlay(tx, ty);
if (quickselect && quickold)
*quickold = oldoverlay;
if (aorb)
*aorb = cancel;
if (stylus)
*stylus = true;
hamfake_clearKeyboardBuffer();
return !cancel;
}
ctrl_getdir(dx, dy);
if (!dx && !dy)
{
// Consume any key in case an invalid key was hit
hamfake_getKeyPress(false);
continue;
}
// Erase old...
gfx_setoverlay(tx, ty, oldoverlay);
tx += dx;
ty += dy;
tx &= MAP_WIDTH - 1;
ty &= MAP_HEIGHT - 1;
oldoverlay = gfx_getoverlay(tx, ty);
if (quickselect && quickold)
*quickold = oldoverlay;
gfx_setoverlay(tx, ty, TILE_CURSOR);
gfx_scrollcenter(tx, ty);
if (quickselect)
return false;
}
}
void writeYesNoBar();
bool
gfx_selectinventory(int &selectx, int &selecty)
{
bool madechoice = false;
int button;
int dx, dy;
bool apress, bpress;
STYLUSLOCK styluslock(REGION_BOTTOMBUTTON);
writeYesNoBar();
gfx_setinvcursor(selectx, selecty, false);
while (1)
{
if (!gfx_isnewframe())
continue;
if (hamfake_forceQuit())
return false;
ctrl_getdir(dx, dy);
apress = ctrl_hit(BUTTON_A);
bpress = ctrl_hit(BUTTON_B);
if (styluslock.getbottombutton(button))
{
// Either a cancel or accept.
if (button == 18)
apress = true;
if (button == 26)
bpress = true;
}
if (bpress)
{
// Cancel
madechoice = false;
break;
}
if (apress)
{
madechoice = true;
break;
}
if (stylus_queryinventoryitem(selectx, selecty))
{
gfx_setinvcursor(selectx, selecty, false);
continue;
}
// Consume invalid keys.
hamfake_getKeyPress(false);
// We refetch this as this will deal
// silently with wrap.
gfx_getinvcursor(selectx, selecty);
selectx += dx;
selecty += dy;
gfx_setinvcursor(selectx, selecty, false);
}
return madechoice;
}
bool
gfx_selectdirection(int otx, int oty, int &rdx, int &rdy, int &rdz)
{
int dx, dy, ndx, ndy, tx, ty;
int oldoverlay;
bool chose = false;
STYLUSLOCK styluslock(REGION_TENDIR);
hamfake_flushdir();
hamfake_buttonreq(5, 1);
rdx = rdy = rdz = 0;
tx = otx;
ty = oty;
oldoverlay = gfx_getoverlay(tx, ty);
gfx_setoverlay(tx, ty, TILE_CURSOR);
dx = dy = 0;
while (1)
{
if (!gfx_isnewframe())
continue;
if (hamfake_forceQuit())
return false;
if (ctrl_hit(BUTTON_A))
{
// Accept....
gfx_setoverlay(tx, ty, oldoverlay);
chose = true;
break;
}
if (ctrl_hit(BUTTON_B))
{
// Cancel
gfx_setoverlay(tx, ty, oldoverlay);
chose = false;
break;
}
ctrl_getdir(ndx, ndy);
if (hamfake_externaldir(dx, dy))
{
gfx_setoverlay(tx, ty, oldoverlay);
chose = true;
break;
}
if (styluslock.gettendir(rdx, rdy, rdz, chose))
{
// Invert sense of choosing.
gfx_setoverlay(tx, ty, oldoverlay);
chose = !chose;
hamfake_buttonreq(5, 0);
return chose;
}
if (!ndx && !ndy)
{
// Consume any key in case an invalid key was hit
hamfake_getKeyPress(false);
continue;
}
// Erase old...
gfx_setoverlay(tx, ty, oldoverlay);
dx += ndx;
dy += ndy;
// Clamp to our funny shape...
if (dy >= 2)
{
dy = 2;
dx = 0;
}
if (dy <= -2)
{
dy = -2;
dx = 0;
}
if (dx < -1)
dx = -1;
if (dx > 1)
dx = 1;
tx = otx + dx;
ty = oty + dy;
tx &= MAP_WIDTH - 1;
ty &= MAP_HEIGHT - 1;
oldoverlay = gfx_getoverlay(tx, ty);
gfx_setoverlay(tx, ty, TILE_CURSOR);
}
// Parse out our dx,dy into rdx, rdy, rdz.
if (dy == 2)
rdz = -1;
else if (dy == -2)
rdz = 1;
else
{
rdx = dx;
rdy = dy;
}
hamfake_buttonreq(5, 0);
return chose;
}
void
gfx_copytiledata(TILE_NAMES desttile, MINI_NAMES minitile, bool ismale)
{
const u8 *tiledata;
int i;
u16 tile;
tile = gfx_lookupTile(desttile);
if (tile == INVALID_TILEIDX)
return;
if (ismale)
tiledata = &glb_tilesets[glb_tileset].mini[minitile * BYTEPERTILE*4];
else
tiledata = &glb_tilesets[glb_tileset].minif[minitile * BYTEPERTILE*4];
if (!glb_comptiledata)
{
// Allocate a u16 to guarantee alignment.
u16 *origdata = new u16 [WORDPERTILE*4];
glb_comptiledata = (u8 *) origdata;
}
for (i = 0; i < BYTEPERTILE*4; i++)
glb_comptiledata[i] = tiledata[i];
// HMm... I think ReloadTIleGfx needs aligned data, which is not
// guaranteed with a static array. No idea how the minitiles
// work around this...
glb_lastdesttile = tile;
ham_ReloadTileGfx(glb_bg1tiles, (u16 *) glb_comptiledata, glb_lastdesttile, 4);
}
u8 *
gfx_extractsprite(int tileset, SPRITE_NAMES tile)
{
int w, h, x, y, srcidx;
u8 *data, *dataout;
u16 c;
w = glb_tilesets[tileset].tilewidth;
h = glb_tilesets[tileset].tilewidth;
data = (u8 *) malloc(4 * w * h * 3);
dataout = data;
for (y = 0; y < h * 2; y++)
{
for (x = 0; x < w * 2; x++)
{
srcidx = 0;
srcidx += y * w;
srcidx += x;
if (x >= w)
srcidx += w * h - w;
if (y >= h)
srcidx += w * h * 2 - w * h;
c = glb_tilesets[tileset].spritepalette[glb_tilesets[tileset].sprite[tile * w * h * 4 + srcidx]];
int cb, cg, cr;
cb = (c >> 10) << 3;
cg = ((c >> 5) & 31) << 3;
cr = (c & 31) << 3;
*dataout++ = cr;
*dataout++ = cg;
*dataout++ = cb;
}
}
return data;
}
u8 *
gfx_extractlargesprite(SPRITE_NAMES tile, int grey, SPRITE_NAMES overlay)
{
#ifdef iPOWDER
int w, h, x, y, srcidx;
u8 *data, *dataout;
u16 c;
w = 48;
h = 48;
grey = 256 - grey;
int cutoff = (grey * h + 128) / 256;
data = (u8 *) malloc(w * h * 4);
dataout = data;
int row, col;
if (tile != SPRITE_INVALID)
{
row = tile / 24;
col = tile - row*24;
for (y = 0; y < h; y++)
{
for (x = 0; x < w; x++)
{
srcidx = col * w + row * w * h * 24;
srcidx += y * w * 24;
srcidx += x;
c = bmp_sprite16_3x[srcidx];
int cb, cg, cr;
cb = (c >> 10) << 3;
cg = ((c >> 5) & 31) << 3;
cr = (c & 31) << 3;
if (y < cutoff)
{
cr = rgbtogrey(cr, cg, cb);
cg = cb = cr;
}
*dataout++ = cr;
*dataout++ = cg;
*dataout++ = cb;
if (c)
*dataout++ = 255;
else
*dataout++ = 0;
}
}
}
else
{
memset(data, 0, w * h * 4);
}
if (overlay != SPRITE_INVALID)
{
row = overlay / 24;
col = overlay - row*24;
dataout = data;
for (y = 0; y < h; y++)
{
for (x = 0; x < w; x++)
{
srcidx = col * w + row * w * h * 24;
srcidx += y * w * 24;
srcidx += x;
c = bmp_sprite16_3x[srcidx];
if (!c)
{
dataout += 4;
}
else
{
int cb, cg, cr;
cb = (c >> 10) << 3;
cg = ((c >> 5) & 31) << 3;
cr = (c & 31) << 3;
*dataout++ = cr;
*dataout++ = cg;
*dataout++ = cb;
*dataout++ = 255;
}
}
}
}
return data;
#else
return 0;
#endif
}
void
gfx_compositetile(ITEMSLOT_NAMES dstslot, MINI_NAMES minitile, bool ismale)
{
const u8 *minidata;
int x, y, sx, sy;
int dstidx, srcidx;
// The use of the suffix "f" to denote the female tileset is
// derogratory: it implies all humans are male until they are explicitly
// labeled FEmale. To atone for this, the parameter to determine gender
// has the opposite sense: ismale implies it is the men that must be
// explicitly specified.
if (ismale)
minidata = &glb_tilesets[glb_tileset].mini[minitile * 4 * BYTEPERTILE];
else
minidata = &glb_tilesets[glb_tileset].minif[minitile * 4 * BYTEPERTILE];
// Determine our offset.
int offx, offy;
bool flipx, flipy;
offx = glb_itemslotdefs[glb_minidefs[minitile].slot].posx;
offx -= glb_itemslotdefs[dstslot].posx;
offy = glb_itemslotdefs[glb_minidefs[minitile].slot].posy;
offy -= glb_itemslotdefs[dstslot].posy;
// Convert our offset into our new tilespace...
if (TILEWIDTH != 8)
{
offx = (int) ((offx / 8.0) * TILEWIDTH + 0.5);
}
if (TILEHEIGHT != 8)
{
offy = (int) ((offy / 8.0) * TILEHEIGHT + 0.5);
}
flipx = glb_itemslotdefs[glb_minidefs[minitile].slot].flipx ^
glb_itemslotdefs[dstslot].flipx;
flipy = glb_itemslotdefs[glb_minidefs[minitile].slot].flipy ^
glb_itemslotdefs[dstslot].flipy;
for (y = 0; y < TILEHEIGHT*2; y++)
{
for (x = 0; x < TILEWIDTH*2; x++)
{
if (flipx)
sx = TILEWIDTH*2-1 - x;
else
sx = x;
if (flipy)
sy = TILEHEIGHT*2-1 - y;
else
sy = y;
sx += offx;
sy += offy;
if (sx < 0 || sx >= TILEWIDTH*2)
continue;
if (sy < 0 || sy >= TILEHEIGHT*2)
continue;
// Get position inside sub tile.
dstidx = 0;
dstidx += y * TILEWIDTH;
dstidx += x;
// Find actual sub tile.
if (x >= TILEWIDTH)
dstidx += BYTEPERTILE - TILEWIDTH;
if (y >= TILEHEIGHT)
dstidx += BYTEPERTILE*2 - BYTEPERTILE;
srcidx = 0;
srcidx += sy * TILEWIDTH;
srcidx += sx;
if (sx >= TILEWIDTH)
srcidx += BYTEPERTILE - TILEWIDTH;
if (sy >= TILEHEIGHT)
srcidx += BYTEPERTILE*2 - BYTEPERTILE;
// Actual comp:
if (minidata[srcidx])
glb_comptiledata[dstidx] = minidata[srcidx];
}
}
ham_ReloadTileGfx(glb_bg1tiles, (u16 *) glb_comptiledata,
glb_lastdesttile, 4);
}
int
gfx_getfont()
{
return glb_currentfont;
}
void
gfx_switchfonts(int newfont)
{
if (newfont == glb_currentfont)
return;
glb_currentfont = newfont;
// Do nothing in mode 3 or -1.
if (glb_gfxmode)
return;
// Reload all locked font tiles..
int c;
int sourceidx;
int idx;
// Active alphabet tiles:
for (c = 0; c < 255; c++)
{
idx = glb_charidx[c];
if (idx != INVALID_TILEIDX)
{
sourceidx = gfx_getchartile(c);
ham_ReloadTileGfx(glb_bg1tiles,
(u16*)(&glb_tilesets[glb_tileset].alphabet[glb_currentfont][sourceidx * BYTEPERTILE]),
idx,
1);
}
}
}
void
gfx_switchtilesets(int newtileset)
{
int i;
if (newtileset == glb_tileset)
return;
glb_tileset = newtileset;
#ifdef iPOWDER
glb_modetilesets[glb_tilesetmode] = newtileset;
#endif
// TODO: Guard if sizes match,
hamfake_setTileSize(glb_tilesets[glb_tileset].tilewidth,
glb_tilesets[glb_tileset].tilewidth);
// Rebuild our arrays:
delete [] glb_bg2tiledata;
glb_bg2tiledata = (char *) new u16[WORDPERTILE*TILESTASH];
memset(glb_bg2tiledata, 0, BYTEPERTILE*TILESTASH);
delete [] glb_comptiledata;
glb_comptiledata = (u8 *) (new u16[WORDPERTILE*4]);
for (i = 0; i < MAX_COLOUR_CHAR; i++)
{
if (glb_colourchardata[i])
{
delete [] glb_colourchardata[i];
glb_colourchardata[i] = (u8 *) new u16[WORDPERTILE];
}
}
delete [] glb_greyscaletiledata;
glb_greyscaletiledata = (u8 *) (new u16[WORDPERTILE*4]);
// If we are in mode 3, reblit our background!
if (glb_gfxmode == 3)
{
gfx_reblitslugandblood();
for (int y = 0; y < 20; y++)
{
for (int x = 0; x < 32; x++)
{
if (glb_charmap[y*32+x] != 0xff)
gfx_printcharraw(x*TILEWIDTH, y*TILEHEIGHT, glb_charmap[y*32+x]);
}
}
}
// Do nothing in mode 3 or -1.
if (glb_gfxmode)
return;
// Swap palette:
ham_LoadBGPal((void *)glb_tilesets[glb_tileset].palette, 512);
hamfake_LoadSpritePal((void *)glb_tilesets[glb_tileset].spritepalette, 512);
// Rebuild our standard colours
gfx_buildstdcolors();
// Reload the sprite.
hamfake_ReloadSpriteGfx((u16*)(&glb_tilesets[glb_tileset].dungeon[glb_sprite*4*BYTEPERTILE]),
0, 4);
// Reload all locked tiles..
int c;
int sourceidx;
int idx;
// Active alphabet tiles:
for (c = 0; c < 255; c++)
{
idx = glb_charidx[c];
if (idx != INVALID_TILEIDX)
{
sourceidx = gfx_getchartile(c);
ham_ReloadTileGfx(glb_bg1tiles,
(u16*)(&glb_tilesets[glb_tileset].alphabet[glb_currentfont][sourceidx * BYTEPERTILE]),
idx,
1);
}
}
// Active dungeon tiles:
int tile;
for (tile = 0; tile < NUM_TILES; tile++)
{
idx = glb_tileidx[tile];
if (idx == INVALID_TILEIDX)
continue;
ham_ReloadTileGfx(glb_bg1tiles,
(u16*)(&glb_tilesets[glb_tileset].dungeon[tile * 4 * BYTEPERTILE]),
idx,
4);
}
// Rebuild custom dude.
if (MOB::getAvatar())
MOB::getAvatar()->rebuildAppearance();
// Reset our center as we may have switched to a different sized
// tile and the lower layer stores the offset in pixels
int tx, ty;
gfx_getscrollcenter(tx, ty);
gfx_scrollcenter(tx, ty);
}
int
gfx_gettileset()
{
return glb_tileset;
}
int
gfx_gettilesetmode(int mode)
{
#ifdef iPOWDER
return glb_modetilesets[mode];
#else
return glb_tileset;
#endif
}
void
gfx_tilesetmode(int mode)
{
#ifdef iPOWDER
if (mode == glb_tilesetmode)
return;
glb_tilesetmode = mode;
gfx_switchtilesets(glb_modetilesets[glb_tilesetmode]);
#endif
}
void
gfx_settilesetmode(int mode, int tileset)
{
#ifdef iPOWDER
glb_modetilesets[mode] = tileset;
if (mode == glb_tilesetmode)
gfx_switchtilesets(tileset);
#else
if (!mode)
gfx_switchtilesets(tileset);
#endif
}
int
gfx_gettilewidth()
{
return TILEWIDTH;
}
int
gfx_gettileheight()
{
return TILEHEIGHT;
}
void
gfx_drawcursor(int x, int y)
{
hamfake_movesprite(0, x-TILEWIDTH, y-TILEHEIGHT);
hamfake_enablesprite(0, true);
}
void
gfx_removecursor()
{
hamfake_enablesprite(0, false);
}
void
gfx_cursortile(SPRITE_NAMES tile)
{
UT_ASSERT(glb_usesprites);
glb_sprite = (u16) tile;
hamfake_ReloadSpriteGfx((u16*)(&glb_tilesets[glb_tileset].sprite[glb_sprite*4*BYTEPERTILE]),
0, 4);
}
void
gfx_spritetile(int spriteno, SPRITE_NAMES tile)
{
UT_ASSERT(glb_usesprites);
hamfake_ReloadSpriteGfx((u16*)(&glb_tilesets[glb_tileset].sprite[tile*4*BYTEPERTILE]),
spriteno*4, 4);
}
u8
gfx_togreyscale(u8 src, const u16 *palette)
{
int cb, cg, cr;
cb = (palette[src] >> 10) << 3;
cg = ((palette[src] >> 5) & 31) << 3;
cr = (palette[src] & 31) << 3;
int grey = rgbtogrey(cr, cg, cb);
return gfx_lookupcolor(grey, grey, grey, palette);
}
void
gfx_updateGreyTables(u8 *greytable, const u16 *palette)
{
int i;
for (i = 0; i < 256; i++)
{
greytable[i] = gfx_togreyscale(i, palette);
}
}
void
gfx_updatespritegreytable()
{
if (!glb_spritegreyscale)
return;
gfx_updateGreyTables(glb_spritegreyscale,
glb_tilesets[glb_tileset].spritepalette);
}
const u8 *
gfx_getspritegreytable()
{
if (!glb_spritegreyscale)
{
glb_spritegreyscale = new u8[256];
gfx_updatespritegreytable();
}
return glb_spritegreyscale;
}
void
gfx_spritetile_grey(int spriteno, SPRITE_NAMES tile, int grey, SPRITE_NAMES overlay)
{
const u8 *greytable;
if (!glb_greyscaletiledata)
{
u16 *origdata = new u16 [WORDPERTILE*4];
glb_greyscaletiledata = (u8 *) origdata;
}
greytable = gfx_getspritegreytable();
u8 *src;
grey = 256 - grey;
int cutoff = (grey * TILEHEIGHT * 2 + 128) / 256;
int i;
int x, y, t, yc;
src = (u8 *) (&glb_tilesets[glb_tileset].sprite[tile*4*BYTEPERTILE]);
i = 0;
for (t = 0; t < 4; t++)
{
if (t < 2)
y = 0;
else
y = TILEHEIGHT;
for (yc = 0; yc < TILEHEIGHT; yc++)
{
if (y + yc >= cutoff)
{
for (x = 0; x < TILEWIDTH; x++)
{
glb_greyscaletiledata[i] = src[i];
i++;
}
}
else
{
for (x = 0; x < TILEWIDTH; x++)
{
glb_greyscaletiledata[i] = greytable[src[i]];
i++;
}
}
}
}
if (overlay != SPRITE_INVALID)
{
src = (u8 *) (&glb_tilesets[glb_tileset].sprite[overlay*4*BYTEPERTILE]);
for (i = 0; i < BYTEPERTILE * 4; i++)
{
if (src[i])
glb_greyscaletiledata[i] = src[i];
}
}
hamfake_ReloadSpriteGfx((u16*)glb_greyscaletiledata,
spriteno*4, 4);
}
void
gfx_spritefromdungeon(int spriteno, TILE_NAMES tile)
{
UT_ASSERT(!glb_usesprites);
hamfake_ReloadSpriteGfx((u16*)(&glb_tilesets[glb_tileset].dungeon[tile*4*BYTEPERTILE]),
spriteno*4, 4);
}
void
gfx_spritemode(bool usesprites)
{
int i;
// Disable all sprites
for (i = 0; i < 128; i++)
hamfake_enablesprite(i, false);
// Swap palette
if (usesprites)
hamfake_LoadSpritePal((void *)glb_tilesets[glb_tileset].spritepalette, 512);
else
hamfake_LoadSpritePal((void *)glb_tilesets[glb_tileset].palette, 512);
glb_usesprites = usesprites;
}
//
// Fake Ham Functions
//
#if !defined(USING_SDL) && !defined(USING_DS)
#if !defined(NO_HAM)
void
hamfake_rebuildScreen()
{
}
void
hamfake_awaitEvent()
{
}
u16 *
hamfake_lockScreen()
{
return ((u16 *) 0x06000000);
}
void
hamfake_unlockScreen(u16 *)
{
}
int
hamfake_peekKeyPress()
{
return 0;
}
int
hamfake_getKeyPress(bool)
{
return 0;
}
void
hamfake_clearKeyboardBuffer()
{
}
void
hamfake_insertKeyPress(u8 key)
{
}
void
hamfake_softReset()
{
return;
}
#endif
int
hamfake_getKeyModifiers()
{
return 0;
}
void
hamfake_setFullScreen(bool fullscreen)
{
}
bool
hamfake_isFullScreen()
{
return true; // Always full :>
}
bool
hamfake_extratileset()
{
return false; // No fat system yet!
}
void
hamfake_getstyluspos(int &x, int &y)
{
x = y = 0;
}
bool
hamfake_getstylusstate()
{
return false;
}
void
hamfake_movesprite(int spriteno, int x, int y)
{
}
void
hamfake_enablesprite(int spriteno, bool enabled)
{
}
void
hamfake_ReloadSpriteGfx(const u16 *data, int tileno, int numtile)
{
}
void
hamfake_LoadSpritePal(void *data, int bytes)
{
}
void
hamfake_setTileSize(int width, int height)
{
}
#endif
#if !defined(iPOWDER)
bool hamfake_forceQuit() { return false; }
void hamfake_awaitShutdown() {}
void hamfake_postShutdown() {}
void hamfake_setinventorymode(bool invmode) {}
void hamfake_enableexternalactions(bool enable) {}
bool hamfake_isunlocked() { return true; }
void hamfake_externalaction(int &iact, int &ispell)
{ iact = ACTION_NONE; ispell = SPELL_NONE; }
void hamfake_flushdir() {}
bool hamfake_externaldir(int &dx, int &dy) { return false; }
void hamfake_buttonreq(int mode, int type) { }
#endif
|