wrap_Graphics.cpp 61 KB

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