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
|
//
//
#include "graphics.h"
#include "globalincs/vmallocator.h"
#include "scripting/api/objs/camera.h"
#include "scripting/api/objs/color.h"
#include "scripting/api/objs/enums.h"
#include "scripting/api/objs/font.h"
#include "scripting/api/objs/model.h"
#include "scripting/api/objs/movie_player.h"
#include "scripting/api/objs/object.h"
#include "scripting/api/objs/particle.h"
#include "scripting/api/objs/streaminganim.h"
#include "scripting/api/objs/subsystem.h"
#include "scripting/api/objs/texture.h"
#include "scripting/api/objs/vecmath.h"
#include "scripting/global_hooks.h"
#include <asteroid/asteroid.h>
#include <camera/camera.h>
#include <debris/debris.h>
#include <freespace.h>
#include <globalincs/systemvars.h>
#include <graphics/2d.h>
#include <graphics/openxr.h>
#include <graphics/material.h>
#include <graphics/matrix.h>
#include <hud/hudbrackets.h>
#include <hud/hudtarget.h>
#include <jumpnode/jumpnode.h>
#include <model/modelrender.h>
#include <parse/parselo.h>
#include <render/3d.h>
#include <render/3dinternal.h>
#include <render/batching.h>
#include <scripting/scripting.h>
#include <ship/ship.h>
#include <weapon/weapon.h>
#include <cmath>
namespace {
static const int NextDrawStringPosInitial[] = {0, 0};
static int NextDrawStringPos[] = {NextDrawStringPosInitial[0], NextDrawStringPosInitial[1]};
static fix PreviousFrametimeOverall = 0;
static bool WarnedBadThicknessLine = false;
}
namespace scripting {
namespace api {
model_draw_list *Current_scene = nullptr;
//**********LIBRARY: Graphics
ADE_LIB(l_Graphics, "Graphics", "gr", "Graphics Library");
static float lua_Opacity = 1.0f;
static int lua_Opacity_type = GR_ALPHABLEND_FILTER;
static int lua_ResizeMode = GR_RESIZE_NONE;
int get_resize_mode() { return lua_ResizeMode; }
//****SUBLIBRARY: Graphics/Cameras
ADE_LIB_DERIV(l_Graphics_Cameras, "Cameras", NULL, "Cameras", l_Graphics);
ADE_INDEXER(l_Graphics_Cameras, "number/string IndexOrName", "Gets camera", "camera", "Ship handle, or invalid ship handle if index was invalid")
{
const char* s = nullptr;
if(!ade_get_args(L, "*s", &s))
return ade_set_error(L, "o", l_Camera.Set(camid()));
camid cid = cam_lookup(s);
if(!cid.isValid())
{
int cn = atoi(s);
if(cn > 0)
{
//Lua-->FS2
cn--;
cid = cam_get_camera(cn);
}
}
return ade_set_args(L, "o", l_Camera.Set(cid));
}
ADE_FUNC(__len, l_Graphics_Cameras, NULL, "Gets number of cameras", "number", "Number of cameras")
{
return ade_set_args(L, "i", (int)cam_get_num());
}
//****SUBLIBRARY: Graphics/Fonts
ADE_LIB_DERIV(l_Graphics_Fonts, "Fonts", nullptr, "Font library", l_Graphics);
ADE_FUNC(__len, l_Graphics_Fonts, NULL, "Number of loaded fonts", "number", "Number of loaded fonts")
{
return ade_set_args(L, "i", font::FontManager::numberOfFonts());
}
ADE_INDEXER(l_Graphics_Fonts, "number/string IndexOrFilename", "Array of loaded fonts", "font", "Font handle, or invalid font handle if index is invalid")
{
if (lua_isnumber(L, 2))
{
int index = -1;
if (!ade_get_args(L, "*i", &index))
return ade_set_error(L, "o", l_Font.Set(font_h()));
auto realIdx = index - 1;
if (realIdx < 0 || realIdx >= font::FontManager::numberOfFonts())
{
return ade_set_error(L, "o", l_Font.Set(font_h()));
}
return ade_set_args(L, "o", l_Font.Set(font_h(font::FontManager::getFont(index - 1))));
}
else
{
const char* s = nullptr;
if(!ade_get_args(L, "*s", &s))
return ade_set_error(L, "o", l_Font.Set(font_h()));
return ade_set_args(L, "o", l_Font.Set(font_h(font::FontManager::getFont(s))));
}
}
ADE_VIRTVAR(CurrentFont, l_Graphics, "font", "Current font", "font", NULL)
{
font_h *newFh = NULL;
if(!ade_get_args(L, "*|o", l_Font.GetPtr(&newFh)))
return ade_set_error(L, "o", l_Font.Set(font_h()));
if(ADE_SETTING_VAR && newFh->isValid()) {
font::FontManager::setCurrentFont(newFh->Get());
}
return ade_set_args(L, "o", l_Font.Set(font_h(font::FontManager::getCurrentFont())));
}
//****SUBLIBRARY: Graphics/PostEffects
ADE_LIB_DERIV(l_Graphics_Posteffects, "PostEffects", nullptr, "Post-processing effects", l_Graphics);
ADE_INDEXER(l_Graphics_Posteffects, "number index", "Gets the name of the specified post-processing index", "string", "post-processing name or empty string on error")
{
int index = -1;
if(!ade_get_args(L, "*i", &index))
return ade_set_error(L, "s", "");
index--; // Lua -> C/C++
if (index < 0)
return ade_set_error(L, "s", "");
SCP_vector<SCP_string> names;
gr_get_post_process_effect_names(names);
names.push_back(SCP_string("lightshafts"));
if (index >= (int) names.size())
return ade_set_error(L, "s", "");
return ade_set_args(L, "s", const_cast<char*>(names[index].c_str()));
}
ADE_FUNC(__len, l_Graphics_Posteffects, nullptr, "Gets the number of available post-processing effects", "number", "number of post-processing effects or 0 on error")
{
SCP_vector<SCP_string> names;
gr_get_post_process_effect_names(names);
// Add one for lightshafts
return ade_set_args(L, "i", ((int) names.size()) + 1);
}
ADE_FUNC(setPostEffect, l_Graphics, "string name, [number value=0, number red=0.0, number green=0.0, number blue=0.0]", "Sets the intensity of the specified post-processing effect. Optionally sets RGB values for effects that use them (valid values are 0.0 to 1.0)", "boolean", "true when successful, false otherwise")
{
const char* name = nullptr;
int intensity = 0;
vec3d rgb; rgb.xyz.x = 0.0f; rgb.xyz.y = 0.0f; rgb.xyz.z = 0.0f; // clang you are a PITA
if (!ade_get_args(L, "s|ifff", &name, &intensity, &rgb.xyz.x, &rgb.xyz.y, &rgb.xyz.z))
return ADE_RETURN_FALSE;
if (name == nullptr || intensity < 0)
return ADE_RETURN_FALSE;
CAP(rgb.xyz.x, 0.0f, 1.0f);
CAP(rgb.xyz.y, 0.0f, 1.0f);
CAP(rgb.xyz.z, 0.0f, 1.0f);
gr_post_process_set_effect(name, intensity, &rgb);
return ADE_RETURN_TRUE;
}
ADE_FUNC(resetPostEffects, l_Graphics, nullptr, "Resets all post-processing effects to their default values", "boolean", "true if successful, false otherwise")
{
gr_post_process_set_defaults();
return ADE_RETURN_TRUE;
}
ADE_VIRTVAR(CurrentOpacityType, l_Graphics, "enumeration", "Current alpha blending type; uses ALPHABLEND_* enumerations", "enumeration", NULL)
{
enum_h *alphatype = NULL;
if(!ade_get_args(L, "*|o", l_Enum.GetPtr(&alphatype)))
return ADE_RETURN_NIL;
if(ADE_SETTING_VAR)
{
if(alphatype != NULL && alphatype->index == LE_ALPHABLEND_FILTER)
lua_Opacity_type = GR_ALPHABLEND_FILTER;
else
lua_Opacity_type = GR_ALPHABLEND_NONE;
}
lua_enum rtn;
switch(lua_Opacity_type)
{
case GR_ALPHABLEND_FILTER:
rtn = LE_ALPHABLEND_FILTER;
break;
default:
rtn = LE_ALPHABLEND_NONE;
}
return ade_set_args(L, "o", l_Enum.Set(enum_h(rtn)));
}
ADE_VIRTVAR(CurrentRenderTarget, l_Graphics, "texture", "Current rendering target", "texture", "Current rendering target, or invalid texture handle if screen is render target")
{
texture_h* newtx = nullptr;
if(ADE_SETTING_VAR && lua_isnil(L, 2))
{
bm_set_render_target(-1);
return ade_set_args(L, "o", l_Texture.Set(texture_h(gr_screen.rendering_to_texture)));
}
else
{
if (!ade_get_args(L, "*|o", l_Texture.GetPtr(&newtx)))
return ade_set_error(L, "o", l_Texture.Set(texture_h()));
if(ADE_SETTING_VAR) {
if (newtx != nullptr && newtx->isValid())
bm_set_render_target(newtx->handle, 0);
else
bm_set_render_target(-1);
}
return ade_set_args(L, "o", l_Texture.Set(texture_h(gr_screen.rendering_to_texture)));
}
}
ADE_VIRTVAR(CurrentResizeMode, l_Graphics, "enumeration ResizeMode", "Current resize mode; uses GR_RESIZE_* enumerations. This resize mode will be used by the gr.* drawing methods.", "enumeration", nullptr)
{
enum_h resize_arg;
if (!ade_get_args(L, "*|o", l_Enum.Get(&resize_arg)))
return ADE_RETURN_NIL;
if (ADE_SETTING_VAR)
{
if (!resize_arg.IsValid() || resize_arg.index < LE_GR_RESIZE_NONE || resize_arg.index > LE_GR_RESIZE_MENU_NO_OFFSET)
{
Warning(LOCATION, "Invalid resize mode index %d", resize_arg.index);
return ADE_RETURN_NIL;
}
lua_ResizeMode = resize_arg.index - LE_GR_RESIZE_NONE;
}
return ade_set_args(L, "o", l_Enum.Set(enum_h(static_cast<lua_enum>(LE_GR_RESIZE_NONE + lua_ResizeMode))));
}
ADE_FUNC(clear, l_Graphics, nullptr, "Calls gr_clear(), which fills the entire screen with the currently active color. Useful if you want to have a fresh start for drawing things. (Call this between setClip and resetClip if you only want to clear part of the screen.)", nullptr, nullptr)
{
gr_clear();
SCP_UNUSED(L); // avoid unused parameter warning
return ADE_RETURN_NIL;
}
ADE_FUNC(clearScreen, l_Graphics, "[number|color /* red value or color object */, number green, number blue, number alpha]",
"Clears the screen to black, or the color specified.",
nullptr,
nullptr)
{
int r, g, b, a;
r = g = b = 0;
a = 255;
if (lua_isnumber(L, 1)) {
ade_get_args(L, "|iiii", &r, &g, &b, &a);
} else {
color col;
gr_init_alphacolor(&col, 0, 0, 0, 255);
ade_get_args(L, "|o", l_Color.Get(&col));
r = col.red;
g = col.green;
b = col.blue;
a = col.alpha;
}
//WMC - Set to valid values
if(r != 0 || g != 0 || b != 0 || a!= 255)
{
CAP(r,0,255);
CAP(g,0,255);
CAP(b,0,255);
CAP(a,0,255);
gr_set_clear_color(r,g,b);
gr_screen.current_clear_color.alpha = (ubyte)a;
gr_clear();
gr_set_clear_color(0,0,0);
return ADE_RETURN_NIL;
}
gr_clear();
return ADE_RETURN_NIL;
}
ADE_FUNC(createCamera, l_Graphics,
"string Name, [vector Position, orientation Orientation]",
"Creates a new camera using the specified position and orientation (World)",
"camera",
"Camera handle, or invalid camera handle if camera couldn't be created")
{
const char* s = nullptr;
vec3d *v = &vmd_zero_vector;
matrix_h *mh = NULL;
if(!ade_get_args(L, "s|oo", &s, l_Vector.GetPtr(&v), l_Matrix.GetPtr(&mh)))
return ADE_RETURN_NIL;
matrix *mtx = &vmd_identity_matrix;
if(mh != NULL)
mtx = mh->GetMatrix();
camid cid = cam_create(s, v, mtx);
//Set position
return ade_set_args(L, "o", l_Camera.Set(cid));
}
ADE_FUNC(isMenuStretched, l_Graphics, NULL, "Returns whether the standard interface is stretched", "boolean", "True if stretched, false if aspect ratio is maintained")
{
if(!Gr_inited)
return ade_set_error(L, "b", false);
return ade_set_args(L, "b", Cmdline_stretch_menu != 0);
}
ADE_FUNC(getScreenWidth, l_Graphics, NULL, "Gets screen width", "number", "Width in pixels, or 0 if graphics are not initialized yet")
{
if(!Gr_inited)
return ade_set_error(L, "i", 0);
return ade_set_args(L, "i", gr_screen.max_w);
}
ADE_FUNC(getScreenHeight, l_Graphics, NULL, "Gets screen height", "number", "Height in pixels, or 0 if graphics are not initialized yet")
{
if(!Gr_inited)
return ade_set_error(L, "i", 0);
return ade_set_args(L, "i", gr_screen.max_h);
}
ADE_FUNC(getCenterWidth, l_Graphics, NULL, "Gets width of center monitor (should be used in conjunction with getCenterOffsetX)", "number", "Width of center monitor in pixels, or 0 if graphics are not initialized yet")
{
if(!Gr_inited)
return ade_set_error(L, "i", 0);
return ade_set_args(L, "i", gr_screen.center_w);
}
ADE_FUNC(getCenterHeight, l_Graphics, NULL, "Gets height of center monitor (should be used in conjunction with getCenterOffsetY)", "number", "Height of center monitor in pixels, or 0 if graphics are not initialized yet")
{
if(!Gr_inited)
return ade_set_error(L, "i", 0);
return ade_set_args(L, "i", gr_screen.center_h);
}
ADE_FUNC(getCenterOffsetX, l_Graphics, NULL, "Gets X offset of center monitor", "number", "X offset of center monitor in pixels")
{
if(!Gr_inited)
return ade_set_error(L, "i", 0);
return ade_set_args(L, "i", gr_screen.center_offset_x);
}
ADE_FUNC(getCenterOffsetY, l_Graphics, NULL, "Gets Y offset of center monitor", "number", "Y offset of center monitor in pixels")
{
if(!Gr_inited)
return ade_set_error(L, "i", 0);
return ade_set_args(L, "i", gr_screen.center_offset_y);
}
ADE_FUNC(getCurrentCamera, l_Graphics, "[boolean]", "Gets the current camera handle, if argument is <i>true</i> then it will also return the main camera when no custom camera is in use", "camera", "camera handle or invalid handle on error")
{
camid current;
bool rtnMain = false;
ade_get_args(L, "|b", &rtnMain);
if (!rtnMain || Viewer_mode & VM_FREECAMERA)
current = cam_get_current();
else
current = Main_camera;
return ade_set_args(L, "o", l_Camera.Set(current));
}
ADE_FUNC(getVectorFromCoords, l_Graphics,
"[number X=center, number Y=center, number Depth, boolean normalize = false]",
"Returns a vector through screen coordinates x and y. "
"If depth is specified, vector is extended to Depth units into space"
"If normalize is true, vector will be normalized.",
"vector",
"Vector, or zero vector on failure")
{
int x = gr_screen.max_w/2;
int y = gr_screen.max_h/2;
float depth = 0.0f;
bool normalize = false;
ade_get_args(L, "|iifb", &x, &y, &depth, &normalize);
vec3d pos = vmd_zero_vector;
bool in_frame = g3_in_frame() > 0;
if(!in_frame) {
g3_start_frame(0);
vec3d cam_pos;
matrix cam_orient;
camid cid;
if (Viewer_mode & VM_FREECAMERA)
cid = cam_get_current();
else
cid = Main_camera;
camera *cam = cid.getCamera();
if (cam != NULL) {
cam->get_info(&cam_pos, &cam_orient);
g3_set_view_matrix(&cam_pos, &cam_orient, View_zoom);
} else {
g3_set_view_matrix(&Eye_position, &Eye_matrix, View_zoom);
}
gr_set_proj_matrix( Proj_fov, gr_screen.clip_aspect, Min_draw_distance, Max_draw_distance);
gr_set_view_matrix(&Eye_position, &Eye_matrix);
}
g3_point_to_vec(&pos, x, y);
if(!in_frame) {
gr_end_view_matrix();
gr_end_proj_matrix();
g3_end_frame();
}
if(depth)
vm_vec_scale(&pos, depth);
if (normalize)
vm_vec_normalize_quick(&pos);
vm_vec_add2(&pos, &View_position);
return ade_set_args(L, "o", l_Vector.Set(pos));
}
ADE_FUNC(setTarget, l_Graphics, "[texture Texture]",
"If texture is specified, sets current rendering surface to a texture."
"Otherwise, sets rendering surface back to screen.",
"boolean",
"True if successful, false otherwise")
{
if(!Gr_inited)
return ade_set_error(L, "b", false);
texture_h* idx = nullptr;
ade_get_args(L, "|o", l_Texture.GetPtr(&idx));
if (idx == nullptr) {
return ade_set_args(L, "b", bm_set_render_target(-1, 0));
}
else {
return ade_set_args(L, "b", bm_set_render_target(idx->isValid() ? idx->handle : -1, 0));
}
}
ADE_FUNC(setCamera, l_Graphics, "[camera Camera]", "Sets current camera, or resets camera if none specified", "boolean", "true if successful, false or nil otherwise")
{
camid cid;
if(!ade_get_args(L, "|o", l_Camera.Get(&cid)))
{
cam_reset_camera();
return ADE_RETURN_NIL;
}
if(!cid.isValid())
return ADE_RETURN_NIL;
cam_set_camera(cid);
return ADE_RETURN_TRUE;
}
ADE_FUNC(setColor,
l_Graphics,
"number|color /* red value or color object */, number Green, number Blue, [number Alpha]",
"Sets 2D drawing color; each color number should be from 0 (darkest) to 255 (brightest)",
nullptr,
nullptr)
{
if (!Gr_inited)
return ADE_RETURN_NIL;
int r, g, b, a = 255;
color col;
if (lua_isnumber(L, 1)) {
if (!ade_get_args(L, "iii|i", &r, &g, &b, &a))
return ADE_RETURN_NIL;
gr_init_alphacolor(&col, r, g, b, a);
} else {
gr_init_alphacolor(&col, 0, 0, 0, 255);
ade_get_args(L, "o", l_Color.Get(&col));
}
gr_set_color_fast(&col);
return ADE_RETURN_NIL;
}
ADE_FUNC(getColor,
l_Graphics,
"boolean",
"Gets the active 2D drawing color. False to return raw rgb, true to return a color object. Defaults to false.",
"number, number, number, number | color",
"rgba color which is currently in use for 2D drawing")
{
if(!Gr_inited)
return ADE_RETURN_NIL;
bool rc = false;
ade_get_args(L, "|b", &rc);
color cur = gr_screen.current_color;
if (!rc) {
return ade_set_args(L, "iiii", (int)cur.red, (int)cur.green, (int)cur.blue, (int)cur.alpha);
} else {
return ade_set_args(L, "o", l_Color.Set(cur));
}
}
ADE_FUNC(setLineWidth, l_Graphics, "[number width=1.0]", "Sets the line width for lines. This call might fail if the specified width is not supported by the graphics implementation. Then the width will be the nearest supported value.", "boolean", "true if succeeded, false otherwise")
{
if(!Gr_inited)
return ADE_RETURN_FALSE;
float width = 1.0f;
ade_get_args(L, "|f", &width);
if (width <= 0.0f)
{
return ADE_RETURN_FALSE;
}
gr_set_line_width(width);
return ADE_RETURN_TRUE;
}
ADE_FUNC(drawCircle, l_Graphics, "number Radius, number X, number Y, [boolean Filled=true]", "Draws a circle", NULL, NULL)
{
if(!Gr_inited)
return ADE_RETURN_NIL;
int x,y,ra;
bool fill = true;
if(!ade_get_args(L, "iii|b", &ra,&x,&y,&fill))
return ADE_RETURN_NIL;
if (fill) {
//WMC - Circle takes...diameter.
gr_circle(x,y, ra*2, lua_ResizeMode);
} else {
gr_unfilled_circle(x,y, ra*2, lua_ResizeMode);
}
return ADE_RETURN_NIL;
}
ADE_FUNC(drawArc, l_Graphics, "number Radius, number X, number Y, number StartAngle, number EndAngle, [boolean Filled=true]", "Draws an arc", NULL, NULL)
{
if(!Gr_inited)
return ADE_RETURN_NIL;
int x,y;
float ra,angle_start,angle_end;
bool fill = true;
if(!ade_get_args(L, "fiiff|b", &ra,&x,&y,&angle_start,&angle_end,&fill)) {
return ADE_RETURN_NIL;
}
gr_arc(x,y, ra, angle_start, angle_end, fill, lua_ResizeMode);
return ADE_RETURN_NIL;
}
ADE_FUNC(drawCurve, l_Graphics, "number X, number Y, number Radius", "Draws a curve", NULL, NULL)
{
if(!Gr_inited)
return ADE_RETURN_NIL;
int x,y,ra,dir = 0;
if(!ade_get_args(L, "iii|i", &x,&y,&ra, &dir))
return ADE_RETURN_NIL;
//WMC - direction should be settable at a certain point via enumerations.
//Not gonna deal with it now.
gr_curve(x, y, ra, dir, lua_ResizeMode);
return ADE_RETURN_NIL;
}
ADE_FUNC(drawGradientLine, l_Graphics, "number X1, number Y1, number X2, number Y2", "Draws a line from (x1,y1) to (x2,y2) with the CurrentColor that steadily fades out", NULL, NULL)
{
if(!Gr_inited)
return 0;
int x1,y1,x2,y2;
if(!ade_get_args(L, "iiii", &x1, &y1, &x2, &y2))
return ADE_RETURN_NIL;
gr_gradient(x1,y1,x2,y2,lua_ResizeMode);
return ADE_RETURN_NIL;
}
ADE_FUNC(drawLine, l_Graphics, "number X1, number Y1, number X2, number Y2", "Draws a line from (x1,y1) to (x2,y2) with CurrentColor", NULL, NULL)
{
if(!Gr_inited)
return ADE_RETURN_NIL;
int x1,y1,x2,y2;
if(!ade_get_args(L, "iiii", &x1, &y1, &x2, &y2))
return ADE_RETURN_NIL;
gr_line(x1,y1,x2,y2,lua_ResizeMode);
return ADE_RETURN_NIL;
}
ADE_FUNC(drawPixel, l_Graphics, "number X, number Y", "Sets pixel to CurrentColor", NULL, NULL)
{
if(!Gr_inited)
return ADE_RETURN_NIL;
int x,y;
if(!ade_get_args(L, "ii", &x, &y))
return ADE_RETURN_NIL;
gr_pixel(x,y,lua_ResizeMode);
return ADE_RETURN_NIL;
}
ADE_FUNC(drawPolygon,
l_Graphics,
"texture Texture, [vector Position /* Default is null vector */, orientation Orientation=nil, number Width=1.0, "
"number Height=1.0]",
"Draws a polygon. May not work properly in hooks other than On Object Render.",
nullptr,
nullptr)
{
texture_h* tdx = nullptr;
vec3d pos = vmd_zero_vector;
matrix_h *mh = NULL;
float width = 1.0f;
float height = 1.0f;
if (!ade_get_args(L, "o|ooff", l_Texture.GetPtr(&tdx), l_Vector.Get(&pos), l_Matrix.GetPtr(&mh), &width, &height))
return ADE_RETURN_NIL;
if (!tdx->isValid())
return ADE_RETURN_FALSE;
matrix *orip = &vmd_identity_matrix;
if(mh != NULL)
orip = mh->GetMatrix();
//Do 3D stuff
bool in_frame = g3_in_frame() > 0;
if(!in_frame)
g3_start_frame(0);
//gr_set_bitmap(tdx, lua_Opacity_type, GR_BITBLT_MODE_NORMAL, lua_Opacity);
//g3_draw_polygon(&pos, orip, width, height, TMAP_FLAG_TEXTURED | TMAP_HTL_3D_UNLIT);
material mat_params;
material_set_unlit(&mat_params, tdx->handle, lua_Opacity, lua_Opacity_type == GR_ALPHABLEND_FILTER ? true : false,
false);
g3_render_rect_oriented(&mat_params, &pos, orip, width, height);
if(!in_frame)
g3_end_frame();
return ADE_RETURN_TRUE;
}
void drawRectInternal(int x1, int x2, int y1, int y2, bool f = true, float a = 0.f)
{
if(f)
{
gr_set_bitmap(0); // gr_rect will use the last bitmaps info, so set to zero to flush any previous alpha state
gr_rect(x1, y1, x2-x1, y2-y1, lua_ResizeMode, a);
}
else
{
if (a != 0) {
float centerX = (x1 + x2) / 2.0f;
float centerY = (y1 + y2) / 2.0f;
//We need to calculate each point individually due to the rotation, as they won't always align horizontally and vertically.
float AX = cosf(a) * (x1 - centerX) - sinf(a) * (y1 - centerY) + centerX;
float AY = sinf(a) * (x1 - centerX) + cosf(a) * (y1 - centerY) + centerY;
float BX = cosf(a) * (x2 - centerX) - sinf(a) * (y1 - centerY) + centerX;
float BY = sinf(a) * (x2 - centerX) + cosf(a) * (y1 - centerY) + centerY;
float CX = cosf(a) * (x2 - centerX) - sinf(a) * (y2 - centerY) + centerX;
float CY = sinf(a) * (x2 - centerX) + cosf(a) * (y2 - centerY) + centerY;
float DX = cosf(a) * (x1 - centerX) - sinf(a) * (y2 - centerY) + centerX;
float DY = sinf(a) * (x1 - centerX) + cosf(a) * (y2 - centerY) + centerY;
gr_line(fl2i(AX), fl2i(AY), fl2i(BX), fl2i(BY), lua_ResizeMode);
gr_line(fl2i(BX), fl2i(BY), fl2i(CX), fl2i(CY), lua_ResizeMode);
gr_line(fl2i(CX), fl2i(CY), fl2i(DX), fl2i(DY), lua_ResizeMode);
gr_line(fl2i(DX), fl2i(DY), fl2i(AX), fl2i(AY), lua_ResizeMode);
}
else {
gr_line(x1, y1, x2, y1, lua_ResizeMode); //Top
gr_line(x1, y2, x2, y2, lua_ResizeMode); //Bottom
gr_line(x1, y1, x1, y2, lua_ResizeMode); //Left
gr_line(x2, y1, x2, y2, lua_ResizeMode); //Right
}
}
}
ADE_FUNC(drawRectangle, l_Graphics, "number X1, number Y1, number X2, number Y2, [boolean Filled=true, number angle=0.0]", "Draws a rectangle with CurrentColor. May be rotated by passing the angle parameter in radians.", nullptr, nullptr)
{
if(!Gr_inited)
return ADE_RETURN_NIL;
int x1,y1,x2,y2;
bool f=true;
float a = 0;
if(!ade_get_args(L, "iiii|bf", &x1, &y1, &x2, &y2, &f, &a))
return ADE_RETURN_NIL;
drawRectInternal(x1, x2, y1, y2, f, a);
return ADE_RETURN_NIL;
}
ADE_FUNC(drawRectangleCentered, l_Graphics,
"number X, number Y, number Width, number Height, [boolean Filled=true, number angle=0.0]",
"Draws a rectangle centered at X,Y with CurrentColor. May be rotated by passing the angle parameter in radians.", nullptr, nullptr)
{
if(!Gr_inited)
return ADE_RETURN_NIL;
int x,y,w,h;
bool f=true;
float a = 0;
if(!ade_get_args(L, "iiii|bf", &x, &y, &w, &h, &f, &a))
return ADE_RETURN_NIL;
int x1 = x - (w / 2);
int x2 = x + (w / 2);
int y1 = y - (h / 2);
int y2 = y + (h / 2);
drawRectInternal(x1, x2, y1, y2, f, a);
return ADE_RETURN_NIL;
}
ADE_FUNC(drawSphere, l_Graphics, "[number Radius = 1.0, vector Position]", "Draws a sphere with radius Radius at world vector Position. May not work properly in hooks other than On Object Render.", "boolean", "True if successful, false or nil otherwise")
{
float rad = 1.0f;
vec3d pos = vmd_zero_vector;
ade_get_args(L, "|fo", &rad, l_Vector.Get(&pos));
bool in_frame = g3_in_frame() > 0;
if(!in_frame) {
g3_start_frame(0);
vec3d cam_pos;
matrix cam_orient;
camid cid;
if (Viewer_mode & VM_FREECAMERA)
cid = cam_get_current();
else
cid = Main_camera;
camera *cam = cid.getCamera();
if (cam != NULL) {
cam->get_info(&cam_pos, &cam_orient);
g3_set_view_matrix(&cam_pos, &cam_orient, View_zoom);
} else {
g3_set_view_matrix(&Eye_position, &Eye_matrix, View_zoom);
}
gr_set_proj_matrix( Proj_fov, gr_screen.clip_aspect, Min_draw_distance, Max_draw_distance);
gr_set_view_matrix(&Eye_position, &Eye_matrix);
}
vertex vtx;
vtx.world = pos;
g3_rotate_vertex(&vtx, &pos);
g3_draw_sphere(&vtx, rad);
if(!in_frame) {
gr_end_view_matrix();
gr_end_proj_matrix();
g3_end_frame();
}
return ADE_RETURN_TRUE;
}
ADE_FUNC(draw3dLine, l_Graphics, "vector origin, vector destination, [boolean translucent=true, number thickness=1.0, number thicknessEnd=thickness]", "Draws a line from origin to destination. "
"The line may be translucent or solid. Translucent lines will NOT use the alpha value, instead being more transparent the darker the color is. "
"The thickness at the start can be different from the thickness at the end, to draw a line that tapers or expands.", nullptr, nullptr)
{
vec3d *v1 = &vmd_zero_vector;
vec3d *v2 = &vmd_zero_vector;
bool translucent = true;
float thickness = 1.0f;
float thicknessEnd = -1.0f;
if (!ade_get_args(L, "oo|bff", l_Vector.GetPtr(&v1), l_Vector.GetPtr(&v2), &translucent, &thickness, &thicknessEnd))
return ADE_RETURN_NIL;
if (thickness < 0) thickness = 0;
if (thicknessEnd < 0) thicknessEnd = thickness;
if (thickness <= 0 && thicknessEnd <= 0)
{
if (!WarnedBadThicknessLine)
{
LuaError(L, "At least one end of a 3D line must be greater than zero! The line won't be drawn!\nThis warning won't be shown again this play session.");
WarnedBadThicknessLine = true;
}
//While the batching_add_line function would also do this check, I feel that its good to return early here.
return ADE_RETURN_NIL;
}
color &clr = gr_screen.current_color;
batching_add_line(v1, v2, thickness, thicknessEnd, clr, translucent);
return ADE_RETURN_NIL;
}
// Aardwolf's test code to render a model, supposed to emulate WMC's gr.drawModel function
ADE_FUNC(drawModel, l_Graphics, "model model, vector position, orientation orientation",
"Draws the given model with the specified position and orientation. "
"Note: this method does NOT use CurrentResizeMode.",
"number", "Zero if successful, otherwise an integer error code")
{
model_h *mdl = NULL;
vec3d *v = &vmd_zero_vector;
matrix_h *mh = NULL;
if(!ade_get_args(L, "ooo", l_Model.GetPtr(&mdl), l_Vector.GetPtr(&v), l_Matrix.GetPtr(&mh)))
return ade_set_args(L, "i", 1);
if(mdl == NULL)
return ade_set_args(L, "i", 2);
int model_num = mdl->GetID();
if(model_num < 0)
return ade_set_args(L, "i", 3);
// Make sure we have a scene to use
// Note that this is only relevant, and thus only expected to be set, in OBJECTRENDER hooks
if (scripting::hooks::OnObjectRender->isActive() && !Current_scene)
return ade_set_args(L, "i", 4);
//Handle angles
matrix *orient = mh->GetMatrix();
//Clip
gr_set_clip(0, 0, gr_screen.max_w, gr_screen.max_h, GR_RESIZE_NONE); // don't use lua_ResizeMode here since this function handles its own resizing
//Handle 3D init stuff
g3_start_frame(1);
vec3d cam_pos;
matrix cam_orient;
camid cid;
if (Viewer_mode & VM_FREECAMERA)
cid = cam_get_current();
else
cid = Main_camera;
camera *cam = cid.getCamera();
if (cam != NULL) {
cam->get_info(&cam_pos, &cam_orient);
g3_set_view_matrix(&cam_pos, &cam_orient, View_zoom);
} else {
g3_set_view_matrix(&Eye_position, &Eye_matrix, View_zoom);
}
gr_set_proj_matrix( Proj_fov, gr_screen.clip_aspect, Min_draw_distance, Max_draw_distance);
gr_set_view_matrix(&Eye_position, &Eye_matrix);
//Draw the ship!!
model_clear_instance(model_num);
model_set_detail_level(0);
model_render_params render_info;
render_info.set_detail_level_lock(0);
// If this function executes in OBJECTRENDER, and if the Current_scene is set, we can take advantage
// of some optimizations by adding this model to the global render queue.
// In all other circumstances, we need to render the model in immediate mode, which may be slow but
// is guaranteed to work.
if (scripting::hooks::OnObjectRender->isActive())
model_render_queue(&render_info, Current_scene, model_num, orient, v);
else
model_render_immediate(&render_info, model_num, orient, v);
//OK we're done
gr_end_view_matrix();
gr_end_proj_matrix();
//Bye!!
g3_end_frame();
gr_reset_clip();
return ade_set_args(L, "i", 0);
}
// Wanderer
ADE_FUNC(drawModelOOR, l_Graphics, "model Model, vector Position, orientation Orientation, [number Flags]",
"Draws the given model with the specified position and orientation",
"number", "Zero if successful, otherwise an integer error code")
{
model_h *mdl = NULL;
vec3d *v = &vmd_zero_vector;
matrix_h *mh = NULL;
int flags = MR_NORMAL;
if(!ade_get_args(L, "ooo|i", l_Model.GetPtr(&mdl), l_Vector.GetPtr(&v), l_Matrix.GetPtr(&mh), &flags))
return ade_set_args(L, "i", 1);
if(mdl == NULL)
return ade_set_args(L, "i", 2);
polymodel *pm = mdl->Get();
if (pm == NULL)
return ade_set_args(L, "i", 3);
int model_num = pm->id;
if(model_num < 0)
return ade_set_args(L, "i", 3);
// Make sure we have a scene to use
// Note that this is only relevant, and thus only expected to be set, in OBJECTRENDER hooks
if (scripting::hooks::OnObjectRender->isActive() && !Current_scene)
return ade_set_args(L, "i", 4);
//Handle angles
matrix *orient = mh->GetMatrix();
//Draw the ship!!
model_clear_instance(model_num);
model_render_params render_info;
render_info.set_flags(flags);
// If this function executes in OBJECTRENDER, and if the Current_scene is set, we can take advantage
// of some optimizations by adding this model to the global render queue.
// In all other circumstances, we need to render the model in immediate mode, which may be slow but
// is guaranteed to work.
if (scripting::hooks::OnObjectRender->isActive())
model_render_queue(&render_info, Current_scene, model_num, orient, v);
else
model_render_immediate(&render_info, model_num, orient, v);
return ade_set_args(L, "i", 0);
}
// Aardwolf's targeting brackets function
ADE_FUNC(drawTargetingBrackets, l_Graphics, "object Object, [boolean draw=true, number padding=5]",
"Gets the edge positions of targeting brackets for the specified object. The brackets will only be drawn if "
"draw is true or the default value of draw is used. Brackets are drawn with the current color. The brackets "
"will have a padding (distance from the actual bounding box); the default value (used elsewhere in FS2) is 5. "
"Note: this method does NOT use CurrentResizeMode.",
"number, number, number, number",
"Left, top, right, and bottom positions of the brackets, or nil if invalid")
{
if(!Gr_inited) {
return ADE_RETURN_NIL;
}
object_h *objh = NULL;
bool draw_box = true;
int padding = 5;
if( !ade_get_args(L, "o|bi", l_Object.GetPtr(&objh), &draw_box, &padding) ) {
return ADE_RETURN_NIL;
}
// The following code is mostly copied from
// void hud_show_brackets(object *targetp, vertex *projected_v)
// in hudtarget.cpp
if( !objh->IsValid()) {
return ADE_RETURN_NIL;
}
object *targetp = objh->objp;
int x1,x2,y1,y2;
int bound_rc, pof;
int modelnum;
bool entered_frame = false;
SCP_list<CJumpNode>::iterator jnp;
if ( !(g3_in_frame( ) > 0 ) )
{
g3_start_frame( 0 );
entered_frame = true;
}
switch ( targetp->type ) {
case OBJ_SHIP:
modelnum = Ship_info[Ships[targetp->instance].ship_info_index].model_num;
bound_rc = model_find_2d_bound_min( modelnum, &targetp->orient, &targetp->pos,&x1,&y1,&x2,&y2 );
if ( bound_rc != 0 ) {
if ( entered_frame )
g3_end_frame( );
return ADE_RETURN_NIL;
}
break;
case OBJ_DEBRIS:
modelnum = Debris[targetp->instance].model_num;
bound_rc = submodel_find_2d_bound_min( modelnum, Debris[targetp->instance].submodel_num, &targetp->orient, &targetp->pos,&x1,&y1,&x2,&y2 );
if ( bound_rc != 0 ) {
if ( entered_frame )
g3_end_frame( );
return ADE_RETURN_NIL;
}
break;
case OBJ_WEAPON:
modelnum = Weapon_info[Weapons[targetp->instance].weapon_info_index].model_num;
if (modelnum != -1)
bound_rc = model_find_2d_bound_min(modelnum, &targetp->orient, &targetp->pos, &x1, &y1, &x2, &y2);
else {
vertex vtx;
g3_rotate_vertex(&vtx, &targetp->pos);
g3_project_vertex(&vtx);
x1 = x2 = (int)vtx.screen.xyw.x;
y1 = y2 = (int)vtx.screen.xyw.y;
}
break;
case OBJ_ASTEROID:
pof = Asteroids[targetp->instance].asteroid_subtype;
modelnum = Asteroid_info[Asteroids[targetp->instance].asteroid_type].model_num[pof];
bound_rc = model_find_2d_bound_min( modelnum, &targetp->orient, &targetp->pos,&x1,&y1,&x2,&y2 );
break;
case OBJ_JUMP_NODE:
for (jnp = Jump_nodes.begin(); jnp != Jump_nodes.end(); ++jnp) {
if(jnp->GetSCPObject() == targetp)
break;
}
modelnum = jnp->GetModelNumber();
bound_rc = model_find_2d_bound_min( modelnum, &targetp->orient, &targetp->pos,&x1,&y1,&x2,&y2 );
break;
default: //Someone passed an invalid pointer.
if ( entered_frame )
g3_end_frame( );
return ADE_RETURN_NIL;
}
x1 -= padding;
x2 += padding;
y1 -= padding;
y2 += padding;
if ( draw_box ) {
graphics::line_draw_list line_draw_list;
draw_brackets_square(&line_draw_list, x1, y1, x2, y2, GR_RESIZE_NONE); // don't use lua_ResizeMode here since this function handles its own resizing
line_draw_list.flush();
}
if ( entered_frame )
g3_end_frame( );
return ade_set_args(L, "iiii", x1, y1, x2, y2);
}
ADE_FUNC(
drawSubsystemTargetingBrackets, l_Graphics, "subsystem subsys, [boolean draw=true, boolean setColor=false]",
"Gets the edge position of the targeting brackets drawn for a subsystem as if they were drawn on the HUD. Only "
"actually draws the brackets if <i>draw</i> is true, optionally sets the color the as if it was drawn on the HUD",
"number, number, number, number",
"Left, top, right, and bottom positions of the brackets, or nil if invalid or off-screen")
{
if(!Gr_inited) {
return ADE_RETURN_NIL;
}
ship_subsys_h *sshp = NULL;
bool draw = true;
bool set_color = false;
if( !ade_get_args(L, "o|bb", l_Subsystem.GetPtr(&sshp), &draw, &set_color) ) {
return ADE_RETURN_NIL;
}
if (!sshp->isSubsystemValid())
{
return ADE_RETURN_NIL;
}
bool entered_frame = false;
if ( !(g3_in_frame( ) > 0 ) )
{
g3_start_frame( 0 );
entered_frame = true;
}
int coords[4];
graphics::line_draw_list line_draw_list;
int in_sight = draw_subsys_brackets(&line_draw_list, sshp->ss, 24, 24, draw, set_color, coords);
line_draw_list.flush();
if ( entered_frame )
g3_end_frame( );
if (in_sight > 0)
{
return ade_set_args(L, "iiii", coords[0], coords[1], coords[2], coords[3]);
}
else
{
return ADE_RETURN_NIL;
}
}
ADE_FUNC(drawOffscreenIndicator, l_Graphics, "object Object, [boolean draw=true, boolean setColor=false]",
"Draws an off-screen indicator for the given object. The indicator will not be drawn if draw=false, but the "
"coordinates will be returned in either case. The indicator will be drawn using the current color if "
"setColor=true and using the IFF color of the object if setColor=false.",
"number, number",
"Coordinates of the indicator (at the very edge of the screen), or nil if object is on-screen")
{
object_h *objh = NULL;
bool draw = false;
bool setcolor = false;
vec2d outpoint = { -1.0f, -1.0f };
if(!Gr_inited) {
return ADE_RETURN_NIL;
}
if( !ade_get_args(L, "o|bb", l_Object.GetPtr(&objh), &draw, &setcolor) ) {
return ADE_RETURN_NIL;
}
if( !objh->IsValid()) {
return ADE_RETURN_NIL;
}
object *targetp = objh->objp;
bool in_frame = g3_in_frame() > 0;
if (!in_frame)
g3_start_frame(0);
vertex target_point;
g3_rotate_vertex(&target_point, &targetp->pos);
g3_project_vertex(&target_point);
if (!in_frame)
g3_end_frame();
if(target_point.codes == 0)
return ADE_RETURN_NIL;
hud_target_clear_display_list();
hud_target_add_display_list(targetp, &target_point, &targetp->pos, 5, NULL, NULL, TARGET_DISPLAY_DIST);
size_t j, num_gauges;
num_gauges = default_hud_gauges.size();
for(j = 0; j < num_gauges; j++) {
if (default_hud_gauges[j]->getObjectType() == HUD_OBJECT_OFFSCREEN) {
HudGaugeOffscreen *offscreengauge = static_cast<HudGaugeOffscreen*>(default_hud_gauges[j].get());
int dir;
float tri_separation;
offscreengauge->calculatePosition(&target_point, &targetp->pos, &outpoint, &dir, &tri_separation);
if (draw) {
// needs to be turned on so it can pass the canRender() condition
// but first make sure to record the original status to use to restore later
bool original_status = offscreengauge->canRender();
offscreengauge->updateActive(true);
// only draw if it can be rendered
if ( offscreengauge->canRender() ) {
offscreengauge->preprocess();
offscreengauge->onFrame(flFrametime);
offscreengauge->resetClip();
offscreengauge->setFont();
float distance = hud_find_target_distance(targetp, Player_obj);
if (!setcolor)
hud_set_iff_color(targetp, 1);
offscreengauge->renderOffscreenIndicator(&outpoint, dir, distance, tri_separation, true);
}
// now that the gauge is rendered restore the original status
offscreengauge->updateActive(original_status);
}
offscreengauge->resize(&outpoint.x, &outpoint.y);
break;
}
}
if (outpoint.x >= 0 && outpoint.y >=0)
return ade_set_args(L, "ii", (int)outpoint.x, (int)outpoint.y);
else
return ADE_RETURN_NIL;
}
#define MAX_TEXT_LINES 256
static const char *BooleanValues[] = {"False", "True"};
static int drawString_sub(lua_State *L, bool use_resize_arg)
{
if(!Gr_inited)
return ade_set_error(L, "i", 0);
int resize_mode = lua_ResizeMode;
// if the frame has changed since the last drawString call, reset the string position
if (PreviousFrametimeOverall != game_get_overall_frametime())
{
PreviousFrametimeOverall = game_get_overall_frametime();
memcpy(NextDrawStringPos, NextDrawStringPosInitial, sizeof(NextDrawStringPos));
}
int x = NextDrawStringPos[0];
int y = NextDrawStringPos[1];
const char *s = "(null)";
int x2=-1,y2=-1;
int num_lines = 0;
if (use_resize_arg)
{
enum_h resize_arg;
if (!ade_get_args(L, "o", l_Enum.Get(&resize_arg)))
return ade_set_error(L, "i", 0);
if (!resize_arg.IsValid() || resize_arg.index < LE_GR_RESIZE_NONE || resize_arg.index > LE_GR_RESIZE_MENU_NO_OFFSET)
{
Warning(LOCATION, "Invalid resize mode index %d", resize_arg.index);
return ade_set_error(L, "i", 0);
}
resize_mode = resize_arg.index - LE_GR_RESIZE_NONE;
// so that ade_get_args below will read the correct positions
internal::Ade_get_args_skip++;
}
if (lua_isboolean(L, 1 + internal::Ade_get_args_skip))
{
bool b = false;
if(!ade_get_args(L, "b|iiii", &b, &x, &y, &x2, &y2))
return ade_set_error(L, "i", 0);
if(b)
s = BooleanValues[1];
else
s = BooleanValues[0];
}
else if (lua_isstring(L, 1 + internal::Ade_get_args_skip))
{
if(!ade_get_args(L, "s|iiii", &s, &x, &y, &x2, &y2))
return ade_set_error(L, "i", 0);
}
else
{
ade_get_args(L, "|*iiii", &x, &y, &x2, &y2);
}
NextDrawStringPos[0] = x;
if(x2 < 0)
{
num_lines = 1;
gr_string(x,y,s,resize_mode);
int height = 0;
gr_get_string_size(NULL, &height, s);
NextDrawStringPos[1] = y+height;
}
else
{
// This would pass a <=0 value to split_str
if (x2 <= x)
{
static bool Warned_drawString_x_x2 = false;
if (!Warned_drawString_x_x2)
{
Warning(LOCATION, "Illegal values passed to gr.drawString: x2 (%d) must be greater than x (%d).", x2, x);
Warned_drawString_x_x2 = true;
}
std::swap(x, x2);
}
// Invalid y2 value
if (y2 >= 0 && y2 <= y)
{
static bool Warned_drawString_y_y2 = false;
if (!Warned_drawString_y_y2)
{
Warning(LOCATION, "Illegal y2 value passed to gr.drawString: y2 (%d) must be greater than y (%d).", y2, y);
Warned_drawString_y_y2 = true;
}
std::swap(y, y2);
}
SCP_vector<SCP_string> lines = str_wrap_to_width(s,x2-x,false);
num_lines = (int) lines.size();
int line_ht = gr_get_font_height();
if (y2 < 0)
y2 = y + line_ht;
//Make sure we don't go over size
num_lines = MIN(num_lines, (y2 - y) / line_ht);
int curr_y = y;
for(int i = 0; i < num_lines; i++)
{
//Draw the string
gr_string(x,curr_y,lines[i].c_str(),resize_mode);
//Increment line height
curr_y += line_ht;
}
if (num_lines <= 0)
{
// If no line was drawn then we need to add one so the next line is
// aligned right
curr_y += line_ht;
}
NextDrawStringPos[1] = curr_y;
}
return ade_set_error(L, "i", num_lines);
}
ADE_FUNC(drawString, l_Graphics, "string|boolean Message, [number X1, number Y1, number X2, number Y2]",
"Draws a string. Use x1/y1 to control position, x2/y2 to limit textbox size."
"Text will automatically move onto new lines, if x2/y2 is specified."
"Additionally, calling drawString with only a string argument will automatically"
"draw that string below the previously drawn string (or 0,0 if no strings"
"have been drawn yet",
"number",
"Number of lines drawn, or 0 on failure")
{
return drawString_sub(L, false);
}
ADE_FUNC(drawStringResized, l_Graphics, "enumeration ResizeMode, string|boolean Message, [number X1, number Y1, number X2, number Y2]",
"Draws a string, scaled according to the GR_RESIZE_* parameter. Use x1/y1 to control position, x2/y2 to limit textbox size."
"Text will automatically move onto new lines, if x2/y2 is specified, however the line spacing will probably not be correct."
"Additionally, calling drawString with only a string argument will automatically"
"draw that string below the previously drawn string (or 0,0 if no strings"
"have been drawn yet",
"number",
"Number of lines drawn, or 0 on failure")
{
return drawString_sub(L, true);
}
ADE_FUNC(setScreenScale, l_Graphics, "number width, number height, [number zoom_x, number zoom_y, number max_x, number max_y, number center_x, number center_y, boolean force_stretch]",
"Calls gr_set_screen_scale with the specified parameters. This is useful for adjusting the drawing of graphics or text to be the same apparent size regardless of resolution.",
nullptr, nullptr)
{
if (!Gr_inited)
return ADE_RETURN_NIL;
int x, y;
int zoom_x = -1, zoom_y = -1;
int max_x = gr_screen.max_w, max_y = gr_screen.max_h;
int center_x = gr_screen.center_w, center_y = gr_screen.center_h;
bool force_stretch = false;
if (!ade_get_args(L, "ii|iiiiiib", &x, &y, &zoom_x, &zoom_y, &max_x, &max_y, ¢er_x, ¢er_y, &force_stretch))
return ADE_RETURN_NIL;
gr_set_screen_scale(x, y, zoom_x, zoom_y, max_x, max_y, center_x, center_y, force_stretch);
return ADE_RETURN_TRUE;
}
ADE_FUNC(resetScreenScale, l_Graphics, nullptr, "Rolls back the most recent call to setScreenScale.", nullptr, nullptr)
{
if (!Gr_inited)
return ADE_RETURN_NIL;
gr_reset_screen_scale();
return ADE_RETURN_TRUE;
}
ADE_FUNC(getStringWidth, l_Graphics, "string String", "Gets string width", "number", "String width, or 0 on failure")
{
if(!Gr_inited)
return ade_set_error(L, "i", 0);
const char* s;
if(!ade_get_args(L, "s", &s))
return ade_set_error(L, "i", 0);
int w;
gr_get_string_size(&w, NULL, s);
return ade_set_args(L, "i", w);
}
ADE_FUNC(loadStreamingAnim, l_Graphics, "string Filename, [boolean loop, boolean reverse, boolean pause, boolean cache, boolean grayscale]",
"Plays a streaming animation, returning its handle. The optional booleans (except cache and grayscale) can also be set via the handle's virtvars<br>"
"cache is best set to false when loading animations that are only intended to play once, e.g. headz<br>"
"Remember to call the unload() function when you're finished using the animation to free up memory.",
"streaminganim",
"Streaming animation handle, or invalid handle if animation couldn't be loaded")
{
const char* s;
int rc = -1;
bool loop = false, reverse = false, pause = false, cache = true, grayscale = false;
if(!ade_get_args(L, "s|bbbbb", &s, &loop, &reverse, &pause, &cache, &grayscale))
return ADE_RETURN_NIL;
streaminganim_h sah(s);
if (!loop) {
sah.ga.direction |= GENERIC_ANIM_DIRECTION_NOLOOP;
}
if (reverse) {
sah.ga.direction |= GENERIC_ANIM_DIRECTION_BACKWARDS;
}
if (pause) {
sah.ga.direction |= GENERIC_ANIM_DIRECTION_PAUSED;
}
if (grayscale) {
sah.ga.use_hud_color = true;
}
rc = generic_anim_stream(&sah.ga, cache);
if(rc < 0)
return ade_set_args(
L, "o",
l_streaminganim.Set(std::move(sah))); // this object should be "invalid", matches loadTexture behaviour
return ade_set_args(L, "o", l_streaminganim.Set(std::move(sah)));
}
ADE_FUNC(createTexture, l_Graphics, "[number Width=512, number Height=512, enumeration Type=TEXTURE_DYNAMIC]",
"Creates a texture for rendering to."
"Types are TEXTURE_STATIC - for infrequent rendering - and TEXTURE_DYNAMIC - for frequent rendering.",
"texture",
"New texture handle, or invalid texture handle if texture couldn't be created")
{
int w=512;
int h=512;
enum_h *e = NULL;
//GET ARGS
ade_get_args(L, "|iio", &w, &h, l_Enum.GetPtr(&e));
int t = BMP_FLAG_RENDER_TARGET_DYNAMIC;
if(e != NULL)
{
if(e->index == LE_TEXTURE_STATIC)
t = BMP_FLAG_RENDER_TARGET_STATIC;
else if(e->index == LE_TEXTURE_DYNAMIC)
t = BMP_FLAG_RENDER_TARGET_DYNAMIC;
}
int idx = bm_make_render_target(w, h, t);
if(idx < 0)
return ade_set_error(L, "o", l_Texture.Set(texture_h()));
//Since creating a render target does not increase load count, when creating the scripting texture object we need to pass true to the constructor so that the constructor will increase the load count for us.
return ade_set_args(L, "o", l_Texture.Set(texture_h(idx, true)));
}
ADE_FUNC(loadTexture, l_Graphics, "string Filename, [boolean LoadIfAnimation, boolean NoDropFrames]",
"Gets a handle to a texture. If second argument is set to true, animations will also be loaded."
"If third argument is set to true, every other animation frame will not be loaded if system has less than 48 MB memory."
"<br><strong>IMPORTANT:</strong> Textures will not be unload themselves unless you explicitly tell them to do so."
"When you are done with a texture, call the unload() function to free up memory.",
"texture",
"Texture handle, or invalid texture handle if texture couldn't be loaded")
{
const char* s;
int idx=-1;
bool b=false;
bool d=false;
if(!ade_get_args(L, "s|bb", &s, &b, &d))
return ade_set_error(L, "o", l_Texture.Set(texture_h()));
if(b) {
idx = bm_load_animation(s, nullptr, nullptr, nullptr, nullptr, d);
}
if(idx < 0) {
idx = bm_load(s);
}
if(idx < 0)
return ade_set_error(L, "o", l_Texture.Set(texture_h()));
//Loading increases load count, so we must pass false to the texture scripting object, so that the constructor doesn't increase the load count by itself again.
return ade_set_args(L, "o", l_Texture.Set(texture_h(idx, false)));
}
ADE_FUNC(drawImage,
l_Graphics,
"string|texture fileNameOrTexture, [number X1=0, number Y1=0, number X2, number Y2, number UVX1 = 0.0, number UVY1 "
"= 0.0, number UVX2=1.0, number UVY2=1.0, number alpha=1.0, boolean aaImage = false, number angle = 0.0]",
"Draws an image file or texture. Any image extension passed will be ignored."
"The UV variables specify the UV value for each corner of the image. "
"In UV coordinates, (0,0) is the top left of the image; (1,1) is the lower right. If aaImage is true, image needs "
"to be monochrome and will be drawn tinted with the currently active color."
"The angle variable can be used to rotate the image in radians.",
"boolean",
"Whether image was drawn")
{
if(!Gr_inited)
return ade_set_error(L, "b", false);
int idx;
int x1 = 0;
int y1 = 0;
int x2=INT_MAX;
int y2=INT_MAX;
float uv_x1=0.0f;
float uv_y1=0.0f;
float uv_x2=1.0f;
float uv_y2=1.0f;
float alpha=1.0f;
bool aabitmap = false;
float angle = 0.f;
if(lua_isstring(L, 1))
{
const char* s = nullptr;
if (!ade_get_args(L, "s|iiiifffffbf", &s, &x1, &y1, &x2, &y2, &uv_x1, &uv_y1, &uv_x2, &uv_y2, &alpha, &aabitmap, &angle))
return ade_set_error(L, "b", false);
idx = Script_system.LoadBm(s);
if(idx < 0)
return ADE_RETURN_FALSE;
}
else
{
texture_h* th = nullptr;
if (!ade_get_args(L,
"o|iiiifffffbf",
l_Texture.GetPtr(&th),
&x1,
&y1,
&x2,
&y2,
&uv_x1,
&uv_y1,
&uv_x2,
&uv_y2,
&alpha,
&aabitmap,
&angle))
return ade_set_error(L, "b", false);
if (!th->isValid()) {
return ade_set_error(L, "b", false);
}
idx = th->handle;
}
if(!bm_is_valid(idx))
return ade_set_error(L, "b", false);
int w, h;
if(bm_get_info(idx, &w, &h) < 0)
return ADE_RETURN_FALSE;
if(x2!=INT_MAX)
w = x2-x1;
if(y2!=INT_MAX)
h = y2-y1;
gr_set_bitmap(idx, lua_Opacity_type, GR_BITBLT_MODE_NORMAL, alpha);
bitmap_rect_list brl = bitmap_rect_list(x1, y1, w, h, uv_x1, uv_y1, uv_x2, uv_y2);
if (aabitmap) {
gr_aabitmap_list(&brl, 1, lua_ResizeMode, angle);
} else {
gr_bitmap_list(&brl, 1, lua_ResizeMode, angle);
}
return ADE_RETURN_TRUE;
}
ADE_FUNC(drawImageCentered,
l_Graphics,
"string|texture fileNameOrTexture, [number X=0, number Y=0, number W, number H, number UVX1 = 0.0, number UVY1 "
"= 0.0, number UVX2=1.0, number UVY2=1.0, number alpha=1.0, boolean aaImage = false, number angle = 0.0]",
"Draws an image file or texture centered on the X,Y position. Any image extension passed will be ignored."
"The UV variables specify the UV value for each corner of the image. "
"In UV coordinates, (0,0) is the top left of the image; (1,1) is the lower right. If aaImage is true, image needs "
"to be monochrome and will be drawn tinted with the currently active color."
"The angle variable can be used to rotate the image in radians.",
"boolean",
"Whether image was drawn") {
if(!Gr_inited)
return ade_set_error(L, "b", false);
int idx;
int x = 0;
int y = 0;
int w=INT_MAX;
int h=INT_MAX;
float uv_x1=0.0f;
float uv_y1=0.0f;
float uv_x2=1.0f;
float uv_y2=1.0f;
float alpha=1.0f;
bool aabitmap = false;
float angle = 0.f;
if(lua_isstring(L, 1))
{
const char* s = nullptr;
if (!ade_get_args(L, "s|iiiifffffbf", &s, &x, &y, &w, &h, &uv_x1, &uv_y1, &uv_x2, &uv_y2, &alpha, &aabitmap, &angle))
return ade_set_error(L, "b", false);
idx = Script_system.LoadBm(s);
if(idx < 0)
return ADE_RETURN_FALSE;
}
else
{
texture_h* th = nullptr;
if (!ade_get_args(L,
"o|iiiifffffbf",
l_Texture.GetPtr(&th),
&x,
&y,
&w,
&h,
&uv_x1,
&uv_y1,
&uv_x2,
&uv_y2,
&alpha,
&aabitmap,
&angle))
return ade_set_error(L, "b", false);
if (!th->isValid()) {
return ade_set_error(L, "b", false);
}
idx = th->handle;
}
if(!bm_is_valid(idx))
return ade_set_error(L, "b", false);
int original_w, original_h;
if(bm_get_info(idx, &original_w, &original_h) < 0)
return ADE_RETURN_FALSE;
if(w==INT_MAX)
w = original_w;
if(h==INT_MAX)
h = original_h;
gr_set_bitmap(idx, lua_Opacity_type, GR_BITBLT_MODE_NORMAL, alpha);
bitmap_rect_list brl = bitmap_rect_list(x-(w/2), y-(h/2), w, h, uv_x1, uv_y1, uv_x2, uv_y2);
if (aabitmap) {
gr_aabitmap_list(&brl, 1, lua_ResizeMode, angle);
} else {
gr_bitmap_list(&brl, 1, lua_ResizeMode, angle);
}
return ADE_RETURN_TRUE;
}
ADE_FUNC_DEPRECATED(drawMonochromeImage,
l_Graphics,
"string|texture fileNameOrTexture, number X1, number Y1, [number X2, number Y2, number alpha=1.0]",
"Draws a monochrome image from a texture or file using the current color",
"boolean",
"Whether image was drawn",
gameversion::version(21, 0),
"gr.drawImage now has support for drawing monochrome images with full UV scaling support")
{
if(!Gr_inited)
return ade_set_error(L, "b", false);
int idx;
int x,y;
int x2=INT_MAX;
int y2=INT_MAX;
int sx=0;
int sy=0;
bool m = false;
float alpha=1.0;
if(lua_isstring(L, 1))
{
const char* s = nullptr;
if(!ade_get_args(L, "sii|iif", &s,&x,&y,&x2,&y2,&alpha))
return ade_set_error(L, "b", false);
idx = Script_system.LoadBm(s);
if(idx < 0)
return ADE_RETURN_FALSE;
}
else
{
texture_h* th = nullptr;
if(!ade_get_args(L, "oii|iif", l_Texture.GetPtr(&th),&x,&y,&x2,&y2,&alpha))
return ade_set_error(L, "b", false);
if (!th->isValid()) {
return ade_set_error(L, "b", false);
}
idx = th->handle;
}
if(!bm_is_valid(idx))
return ade_set_error(L, "b", false);
int w, h;
if(bm_get_info(idx, &w, &h) < 0)
return ADE_RETURN_FALSE;
if(sx < 0)
sx = w + sx;
if(sy < 0)
sy = h + sy;
if(x2!=INT_MAX)
w = x2-x;
if(y2!=INT_MAX)
h = y2-y;
gr_set_bitmap(idx, lua_Opacity_type, GR_BITBLT_MODE_NORMAL,alpha);
gr_aabitmap_ex(x, y, w, h, sx, sy, lua_ResizeMode, m);
return ADE_RETURN_TRUE;
}
ADE_FUNC(getImageWidth, l_Graphics, "string Filename", "Gets image width", "number", "Image width, or 0 if filename is invalid")
{
const char* s;
if(!ade_get_args(L, "s", &s))
return ade_set_error(L, "i", 0);
int w;
int idx = bm_load(s);
if(idx < 0)
return ade_set_error(L, "i", 0);
bm_get_info(idx, &w);
return ade_set_args(L, "i", w);
}
ADE_FUNC(getImageHeight, l_Graphics, "string name", "Gets image height", "number", "Image height, or 0 if filename is invalid")
{
const char* s;
if(!ade_get_args(L, "s", &s))
return ade_set_error(L, "i", 0);
int h;
int idx = bm_load(s);
if(idx < 0)
return ade_set_error(L, "i", 0);
bm_get_info(idx, NULL, &h);
return ade_set_args(L, "i", h);
}
ADE_FUNC(flashScreen,
l_Graphics,
"number|color /* red value or color object */, number Green, number Blue",
"Flashes the screen",
nullptr,
nullptr)
{
if(!Gr_inited)
return ADE_RETURN_NIL;
int r, g, b;
if (lua_isnumber(L, 1)) {
if (!ade_get_args(L, "iii", &r, &g, &b))
return ADE_RETURN_NIL;
} else {
color col;
gr_init_alphacolor(&col, 0, 0, 0, 255);
if (!ade_get_args(L, "o", l_Color.Get(&col)))
return ADE_RETURN_NIL;
r = col.red;
g = col.green;
b = col.blue;
}
gr_flash(r,g,b);
return ADE_RETURN_NIL;
}
ADE_FUNC(loadModel, l_Graphics, "string Filename", "Loads the model - will not setup subsystem data, DO NOT USE FOR LOADING SHIP MODELS", "model", "Handle to a model")
{
const char* s;
int model_num = -1;
if(!ade_get_args(L, "s", &s))
return ade_set_error(L, "o", l_Model.Set(model_h(-1)));
if (s[0] == '\0')
return ade_set_error(L, "o", l_Model.Set(model_h(-1)));
model_num = model_load(s, 0, NULL);
return ade_set_args(L, "o", l_Model.Set(model_h(model_num)));
}
ADE_FUNC(hasViewmode, l_Graphics, "enumeration", "Specifies if the current viemode has the specified flag, see VM_* enumeration", "boolean", "true if flag is present, false otherwise")
{
enum_h *type = NULL;
if (!ade_get_args(L, "o", l_Enum.GetPtr(&type)))
return ade_set_error(L, "b", false);
if (type == NULL || !type->IsValid())
return ade_set_error(L, "b", false);
int bit = 0;
static bool VM_EXTERNAL_CAMERA_LOCKED_WARNED = false;
switch(type->index)
{
case LE_VM_INTERNAL:
return ade_set_args(L, "b", (Viewer_mode & ~(VM_CAMERA_LOCKED | VM_CENTERING)) == 0); // z64: Ignore camera lock state and centering state
break;
case LE_VM_EXTERNAL:
bit = VM_EXTERNAL;
break;
case LE_VM_OTHER_SHIP:
bit = VM_OTHER_SHIP;
break;
case LE_VM_CHASE:
bit = VM_CHASE;
break;
case LE_VM_DEAD_VIEW:
bit = VM_DEAD_VIEW;
break;
case LE_VM_EXTERNAL_CAMERA_LOCKED:
if (!VM_EXTERNAL_CAMERA_LOCKED_WARNED) {
Warning(LOCATION, "The enumeration VM_EXTERNAL_CAMERA_LOCKED has been deprecated for lua function "
"hasViewmode()! To ensure future compatibility, please check for either "
"VM_CAMERA_LOCKED, VM_EXTERNAL, or both, instead.");
VM_EXTERNAL_CAMERA_LOCKED_WARNED = true;
}
return ade_set_args(L, "b", ((Viewer_mode & VM_CAMERA_LOCKED) && (Viewer_mode & VM_EXTERNAL)) != 0);
break;
case LE_VM_CAMERA_LOCKED:
bit = VM_CAMERA_LOCKED;
break;
case LE_VM_FREECAMERA:
bit = VM_FREECAMERA;
break;
case LE_VM_PADLOCK_LEFT:
bit = VM_PADLOCK_LEFT;
break;
case LE_VM_PADLOCK_REAR:
bit = VM_PADLOCK_REAR;
break;
case LE_VM_PADLOCK_RIGHT:
bit = VM_PADLOCK_RIGHT;
break;
case LE_VM_PADLOCK_UP:
bit = VM_PADLOCK_UP;
break;
case LE_VM_TOPDOWN:
bit = VM_TOPDOWN;
break;
case LE_VM_TRACK:
bit = VM_TRACK;
break;
case LE_VM_WARP_CHASE:
bit = VM_WARP_CHASE;
break;
case LE_VM_WARPIN_ANCHOR:
bit = VM_WARPIN_ANCHOR;
break;
case LE_VM_CENTERING:
bit = VM_CENTERING;
break;
default:
LuaError(L, "Attempted to use hasViewmode with an invalid enumeration! Only VM_* enumerations are allowed!");
return ade_set_error(L, "b", false);
break;
}
return ade_set_args(L, "b", (Viewer_mode & bit) != 0);
}
ADE_FUNC(setClip, l_Graphics, "number x, number y, number width, number height", "Sets the clipping region to the specified rectangle. Most drawing functions are able to handle the offset.", "boolean", "true if successful, false otherwise")
{
int x, y, width, height;
if (!ade_get_args(L, "iiii", &x, &y, &width, &height))
return ADE_RETURN_FALSE;
gr_set_clip(x, y, width, height, lua_ResizeMode);
return ADE_RETURN_TRUE;
}
ADE_FUNC(resetClip, l_Graphics, NULL, "Resets the clipping region that might have been set", "boolean", "true if successful, false otherwise")
{
gr_reset_clip();
return ADE_RETURN_TRUE;
}
ADE_FUNC(openMovie, l_Graphics, "string name, boolean looping = false",
"Opens the movie with the specified name. If the name has an extension it will be removed. This function will "
"try all movie formats supported by the engine and use the first that is found.",
"movie_player", "The cutscene player handle or invalid handle if cutscene could not be opened.")
{
const char* name = nullptr;
bool looping = false;
if (!ade_get_args(L, "s|b", &name, &looping)) {
return ade_set_error(L, "o", l_MoviePlayer.Set(movie_player_h()));
}
// Audio is disabled for scripted movies at the moment
cutscene::PlaybackProperties props;
props.with_audio = false;
props.looping = looping;
auto player = cutscene::Player::newPlayer(name, props);
if (!player) {
return ade_set_args(L, "o", l_MoviePlayer.Set(movie_player_h()));
}
return ade_set_args(L, "o", l_MoviePlayer.Set(movie_player_h(std::move(player))));
}
ADE_FUNC(createPersistentParticle,
l_Graphics,
"vector Position, vector Velocity, number Lifetime, number Radius, [enumeration Type=PARTICLE_DEBUG, number TracerLength=-1, "
"boolean Reverse=false, texture Texture=Nil, object AttachedObject=Nil]",
"Creates a persistent particle. Persistent variables are handled specially by the engine so that this "
"function can return a handle to the caller. Only use this if you absolutely need it. Use createParticle if "
"the returned handle is not required. Use PARTICLE_* enumerations for type."
"Reverse reverse animation, if one is specified"
"Attached object specifies object that Position will be (and always be) relative to.",
"particle",
"Handle to the created particle")
{
particle::particle_info pi;
pi.type = particle::PARTICLE_DEBUG;
pi.optional_data = -1;
pi.attached_objnum = -1;
pi.attached_sig = -1;
pi.reverse = false;
// Need to consume tracer_length parameter but it isn't used anymore
float temp;
enum_h* type = nullptr;
bool rev = false;
object_h* objh = nullptr;
texture_h* texture = nullptr;
if (!ade_get_args(L, "ooff|ofboo", l_Vector.Get(&pi.pos), l_Vector.Get(&pi.vel), &pi.lifetime, &pi.rad,
l_Enum.GetPtr(&type), &temp, &rev, l_Texture.GetPtr(&texture), l_Object.GetPtr(&objh)))
return ADE_RETURN_NIL;
if (type != nullptr) {
switch (type->index) {
case LE_PARTICLE_DEBUG:
pi.type = particle::PARTICLE_DEBUG;
break;
case LE_PARTICLE_FIRE:
pi.type = particle::PARTICLE_FIRE;
break;
case LE_PARTICLE_SMOKE:
pi.type = particle::PARTICLE_SMOKE;
break;
case LE_PARTICLE_SMOKE2:
pi.type = particle::PARTICLE_SMOKE2;
break;
case LE_PARTICLE_BITMAP:
if (texture == nullptr || !texture->isValid()) {
LuaError(L, "Invalid texture specified for createParticle()!");
return ADE_RETURN_NIL;
} else {
pi.optional_data = texture->handle;
pi.type = particle::PARTICLE_BITMAP;
}
break;
default:
LuaError(L, "Invalid particle enum for createParticle(). Can only support PARTICLE_* enums!");
return ADE_RETURN_NIL;
}
}
if (rev)
pi.reverse = false;
if (objh != nullptr && objh->IsValid()) {
pi.attached_objnum = OBJ_INDEX(objh->objp);
pi.attached_sig = objh->objp->signature;
}
particle::WeakParticlePtr p = particle::createPersistent(&pi);
if (!p.expired())
return ade_set_args(L, "o", l_Particle.Set(particle_h(p)));
else
return ADE_RETURN_NIL;
}
ADE_FUNC(createParticle,
l_Graphics,
"vector Position, vector Velocity, number Lifetime, number Radius, [enumeration Type=PARTICLE_DEBUG, number TracerLength=-1, "
"boolean Reverse=false, texture Texture=Nil, object AttachedObject=Nil]",
"Creates a non-persistent particle. Use PARTICLE_* enumerations for type."
"Reverse reverse animation, if one is specified"
"Attached object specifies object that Position will be (and always be) relative to.",
"boolean",
"true if particle was created, false otherwise")
{
particle::particle_info pi;
pi.type = particle::PARTICLE_DEBUG;
pi.optional_data = -1;
pi.attached_objnum = -1;
pi.attached_sig = -1;
pi.reverse = false;
// Need to consume tracer_length parameter but it isn't used anymore
float temp;
enum_h* type = nullptr;
bool rev = false;
object_h* objh = nullptr;
texture_h* texture = nullptr;
if (!ade_get_args(L, "ooff|ofboo", l_Vector.Get(&pi.pos), l_Vector.Get(&pi.vel), &pi.lifetime, &pi.rad,
l_Enum.GetPtr(&type), &temp, &rev, l_Texture.GetPtr(&texture), l_Object.GetPtr(&objh)))
return ADE_RETURN_FALSE;
if (type != nullptr) {
switch (type->index) {
case LE_PARTICLE_DEBUG:
pi.type = particle::PARTICLE_DEBUG;
break;
case LE_PARTICLE_FIRE:
pi.type = particle::PARTICLE_FIRE;
break;
case LE_PARTICLE_SMOKE:
pi.type = particle::PARTICLE_SMOKE;
break;
case LE_PARTICLE_SMOKE2:
pi.type = particle::PARTICLE_SMOKE2;
break;
case LE_PARTICLE_BITMAP:
if (texture == nullptr || !texture->isValid()) {
LuaError(L, "Invalid texture specified for createParticle()!");
return ADE_RETURN_NIL;
} else {
pi.optional_data = texture->handle;
pi.type = particle::PARTICLE_BITMAP;
}
break;
default:
LuaError(L, "Invalid particle enum for createParticle(). Can only support PARTICLE_* enums!");
return ADE_RETURN_NIL;
}
}
if (rev)
pi.reverse = false;
if (objh != nullptr && objh->IsValid()) {
pi.attached_objnum = OBJ_INDEX(objh->objp);
pi.attached_sig = objh->objp->signature;
}
particle::create(&pi);
return ADE_RETURN_TRUE;
}
ADE_FUNC(killAllParticles, l_Graphics, nullptr, "Clears all particles from a mission", nullptr, nullptr)
{
SCP_UNUSED(L);
particle::kill_all();
return ADE_RETURN_NIL;
}
ADE_FUNC(screenToBlob, l_Graphics, nullptr, "Captures the current render target and encodes it into a blob-PNG", "string", "The png blob string")
{
if (!Gr_inited)
return ade_set_error(L, "s", "");
return ade_set_args(L, "s", gr_blob_screen().c_str());
}
ADE_FUNC(freeAllModels, l_Graphics, nullptr, "Releases all loaded models and frees the memory. Intended for use in UI situations "
"and not within missions. Do not use after mission parse. Use at your own risk!", nullptr, nullptr)
{
SCP_UNUSED(L);
model_free_all();
return ADE_RETURN_NIL;
}
ADE_FUNC(createColor,
l_Graphics,
"number Red, number Green, number Blue, [number Alpha]",
"Creates a color object. Values are capped 0-255. Alpha defaults to 255.",
"color",
"The color")
{
int r;
int g;
int b;
int a = 255;
if (!ade_get_args(L, "iii|i", &r, &g, &b, &a)) {
return ADE_RETURN_NIL;
}
CLAMP(r, 0, 255);
CLAMP(g, 0, 255);
CLAMP(b, 0, 255);
CLAMP(a, 0, 255);
color thisColor;
gr_init_alphacolor(&thisColor, r, g, b, a);
return ade_set_args(L, "o", l_Color.Set(thisColor));
}
ADE_FUNC(isVR, l_Graphics, nullptr, "Queries whether or not FSO is currently trying to render to a head-mounted VR display.", "boolean", "true if FSO is currently outputting frames to a VR headset.")
{
return ade_set_args(L, "b", openxr_enabled());
}
} // namespace api
} // namespace scripting
|