wrap_Graphics.cpp 62 KB

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