wrap_Graphics.cpp 44 KB

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