wrap_Graphics.cpp 44 KB

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