wrap_Graphics.cpp 54 KB

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