wrap_Graphics.cpp 35 KB

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