wrap_Graphics.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141
  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, 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, 1.0);
  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, 1.0);
  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, love::filesystem::File::type) || luax_istype(L, 1, love::filesystem::FileData::type))
  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, love::image::CompressedImageData::type))
  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, love::image::ImageData::type))
  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, love::image::CompressedImageData::type))
  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, 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, 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, love::font::Rasterizer::type))
  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);
  365. luax_catchexcept(L, [&]() {
  366. font = instance()->newFont(rasterizer, instance()->getDefaultFilter()); }
  367. );
  368. // Push the type.
  369. luax_pushtype(L, 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, Image::type))
  380. {
  381. Image *i = luax_checktype<Image>(L, 1);
  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, idlevels[0].get());
  387. lua_replace(L, 1);
  388. }
  389. // Convert to Rasterizer if necessary.
  390. if (!luax_istype(L, 1, love::font::Rasterizer::type))
  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);
  399. // Create the font.
  400. Font *font = instance()->newFont(rasterizer, filter);
  401. // Push the type.
  402. luax_pushtype(L, 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, 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, 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, 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, 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, 1.0) * 255.0);
  593. v.g = (unsigned char) (luaL_optnumber(L, -3, 1.0) * 255.0);
  594. v.b = (unsigned char) (luaL_optnumber(L, -2, 1.0) * 255.0);
  595. v.a = (unsigned char) (luaL_optnumber(L, -1, 1.0) * 255.0);
  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::type))
  653. {
  654. // Vertex data comes directly from a Data object.
  655. Data *data = luax_checktype<Data>(L, 2);
  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, 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, 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, love::video::VideoStream::type))
  742. luax_convobj(L, 1, "video", "newVideoStream");
  743. auto stream = luax_checktype<love::video::VideoStream>(L, 1);
  744. Video *video = nullptr;
  745. luax_catchexcept(L, [&]() { video = instance()->newVideo(stream); });
  746. luax_pushtype(L, 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, 1.0);
  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, 1.0);
  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, 1.0);
  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, 1.0);
  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);
  818. instance()->setFont(font);
  819. return ret;
  820. }
  821. int w_setFont(lua_State *L)
  822. {
  823. Font *font = luax_checktype<Font>(L, 1);
  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, 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);
  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, 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, 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, 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. luaL_checktype(L, 2, LUA_TTABLE);
  1096. for (int i = 0; i < 2; i++)
  1097. {
  1098. for (int renderer = 0; renderer < Graphics::RENDERER_MAX_ENUM; renderer++)
  1099. {
  1100. const char *lang = renderer == Graphics::RENDERER_OPENGLES ? "glsles" : "glsl";
  1101. lua_getfield(L, i + 1, lang);
  1102. lua_getfield(L, -1, "vertex");
  1103. lua_getfield(L, -2, "pixel");
  1104. lua_getfield(L, -3, "videopixel");
  1105. Shader::ShaderSource code;
  1106. code.vertex = luax_checkstring(L, -3);
  1107. code.pixel = luax_checkstring(L, -2);
  1108. Shader::ShaderSource videocode;
  1109. videocode.vertex = luax_checkstring(L, -3);
  1110. videocode.pixel = luax_checkstring(L, -1);
  1111. lua_pop(L, 4);
  1112. Shader::defaultCode[renderer][i] = code;
  1113. Shader::defaultVideoCode[renderer][i] = videocode;
  1114. }
  1115. }
  1116. return 0;
  1117. }
  1118. int w_getSupported(lua_State *L)
  1119. {
  1120. lua_createtable(L, 0, (int) Graphics::FEATURE_MAX_ENUM);
  1121. for (int i = 0; i < (int) Graphics::FEATURE_MAX_ENUM; i++)
  1122. {
  1123. auto feature = (Graphics::Feature) i;
  1124. const char *name = nullptr;
  1125. if (!Graphics::getConstant(feature, name))
  1126. continue;
  1127. luax_pushboolean(L, instance()->isSupported(feature));
  1128. lua_setfield(L, -2, name);
  1129. }
  1130. return 1;
  1131. }
  1132. int w_getCanvasFormats(lua_State *L)
  1133. {
  1134. lua_createtable(L, 0, (int) Canvas::FORMAT_MAX_ENUM);
  1135. for (int i = 0; i < (int) Canvas::FORMAT_MAX_ENUM; i++)
  1136. {
  1137. Canvas::Format format = (Canvas::Format) i;
  1138. const char *name = nullptr;
  1139. if (!Canvas::getConstant(format, name))
  1140. continue;
  1141. luax_pushboolean(L, Canvas::isFormatSupported(format));
  1142. lua_setfield(L, -2, name);
  1143. }
  1144. return 1;
  1145. }
  1146. int w_getRawImageFormats(lua_State *L)
  1147. {
  1148. lua_createtable(L, 0, (int) image::ImageData::FORMAT_MAX_ENUM);
  1149. for (int i = 0; i < (int) image::ImageData::FORMAT_MAX_ENUM; i++)
  1150. {
  1151. auto format = (image::ImageData::Format) i;
  1152. const char *name = nullptr;
  1153. if (!image::ImageData::getConstant(format, name))
  1154. continue;
  1155. luax_pushboolean(L, Image::hasTextureSupport(format));
  1156. lua_setfield(L, -2, name);
  1157. }
  1158. return 1;
  1159. }
  1160. int w_getCompressedImageFormats(lua_State *L)
  1161. {
  1162. lua_createtable(L, 0, (int) image::CompressedImageData::FORMAT_MAX_ENUM);
  1163. for (int i = 0; i < (int) image::CompressedImageData::FORMAT_MAX_ENUM; i++)
  1164. {
  1165. auto format = (image::CompressedImageData::Format) i;
  1166. const char *name = nullptr;
  1167. if (format == image::CompressedImageData::FORMAT_UNKNOWN)
  1168. continue;
  1169. if (!image::CompressedImageData::getConstant(format, name))
  1170. continue;
  1171. luax_pushboolean(L, Image::hasCompressedTextureSupport(format, false));
  1172. lua_setfield(L, -2, name);
  1173. }
  1174. return 1;
  1175. }
  1176. int w_getRendererInfo(lua_State *L)
  1177. {
  1178. Graphics::RendererInfo info;
  1179. luax_catchexcept(L, [&](){ info = instance()->getRendererInfo(); });
  1180. luax_pushstring(L, info.name);
  1181. luax_pushstring(L, info.version);
  1182. luax_pushstring(L, info.vendor);
  1183. luax_pushstring(L, info.device);
  1184. return 4;
  1185. }
  1186. int w_getSystemLimits(lua_State *L)
  1187. {
  1188. lua_createtable(L, 0, (int) Graphics::LIMIT_MAX_ENUM);
  1189. for (int i = 0; i < (int) Graphics::LIMIT_MAX_ENUM; i++)
  1190. {
  1191. Graphics::SystemLimit limittype = (Graphics::SystemLimit) i;
  1192. const char *name = nullptr;
  1193. if (!Graphics::getConstant(limittype, name))
  1194. continue;
  1195. lua_pushnumber(L, instance()->getSystemLimit(limittype));
  1196. lua_setfield(L, -2, name);
  1197. }
  1198. return 1;
  1199. }
  1200. int w_getStats(lua_State *L)
  1201. {
  1202. Graphics::Stats stats = instance()->getStats();
  1203. lua_createtable(L, 0, 7);
  1204. lua_pushinteger(L, stats.drawCalls);
  1205. lua_setfield(L, -2, "drawcalls");
  1206. lua_pushinteger(L, stats.canvasSwitches);
  1207. lua_setfield(L, -2, "canvasswitches");
  1208. lua_pushinteger(L, stats.shaderSwitches);
  1209. lua_setfield(L, -2, "shaderswitches");
  1210. lua_pushinteger(L, stats.canvases);
  1211. lua_setfield(L, -2, "canvases");
  1212. lua_pushinteger(L, stats.images);
  1213. lua_setfield(L, -2, "images");
  1214. lua_pushinteger(L, stats.fonts);
  1215. lua_setfield(L, -2, "fonts");
  1216. lua_pushinteger(L, stats.textureMemory);
  1217. lua_setfield(L, -2, "texturememory");
  1218. return 1;
  1219. }
  1220. int w_draw(lua_State *L)
  1221. {
  1222. Drawable *drawable = nullptr;
  1223. Texture *texture = nullptr;
  1224. Quad *quad = nullptr;
  1225. int startidx = 2;
  1226. if (luax_istype(L, 2, Quad::type))
  1227. {
  1228. texture = luax_checktexture(L, 1);
  1229. quad = luax_totype<Quad>(L, 2);
  1230. startidx = 3;
  1231. }
  1232. else if (lua_isnil(L, 2) && !lua_isnoneornil(L, 3))
  1233. {
  1234. return luax_typerror(L, 2, "Quad");
  1235. }
  1236. else
  1237. {
  1238. drawable = luax_checktype<Drawable>(L, 1);
  1239. startidx = 2;
  1240. }
  1241. float x = (float) luaL_optnumber(L, startidx + 0, 0.0);
  1242. float y = (float) luaL_optnumber(L, startidx + 1, 0.0);
  1243. float a = (float) luaL_optnumber(L, startidx + 2, 0.0);
  1244. float sx = (float) luaL_optnumber(L, startidx + 3, 1.0);
  1245. float sy = (float) luaL_optnumber(L, startidx + 4, sx);
  1246. float ox = (float) luaL_optnumber(L, startidx + 5, 0.0);
  1247. float oy = (float) luaL_optnumber(L, startidx + 6, 0.0);
  1248. float kx = (float) luaL_optnumber(L, startidx + 7, 0.0);
  1249. float ky = (float) luaL_optnumber(L, startidx + 8, 0.0);
  1250. Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
  1251. luax_catchexcept(L, [&]() {
  1252. if (texture && quad)
  1253. texture->drawq(quad, m);
  1254. else if (drawable)
  1255. drawable->draw(m);
  1256. });
  1257. return 0;
  1258. }
  1259. int w_print(lua_State *L)
  1260. {
  1261. std::vector<Font::ColoredString> str;
  1262. luax_checkcoloredstring(L, 1, str);
  1263. float x = (float)luaL_optnumber(L, 2, 0.0);
  1264. float y = (float)luaL_optnumber(L, 3, 0.0);
  1265. float angle = (float)luaL_optnumber(L, 4, 0.0f);
  1266. float sx = (float)luaL_optnumber(L, 5, 1.0f);
  1267. float sy = (float)luaL_optnumber(L, 6, sx);
  1268. float ox = (float)luaL_optnumber(L, 7, 0.0f);
  1269. float oy = (float)luaL_optnumber(L, 8, 0.0f);
  1270. float kx = (float)luaL_optnumber(L, 9, 0.0f);
  1271. float ky = (float)luaL_optnumber(L, 10, 0.0f);
  1272. Matrix4 m(x, y, angle, sx, sy, ox, oy, kx, ky);
  1273. luax_catchexcept(L, [&](){ instance()->print(str, m); });
  1274. return 0;
  1275. }
  1276. int w_printf(lua_State *L)
  1277. {
  1278. std::vector<Font::ColoredString> str;
  1279. luax_checkcoloredstring(L, 1, str);
  1280. float x = (float)luaL_checknumber(L, 2);
  1281. float y = (float)luaL_checknumber(L, 3);
  1282. float wrap = (float)luaL_checknumber(L, 4);
  1283. float angle = 0.0f;
  1284. float sx = 1.0f, sy = 1.0f;
  1285. float ox = 0.0f, oy = 0.0f;
  1286. float kx = 0.0f, ky = 0.0f;
  1287. Font::AlignMode align = Font::ALIGN_LEFT;
  1288. if (lua_gettop(L) >= 5)
  1289. {
  1290. if (!lua_isnil(L, 5))
  1291. {
  1292. const char *str = luaL_checkstring(L, 5);
  1293. if (!Font::getConstant(str, align))
  1294. return luaL_error(L, "Incorrect alignment: %s", str);
  1295. }
  1296. angle = (float) luaL_optnumber(L, 6, 0.0f);
  1297. sx = (float) luaL_optnumber(L, 7, 1.0f);
  1298. sy = (float) luaL_optnumber(L, 8, sx);
  1299. ox = (float) luaL_optnumber(L, 9, 0.0f);
  1300. oy = (float) luaL_optnumber(L, 10, 0.0f);
  1301. kx = (float) luaL_optnumber(L, 11, 0.0f);
  1302. ky = (float) luaL_optnumber(L, 12, 0.0f);
  1303. }
  1304. Matrix4 m(x, y, angle, sx, sy, ox, oy, kx, ky);
  1305. luax_catchexcept(L, [&](){ instance()->printf(str, wrap, align, m); });
  1306. return 0;
  1307. }
  1308. int w_points(lua_State *L)
  1309. {
  1310. // love.graphics.points has 3 variants:
  1311. // - points(x1, y1, x2, y2, ...)
  1312. // - points({x1, y1, x2, y2, ...})
  1313. // - points({{x1, y1 [, r, g, b, a]}, {x2, y2 [, r, g, b, a]}, ...})
  1314. int args = lua_gettop(L);
  1315. bool is_table = false;
  1316. bool is_table_of_tables = false;
  1317. if (args == 1 && lua_istable(L, 1))
  1318. {
  1319. is_table = true;
  1320. args = (int) luax_objlen(L, 1);
  1321. lua_rawgeti(L, 1, 1);
  1322. is_table_of_tables = lua_istable(L, -1);
  1323. lua_pop(L, 1);
  1324. }
  1325. if (args % 2 != 0 && !is_table_of_tables)
  1326. return luaL_error(L, "Number of vertex components must be a multiple of two");
  1327. int numpoints = args / 2;
  1328. if (is_table_of_tables)
  1329. numpoints = args;
  1330. float *coords = nullptr;
  1331. uint8 *colors = nullptr;
  1332. coords = new float[numpoints * 2];
  1333. if (is_table_of_tables)
  1334. colors = new uint8[numpoints * 4];
  1335. if (is_table)
  1336. {
  1337. if (is_table_of_tables)
  1338. {
  1339. // points({{x1, y1 [, r, g, b, a]}, {x2, y2 [, r, g, b, a]}, ...})
  1340. for (int i = 0; i < args; i++)
  1341. {
  1342. lua_rawgeti(L, 1, i + 1);
  1343. for (int j = 1; j <= 6; j++)
  1344. lua_rawgeti(L, -j, j);
  1345. coords[i * 2 + 0] = luax_tofloat(L, -6);
  1346. coords[i * 2 + 1] = luax_tofloat(L, -5);
  1347. colors[i * 4 + 0] = (uint8) (luaL_optnumber(L, -4, 1.0) * 255.0);
  1348. colors[i * 4 + 1] = (uint8) (luaL_optnumber(L, -3, 1.0) * 255.0);
  1349. colors[i * 4 + 2] = (uint8) (luaL_optnumber(L, -2, 1.0) * 255.0);
  1350. colors[i * 4 + 3] = (uint8) (luaL_optnumber(L, -1, 1.0) * 255.0);
  1351. lua_pop(L, 7);
  1352. }
  1353. }
  1354. else
  1355. {
  1356. // points({x1, y1, x2, y2, ...})
  1357. for (int i = 0; i < args; i++)
  1358. {
  1359. lua_rawgeti(L, 1, i + 1);
  1360. coords[i] = luax_tofloat(L, -1);
  1361. lua_pop(L, 1);
  1362. }
  1363. }
  1364. }
  1365. else
  1366. {
  1367. for (int i = 0; i < args; i++)
  1368. coords[i] = luax_tofloat(L, i + 1);
  1369. }
  1370. instance()->points(coords, colors, numpoints);
  1371. delete[] coords;
  1372. if (colors)
  1373. delete[] colors;
  1374. return 0;
  1375. }
  1376. int w_line(lua_State *L)
  1377. {
  1378. int args = lua_gettop(L);
  1379. bool is_table = false;
  1380. if (args == 1 && lua_istable(L, 1))
  1381. {
  1382. args = (int) luax_objlen(L, 1);
  1383. is_table = true;
  1384. }
  1385. if (args % 2 != 0)
  1386. return luaL_error(L, "Number of vertex components must be a multiple of two");
  1387. else if (args < 4)
  1388. return luaL_error(L, "Need at least two vertices to draw a line");
  1389. float *coords = new float[args];
  1390. if (is_table)
  1391. {
  1392. for (int i = 0; i < args; ++i)
  1393. {
  1394. lua_rawgeti(L, 1, i + 1);
  1395. coords[i] = luax_tofloat(L, -1);
  1396. lua_pop(L, 1);
  1397. }
  1398. }
  1399. else
  1400. {
  1401. for (int i = 0; i < args; ++i)
  1402. coords[i] = luax_tofloat(L, i + 1);
  1403. }
  1404. instance()->polyline(coords, args);
  1405. delete[] coords;
  1406. return 0;
  1407. }
  1408. int w_rectangle(lua_State *L)
  1409. {
  1410. Graphics::DrawMode mode;
  1411. const char *str = luaL_checkstring(L, 1);
  1412. if (!Graphics::getConstant(str, mode))
  1413. return luaL_error(L, "Invalid draw mode: %s", str);
  1414. float x = (float)luaL_checknumber(L, 2);
  1415. float y = (float)luaL_checknumber(L, 3);
  1416. float w = (float)luaL_checknumber(L, 4);
  1417. float h = (float)luaL_checknumber(L, 5);
  1418. if (lua_isnoneornil(L, 6))
  1419. {
  1420. instance()->rectangle(mode, x, y, w, h);
  1421. return 0;
  1422. }
  1423. float rx = (float)luaL_optnumber(L, 6, 0.0);
  1424. float ry = (float)luaL_optnumber(L, 7, rx);
  1425. if (lua_isnoneornil(L, 8))
  1426. instance()->rectangle(mode, x, y, w, h, rx, ry);
  1427. else
  1428. {
  1429. int points = (int) luaL_checknumber(L, 8);
  1430. instance()->rectangle(mode, x, y, w, h, rx, ry, points);
  1431. }
  1432. return 0;
  1433. }
  1434. int w_circle(lua_State *L)
  1435. {
  1436. Graphics::DrawMode mode;
  1437. const char *str = luaL_checkstring(L, 1);
  1438. if (!Graphics::getConstant(str, mode))
  1439. return luaL_error(L, "Invalid draw mode: %s", str);
  1440. float x = (float)luaL_checknumber(L, 2);
  1441. float y = (float)luaL_checknumber(L, 3);
  1442. float radius = (float)luaL_checknumber(L, 4);
  1443. if (lua_isnoneornil(L, 5))
  1444. instance()->circle(mode, x, y, radius);
  1445. else
  1446. {
  1447. int points = (int) luaL_checknumber(L, 5);
  1448. instance()->circle(mode, x, y, radius, points);
  1449. }
  1450. return 0;
  1451. }
  1452. int w_ellipse(lua_State *L)
  1453. {
  1454. Graphics::DrawMode mode;
  1455. const char *str = luaL_checkstring(L, 1);
  1456. if (!Graphics::getConstant(str, mode))
  1457. return luaL_error(L, "Invalid draw mode: %s", str);
  1458. float x = (float)luaL_checknumber(L, 2);
  1459. float y = (float)luaL_checknumber(L, 3);
  1460. float a = (float)luaL_checknumber(L, 4);
  1461. float b = (float)luaL_optnumber(L, 5, a);
  1462. if (lua_isnoneornil(L, 6))
  1463. instance()->ellipse(mode, x, y, a, b);
  1464. else
  1465. {
  1466. int points = (int) luaL_checknumber(L, 6);
  1467. instance()->ellipse(mode, x, y, a, b, points);
  1468. }
  1469. return 0;
  1470. }
  1471. int w_arc(lua_State *L)
  1472. {
  1473. Graphics::DrawMode drawmode;
  1474. const char *drawstr = luaL_checkstring(L, 1);
  1475. if (!Graphics::getConstant(drawstr, drawmode))
  1476. return luaL_error(L, "Invalid draw mode: %s", drawstr);
  1477. int startidx = 2;
  1478. Graphics::ArcMode arcmode = Graphics::ARC_PIE;
  1479. if (lua_type(L, 2) == LUA_TSTRING)
  1480. {
  1481. const char *arcstr = luaL_checkstring(L, 2);
  1482. if (!Graphics::getConstant(arcstr, arcmode))
  1483. return luaL_error(L, "Invalid arc mode: %s", arcstr);
  1484. startidx = 3;
  1485. }
  1486. float x = (float) luaL_checknumber(L, startidx + 0);
  1487. float y = (float) luaL_checknumber(L, startidx + 1);
  1488. float radius = (float) luaL_checknumber(L, startidx + 2);
  1489. float angle1 = (float) luaL_checknumber(L, startidx + 3);
  1490. float angle2 = (float) luaL_checknumber(L, startidx + 4);
  1491. if (lua_isnoneornil(L, startidx + 5))
  1492. instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2);
  1493. else
  1494. {
  1495. int points = (int) luaL_checknumber(L, startidx + 5);
  1496. instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2, points);
  1497. }
  1498. return 0;
  1499. }
  1500. int w_polygon(lua_State *L)
  1501. {
  1502. int args = lua_gettop(L) - 1;
  1503. Graphics::DrawMode mode;
  1504. const char *str = luaL_checkstring(L, 1);
  1505. if (!Graphics::getConstant(str, mode))
  1506. return luaL_error(L, "Invalid draw mode: %s", str);
  1507. bool is_table = false;
  1508. float *coords;
  1509. if (args == 1 && lua_istable(L, 2))
  1510. {
  1511. args = (int) luax_objlen(L, 2);
  1512. is_table = true;
  1513. }
  1514. if (args % 2 != 0)
  1515. return luaL_error(L, "Number of vertex components must be a multiple of two");
  1516. else if (args < 6)
  1517. return luaL_error(L, "Need at least three vertices to draw a polygon");
  1518. // fetch coords
  1519. coords = new float[args + 2];
  1520. if (is_table)
  1521. {
  1522. for (int i = 0; i < args; ++i)
  1523. {
  1524. lua_rawgeti(L, 2, i + 1);
  1525. coords[i] = luax_tofloat(L, -1);
  1526. lua_pop(L, 1);
  1527. }
  1528. }
  1529. else
  1530. {
  1531. for (int i = 0; i < args; ++i)
  1532. coords[i] = luax_tofloat(L, i + 2);
  1533. }
  1534. // make a closed loop
  1535. coords[args] = coords[0];
  1536. coords[args+1] = coords[1];
  1537. instance()->polygon(mode, coords, args+2);
  1538. delete[] coords;
  1539. return 0;
  1540. }
  1541. int w_push(lua_State *L)
  1542. {
  1543. Graphics::StackType stype = Graphics::STACK_TRANSFORM;
  1544. const char *sname = lua_isnoneornil(L, 1) ? nullptr : luaL_checkstring(L, 1);
  1545. if (sname && !Graphics::getConstant(sname, stype))
  1546. return luaL_error(L, "Invalid graphics stack type: %s", sname);
  1547. luax_catchexcept(L, [&](){ instance()->push(stype); });
  1548. return 0;
  1549. }
  1550. int w_pop(lua_State *L)
  1551. {
  1552. luax_catchexcept(L, [&](){ instance()->pop(); });
  1553. return 0;
  1554. }
  1555. int w_rotate(lua_State *L)
  1556. {
  1557. float angle = (float)luaL_checknumber(L, 1);
  1558. instance()->rotate(angle);
  1559. return 0;
  1560. }
  1561. int w_scale(lua_State *L)
  1562. {
  1563. float sx = (float)luaL_optnumber(L, 1, 1.0f);
  1564. float sy = (float)luaL_optnumber(L, 2, sx);
  1565. instance()->scale(sx, sy);
  1566. return 0;
  1567. }
  1568. int w_translate(lua_State *L)
  1569. {
  1570. float x = (float)luaL_checknumber(L, 1);
  1571. float y = (float)luaL_checknumber(L, 2);
  1572. instance()->translate(x, y);
  1573. return 0;
  1574. }
  1575. int w_shear(lua_State *L)
  1576. {
  1577. float kx = (float)luaL_checknumber(L, 1);
  1578. float ky = (float)luaL_checknumber(L, 2);
  1579. instance()->shear(kx, ky);
  1580. return 0;
  1581. }
  1582. int w_origin(lua_State * /*L*/)
  1583. {
  1584. instance()->origin();
  1585. return 0;
  1586. }
  1587. int w_transformPoint(lua_State *L)
  1588. {
  1589. Vector p;
  1590. p.x = (float) luaL_checknumber(L, 1);
  1591. p.y = (float) luaL_checknumber(L, 2);
  1592. p = instance()->transformPoint(p);
  1593. lua_pushnumber(L, p.x);
  1594. lua_pushnumber(L, p.y);
  1595. return 2;
  1596. }
  1597. int w_inverseTransformPoint(lua_State *L)
  1598. {
  1599. Vector p;
  1600. p.x = (float) luaL_checknumber(L, 1);
  1601. p.y = (float) luaL_checknumber(L, 2);
  1602. p = instance()->inverseTransformPoint(p);
  1603. lua_pushnumber(L, p.x);
  1604. lua_pushnumber(L, p.y);
  1605. return 2;
  1606. }
  1607. // List of functions to wrap.
  1608. static const luaL_Reg functions[] =
  1609. {
  1610. { "reset", w_reset },
  1611. { "clear", w_clear },
  1612. { "discard", w_discard },
  1613. { "present", w_present },
  1614. { "newImage", w_newImage },
  1615. { "newQuad", w_newQuad },
  1616. { "newFont", w_newFont },
  1617. { "newImageFont", w_newImageFont },
  1618. { "newSpriteBatch", w_newSpriteBatch },
  1619. { "newParticleSystem", w_newParticleSystem },
  1620. { "newCanvas", w_newCanvas },
  1621. { "newShader", w_newShader },
  1622. { "newMesh", w_newMesh },
  1623. { "newText", w_newText },
  1624. { "_newVideo", w_newVideo },
  1625. { "setColor", w_setColor },
  1626. { "getColor", w_getColor },
  1627. { "setBackgroundColor", w_setBackgroundColor },
  1628. { "getBackgroundColor", w_getBackgroundColor },
  1629. { "setNewFont", w_setNewFont },
  1630. { "setFont", w_setFont },
  1631. { "getFont", w_getFont },
  1632. { "setColorMask", w_setColorMask },
  1633. { "getColorMask", w_getColorMask },
  1634. { "setBlendMode", w_setBlendMode },
  1635. { "getBlendMode", w_getBlendMode },
  1636. { "setDefaultFilter", w_setDefaultFilter },
  1637. { "getDefaultFilter", w_getDefaultFilter },
  1638. { "setDefaultMipmapFilter", w_setDefaultMipmapFilter },
  1639. { "getDefaultMipmapFilter", w_getDefaultMipmapFilter },
  1640. { "setLineWidth", w_setLineWidth },
  1641. { "setLineStyle", w_setLineStyle },
  1642. { "setLineJoin", w_setLineJoin },
  1643. { "getLineWidth", w_getLineWidth },
  1644. { "getLineStyle", w_getLineStyle },
  1645. { "getLineJoin", w_getLineJoin },
  1646. { "setPointSize", w_setPointSize },
  1647. { "getPointSize", w_getPointSize },
  1648. { "setWireframe", w_setWireframe },
  1649. { "isWireframe", w_isWireframe },
  1650. { "newScreenshot", w_newScreenshot },
  1651. { "setCanvas", w_setCanvas },
  1652. { "getCanvas", w_getCanvas },
  1653. { "setShader", w_setShader },
  1654. { "getShader", w_getShader },
  1655. { "_setDefaultShaderCode", w_setDefaultShaderCode },
  1656. { "getSupported", w_getSupported },
  1657. { "getCanvasFormats", w_getCanvasFormats },
  1658. { "getRawImageFormats", w_getRawImageFormats },
  1659. { "getCompressedImageFormats", w_getCompressedImageFormats },
  1660. { "getRendererInfo", w_getRendererInfo },
  1661. { "getSystemLimits", w_getSystemLimits },
  1662. { "getStats", w_getStats },
  1663. { "draw", w_draw },
  1664. { "print", w_print },
  1665. { "printf", w_printf },
  1666. { "isCreated", w_isCreated },
  1667. { "isActive", w_isActive },
  1668. { "isGammaCorrect", w_isGammaCorrect },
  1669. { "getWidth", w_getWidth },
  1670. { "getHeight", w_getHeight },
  1671. { "getDimensions", w_getDimensions },
  1672. { "setScissor", w_setScissor },
  1673. { "intersectScissor", w_intersectScissor },
  1674. { "getScissor", w_getScissor },
  1675. { "stencil", w_stencil },
  1676. { "setStencilTest", w_setStencilTest },
  1677. { "getStencilTest", w_getStencilTest },
  1678. { "points", w_points },
  1679. { "line", w_line },
  1680. { "rectangle", w_rectangle },
  1681. { "circle", w_circle },
  1682. { "ellipse", w_ellipse },
  1683. { "arc", w_arc },
  1684. { "polygon", w_polygon },
  1685. { "push", w_push },
  1686. { "pop", w_pop },
  1687. { "rotate", w_rotate },
  1688. { "scale", w_scale },
  1689. { "translate", w_translate },
  1690. { "shear", w_shear },
  1691. { "origin", w_origin },
  1692. { "transformPoint", w_transformPoint },
  1693. { "inverseTransformPoint", w_inverseTransformPoint },
  1694. { 0, 0 }
  1695. };
  1696. static int luaopen_drawable(lua_State *L)
  1697. {
  1698. return luax_register_type(L, Drawable::type, nullptr);
  1699. }
  1700. // Types for this module.
  1701. static const lua_CFunction types[] =
  1702. {
  1703. luaopen_drawable,
  1704. luaopen_texture,
  1705. luaopen_font,
  1706. luaopen_image,
  1707. luaopen_quad,
  1708. luaopen_spritebatch,
  1709. luaopen_particlesystem,
  1710. luaopen_canvas,
  1711. luaopen_shader,
  1712. luaopen_mesh,
  1713. luaopen_text,
  1714. luaopen_video,
  1715. 0
  1716. };
  1717. extern "C" int luaopen_love_graphics(lua_State *L)
  1718. {
  1719. Graphics *instance = instance();
  1720. if (instance == nullptr)
  1721. {
  1722. luax_catchexcept(L, [&](){ instance = new Graphics(); });
  1723. }
  1724. else
  1725. instance->retain();
  1726. WrappedModule w;
  1727. w.module = instance;
  1728. w.name = "graphics";
  1729. w.type = &Graphics::type;
  1730. w.functions = functions;
  1731. w.types = types;
  1732. int n = luax_register_module(L, w);
  1733. if (luaL_loadbuffer(L, (const char *)graphics_lua, sizeof(graphics_lua), "wrap_Graphics.lua") == 0)
  1734. lua_call(L, 0, 0);
  1735. return n;
  1736. }
  1737. } // opengl
  1738. } // graphics
  1739. } // love