wrap_Graphics.cpp 61 KB

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