wrap_Graphics.cpp 36 KB

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