wrap_Graphics.cpp 49 KB

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