wrap_Graphics.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328
  1. /**
  2. * Copyright (c) 2006-2017 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "wrap_Graphics.h"
  21. #include "graphics/Texture.h"
  22. #include "image/ImageData.h"
  23. #include "image/Image.h"
  24. #include "font/Rasterizer.h"
  25. #include "filesystem/wrap_Filesystem.h"
  26. #include "video/VideoStream.h"
  27. #include "image/wrap_Image.h"
  28. #include "common/Reference.h"
  29. #include "math/wrap_Transform.h"
  30. #include <cassert>
  31. #include <cstring>
  32. #include <cstdlib>
  33. #include <algorithm>
  34. // Shove the wrap_Graphics.lua code directly into a raw string literal.
  35. static const char graphics_lua[] =
  36. #include "wrap_Graphics.lua"
  37. ;
  38. namespace love
  39. {
  40. namespace graphics
  41. {
  42. namespace opengl
  43. {
  44. #define instance() (Module::getInstance<Graphics>(Module::M_GRAPHICS))
  45. static int luax_checkgraphicscreated(lua_State *L)
  46. {
  47. if (!instance()->isCreated())
  48. return luaL_error(L, "love.graphics cannot function without a window!");
  49. return 0;
  50. }
  51. int w_reset(lua_State *)
  52. {
  53. instance()->reset();
  54. return 0;
  55. }
  56. int w_clear(lua_State *L)
  57. {
  58. Colorf color;
  59. if (lua_isnoneornil(L, 1))
  60. color.set(0.0, 0.0, 0.0, 0.0);
  61. else if (lua_istable(L, 1))
  62. {
  63. std::vector<Graphics::OptionalColorf> colors((size_t) lua_gettop(L));
  64. for (int i = 0; i < lua_gettop(L); i++)
  65. {
  66. if (lua_isnoneornil(L, i + 1) || luax_objlen(L, i + 1) == 0)
  67. {
  68. colors[i].enabled = false;
  69. continue;
  70. }
  71. for (int j = 1; j <= 4; j++)
  72. lua_rawgeti(L, i + 1, j);
  73. colors[i].enabled = true;
  74. colors[i].c.r = (float) luaL_checknumber(L, -4);
  75. colors[i].c.g = (float) luaL_checknumber(L, -3);
  76. colors[i].c.b = (float) luaL_checknumber(L, -2);
  77. colors[i].c.a = (float) luaL_optnumber(L, -1, 1.0);
  78. lua_pop(L, 4);
  79. }
  80. luax_catchexcept(L, [&]() { instance()->clear(colors); });
  81. return 0;
  82. }
  83. else
  84. {
  85. color.r = (float) luaL_checknumber(L, 1);
  86. color.g = (float) luaL_checknumber(L, 2);
  87. color.b = (float) luaL_checknumber(L, 3);
  88. color.a = (float) luaL_optnumber(L, 4, 1.0);
  89. }
  90. luax_catchexcept(L, [&]() { instance()->clear(color); });
  91. return 0;
  92. }
  93. int w_discard(lua_State *L)
  94. {
  95. std::vector<bool> colorbuffers;
  96. if (lua_istable(L, 1))
  97. {
  98. for (size_t i = 1; i <= luax_objlen(L, 1); i++)
  99. {
  100. lua_rawgeti(L, 1, i);
  101. colorbuffers.push_back(luax_optboolean(L, -1, true));
  102. lua_pop(L, 1);
  103. }
  104. }
  105. else
  106. {
  107. bool discardcolor = luax_optboolean(L, 1, true);
  108. size_t numbuffers = std::max((size_t) 1, instance()->getCanvas().size());
  109. colorbuffers = std::vector<bool>(numbuffers, discardcolor);
  110. }
  111. bool stencil = luax_optboolean(L, 2, true);
  112. instance()->discard(colorbuffers, stencil);
  113. return 0;
  114. }
  115. int w_present(lua_State *L)
  116. {
  117. luax_catchexcept(L, [&]() { instance()->present(L); });
  118. return 0;
  119. }
  120. int w_isCreated(lua_State *L)
  121. {
  122. luax_pushboolean(L, instance()->isCreated());
  123. return 1;
  124. }
  125. int w_isActive(lua_State *L)
  126. {
  127. luax_pushboolean(L, instance()->isActive());
  128. return 1;
  129. }
  130. int w_isGammaCorrect(lua_State *L)
  131. {
  132. luax_pushboolean(L, graphics::isGammaCorrect());
  133. return 1;
  134. }
  135. int w_getWidth(lua_State *L)
  136. {
  137. lua_pushinteger(L, instance()->getWidth());
  138. return 1;
  139. }
  140. int w_getHeight(lua_State *L)
  141. {
  142. lua_pushinteger(L, instance()->getHeight());
  143. return 1;
  144. }
  145. int w_getDimensions(lua_State *L)
  146. {
  147. lua_pushinteger(L, instance()->getWidth());
  148. lua_pushinteger(L, instance()->getHeight());
  149. return 2;
  150. }
  151. int w_getPixelWidth(lua_State *L)
  152. {
  153. lua_pushinteger(L, instance()->getPixelWidth());
  154. return 1;
  155. }
  156. int w_getPixelHeight(lua_State *L)
  157. {
  158. lua_pushinteger(L, instance()->getPixelHeight());
  159. return 1;
  160. }
  161. int w_getPixelDimensions(lua_State *L)
  162. {
  163. lua_pushinteger(L, instance()->getPixelWidth());
  164. lua_pushinteger(L, instance()->getPixelHeight());
  165. return 2;
  166. }
  167. int w_getPixelDensity(lua_State *L)
  168. {
  169. lua_pushnumber(L, instance()->getScreenPixelDensity());
  170. return 1;
  171. }
  172. int w_setCanvas(lua_State *L)
  173. {
  174. // Disable stencil writes.
  175. luax_catchexcept(L, [](){ instance()->stopDrawToStencilBuffer(); });
  176. // called with none -> reset to default buffer
  177. if (lua_isnoneornil(L, 1))
  178. {
  179. instance()->setCanvas();
  180. return 0;
  181. }
  182. bool is_table = lua_istable(L, 1);
  183. std::vector<love::graphics::Canvas *> canvases;
  184. if (is_table)
  185. {
  186. for (int i = 1; i <= (int) luax_objlen(L, 1); i++)
  187. {
  188. lua_rawgeti(L, 1, i);
  189. canvases.push_back(luax_checkcanvas(L, -1));
  190. lua_pop(L, 1);
  191. }
  192. }
  193. else
  194. {
  195. for (int i = 1; i <= lua_gettop(L); i++)
  196. canvases.push_back(luax_checkcanvas(L, i));
  197. }
  198. luax_catchexcept(L, [&]() {
  199. if (canvases.size() > 0)
  200. instance()->setCanvas(canvases);
  201. else
  202. instance()->setCanvas();
  203. });
  204. return 0;
  205. }
  206. int w_getCanvas(lua_State *L)
  207. {
  208. const std::vector<love::graphics::Canvas *> canvases = instance()->getCanvas();
  209. int n = 0;
  210. for (love::graphics::Canvas *c : canvases)
  211. {
  212. luax_pushtype(L, c);
  213. n++;
  214. }
  215. if (n == 0)
  216. {
  217. lua_pushnil(L);
  218. n = 1;
  219. }
  220. return n;
  221. }
  222. static void screenshotCallback(love::image::ImageData *i, Reference *ref, void *gd)
  223. {
  224. if (i != nullptr)
  225. {
  226. lua_State *L = (lua_State *) gd;
  227. ref->push(L);
  228. delete ref;
  229. luax_pushtype(L, i);
  230. lua_call(L, 1, 0);
  231. }
  232. else
  233. delete ref;
  234. }
  235. int w_captureScreenshot(lua_State *L)
  236. {
  237. luaL_checktype(L, 1, LUA_TFUNCTION);
  238. Graphics::ScreenshotInfo info;
  239. info.callback = screenshotCallback;
  240. lua_pushvalue(L, 1);
  241. info.ref = luax_refif(L, LUA_TFUNCTION);
  242. lua_pop(L, 1);
  243. luax_catchexcept(L,
  244. [&]() { instance()->captureScreenshot(info); },
  245. [&](bool except) { if (except) delete info.ref; }
  246. );
  247. return 0;
  248. }
  249. int w_setScissor(lua_State *L)
  250. {
  251. int nargs = lua_gettop(L);
  252. if (nargs == 0 || (nargs == 4 && lua_isnil(L, 1) && lua_isnil(L, 2)
  253. && lua_isnil(L, 3) && lua_isnil(L, 4)))
  254. {
  255. instance()->setScissor();
  256. return 0;
  257. }
  258. Rect rect;
  259. rect.x = (int) luaL_checknumber(L, 1);
  260. rect.y = (int) luaL_checknumber(L, 2);
  261. rect.w = (int) luaL_checknumber(L, 3);
  262. rect.h = (int) luaL_checknumber(L, 4);
  263. if (rect.w < 0 || rect.h < 0)
  264. return luaL_error(L, "Can't set scissor with negative width and/or height.");
  265. instance()->setScissor(rect);
  266. return 0;
  267. }
  268. int w_intersectScissor(lua_State *L)
  269. {
  270. Rect rect;
  271. rect.x = (int) luaL_checknumber(L, 1);
  272. rect.y = (int) luaL_checknumber(L, 2);
  273. rect.w = (int) luaL_checknumber(L, 3);
  274. rect.h = (int) luaL_checknumber(L, 4);
  275. if (rect.w < 0 || rect.h < 0)
  276. return luaL_error(L, "Can't set scissor with negative width and/or height.");
  277. instance()->intersectScissor(rect);
  278. return 0;
  279. }
  280. int w_getScissor(lua_State *L)
  281. {
  282. Rect rect;
  283. if (!instance()->getScissor(rect))
  284. return 0;
  285. lua_pushinteger(L, rect.x);
  286. lua_pushinteger(L, rect.y);
  287. lua_pushinteger(L, rect.w);
  288. lua_pushinteger(L, rect.h);
  289. return 4;
  290. }
  291. int w_stencil(lua_State *L)
  292. {
  293. luaL_checktype(L, 1, LUA_TFUNCTION);
  294. Graphics::StencilAction action = Graphics::STENCIL_REPLACE;
  295. if (!lua_isnoneornil(L, 2))
  296. {
  297. const char *actionstr = luaL_checkstring(L, 2);
  298. if (!Graphics::getConstant(actionstr, action))
  299. return luaL_error(L, "Invalid stencil draw action: %s", actionstr);
  300. }
  301. int stencilvalue = (int) luaL_optnumber(L, 3, 1);
  302. // Fourth argument: whether to keep the contents of the stencil buffer.
  303. if (lua_toboolean(L, 4) == 0)
  304. instance()->clearStencil();
  305. luax_catchexcept(L, [&](){ instance()->drawToStencilBuffer(action, stencilvalue); });
  306. // Call stencilfunc()
  307. lua_pushvalue(L, 1);
  308. lua_call(L, 0, 0);
  309. luax_catchexcept(L, [&](){ instance()->stopDrawToStencilBuffer(); });
  310. return 0;
  311. }
  312. int w_setStencilTest(lua_State *L)
  313. {
  314. // COMPARE_ALWAYS effectively disables stencil testing.
  315. Graphics::CompareMode compare = Graphics::COMPARE_ALWAYS;
  316. int comparevalue = 0;
  317. if (!lua_isnoneornil(L, 1))
  318. {
  319. const char *comparestr = luaL_checkstring(L, 1);
  320. if (!Graphics::getConstant(comparestr, compare))
  321. return luaL_error(L, "Invalid compare mode: %s", comparestr);
  322. comparevalue = (int) luaL_checknumber(L, 2);
  323. }
  324. luax_catchexcept(L, [&](){ instance()->setStencilTest(compare, comparevalue); });
  325. return 0;
  326. }
  327. int w_getStencilTest(lua_State *L)
  328. {
  329. Graphics::CompareMode compare = Graphics::COMPARE_ALWAYS;
  330. int comparevalue = 1;
  331. instance()->getStencilTest(compare, comparevalue);
  332. const char *comparestr;
  333. if (!Graphics::getConstant(compare, comparestr))
  334. return luaL_error(L, "Unknown compare mode.");
  335. lua_pushstring(L, comparestr);
  336. lua_pushnumber(L, comparevalue);
  337. return 2;
  338. }
  339. int w_newImage(lua_State *L)
  340. {
  341. luax_checkgraphicscreated(L);
  342. std::vector<love::image::ImageData *> data;
  343. std::vector<love::image::CompressedImageData *> cdata;
  344. Image::Settings settings;
  345. bool releasedata = false;
  346. // Convert to ImageData / CompressedImageData, if necessary.
  347. if (lua_isstring(L, 1) || luax_istype(L, 1, love::filesystem::File::type) || luax_istype(L, 1, love::filesystem::FileData::type))
  348. {
  349. auto imagemodule = Module::getInstance<love::image::Image>(Module::M_IMAGE);
  350. if (imagemodule == nullptr)
  351. return luaL_error(L, "Cannot load images without the love.image module.");
  352. love::filesystem::FileData *fdata = love::filesystem::luax_getfiledata(L, 1);
  353. // Parse a density scale of 2.0 from "[email protected]".
  354. const std::string &fname = fdata->getName();
  355. size_t namelen = fname.length();
  356. size_t atpos = fname.rfind('@');
  357. if (atpos != std::string::npos && atpos + 2 < namelen
  358. && (fname[namelen - 1] == 'x' || fname[namelen - 1] == 'X'))
  359. {
  360. char *end = nullptr;
  361. long density = strtol(fname.c_str() + atpos + 1, &end, 10);
  362. if (end != nullptr && density > 0)
  363. settings.pixeldensity = (float) density;
  364. }
  365. if (imagemodule->isCompressed(fdata))
  366. {
  367. luax_catchexcept(L,
  368. [&]() { cdata.push_back(imagemodule->newCompressedData(fdata)); },
  369. [&](bool) { fdata->release(); }
  370. );
  371. }
  372. else
  373. {
  374. luax_catchexcept(L,
  375. [&]() { data.push_back(imagemodule->newImageData(fdata)); },
  376. [&](bool) { fdata->release(); }
  377. );
  378. }
  379. // Lua's GC won't release the image data, so we should do it ourselves.
  380. releasedata = true;
  381. }
  382. else if (luax_istype(L, 1, love::image::CompressedImageData::type))
  383. cdata.push_back(love::image::luax_checkcompressedimagedata(L, 1));
  384. else
  385. data.push_back(love::image::luax_checkimagedata(L, 1));
  386. if (!lua_isnoneornil(L, 2))
  387. {
  388. luaL_checktype(L, 2, LUA_TTABLE);
  389. settings.mipmaps = luax_boolflag(L, 2, luax_imageSettingName(Image::SETTING_MIPMAPS), settings.mipmaps);
  390. settings.linear = luax_boolflag(L, 2, luax_imageSettingName(Image::SETTING_LINEAR), settings.linear);
  391. settings.pixeldensity = (float) luax_numberflag(L, 2, luax_imageSettingName(Image::SETTING_PIXELDENSITY), settings.pixeldensity);
  392. lua_getfield(L, 2, luax_imageSettingName(Image::SETTING_MIPMAPS));
  393. // Add all manually specified mipmap images to the array of imagedata.
  394. // i.e. settings = {mipmaps = {mip1, mip2, ...}}.
  395. if (lua_istable(L, -1))
  396. {
  397. for (size_t i = 1; i <= luax_objlen(L, -1); i++)
  398. {
  399. lua_rawgeti(L, -1, i);
  400. if (!data.empty())
  401. {
  402. if (!luax_istype(L, -1, love::image::ImageData::type))
  403. luax_convobj(L, -1, "image", "newImageData");
  404. data.push_back(love::image::luax_checkimagedata(L, -1));
  405. }
  406. else if (!cdata.empty())
  407. {
  408. if (!luax_istype(L, -1, love::image::CompressedImageData::type))
  409. luax_convobj(L, -1, "image", "newCompressedData");
  410. cdata.push_back(love::image::luax_checkcompressedimagedata(L, -1));
  411. }
  412. lua_pop(L, 1);
  413. }
  414. }
  415. lua_pop(L, 1);
  416. }
  417. // Create the image.
  418. Image *image = nullptr;
  419. luax_catchexcept(L,
  420. [&]() {
  421. if (!cdata.empty())
  422. image = instance()->newImage(cdata, settings);
  423. else if (!data.empty())
  424. image = instance()->newImage(data, settings);
  425. },
  426. [&](bool) {
  427. if (releasedata)
  428. {
  429. for (auto d : data)
  430. d->release();
  431. for (auto d : cdata)
  432. d->release();
  433. }
  434. }
  435. );
  436. if (image == nullptr)
  437. return luaL_error(L, "Could not load image.");
  438. // Push the type.
  439. luax_pushtype(L, image);
  440. image->release();
  441. return 1;
  442. }
  443. int w_newQuad(lua_State *L)
  444. {
  445. luax_checkgraphicscreated(L);
  446. Quad::Viewport v;
  447. v.x = luaL_checknumber(L, 1);
  448. v.y = luaL_checknumber(L, 2);
  449. v.w = luaL_checknumber(L, 3);
  450. v.h = luaL_checknumber(L, 4);
  451. double sw = 0.0f;
  452. double sh = 0.0f;
  453. if (luax_istype(L, 5, Texture::type))
  454. {
  455. Texture *texture = luax_checktexture(L, 5);
  456. sw = texture->getWidth();
  457. sh = texture->getHeight();
  458. }
  459. else
  460. {
  461. sw = luaL_checknumber(L, 5);
  462. sh = luaL_checknumber(L, 6);
  463. }
  464. Quad *quad = instance()->newQuad(v, sw, sh);
  465. luax_pushtype(L, quad);
  466. quad->release();
  467. return 1;
  468. }
  469. int w_newFont(lua_State *L)
  470. {
  471. luax_checkgraphicscreated(L);
  472. graphics::Font *font = nullptr;
  473. // Convert to Rasterizer, if necessary.
  474. if (!luax_istype(L, 1, love::font::Rasterizer::type))
  475. {
  476. std::vector<int> idxs;
  477. for (int i = 0; i < lua_gettop(L); i++)
  478. idxs.push_back(i + 1);
  479. luax_convobj(L, &idxs[0], (int) idxs.size(), "font", "newRasterizer");
  480. }
  481. love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1);
  482. luax_catchexcept(L, [&]() {
  483. font = instance()->newFont(rasterizer, instance()->getDefaultFilter()); }
  484. );
  485. // Push the type.
  486. luax_pushtype(L, font);
  487. font->release();
  488. return 1;
  489. }
  490. int w_newImageFont(lua_State *L)
  491. {
  492. luax_checkgraphicscreated(L);
  493. // filter for glyphs
  494. Texture::Filter filter = instance()->getDefaultFilter();
  495. // Convert to ImageData if necessary.
  496. if (luax_istype(L, 1, Image::type))
  497. {
  498. Image *i = luax_checktype<Image>(L, 1);
  499. filter = i->getFilter();
  500. const auto &idlevels = i->getImageData();
  501. if (idlevels.empty())
  502. return luaL_argerror(L, 1, "Image must not be compressed.");
  503. luax_pushtype(L, idlevels[0].get());
  504. lua_replace(L, 1);
  505. }
  506. // Convert to Rasterizer if necessary.
  507. if (!luax_istype(L, 1, love::font::Rasterizer::type))
  508. {
  509. luaL_checktype(L, 2, LUA_TSTRING);
  510. std::vector<int> idxs;
  511. for (int i = 0; i < lua_gettop(L); i++)
  512. idxs.push_back(i + 1);
  513. luax_convobj(L, &idxs[0], (int) idxs.size(), "font", "newImageRasterizer");
  514. }
  515. love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1);
  516. // Create the font.
  517. love::graphics::Font *font = instance()->newFont(rasterizer, filter);
  518. // Push the type.
  519. luax_pushtype(L, font);
  520. font->release();
  521. return 1;
  522. }
  523. int w_newSpriteBatch(lua_State *L)
  524. {
  525. luax_checkgraphicscreated(L);
  526. Texture *texture = luax_checktexture(L, 1);
  527. int size = (int) luaL_optnumber(L, 2, 1000);
  528. vertex::Usage usage = vertex::USAGE_DYNAMIC;
  529. if (lua_gettop(L) > 2)
  530. {
  531. const char *usagestr = luaL_checkstring(L, 3);
  532. if (!vertex::getConstant(usagestr, usage))
  533. return luaL_error(L, "Invalid usage hint: %s", usagestr);
  534. }
  535. love::graphics::SpriteBatch *t = nullptr;
  536. luax_catchexcept(L,
  537. [&](){ t = instance()->newSpriteBatch(texture, size, usage); }
  538. );
  539. luax_pushtype(L, t);
  540. t->release();
  541. return 1;
  542. }
  543. int w_newParticleSystem(lua_State *L)
  544. {
  545. luax_checkgraphicscreated(L);
  546. Texture *texture = luax_checktexture(L, 1);
  547. lua_Number size = luaL_optnumber(L, 2, 1000);
  548. love::graphics::ParticleSystem *t = nullptr;
  549. if (size < 1.0 || size > ParticleSystem::MAX_PARTICLES)
  550. return luaL_error(L, "Invalid ParticleSystem size");
  551. luax_catchexcept(L,
  552. [&](){ t = instance()->newParticleSystem(texture, int(size)); }
  553. );
  554. luax_pushtype(L, t);
  555. t->release();
  556. return 1;
  557. }
  558. int w_newCanvas(lua_State *L)
  559. {
  560. luax_checkgraphicscreated(L);
  561. // check if width and height are given. else default to screen dimensions.
  562. int width = (int) luaL_optnumber(L, 1, instance()->getWidth());
  563. int height = (int) luaL_optnumber(L, 2, instance()->getHeight());
  564. Canvas::Settings settings;
  565. // Default to the screen's current pixel density scale.
  566. settings.pixeldensity = instance()->getScreenPixelDensity();
  567. if (!lua_isnoneornil(L, 3))
  568. {
  569. lua_getfield(L, 3, "format");
  570. if (!lua_isnoneornil(L, -1))
  571. {
  572. const char *str = luaL_checkstring(L, -1);
  573. if (!getConstant(str, settings.format))
  574. return luaL_error(L, "Invalid Canvas format: %s", str);
  575. }
  576. lua_pop(L, 1);
  577. settings.pixeldensity = (float) luax_numberflag(L, 3, "pixeldensity", settings.pixeldensity);
  578. settings.msaa = luax_intflag(L, 3, "msaa", settings.msaa);
  579. }
  580. love::graphics::Canvas *canvas = nullptr;
  581. luax_catchexcept(L, [&](){ canvas = instance()->newCanvas(width, height, settings); });
  582. if (canvas == nullptr)
  583. return luaL_error(L, "Canvas not created, but no error thrown. I don't even...");
  584. luax_pushtype(L, canvas);
  585. canvas->release();
  586. return 1;
  587. }
  588. static int w_getShaderSource(lua_State *L, int startidx, bool gles, Shader::ShaderSource &source)
  589. {
  590. luax_checkgraphicscreated(L);
  591. // read any filepath arguments
  592. for (int i = startidx; i < startidx + 2; i++)
  593. {
  594. if (!lua_isstring(L, i))
  595. continue;
  596. // call love.filesystem.isFile(arg_i)
  597. luax_getfunction(L, "filesystem", "isFile");
  598. lua_pushvalue(L, i);
  599. lua_call(L, 1, 1);
  600. bool isFile = luax_toboolean(L, -1);
  601. lua_pop(L, 1);
  602. if (isFile)
  603. {
  604. luax_getfunction(L, "filesystem", "read");
  605. lua_pushvalue(L, i);
  606. lua_call(L, 1, 1);
  607. lua_replace(L, i);
  608. }
  609. else
  610. {
  611. // Check if the argument looks like a filepath - we want a nicer
  612. // error for misspelled filepath arguments.
  613. size_t slen = 0;
  614. const char *str = lua_tolstring(L, i, &slen);
  615. if (slen > 0 && slen < 256 && !strchr(str, '\n'))
  616. {
  617. const char *ext = strchr(str, '.');
  618. if (ext != nullptr && !strchr(ext, ';') && !strchr(ext, ' '))
  619. return luaL_error(L, "Could not open file %s. Does not exist.", str);
  620. }
  621. }
  622. }
  623. bool has_arg1 = lua_isstring(L, startidx + 0) != 0;
  624. bool has_arg2 = lua_isstring(L, startidx + 1) != 0;
  625. // require at least one string argument
  626. if (!(has_arg1 || has_arg2))
  627. luaL_checkstring(L, startidx);
  628. luax_getfunction(L, "graphics", "_shaderCodeToGLSL");
  629. // push vertexcode and pixelcode strings to the top of the stack
  630. lua_pushboolean(L, gles);
  631. if (has_arg1)
  632. lua_pushvalue(L, startidx + 0);
  633. else
  634. lua_pushnil(L);
  635. if (has_arg2)
  636. lua_pushvalue(L, startidx + 1);
  637. else
  638. lua_pushnil(L);
  639. // call effectCodeToGLSL, returned values will be at the top of the stack
  640. if (lua_pcall(L, 3, 2, 0) != 0)
  641. return luaL_error(L, "%s", lua_tostring(L, -1));
  642. // vertex shader code
  643. if (lua_isstring(L, -2))
  644. source.vertex = luax_checkstring(L, -2);
  645. else if (has_arg1 && has_arg2)
  646. return luaL_error(L, "Could not parse vertex shader code (missing 'position' function?)");
  647. // pixel shader code
  648. if (lua_isstring(L, -1))
  649. source.pixel = luax_checkstring(L, -1);
  650. else if (has_arg1 && has_arg2)
  651. return luaL_error(L, "Could not parse pixel shader code (missing 'effect' function?)");
  652. if (source.vertex.empty() && source.pixel.empty())
  653. {
  654. // Original args had source code, but effectCodeToGLSL couldn't translate it
  655. for (int i = startidx; i < startidx + 2; i++)
  656. {
  657. if (lua_isstring(L, i))
  658. return luaL_argerror(L, i, "missing 'position' or 'effect' function?");
  659. }
  660. }
  661. return 0;
  662. }
  663. int w_newShader(lua_State *L)
  664. {
  665. bool gles = instance()->getRenderer() == Graphics::RENDERER_OPENGLES;
  666. Shader::ShaderSource source;
  667. w_getShaderSource(L, 1, gles, source);
  668. bool should_error = false;
  669. try
  670. {
  671. love::graphics::Shader *shader = instance()->newShader(source);
  672. luax_pushtype(L, shader);
  673. shader->release();
  674. }
  675. catch (love::Exception &e)
  676. {
  677. luax_getfunction(L, "graphics", "_transformGLSLErrorMessages");
  678. lua_pushstring(L, e.what());
  679. // Function pushes the new error string onto the stack.
  680. lua_pcall(L, 1, 1, 0);
  681. should_error = true;
  682. }
  683. if (should_error)
  684. return lua_error(L);
  685. return 1;
  686. }
  687. int w_validateShader(lua_State *L)
  688. {
  689. luaL_checktype(L, 1, LUA_TBOOLEAN);
  690. bool gles = luax_toboolean(L, 1);
  691. Shader::ShaderSource source;
  692. w_getShaderSource(L, 2, gles, source);
  693. std::string err;
  694. bool success = instance()->validateShader(gles, source, err);
  695. luax_pushboolean(L, success);
  696. if (!success)
  697. {
  698. luax_pushstring(L, err);
  699. return 2;
  700. }
  701. return 1;
  702. }
  703. static vertex::Usage luax_optmeshusage(lua_State *L, int idx, vertex::Usage def)
  704. {
  705. const char *usagestr = lua_isnoneornil(L, idx) ? nullptr : luaL_checkstring(L, idx);
  706. if (usagestr && !vertex::getConstant(usagestr, def))
  707. luaL_error(L, "Invalid usage hint: %s", usagestr);
  708. return def;
  709. }
  710. static Mesh::DrawMode luax_optmeshdrawmode(lua_State *L, int idx, Mesh::DrawMode def)
  711. {
  712. const char *modestr = lua_isnoneornil(L, idx) ? nullptr : luaL_checkstring(L, idx);
  713. if (modestr && !Mesh::getConstant(modestr, def))
  714. luaL_error(L, "Invalid mesh draw mode: %s", modestr);
  715. return def;
  716. }
  717. static love::graphics::Mesh *newStandardMesh(lua_State *L)
  718. {
  719. love::graphics::Mesh *t = nullptr;
  720. Mesh::DrawMode drawmode = luax_optmeshdrawmode(L, 2, Mesh::DRAWMODE_FAN);
  721. vertex::Usage usage = luax_optmeshusage(L, 3, vertex::USAGE_DYNAMIC);
  722. // First argument is a table of standard vertices, or the number of
  723. // standard vertices.
  724. if (lua_istable(L, 1))
  725. {
  726. size_t vertexcount = luax_objlen(L, 1);
  727. std::vector<Vertex> vertices;
  728. vertices.reserve(vertexcount);
  729. // Get the vertices from the table.
  730. for (size_t i = 1; i <= vertexcount; i++)
  731. {
  732. lua_rawgeti(L, 1, (int) i);
  733. if (lua_type(L, -1) != LUA_TTABLE)
  734. {
  735. luax_typerror(L, 1, "table of tables");
  736. return nullptr;
  737. }
  738. for (int j = 1; j <= 8; j++)
  739. lua_rawgeti(L, -j, j);
  740. Vertex v;
  741. v.x = (float) luaL_checknumber(L, -8);
  742. v.y = (float) luaL_checknumber(L, -7);
  743. v.s = (float) luaL_optnumber(L, -6, 0.0);
  744. v.t = (float) luaL_optnumber(L, -5, 0.0);
  745. v.color.r = (unsigned char) (luaL_optnumber(L, -4, 1.0) * 255.0);
  746. v.color.g = (unsigned char) (luaL_optnumber(L, -3, 1.0) * 255.0);
  747. v.color.b = (unsigned char) (luaL_optnumber(L, -2, 1.0) * 255.0);
  748. v.color.a = (unsigned char) (luaL_optnumber(L, -1, 1.0) * 255.0);
  749. lua_pop(L, 9);
  750. vertices.push_back(v);
  751. }
  752. luax_catchexcept(L, [&](){ t = instance()->newMesh(vertices, drawmode, usage); });
  753. }
  754. else
  755. {
  756. int count = (int) luaL_checknumber(L, 1);
  757. luax_catchexcept(L, [&](){ t = instance()->newMesh(count, drawmode, usage); });
  758. }
  759. return t;
  760. }
  761. static love::graphics::Mesh *newCustomMesh(lua_State *L)
  762. {
  763. love::graphics::Mesh *t = nullptr;
  764. // First argument is the vertex format, second is a table of vertices or
  765. // the number of vertices.
  766. std::vector<Mesh::AttribFormat> vertexformat;
  767. Mesh::DrawMode drawmode = luax_optmeshdrawmode(L, 3, Mesh::DRAWMODE_FAN);
  768. vertex::Usage usage = luax_optmeshusage(L, 4, vertex::USAGE_DYNAMIC);
  769. lua_rawgeti(L, 1, 1);
  770. if (!lua_istable(L, -1))
  771. {
  772. luaL_argerror(L, 1, "table of tables expected");
  773. return nullptr;
  774. }
  775. lua_pop(L, 1);
  776. // Per-vertex attribute formats.
  777. for (int i = 1; i <= (int) luax_objlen(L, 1); i++)
  778. {
  779. lua_rawgeti(L, 1, i);
  780. // {name, datatype, components}
  781. for (int j = 1; j <= 3; j++)
  782. lua_rawgeti(L, -j, j);
  783. Mesh::AttribFormat format;
  784. format.name = luaL_checkstring(L, -3);
  785. const char *tname = luaL_checkstring(L, -2);
  786. if (!Mesh::getConstant(tname, format.type))
  787. {
  788. luaL_error(L, "Invalid Mesh vertex data type name: %s", tname);
  789. return nullptr;
  790. }
  791. format.components = (int) luaL_checknumber(L, -1);
  792. if (format.components <= 0 || format.components > 4)
  793. {
  794. luaL_error(L, "Number of vertex attribute components must be between 1 and 4 (got %d)", format.components);
  795. return nullptr;
  796. }
  797. lua_pop(L, 4);
  798. vertexformat.push_back(format);
  799. }
  800. if (lua_isnumber(L, 2))
  801. {
  802. int vertexcount = (int) luaL_checknumber(L, 2);
  803. luax_catchexcept(L, [&](){ t = instance()->newMesh(vertexformat, vertexcount, drawmode, usage); });
  804. }
  805. else if (luax_istype(L, 2, Data::type))
  806. {
  807. // Vertex data comes directly from a Data object.
  808. Data *data = luax_checktype<Data>(L, 2);
  809. luax_catchexcept(L, [&](){ t = instance()->newMesh(vertexformat, data->getData(), data->getSize(), drawmode, usage); });
  810. }
  811. else
  812. {
  813. // Table of vertices.
  814. lua_rawgeti(L, 2, 1);
  815. if (!lua_istable(L, -1))
  816. {
  817. luaL_argerror(L, 2, "expected table of tables");
  818. return nullptr;
  819. }
  820. lua_pop(L, 1);
  821. int vertexcomponents = 0;
  822. for (const Mesh::AttribFormat &format : vertexformat)
  823. vertexcomponents += format.components;
  824. size_t numvertices = luax_objlen(L, 2);
  825. luax_catchexcept(L, [&](){ t = instance()->newMesh(vertexformat, numvertices, drawmode, usage); });
  826. // Maximum possible data size for a single vertex attribute.
  827. char data[sizeof(float) * 4];
  828. for (size_t vertindex = 0; vertindex < numvertices; vertindex++)
  829. {
  830. // get vertices[vertindex]
  831. lua_rawgeti(L, 2, vertindex + 1);
  832. luaL_checktype(L, -1, LUA_TTABLE);
  833. int n = 0;
  834. for (size_t i = 0; i < vertexformat.size(); i++)
  835. {
  836. int components = vertexformat[i].components;
  837. // get vertices[vertindex][n]
  838. for (int c = 0; c < components; c++)
  839. {
  840. n++;
  841. lua_rawgeti(L, -(c + 1), n);
  842. }
  843. // Fetch the values from Lua and store them in data buffer.
  844. luax_writeAttributeData(L, -components, vertexformat[i].type, components, data);
  845. lua_pop(L, components);
  846. luax_catchexcept(L,
  847. [&](){ t->setVertexAttribute(vertindex, i, data, sizeof(float) * 4); },
  848. [&](bool diderror){ if (diderror) t->release(); }
  849. );
  850. }
  851. lua_pop(L, 1); // pop vertices[vertindex]
  852. }
  853. t->flush();
  854. }
  855. return t;
  856. }
  857. int w_newMesh(lua_State *L)
  858. {
  859. luax_checkgraphicscreated(L);
  860. // Check first argument: table or number of vertices.
  861. int arg1type = lua_type(L, 1);
  862. if (arg1type != LUA_TTABLE && arg1type != LUA_TNUMBER)
  863. luaL_argerror(L, 1, "table or number expected");
  864. love::graphics::Mesh *t = nullptr;
  865. int arg2type = lua_type(L, 2);
  866. if (arg1type == LUA_TTABLE && (arg2type == LUA_TTABLE || arg2type == LUA_TNUMBER || arg2type == LUA_TUSERDATA))
  867. t = newCustomMesh(L);
  868. else
  869. t = newStandardMesh(L);
  870. luax_pushtype(L, t);
  871. t->release();
  872. return 1;
  873. }
  874. int w_newText(lua_State *L)
  875. {
  876. luax_checkgraphicscreated(L);
  877. graphics::Font *font = luax_checkfont(L, 1);
  878. Text *t = nullptr;
  879. if (lua_isnoneornil(L, 2))
  880. luax_catchexcept(L, [&](){ t = instance()->newText(font); });
  881. else
  882. {
  883. std::vector<Font::ColoredString> text;
  884. luax_checkcoloredstring(L, 2, text);
  885. luax_catchexcept(L, [&](){ t = instance()->newText(font, text); });
  886. }
  887. luax_pushtype(L, t);
  888. t->release();
  889. return 1;
  890. }
  891. int w_newVideo(lua_State *L)
  892. {
  893. luax_checkgraphicscreated(L);
  894. if (!luax_istype(L, 1, love::video::VideoStream::type))
  895. luax_convobj(L, 1, "video", "newVideoStream");
  896. auto stream = luax_checktype<love::video::VideoStream>(L, 1);
  897. float pixeldensity = (float) luaL_optnumber(L, 2, 1.0);
  898. Video *video = nullptr;
  899. luax_catchexcept(L, [&]() { video = instance()->newVideo(stream, pixeldensity); });
  900. luax_pushtype(L, video);
  901. video->release();
  902. return 1;
  903. }
  904. int w_setColor(lua_State *L)
  905. {
  906. Colorf c;
  907. if (lua_istable(L, 1))
  908. {
  909. for (int i = 1; i <= 4; i++)
  910. lua_rawgeti(L, 1, i);
  911. c.r = (float) luaL_checknumber(L, -4);
  912. c.g = (float) luaL_checknumber(L, -3);
  913. c.b = (float) luaL_checknumber(L, -2);
  914. c.a = (float) luaL_optnumber(L, -1, 1.0);
  915. lua_pop(L, 4);
  916. }
  917. else
  918. {
  919. c.r = (float) luaL_checknumber(L, 1);
  920. c.g = (float) luaL_checknumber(L, 2);
  921. c.b = (float) luaL_checknumber(L, 3);
  922. c.a = (float) luaL_optnumber(L, 4, 1.0);
  923. }
  924. instance()->setColor(c);
  925. return 0;
  926. }
  927. int w_getColor(lua_State *L)
  928. {
  929. Colorf c = instance()->getColor();
  930. lua_pushnumber(L, c.r);
  931. lua_pushnumber(L, c.g);
  932. lua_pushnumber(L, c.b);
  933. lua_pushnumber(L, c.a);
  934. return 4;
  935. }
  936. int w_setBackgroundColor(lua_State *L)
  937. {
  938. Colorf c;
  939. if (lua_istable(L, 1))
  940. {
  941. for (int i = 1; i <= 4; i++)
  942. lua_rawgeti(L, 1, i);
  943. c.r = (float) luaL_checknumber(L, -4);
  944. c.g = (float) luaL_checknumber(L, -3);
  945. c.b = (float) luaL_checknumber(L, -2);
  946. c.a = (float) luaL_optnumber(L, -1, 1.0);
  947. lua_pop(L, 4);
  948. }
  949. else
  950. {
  951. c.r = (float) luaL_checknumber(L, 1);
  952. c.g = (float) luaL_checknumber(L, 2);
  953. c.b = (float) luaL_checknumber(L, 3);
  954. c.a = (float) luaL_optnumber(L, 4, 1.0);
  955. }
  956. instance()->setBackgroundColor(c);
  957. return 0;
  958. }
  959. int w_getBackgroundColor(lua_State *L)
  960. {
  961. Colorf c = instance()->getBackgroundColor();
  962. lua_pushnumber(L, c.r);
  963. lua_pushnumber(L, c.g);
  964. lua_pushnumber(L, c.b);
  965. lua_pushnumber(L, c.a);
  966. return 4;
  967. }
  968. int w_setNewFont(lua_State *L)
  969. {
  970. int ret = w_newFont(L);
  971. love::graphics::Font *font = luax_checktype<love::graphics::Font>(L, -1);
  972. instance()->setFont(font);
  973. return ret;
  974. }
  975. int w_setFont(lua_State *L)
  976. {
  977. love::graphics::Font *font = luax_checktype<love::graphics::Font>(L, 1);
  978. instance()->setFont(font);
  979. return 0;
  980. }
  981. int w_getFont(lua_State *L)
  982. {
  983. love::graphics::Font *f = nullptr;
  984. luax_catchexcept(L, [&](){ f = instance()->getFont(); });
  985. luax_pushtype(L, f);
  986. return 1;
  987. }
  988. int w_setColorMask(lua_State *L)
  989. {
  990. Graphics::ColorMask mask;
  991. if (lua_gettop(L) <= 1 && lua_isnoneornil(L, 1))
  992. {
  993. // Enable all color components if no argument is given.
  994. mask.r = mask.g = mask.b = mask.a = true;
  995. }
  996. else
  997. {
  998. mask.r = luax_toboolean(L, 1);
  999. mask.g = luax_toboolean(L, 2);
  1000. mask.b = luax_toboolean(L, 3);
  1001. mask.a = luax_toboolean(L, 4);
  1002. }
  1003. instance()->setColorMask(mask);
  1004. return 0;
  1005. }
  1006. int w_getColorMask(lua_State *L)
  1007. {
  1008. Graphics::ColorMask mask = instance()->getColorMask();
  1009. luax_pushboolean(L, mask.r);
  1010. luax_pushboolean(L, mask.g);
  1011. luax_pushboolean(L, mask.b);
  1012. luax_pushboolean(L, mask.a);
  1013. return 4;
  1014. }
  1015. int w_setBlendMode(lua_State *L)
  1016. {
  1017. Graphics::BlendMode mode;
  1018. const char *str = luaL_checkstring(L, 1);
  1019. if (!Graphics::getConstant(str, mode))
  1020. return luaL_error(L, "Invalid blend mode: %s", str);
  1021. Graphics::BlendAlpha alphamode = Graphics::BLENDALPHA_MULTIPLY;
  1022. if (!lua_isnoneornil(L, 2))
  1023. {
  1024. const char *alphastr = luaL_checkstring(L, 2);
  1025. if (!Graphics::getConstant(alphastr, alphamode))
  1026. return luaL_error(L, "Invalid blend alpha mode: %s", alphastr);
  1027. }
  1028. luax_catchexcept(L, [&](){ instance()->setBlendMode(mode, alphamode); });
  1029. return 0;
  1030. }
  1031. int w_getBlendMode(lua_State *L)
  1032. {
  1033. const char *str;
  1034. const char *alphastr;
  1035. Graphics::BlendAlpha alphamode;
  1036. Graphics::BlendMode mode = instance()->getBlendMode(alphamode);
  1037. if (!Graphics::getConstant(mode, str))
  1038. return luaL_error(L, "Unknown blend mode");
  1039. if (!Graphics::getConstant(alphamode, alphastr))
  1040. return luaL_error(L, "Unknown blend alpha mode");
  1041. lua_pushstring(L, str);
  1042. lua_pushstring(L, alphastr);
  1043. return 2;
  1044. }
  1045. int w_setDefaultFilter(lua_State *L)
  1046. {
  1047. Texture::Filter f;
  1048. const char *minstr = luaL_checkstring(L, 1);
  1049. const char *magstr = luaL_optstring(L, 2, minstr);
  1050. if (!Texture::getConstant(minstr, f.min))
  1051. return luaL_error(L, "Invalid filter mode: %s", minstr);
  1052. if (!Texture::getConstant(magstr, f.mag))
  1053. return luaL_error(L, "Invalid filter mode: %s", magstr);
  1054. f.anisotropy = (float) luaL_optnumber(L, 3, 1.0);
  1055. instance()->setDefaultFilter(f);
  1056. return 0;
  1057. }
  1058. int w_getDefaultFilter(lua_State *L)
  1059. {
  1060. const Texture::Filter &f = instance()->getDefaultFilter();
  1061. const char *minstr;
  1062. const char *magstr;
  1063. if (!Texture::getConstant(f.min, minstr))
  1064. return luaL_error(L, "Unknown minification filter mode");
  1065. if (!Texture::getConstant(f.mag, magstr))
  1066. return luaL_error(L, "Unknown magnification filter mode");
  1067. lua_pushstring(L, minstr);
  1068. lua_pushstring(L, magstr);
  1069. lua_pushnumber(L, f.anisotropy);
  1070. return 3;
  1071. }
  1072. int w_setDefaultMipmapFilter(lua_State *L)
  1073. {
  1074. Texture::FilterMode filter = Texture::FILTER_NONE;
  1075. if (!lua_isnoneornil(L, 1))
  1076. {
  1077. const char *str = luaL_checkstring(L, 1);
  1078. if (!Texture::getConstant(str, filter))
  1079. return luaL_error(L, "Invalid filter mode: %s", str);
  1080. }
  1081. float sharpness = (float) luaL_optnumber(L, 2, 0);
  1082. instance()->setDefaultMipmapFilter(filter, sharpness);
  1083. return 0;
  1084. }
  1085. int w_getDefaultMipmapFilter(lua_State *L)
  1086. {
  1087. Texture::FilterMode filter;
  1088. float sharpness;
  1089. instance()->getDefaultMipmapFilter(&filter, &sharpness);
  1090. const char *str;
  1091. if (Texture::getConstant(filter, str))
  1092. lua_pushstring(L, str);
  1093. else
  1094. lua_pushnil(L);
  1095. lua_pushnumber(L, sharpness);
  1096. return 2;
  1097. }
  1098. int w_setLineWidth(lua_State *L)
  1099. {
  1100. float width = (float)luaL_checknumber(L, 1);
  1101. instance()->setLineWidth(width);
  1102. return 0;
  1103. }
  1104. int w_setLineStyle(lua_State *L)
  1105. {
  1106. Graphics::LineStyle style;
  1107. const char *str = luaL_checkstring(L, 1);
  1108. if (!Graphics::getConstant(str, style))
  1109. return luaL_error(L, "Invalid line style: %s", str);
  1110. instance()->setLineStyle(style);
  1111. return 0;
  1112. }
  1113. int w_setLineJoin(lua_State *L)
  1114. {
  1115. Graphics::LineJoin join;
  1116. const char *str = luaL_checkstring(L, 1);
  1117. if (!Graphics::getConstant(str, join))
  1118. return luaL_error(L, "Invalid line join mode: %s", str);
  1119. instance()->setLineJoin(join);
  1120. return 0;
  1121. }
  1122. int w_getLineWidth(lua_State *L)
  1123. {
  1124. lua_pushnumber(L, instance()->getLineWidth());
  1125. return 1;
  1126. }
  1127. int w_getLineStyle(lua_State *L)
  1128. {
  1129. Graphics::LineStyle style = instance()->getLineStyle();
  1130. const char *str;
  1131. if (!Graphics::getConstant(style, str))
  1132. return luaL_error(L, "Unknown line style");
  1133. lua_pushstring(L, str);
  1134. return 1;
  1135. }
  1136. int w_getLineJoin(lua_State *L)
  1137. {
  1138. Graphics::LineJoin join = instance()->getLineJoin();
  1139. const char *str;
  1140. if (!Graphics::getConstant(join, str))
  1141. return luaL_error(L, "Unknown line join");
  1142. lua_pushstring(L, str);
  1143. return 1;
  1144. }
  1145. int w_setPointSize(lua_State *L)
  1146. {
  1147. float size = (float)luaL_checknumber(L, 1);
  1148. instance()->setPointSize(size);
  1149. return 0;
  1150. }
  1151. int w_getPointSize(lua_State *L)
  1152. {
  1153. lua_pushnumber(L, instance()->getPointSize());
  1154. return 1;
  1155. }
  1156. int w_setWireframe(lua_State *L)
  1157. {
  1158. instance()->setWireframe(luax_toboolean(L, 1));
  1159. return 0;
  1160. }
  1161. int w_isWireframe(lua_State *L)
  1162. {
  1163. luax_pushboolean(L, instance()->isWireframe());
  1164. return 1;
  1165. }
  1166. int w_setShader(lua_State *L)
  1167. {
  1168. if (lua_isnoneornil(L,1))
  1169. {
  1170. instance()->setShader();
  1171. return 0;
  1172. }
  1173. love::graphics::Shader *shader = luax_checkshader(L, 1);
  1174. instance()->setShader(shader);
  1175. return 0;
  1176. }
  1177. int w_getShader(lua_State *L)
  1178. {
  1179. love::graphics::Shader *shader = instance()->getShader();
  1180. if (shader)
  1181. luax_pushtype(L, shader);
  1182. else
  1183. lua_pushnil(L);
  1184. return 1;
  1185. }
  1186. int w_setDefaultShaderCode(lua_State *L)
  1187. {
  1188. for (int i = 0; i < 2; i++)
  1189. {
  1190. luaL_checktype(L, i + 1, LUA_TTABLE);
  1191. for (int lang = 0; lang < Shader::LANGUAGE_MAX_ENUM; lang++)
  1192. {
  1193. const char *langname;
  1194. if (!Shader::getConstant((Shader::Language) lang, langname))
  1195. continue;
  1196. lua_getfield(L, i + 1, langname);
  1197. lua_getfield(L, -1, "vertex");
  1198. lua_getfield(L, -2, "pixel");
  1199. lua_getfield(L, -3, "videopixel");
  1200. Shader::ShaderSource code;
  1201. code.vertex = luax_checkstring(L, -3);
  1202. code.pixel = luax_checkstring(L, -2);
  1203. Shader::ShaderSource videocode;
  1204. videocode.vertex = luax_checkstring(L, -3);
  1205. videocode.pixel = luax_checkstring(L, -1);
  1206. lua_pop(L, 4);
  1207. Graphics::defaultShaderCode[lang][i] = code;
  1208. Graphics::defaultVideoShaderCode[lang][i] = videocode;
  1209. }
  1210. }
  1211. return 0;
  1212. }
  1213. int w_getSupported(lua_State *L)
  1214. {
  1215. lua_createtable(L, 0, (int) Graphics::FEATURE_MAX_ENUM);
  1216. for (int i = 0; i < (int) Graphics::FEATURE_MAX_ENUM; i++)
  1217. {
  1218. auto feature = (Graphics::Feature) i;
  1219. const char *name = nullptr;
  1220. if (!Graphics::getConstant(feature, name))
  1221. continue;
  1222. luax_pushboolean(L, instance()->isSupported(feature));
  1223. lua_setfield(L, -2, name);
  1224. }
  1225. return 1;
  1226. }
  1227. static int w__getFormats(lua_State *L, bool (*isFormatSupported)(PixelFormat), bool (*ignore)(PixelFormat))
  1228. {
  1229. lua_createtable(L, 0, (int) PIXELFORMAT_MAX_ENUM);
  1230. for (int i = 0; i < (int) PIXELFORMAT_MAX_ENUM; i++)
  1231. {
  1232. PixelFormat format = (PixelFormat) i;
  1233. const char *name = nullptr;
  1234. if (format == PIXELFORMAT_UNKNOWN || !love::getConstant(format, name) || ignore(format))
  1235. continue;
  1236. luax_pushboolean(L, isFormatSupported(format));
  1237. lua_setfield(L, -2, name);
  1238. }
  1239. return 1;
  1240. }
  1241. int w_getCanvasFormats(lua_State *L)
  1242. {
  1243. return w__getFormats(L, Canvas::isFormatSupported, isPixelFormatCompressed);
  1244. }
  1245. int w_getImageFormats(lua_State *L)
  1246. {
  1247. const auto ignore = [](PixelFormat format)
  1248. {
  1249. return !(image::ImageData::validPixelFormat(format) || isPixelFormatCompressed(format));
  1250. };
  1251. return w__getFormats(L, Image::isFormatSupported, ignore);
  1252. }
  1253. int w_getRendererInfo(lua_State *L)
  1254. {
  1255. Graphics::RendererInfo info;
  1256. luax_catchexcept(L, [&](){ info = instance()->getRendererInfo(); });
  1257. luax_pushstring(L, info.name);
  1258. luax_pushstring(L, info.version);
  1259. luax_pushstring(L, info.vendor);
  1260. luax_pushstring(L, info.device);
  1261. return 4;
  1262. }
  1263. int w_getSystemLimits(lua_State *L)
  1264. {
  1265. lua_createtable(L, 0, (int) Graphics::LIMIT_MAX_ENUM);
  1266. for (int i = 0; i < (int) Graphics::LIMIT_MAX_ENUM; i++)
  1267. {
  1268. Graphics::SystemLimit limittype = (Graphics::SystemLimit) i;
  1269. const char *name = nullptr;
  1270. if (!Graphics::getConstant(limittype, name))
  1271. continue;
  1272. lua_pushnumber(L, instance()->getSystemLimit(limittype));
  1273. lua_setfield(L, -2, name);
  1274. }
  1275. return 1;
  1276. }
  1277. int w_getStats(lua_State *L)
  1278. {
  1279. Graphics::Stats stats = instance()->getStats();
  1280. lua_createtable(L, 0, 7);
  1281. lua_pushinteger(L, stats.drawCalls);
  1282. lua_setfield(L, -2, "drawcalls");
  1283. lua_pushinteger(L, stats.canvasSwitches);
  1284. lua_setfield(L, -2, "canvasswitches");
  1285. lua_pushinteger(L, stats.shaderSwitches);
  1286. lua_setfield(L, -2, "shaderswitches");
  1287. lua_pushinteger(L, stats.canvases);
  1288. lua_setfield(L, -2, "canvases");
  1289. lua_pushinteger(L, stats.images);
  1290. lua_setfield(L, -2, "images");
  1291. lua_pushinteger(L, stats.fonts);
  1292. lua_setfield(L, -2, "fonts");
  1293. lua_pushinteger(L, stats.textureMemory);
  1294. lua_setfield(L, -2, "texturememory");
  1295. return 1;
  1296. }
  1297. int w_draw(lua_State *L)
  1298. {
  1299. Drawable *drawable = nullptr;
  1300. Texture *texture = nullptr;
  1301. Quad *quad = nullptr;
  1302. int startidx = 2;
  1303. if (luax_istype(L, 2, Quad::type))
  1304. {
  1305. texture = luax_checktexture(L, 1);
  1306. quad = luax_totype<Quad>(L, 2);
  1307. startidx = 3;
  1308. }
  1309. else if (lua_isnil(L, 2) && !lua_isnoneornil(L, 3))
  1310. {
  1311. return luax_typerror(L, 2, "Quad");
  1312. }
  1313. else
  1314. {
  1315. drawable = luax_checktype<Drawable>(L, 1);
  1316. startidx = 2;
  1317. }
  1318. if (luax_istype(L, startidx, math::Transform::type))
  1319. {
  1320. math::Transform *tf = luax_totype<math::Transform>(L, startidx);
  1321. luax_catchexcept(L, [&]() {
  1322. if (texture && quad)
  1323. instance()->draw(texture, quad, tf->getMatrix());
  1324. else
  1325. instance()->draw(drawable, tf->getMatrix());
  1326. });
  1327. }
  1328. else
  1329. {
  1330. float x = (float) luaL_optnumber(L, startidx + 0, 0.0);
  1331. float y = (float) luaL_optnumber(L, startidx + 1, 0.0);
  1332. float a = (float) luaL_optnumber(L, startidx + 2, 0.0);
  1333. float sx = (float) luaL_optnumber(L, startidx + 3, 1.0);
  1334. float sy = (float) luaL_optnumber(L, startidx + 4, sx);
  1335. float ox = (float) luaL_optnumber(L, startidx + 5, 0.0);
  1336. float oy = (float) luaL_optnumber(L, startidx + 6, 0.0);
  1337. float kx = (float) luaL_optnumber(L, startidx + 7, 0.0);
  1338. float ky = (float) luaL_optnumber(L, startidx + 8, 0.0);
  1339. Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
  1340. luax_catchexcept(L, [&]() {
  1341. if (texture && quad)
  1342. instance()->draw(texture, quad, m);
  1343. else if (drawable)
  1344. instance()->draw(drawable, m);
  1345. });
  1346. }
  1347. return 0;
  1348. }
  1349. int w_drawInstanced(lua_State *L)
  1350. {
  1351. love::graphics::Mesh *t = luax_checkmesh(L, 1);
  1352. int instancecount = (int) luaL_checkinteger(L, 2);
  1353. if (luax_istype(L, 3, math::Transform::type))
  1354. {
  1355. math::Transform *tf = luax_totype<math::Transform>(L, 3);
  1356. luax_catchexcept(L, [&]() { instance()->drawInstanced(t, tf->getMatrix(), instancecount); });
  1357. }
  1358. else
  1359. {
  1360. float x = (float) luaL_optnumber(L, 3, 0.0);
  1361. float y = (float) luaL_optnumber(L, 4, 0.0);
  1362. float a = (float) luaL_optnumber(L, 5, 0.0);
  1363. float sx = (float) luaL_optnumber(L, 6, 1.0);
  1364. float sy = (float) luaL_optnumber(L, 7, sx);
  1365. float ox = (float) luaL_optnumber(L, 8, 0.0);
  1366. float oy = (float) luaL_optnumber(L, 9, 0.0);
  1367. float kx = (float) luaL_optnumber(L, 10, 0.0);
  1368. float ky = (float) luaL_optnumber(L, 11, 0.0);
  1369. Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
  1370. luax_catchexcept(L, [&]() { instance()->drawInstanced(t, m, instancecount); });
  1371. }
  1372. return 0;
  1373. }
  1374. int w_print(lua_State *L)
  1375. {
  1376. std::vector<Font::ColoredString> str;
  1377. luax_checkcoloredstring(L, 1, str);
  1378. if (luax_istype(L, 2, math::Transform::type))
  1379. {
  1380. math::Transform *tf = luax_totype<math::Transform>(L, 2);
  1381. luax_catchexcept(L, [&](){ instance()->print(str, tf->getMatrix()); });
  1382. }
  1383. else
  1384. {
  1385. float x = (float)luaL_optnumber(L, 2, 0.0);
  1386. float y = (float)luaL_optnumber(L, 3, 0.0);
  1387. float angle = (float)luaL_optnumber(L, 4, 0.0f);
  1388. float sx = (float)luaL_optnumber(L, 5, 1.0f);
  1389. float sy = (float)luaL_optnumber(L, 6, sx);
  1390. float ox = (float)luaL_optnumber(L, 7, 0.0f);
  1391. float oy = (float)luaL_optnumber(L, 8, 0.0f);
  1392. float kx = (float)luaL_optnumber(L, 9, 0.0f);
  1393. float ky = (float)luaL_optnumber(L, 10, 0.0f);
  1394. Matrix4 m(x, y, angle, sx, sy, ox, oy, kx, ky);
  1395. luax_catchexcept(L, [&](){ instance()->print(str, m); });
  1396. }
  1397. return 0;
  1398. }
  1399. int w_printf(lua_State *L)
  1400. {
  1401. std::vector<Font::ColoredString> str;
  1402. luax_checkcoloredstring(L, 1, str);
  1403. Font::AlignMode align = Font::ALIGN_LEFT;
  1404. Matrix4 m;
  1405. int formatidx = 4;
  1406. if (luax_istype(L, 2, math::Transform::type))
  1407. {
  1408. math::Transform *tf = luax_totype<math::Transform>(L, 2);
  1409. m = tf->getMatrix();
  1410. formatidx = 3;
  1411. }
  1412. else
  1413. {
  1414. float x = (float)luaL_checknumber(L, 2);
  1415. float y = (float)luaL_checknumber(L, 3);
  1416. float angle = (float) luaL_optnumber(L, 6, 0.0f);
  1417. float sx = (float) luaL_optnumber(L, 7, 1.0f);
  1418. float sy = (float) luaL_optnumber(L, 8, sx);
  1419. float ox = (float) luaL_optnumber(L, 9, 0.0f);
  1420. float oy = (float) luaL_optnumber(L, 10, 0.0f);
  1421. float kx = (float) luaL_optnumber(L, 11, 0.0f);
  1422. float ky = (float) luaL_optnumber(L, 12, 0.0f);
  1423. m = Matrix4(x, y, angle, sx, sy, ox, oy, kx, ky);
  1424. }
  1425. float wrap = (float)luaL_checknumber(L, formatidx);
  1426. const char *astr = lua_isnoneornil(L, formatidx + 1) ? nullptr : luaL_checkstring(L, formatidx + 1);
  1427. if (astr != nullptr && !Font::getConstant(astr, align))
  1428. return luaL_error(L, "Incorrect alignment: %s", astr);
  1429. luax_catchexcept(L, [&](){ instance()->printf(str, wrap, align, m); });
  1430. return 0;
  1431. }
  1432. int w_points(lua_State *L)
  1433. {
  1434. // love.graphics.points has 3 variants:
  1435. // - points(x1, y1, x2, y2, ...)
  1436. // - points({x1, y1, x2, y2, ...})
  1437. // - points({{x1, y1 [, r, g, b, a]}, {x2, y2 [, r, g, b, a]}, ...})
  1438. int args = lua_gettop(L);
  1439. bool is_table = false;
  1440. bool is_table_of_tables = false;
  1441. if (args == 1 && lua_istable(L, 1))
  1442. {
  1443. is_table = true;
  1444. args = (int) luax_objlen(L, 1);
  1445. lua_rawgeti(L, 1, 1);
  1446. is_table_of_tables = lua_istable(L, -1);
  1447. lua_pop(L, 1);
  1448. }
  1449. if (args % 2 != 0 && !is_table_of_tables)
  1450. return luaL_error(L, "Number of vertex components must be a multiple of two");
  1451. int numpoints = args / 2;
  1452. if (is_table_of_tables)
  1453. numpoints = args;
  1454. float *coords = nullptr;
  1455. Colorf *colors = nullptr;
  1456. if (is_table_of_tables)
  1457. {
  1458. size_t datasize = (sizeof(float) * 2 + sizeof(Colorf)) * numpoints;
  1459. uint8 *data = instance()->getScratchBuffer<uint8>(datasize);
  1460. coords = (float *) data;
  1461. colors = (Colorf *) (data + sizeof(float) * numpoints * 2);
  1462. }
  1463. else
  1464. coords = instance()->getScratchBuffer<float>(numpoints * 2);
  1465. if (is_table)
  1466. {
  1467. if (is_table_of_tables)
  1468. {
  1469. // points({{x1, y1 [, r, g, b, a]}, {x2, y2 [, r, g, b, a]}, ...})
  1470. for (int i = 0; i < args; i++)
  1471. {
  1472. lua_rawgeti(L, 1, i + 1);
  1473. for (int j = 1; j <= 6; j++)
  1474. lua_rawgeti(L, -j, j);
  1475. coords[i * 2 + 0] = luax_tofloat(L, -6);
  1476. coords[i * 2 + 1] = luax_tofloat(L, -5);
  1477. colors[i].r = luaL_optnumber(L, -4, 1.0);
  1478. colors[i].g = luaL_optnumber(L, -3, 1.0);
  1479. colors[i].b = luaL_optnumber(L, -2, 1.0);
  1480. colors[i].a = luaL_optnumber(L, -1, 1.0);
  1481. lua_pop(L, 7);
  1482. }
  1483. }
  1484. else
  1485. {
  1486. // points({x1, y1, x2, y2, ...})
  1487. for (int i = 0; i < args; i++)
  1488. {
  1489. lua_rawgeti(L, 1, i + 1);
  1490. coords[i] = luax_tofloat(L, -1);
  1491. lua_pop(L, 1);
  1492. }
  1493. }
  1494. }
  1495. else
  1496. {
  1497. for (int i = 0; i < args; i++)
  1498. coords[i] = luax_tofloat(L, i + 1);
  1499. }
  1500. luax_catchexcept(L, [&](){ instance()->points(coords, colors, numpoints); });
  1501. return 0;
  1502. }
  1503. int w_line(lua_State *L)
  1504. {
  1505. int args = lua_gettop(L);
  1506. bool is_table = false;
  1507. if (args == 1 && lua_istable(L, 1))
  1508. {
  1509. args = (int) luax_objlen(L, 1);
  1510. is_table = true;
  1511. }
  1512. if (args % 2 != 0)
  1513. return luaL_error(L, "Number of vertex components must be a multiple of two");
  1514. else if (args < 4)
  1515. return luaL_error(L, "Need at least two vertices to draw a line");
  1516. float *coords = instance()->getScratchBuffer<float>(args);
  1517. if (is_table)
  1518. {
  1519. for (int i = 0; i < args; ++i)
  1520. {
  1521. lua_rawgeti(L, 1, i + 1);
  1522. coords[i] = luax_tofloat(L, -1);
  1523. lua_pop(L, 1);
  1524. }
  1525. }
  1526. else
  1527. {
  1528. for (int i = 0; i < args; ++i)
  1529. coords[i] = luax_tofloat(L, i + 1);
  1530. }
  1531. luax_catchexcept(L,
  1532. [&](){ instance()->polyline(coords, args); },
  1533. [&](bool) { delete[] coords; }
  1534. );
  1535. return 0;
  1536. }
  1537. int w_rectangle(lua_State *L)
  1538. {
  1539. Graphics::DrawMode mode;
  1540. const char *str = luaL_checkstring(L, 1);
  1541. if (!Graphics::getConstant(str, mode))
  1542. return luaL_error(L, "Invalid draw mode: %s", str);
  1543. float x = (float)luaL_checknumber(L, 2);
  1544. float y = (float)luaL_checknumber(L, 3);
  1545. float w = (float)luaL_checknumber(L, 4);
  1546. float h = (float)luaL_checknumber(L, 5);
  1547. if (lua_isnoneornil(L, 6))
  1548. {
  1549. instance()->rectangle(mode, x, y, w, h);
  1550. return 0;
  1551. }
  1552. float rx = (float)luaL_optnumber(L, 6, 0.0);
  1553. float ry = (float)luaL_optnumber(L, 7, rx);
  1554. if (lua_isnoneornil(L, 8))
  1555. luax_catchexcept(L, [&](){ instance()->rectangle(mode, x, y, w, h, rx, ry); });
  1556. else
  1557. {
  1558. int points = (int) luaL_checknumber(L, 8);
  1559. luax_catchexcept(L, [&](){ instance()->rectangle(mode, x, y, w, h, rx, ry, points); });
  1560. }
  1561. return 0;
  1562. }
  1563. int w_circle(lua_State *L)
  1564. {
  1565. Graphics::DrawMode mode;
  1566. const char *str = luaL_checkstring(L, 1);
  1567. if (!Graphics::getConstant(str, mode))
  1568. return luaL_error(L, "Invalid draw mode: %s", str);
  1569. float x = (float)luaL_checknumber(L, 2);
  1570. float y = (float)luaL_checknumber(L, 3);
  1571. float radius = (float)luaL_checknumber(L, 4);
  1572. if (lua_isnoneornil(L, 5))
  1573. luax_catchexcept(L, [&](){ instance()->circle(mode, x, y, radius); });
  1574. else
  1575. {
  1576. int points = (int) luaL_checknumber(L, 5);
  1577. luax_catchexcept(L, [&](){ instance()->circle(mode, x, y, radius, points); });
  1578. }
  1579. return 0;
  1580. }
  1581. int w_ellipse(lua_State *L)
  1582. {
  1583. Graphics::DrawMode mode;
  1584. const char *str = luaL_checkstring(L, 1);
  1585. if (!Graphics::getConstant(str, mode))
  1586. return luaL_error(L, "Invalid draw mode: %s", str);
  1587. float x = (float)luaL_checknumber(L, 2);
  1588. float y = (float)luaL_checknumber(L, 3);
  1589. float a = (float)luaL_checknumber(L, 4);
  1590. float b = (float)luaL_optnumber(L, 5, a);
  1591. if (lua_isnoneornil(L, 6))
  1592. luax_catchexcept(L, [&](){ instance()->ellipse(mode, x, y, a, b); });
  1593. else
  1594. {
  1595. int points = (int) luaL_checknumber(L, 6);
  1596. luax_catchexcept(L, [&](){ instance()->ellipse(mode, x, y, a, b, points); });
  1597. }
  1598. return 0;
  1599. }
  1600. int w_arc(lua_State *L)
  1601. {
  1602. Graphics::DrawMode drawmode;
  1603. const char *drawstr = luaL_checkstring(L, 1);
  1604. if (!Graphics::getConstant(drawstr, drawmode))
  1605. return luaL_error(L, "Invalid draw mode: %s", drawstr);
  1606. int startidx = 2;
  1607. Graphics::ArcMode arcmode = Graphics::ARC_PIE;
  1608. if (lua_type(L, 2) == LUA_TSTRING)
  1609. {
  1610. const char *arcstr = luaL_checkstring(L, 2);
  1611. if (!Graphics::getConstant(arcstr, arcmode))
  1612. return luaL_error(L, "Invalid arc mode: %s", arcstr);
  1613. startidx = 3;
  1614. }
  1615. float x = (float) luaL_checknumber(L, startidx + 0);
  1616. float y = (float) luaL_checknumber(L, startidx + 1);
  1617. float radius = (float) luaL_checknumber(L, startidx + 2);
  1618. float angle1 = (float) luaL_checknumber(L, startidx + 3);
  1619. float angle2 = (float) luaL_checknumber(L, startidx + 4);
  1620. if (lua_isnoneornil(L, startidx + 5))
  1621. luax_catchexcept(L, [&](){ instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2); });
  1622. else
  1623. {
  1624. int points = (int) luaL_checknumber(L, startidx + 5);
  1625. luax_catchexcept(L, [&](){ instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2, points); });
  1626. }
  1627. return 0;
  1628. }
  1629. int w_polygon(lua_State *L)
  1630. {
  1631. int args = lua_gettop(L) - 1;
  1632. Graphics::DrawMode mode;
  1633. const char *str = luaL_checkstring(L, 1);
  1634. if (!Graphics::getConstant(str, mode))
  1635. return luaL_error(L, "Invalid draw mode: %s", str);
  1636. bool is_table = false;
  1637. if (args == 1 && lua_istable(L, 2))
  1638. {
  1639. args = (int) luax_objlen(L, 2);
  1640. is_table = true;
  1641. }
  1642. if (args % 2 != 0)
  1643. return luaL_error(L, "Number of vertex components must be a multiple of two");
  1644. else if (args < 6)
  1645. return luaL_error(L, "Need at least three vertices to draw a polygon");
  1646. // fetch coords
  1647. float *coords = instance()->getScratchBuffer<float>(args + 2);
  1648. if (is_table)
  1649. {
  1650. for (int i = 0; i < args; ++i)
  1651. {
  1652. lua_rawgeti(L, 2, i + 1);
  1653. coords[i] = luax_tofloat(L, -1);
  1654. lua_pop(L, 1);
  1655. }
  1656. }
  1657. else
  1658. {
  1659. for (int i = 0; i < args; ++i)
  1660. coords[i] = luax_tofloat(L, i + 2);
  1661. }
  1662. // make a closed loop
  1663. coords[args] = coords[0];
  1664. coords[args+1] = coords[1];
  1665. luax_catchexcept(L, [&](){ instance()->polygon(mode, coords, args+2); });
  1666. return 0;
  1667. }
  1668. int w_flush(lua_State *)
  1669. {
  1670. instance()->flushStreamDraws();
  1671. return 0;
  1672. }
  1673. int w_push(lua_State *L)
  1674. {
  1675. Graphics::StackType stype = Graphics::STACK_TRANSFORM;
  1676. const char *sname = lua_isnoneornil(L, 1) ? nullptr : luaL_checkstring(L, 1);
  1677. if (sname && !Graphics::getConstant(sname, stype))
  1678. return luaL_error(L, "Invalid graphics stack type: %s", sname);
  1679. luax_catchexcept(L, [&](){ instance()->push(stype); });
  1680. if (luax_istype(L, 2, math::Transform::type))
  1681. {
  1682. math::Transform *t = luax_totype<math::Transform>(L, 2);
  1683. instance()->applyTransform(t);
  1684. }
  1685. return 0;
  1686. }
  1687. int w_pop(lua_State *L)
  1688. {
  1689. luax_catchexcept(L, [&](){ instance()->pop(); });
  1690. return 0;
  1691. }
  1692. int w_rotate(lua_State *L)
  1693. {
  1694. float angle = (float)luaL_checknumber(L, 1);
  1695. instance()->rotate(angle);
  1696. return 0;
  1697. }
  1698. int w_scale(lua_State *L)
  1699. {
  1700. float sx = (float)luaL_optnumber(L, 1, 1.0f);
  1701. float sy = (float)luaL_optnumber(L, 2, sx);
  1702. instance()->scale(sx, sy);
  1703. return 0;
  1704. }
  1705. int w_translate(lua_State *L)
  1706. {
  1707. float x = (float)luaL_checknumber(L, 1);
  1708. float y = (float)luaL_checknumber(L, 2);
  1709. instance()->translate(x, y);
  1710. return 0;
  1711. }
  1712. int w_shear(lua_State *L)
  1713. {
  1714. float kx = (float)luaL_checknumber(L, 1);
  1715. float ky = (float)luaL_checknumber(L, 2);
  1716. instance()->shear(kx, ky);
  1717. return 0;
  1718. }
  1719. int w_origin(lua_State * /*L*/)
  1720. {
  1721. instance()->origin();
  1722. return 0;
  1723. }
  1724. int w_applyTransform(lua_State *L)
  1725. {
  1726. math::Transform *t = math::luax_checktransform(L, 1);
  1727. instance()->applyTransform(t);
  1728. return 0;
  1729. }
  1730. int w_replaceTransform(lua_State *L)
  1731. {
  1732. math::Transform *t = math::luax_checktransform(L, 1);
  1733. instance()->replaceTransform(t);
  1734. return 0;
  1735. }
  1736. int w_transformPoint(lua_State *L)
  1737. {
  1738. Vector p;
  1739. p.x = (float) luaL_checknumber(L, 1);
  1740. p.y = (float) luaL_checknumber(L, 2);
  1741. p = instance()->transformPoint(p);
  1742. lua_pushnumber(L, p.x);
  1743. lua_pushnumber(L, p.y);
  1744. return 2;
  1745. }
  1746. int w_inverseTransformPoint(lua_State *L)
  1747. {
  1748. Vector p;
  1749. p.x = (float) luaL_checknumber(L, 1);
  1750. p.y = (float) luaL_checknumber(L, 2);
  1751. p = instance()->inverseTransformPoint(p);
  1752. lua_pushnumber(L, p.x);
  1753. lua_pushnumber(L, p.y);
  1754. return 2;
  1755. }
  1756. // List of functions to wrap.
  1757. static const luaL_Reg functions[] =
  1758. {
  1759. { "reset", w_reset },
  1760. { "clear", w_clear },
  1761. { "discard", w_discard },
  1762. { "present", w_present },
  1763. { "newImage", w_newImage },
  1764. { "newQuad", w_newQuad },
  1765. { "newFont", w_newFont },
  1766. { "newImageFont", w_newImageFont },
  1767. { "newSpriteBatch", w_newSpriteBatch },
  1768. { "newParticleSystem", w_newParticleSystem },
  1769. { "newCanvas", w_newCanvas },
  1770. { "newShader", w_newShader },
  1771. { "newMesh", w_newMesh },
  1772. { "newText", w_newText },
  1773. { "_newVideo", w_newVideo },
  1774. { "validateShader", w_validateShader },
  1775. { "setCanvas", w_setCanvas },
  1776. { "getCanvas", w_getCanvas },
  1777. { "setColor", w_setColor },
  1778. { "getColor", w_getColor },
  1779. { "setBackgroundColor", w_setBackgroundColor },
  1780. { "getBackgroundColor", w_getBackgroundColor },
  1781. { "setNewFont", w_setNewFont },
  1782. { "setFont", w_setFont },
  1783. { "getFont", w_getFont },
  1784. { "setColorMask", w_setColorMask },
  1785. { "getColorMask", w_getColorMask },
  1786. { "setBlendMode", w_setBlendMode },
  1787. { "getBlendMode", w_getBlendMode },
  1788. { "setDefaultFilter", w_setDefaultFilter },
  1789. { "getDefaultFilter", w_getDefaultFilter },
  1790. { "setDefaultMipmapFilter", w_setDefaultMipmapFilter },
  1791. { "getDefaultMipmapFilter", w_getDefaultMipmapFilter },
  1792. { "setLineWidth", w_setLineWidth },
  1793. { "setLineStyle", w_setLineStyle },
  1794. { "setLineJoin", w_setLineJoin },
  1795. { "getLineWidth", w_getLineWidth },
  1796. { "getLineStyle", w_getLineStyle },
  1797. { "getLineJoin", w_getLineJoin },
  1798. { "setPointSize", w_setPointSize },
  1799. { "getPointSize", w_getPointSize },
  1800. { "setWireframe", w_setWireframe },
  1801. { "isWireframe", w_isWireframe },
  1802. { "setShader", w_setShader },
  1803. { "getShader", w_getShader },
  1804. { "_setDefaultShaderCode", w_setDefaultShaderCode },
  1805. { "getSupported", w_getSupported },
  1806. { "getCanvasFormats", w_getCanvasFormats },
  1807. { "getImageFormats", w_getImageFormats },
  1808. { "getRendererInfo", w_getRendererInfo },
  1809. { "getSystemLimits", w_getSystemLimits },
  1810. { "getStats", w_getStats },
  1811. { "captureScreenshot", w_captureScreenshot },
  1812. { "draw", w_draw },
  1813. { "drawInstanced", w_drawInstanced },
  1814. { "print", w_print },
  1815. { "printf", w_printf },
  1816. { "isCreated", w_isCreated },
  1817. { "isActive", w_isActive },
  1818. { "isGammaCorrect", w_isGammaCorrect },
  1819. { "getWidth", w_getWidth },
  1820. { "getHeight", w_getHeight },
  1821. { "getDimensions", w_getDimensions },
  1822. { "getPixelWidth", w_getPixelWidth },
  1823. { "getPixelHeight", w_getPixelHeight },
  1824. { "getPixelDimensions", w_getPixelDimensions },
  1825. { "getPixelDensity", w_getPixelDensity },
  1826. { "setScissor", w_setScissor },
  1827. { "intersectScissor", w_intersectScissor },
  1828. { "getScissor", w_getScissor },
  1829. { "stencil", w_stencil },
  1830. { "setStencilTest", w_setStencilTest },
  1831. { "getStencilTest", w_getStencilTest },
  1832. { "points", w_points },
  1833. { "line", w_line },
  1834. { "rectangle", w_rectangle },
  1835. { "circle", w_circle },
  1836. { "ellipse", w_ellipse },
  1837. { "arc", w_arc },
  1838. { "polygon", w_polygon },
  1839. { "flush", w_flush },
  1840. { "push", w_push },
  1841. { "pop", w_pop },
  1842. { "rotate", w_rotate },
  1843. { "scale", w_scale },
  1844. { "translate", w_translate },
  1845. { "shear", w_shear },
  1846. { "origin", w_origin },
  1847. { "applyTransform", w_applyTransform },
  1848. { "replaceTransform", w_replaceTransform },
  1849. { "transformPoint", w_transformPoint },
  1850. { "inverseTransformPoint", w_inverseTransformPoint },
  1851. { 0, 0 }
  1852. };
  1853. static int luaopen_drawable(lua_State *L)
  1854. {
  1855. return luax_register_type(L, &Drawable::type, nullptr);
  1856. }
  1857. // Types for this module.
  1858. static const lua_CFunction types[] =
  1859. {
  1860. luaopen_drawable,
  1861. luaopen_texture,
  1862. luaopen_font,
  1863. luaopen_image,
  1864. luaopen_quad,
  1865. luaopen_spritebatch,
  1866. luaopen_particlesystem,
  1867. luaopen_canvas,
  1868. luaopen_shader,
  1869. luaopen_mesh,
  1870. luaopen_text,
  1871. luaopen_video,
  1872. 0
  1873. };
  1874. extern "C" int luaopen_love_graphics(lua_State *L)
  1875. {
  1876. Graphics *instance = instance();
  1877. if (instance == nullptr)
  1878. {
  1879. luax_catchexcept(L, [&](){ instance = new Graphics(); });
  1880. }
  1881. else
  1882. instance->retain();
  1883. WrappedModule w;
  1884. w.module = instance;
  1885. w.name = "graphics";
  1886. w.type = &Graphics::type;
  1887. w.functions = functions;
  1888. w.types = types;
  1889. int n = luax_register_module(L, w);
  1890. if (luaL_loadbuffer(L, (const char *)graphics_lua, sizeof(graphics_lua), "wrap_Graphics.lua") == 0)
  1891. lua_call(L, 0, 0);
  1892. else
  1893. lua_error(L);
  1894. return n;
  1895. }
  1896. } // opengl
  1897. } // graphics
  1898. } // love