wrap_Graphics.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  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_newText(lua_State *L)
  466. {
  467. Font *font = luax_checkfont(L, 1);
  468. Text *t = nullptr;
  469. if (lua_isnoneornil(L, 2))
  470. luax_catchexcept(L, [&](){ t = instance()->newText(font); });
  471. else
  472. {
  473. std::string text = luax_checkstring(L, 2);
  474. luax_catchexcept(L, [&](){ t = instance()->newText(font, text); });
  475. }
  476. luax_pushtype(L, "Text", GRAPHICS_TEXT_T, t);
  477. return 1;
  478. }
  479. int w_setColor(lua_State *L)
  480. {
  481. Color c;
  482. if (lua_istable(L, 1))
  483. {
  484. for (int i = 1; i <= 4; i++)
  485. lua_rawgeti(L, 1, i);
  486. c.r = (unsigned char)luaL_checkint(L, -4);
  487. c.g = (unsigned char)luaL_checkint(L, -3);
  488. c.b = (unsigned char)luaL_checkint(L, -2);
  489. c.a = (unsigned char)luaL_optint(L, -1, 255);
  490. lua_pop(L, 4);
  491. }
  492. else
  493. {
  494. c.r = (unsigned char)luaL_checkint(L, 1);
  495. c.g = (unsigned char)luaL_checkint(L, 2);
  496. c.b = (unsigned char)luaL_checkint(L, 3);
  497. c.a = (unsigned char)luaL_optint(L, 4, 255);
  498. }
  499. instance()->setColor(c);
  500. return 0;
  501. }
  502. int w_getColor(lua_State *L)
  503. {
  504. Color c = instance()->getColor();
  505. lua_pushinteger(L, c.r);
  506. lua_pushinteger(L, c.g);
  507. lua_pushinteger(L, c.b);
  508. lua_pushinteger(L, c.a);
  509. return 4;
  510. }
  511. int w_setBackgroundColor(lua_State *L)
  512. {
  513. Color c;
  514. if (lua_istable(L, 1))
  515. {
  516. for (int i = 1; i <= 4; i++)
  517. lua_rawgeti(L, 1, i);
  518. c.r = (unsigned char)luaL_checkint(L, -4);
  519. c.g = (unsigned char)luaL_checkint(L, -3);
  520. c.b = (unsigned char)luaL_checkint(L, -2);
  521. c.a = (unsigned char)luaL_optint(L, -1, 255);
  522. lua_pop(L, 4);
  523. }
  524. else
  525. {
  526. c.r = (unsigned char)luaL_checkint(L, 1);
  527. c.g = (unsigned char)luaL_checkint(L, 2);
  528. c.b = (unsigned char)luaL_checkint(L, 3);
  529. c.a = (unsigned char)luaL_optint(L, 4, 255);
  530. }
  531. instance()->setBackgroundColor(c);
  532. return 0;
  533. }
  534. int w_getBackgroundColor(lua_State *L)
  535. {
  536. Color c = instance()->getBackgroundColor();
  537. lua_pushinteger(L, c.r);
  538. lua_pushinteger(L, c.g);
  539. lua_pushinteger(L, c.b);
  540. lua_pushinteger(L, c.a);
  541. return 4;
  542. }
  543. int w_setFont(lua_State *L)
  544. {
  545. Font *font = luax_checktype<Font>(L, 1, "Font", GRAPHICS_FONT_T);
  546. instance()->setFont(font);
  547. return 0;
  548. }
  549. int w_getFont(lua_State *L)
  550. {
  551. Font *f = instance()->getFont();
  552. if (f == 0)
  553. return 0;
  554. luax_pushtype(L, "Font", GRAPHICS_FONT_T, f);
  555. return 1;
  556. }
  557. int w_setColorMask(lua_State *L)
  558. {
  559. Graphics::ColorMask mask;
  560. if (lua_gettop(L) <= 1 && lua_isnoneornil(L, 1))
  561. {
  562. // Enable all color components if no argument is given.
  563. mask.r = mask.g = mask.b = mask.a = true;
  564. }
  565. else
  566. {
  567. mask.r = luax_toboolean(L, 1);
  568. mask.g = luax_toboolean(L, 2);
  569. mask.b = luax_toboolean(L, 3);
  570. mask.a = luax_toboolean(L, 4);
  571. }
  572. instance()->setColorMask(mask);
  573. return 0;
  574. }
  575. int w_getColorMask(lua_State *L)
  576. {
  577. Graphics::ColorMask mask = instance()->getColorMask();
  578. luax_pushboolean(L, mask.r);
  579. luax_pushboolean(L, mask.g);
  580. luax_pushboolean(L, mask.b);
  581. luax_pushboolean(L, mask.a);
  582. return 4;
  583. }
  584. int w_setBlendMode(lua_State *L)
  585. {
  586. Graphics::BlendMode mode;
  587. const char *str = luaL_checkstring(L, 1);
  588. if (!Graphics::getConstant(str, mode))
  589. return luaL_error(L, "Invalid blend mode: %s", str);
  590. luax_catchexcept(L, [&](){ instance()->setBlendMode(mode); });
  591. return 0;
  592. }
  593. int w_getBlendMode(lua_State *L)
  594. {
  595. const char *str;
  596. Graphics::BlendMode mode;
  597. luax_catchexcept(L, [&](){ mode = instance()->getBlendMode(); });
  598. if (!Graphics::getConstant(mode, str))
  599. return luaL_error(L, "Unknown blend mode");
  600. lua_pushstring(L, str);
  601. return 1;
  602. }
  603. int w_setDefaultFilter(lua_State *L)
  604. {
  605. Texture::Filter f;
  606. const char *minstr = luaL_checkstring(L, 1);
  607. const char *magstr = luaL_optstring(L, 2, minstr);
  608. if (!Texture::getConstant(minstr, f.min))
  609. return luaL_error(L, "Invalid filter mode: %s", minstr);
  610. if (!Texture::getConstant(magstr, f.mag))
  611. return luaL_error(L, "Invalid filter mode: %s", magstr);
  612. f.anisotropy = (float) luaL_optnumber(L, 3, 1.0);
  613. instance()->setDefaultFilter(f);
  614. return 0;
  615. }
  616. int w_getDefaultFilter(lua_State *L)
  617. {
  618. const Texture::Filter &f = instance()->getDefaultFilter();
  619. const char *minstr;
  620. const char *magstr;
  621. if (!Texture::getConstant(f.min, minstr))
  622. return luaL_error(L, "Unknown minification filter mode");
  623. if (!Texture::getConstant(f.mag, magstr))
  624. return luaL_error(L, "Unknown magnification filter mode");
  625. lua_pushstring(L, minstr);
  626. lua_pushstring(L, magstr);
  627. lua_pushnumber(L, f.anisotropy);
  628. return 3;
  629. }
  630. int w_setDefaultMipmapFilter(lua_State *L)
  631. {
  632. Texture::FilterMode filter = Texture::FILTER_NONE;
  633. if (!lua_isnoneornil(L, 1))
  634. {
  635. const char *str = luaL_checkstring(L, 1);
  636. if (!Texture::getConstant(str, filter))
  637. return luaL_error(L, "Invalid filter mode: %s", str);
  638. }
  639. float sharpness = (float) luaL_optnumber(L, 2, 0);
  640. instance()->setDefaultMipmapFilter(filter, sharpness);
  641. return 0;
  642. }
  643. int w_getDefaultMipmapFilter(lua_State *L)
  644. {
  645. Texture::FilterMode filter;
  646. float sharpness;
  647. instance()->getDefaultMipmapFilter(&filter, &sharpness);
  648. const char *str;
  649. if (Texture::getConstant(filter, str))
  650. lua_pushstring(L, str);
  651. else
  652. lua_pushnil(L);
  653. lua_pushnumber(L, sharpness);
  654. return 2;
  655. }
  656. int w_setLineWidth(lua_State *L)
  657. {
  658. float width = (float)luaL_checknumber(L, 1);
  659. instance()->setLineWidth(width);
  660. return 0;
  661. }
  662. int w_setLineStyle(lua_State *L)
  663. {
  664. Graphics::LineStyle style;
  665. const char *str = luaL_checkstring(L, 1);
  666. if (!Graphics::getConstant(str, style))
  667. return luaL_error(L, "Invalid line style: %s", str);
  668. instance()->setLineStyle(style);
  669. return 0;
  670. }
  671. int w_setLineJoin(lua_State *L)
  672. {
  673. Graphics::LineJoin join;
  674. const char *str = luaL_checkstring(L, 1);
  675. if (!Graphics::getConstant(str, join))
  676. return luaL_error(L, "Invalid line join mode: %s", str);
  677. instance()->setLineJoin(join);
  678. return 0;
  679. }
  680. int w_getLineWidth(lua_State *L)
  681. {
  682. lua_pushnumber(L, instance()->getLineWidth());
  683. return 1;
  684. }
  685. int w_getLineStyle(lua_State *L)
  686. {
  687. Graphics::LineStyle style = instance()->getLineStyle();
  688. const char *str;
  689. if (!Graphics::getConstant(style, str))
  690. return luaL_error(L, "Unknown line style");
  691. lua_pushstring(L, str);
  692. return 1;
  693. }
  694. int w_getLineJoin(lua_State *L)
  695. {
  696. Graphics::LineJoin join = instance()->getLineJoin();
  697. const char *str;
  698. if (!Graphics::getConstant(join, str))
  699. return luaL_error(L, "Unknown line join");
  700. lua_pushstring(L, str);
  701. return 1;
  702. }
  703. int w_setPointSize(lua_State *L)
  704. {
  705. float size = (float)luaL_checknumber(L, 1);
  706. instance()->setPointSize(size);
  707. return 0;
  708. }
  709. int w_getPointSize(lua_State *L)
  710. {
  711. lua_pushnumber(L, instance()->getPointSize());
  712. return 1;
  713. }
  714. int w_setWireframe(lua_State *L)
  715. {
  716. instance()->setWireframe(luax_toboolean(L, 1));
  717. return 0;
  718. }
  719. int w_isWireframe(lua_State *L)
  720. {
  721. luax_pushboolean(L, instance()->isWireframe());
  722. return 1;
  723. }
  724. int w_newScreenshot(lua_State *L)
  725. {
  726. love::image::Image *image = luax_getmodule<love::image::Image>(L, "image", MODULE_IMAGE_T);
  727. bool copyAlpha = luax_optboolean(L, 1, false);
  728. love::image::ImageData *i = 0;
  729. luax_catchexcept(L, [&](){ i = instance()->newScreenshot(image, copyAlpha); });
  730. luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, i);
  731. i->release();
  732. return 1;
  733. }
  734. int w_setCanvas(lua_State *L)
  735. {
  736. // Disable stencil writes.
  737. instance()->drawToStencilBuffer(false);
  738. // called with none -> reset to default buffer
  739. if (lua_isnoneornil(L, 1))
  740. {
  741. instance()->setCanvas();
  742. return 0;
  743. }
  744. bool is_table = lua_istable(L, 1);
  745. std::vector<Canvas *> canvases;
  746. if (is_table)
  747. {
  748. for (size_t i = 1; i <= lua_objlen(L, 1); i++)
  749. {
  750. lua_rawgeti(L, 1, i);
  751. canvases.push_back(luax_checkcanvas(L, -1));
  752. lua_pop(L, 1);
  753. }
  754. }
  755. else
  756. {
  757. for (int i = 1; i <= lua_gettop(L); i++)
  758. canvases.push_back(luax_checkcanvas(L, i));
  759. }
  760. luax_catchexcept(L, [&]() {
  761. if (canvases.size() > 0)
  762. instance()->setCanvas(canvases);
  763. else
  764. instance()->setCanvas();
  765. });
  766. return 0;
  767. }
  768. int w_getCanvas(lua_State *L)
  769. {
  770. const std::vector<Canvas *> canvases = instance()->getCanvas();
  771. int n = 0;
  772. for (Canvas *c : canvases)
  773. {
  774. luax_pushtype(L, "Canvas", GRAPHICS_CANVAS_T, c);
  775. n++;
  776. }
  777. if (n == 0)
  778. {
  779. lua_pushnil(L);
  780. n = 1;
  781. }
  782. return n;
  783. }
  784. int w_setShader(lua_State *L)
  785. {
  786. if (lua_isnoneornil(L,1))
  787. {
  788. instance()->setShader();
  789. return 0;
  790. }
  791. Shader *shader = luax_checkshader(L, 1);
  792. instance()->setShader(shader);
  793. return 0;
  794. }
  795. int w_getShader(lua_State *L)
  796. {
  797. Shader *shader = instance()->getShader();
  798. if (shader)
  799. luax_pushtype(L, "Shader", GRAPHICS_SHADER_T, shader);
  800. else
  801. lua_pushnil(L);
  802. return 1;
  803. }
  804. int w_setDefaultShaderCode(lua_State *L)
  805. {
  806. luaL_checktype(L, 1, LUA_TTABLE);
  807. lua_getfield(L, 1, "opengl");
  808. lua_rawgeti(L, -1, 1);
  809. lua_rawgeti(L, -2, 2);
  810. Shader::ShaderSource openglcode;
  811. openglcode.vertex = luax_checkstring(L, -2);
  812. openglcode.pixel = luax_checkstring(L, -1);
  813. lua_pop(L, 3);
  814. lua_getfield(L, 1, "opengles");
  815. lua_rawgeti(L, -1, 1);
  816. lua_rawgeti(L, -2, 2);
  817. Shader::ShaderSource openglescode;
  818. openglescode.vertex = luax_checkstring(L, -2);
  819. openglescode.pixel = luax_checkstring(L, -1);
  820. lua_pop(L, 3);
  821. Shader::defaultCode[Graphics::RENDERER_OPENGL] = openglcode;
  822. Shader::defaultCode[Graphics::RENDERER_OPENGLES] = openglescode;
  823. return 0;
  824. }
  825. int w_getSupported(lua_State *L)
  826. {
  827. lua_createtable(L, 0, (int) Graphics::SUPPORT_MAX_ENUM);
  828. for (int i = 0; i < (int) Graphics::SUPPORT_MAX_ENUM; i++)
  829. {
  830. Graphics::Support feature = (Graphics::Support) i;
  831. const char *name = nullptr;
  832. if (!Graphics::getConstant(feature, name))
  833. continue;
  834. luax_pushboolean(L, instance()->isSupported(feature));
  835. lua_setfield(L, -2, name);
  836. }
  837. return 1;
  838. }
  839. int w_getCanvasFormats(lua_State *L)
  840. {
  841. lua_createtable(L, 0, (int) Canvas::FORMAT_MAX_ENUM);
  842. for (int i = 0; i < (int) Canvas::FORMAT_MAX_ENUM; i++)
  843. {
  844. Canvas::Format format = (Canvas::Format) i;
  845. const char *name = nullptr;
  846. if (!Canvas::getConstant(format, name))
  847. continue;
  848. luax_pushboolean(L, Canvas::isFormatSupported(format));
  849. lua_setfield(L, -2, name);
  850. }
  851. return 1;
  852. }
  853. int w_getCompressedImageFormats(lua_State *L)
  854. {
  855. lua_createtable(L, 0, (int) image::CompressedData::FORMAT_MAX_ENUM);
  856. for (int i = 0; i < (int) image::CompressedData::FORMAT_MAX_ENUM; i++)
  857. {
  858. image::CompressedData::Format format = (image::CompressedData::Format) i;
  859. const char *name = nullptr;
  860. if (format == image::CompressedData::FORMAT_UNKNOWN)
  861. continue;
  862. if (!image::CompressedData::getConstant(format, name))
  863. continue;
  864. luax_pushboolean(L, Image::hasCompressedTextureSupport(format, false));
  865. lua_setfield(L, -2, name);
  866. }
  867. return 1;
  868. }
  869. int w_getRendererInfo(lua_State *L)
  870. {
  871. Graphics::RendererInfo info;
  872. luax_catchexcept(L, [&](){ info = instance()->getRendererInfo(); });
  873. luax_pushstring(L, info.name);
  874. luax_pushstring(L, info.version);
  875. luax_pushstring(L, info.vendor);
  876. luax_pushstring(L, info.device);
  877. return 4;
  878. }
  879. int w_getSystemLimits(lua_State *L)
  880. {
  881. lua_createtable(L, 0, (int) Graphics::LIMIT_MAX_ENUM);
  882. for (int i = 0; i < (int) Graphics::LIMIT_MAX_ENUM; i++)
  883. {
  884. Graphics::SystemLimit limittype = (Graphics::SystemLimit) i;
  885. const char *name = nullptr;
  886. if (!Graphics::getConstant(limittype, name))
  887. continue;
  888. lua_pushnumber(L, instance()->getSystemLimit(limittype));
  889. lua_setfield(L, -2, name);
  890. }
  891. return 1;
  892. }
  893. int w_getStats(lua_State *L)
  894. {
  895. Graphics::Stats stats = instance()->getStats();
  896. lua_createtable(L, 0, (int) Graphics::STAT_MAX_ENUM);
  897. const char *sname = nullptr;
  898. Graphics::getConstant(Graphics::STAT_DRAW_CALLS, sname);
  899. lua_pushinteger(L, stats.drawCalls);
  900. lua_setfield(L, -2, sname);
  901. Graphics::getConstant(Graphics::STAT_CANVAS_SWITCHES, sname);
  902. lua_pushinteger(L, stats.canvasSwitches);
  903. lua_setfield(L, -2, sname);
  904. Graphics::getConstant(Graphics::STAT_CANVASES, sname);
  905. lua_pushinteger(L, stats.canvases);
  906. lua_setfield(L, -2, sname);
  907. Graphics::getConstant(Graphics::STAT_IMAGES, sname);
  908. lua_pushinteger(L, stats.images);
  909. lua_setfield(L, -2, sname);
  910. Graphics::getConstant(Graphics::STAT_FONTS, sname);
  911. lua_pushinteger(L, stats.fonts);
  912. lua_setfield(L, -2, sname);
  913. Graphics::getConstant(Graphics::STAT_TEXTURE_MEMORY, sname);
  914. lua_pushnumber(L, (lua_Number) stats.textureMemory);
  915. lua_setfield(L, -2, sname);
  916. return 1;
  917. }
  918. int w_draw(lua_State *L)
  919. {
  920. Drawable *drawable = nullptr;
  921. Texture *texture = nullptr;
  922. Quad *quad = nullptr;
  923. int startidx = 2;
  924. if (luax_istype(L, 2, GRAPHICS_QUAD_T))
  925. {
  926. texture = luax_checktexture(L, 1);
  927. quad = luax_totype<Quad>(L, 2, "Quad", GRAPHICS_QUAD_T);
  928. startidx = 3;
  929. }
  930. else if (lua_isnil(L, 2) && !lua_isnoneornil(L, 3))
  931. {
  932. return luax_typerror(L, 2, "Quad");
  933. }
  934. else
  935. {
  936. drawable = luax_checktype<Drawable>(L, 1, "Drawable", GRAPHICS_DRAWABLE_T);
  937. startidx = 2;
  938. }
  939. float x = (float) luaL_optnumber(L, startidx + 0, 0.0);
  940. float y = (float) luaL_optnumber(L, startidx + 1, 0.0);
  941. float a = (float) luaL_optnumber(L, startidx + 2, 0.0);
  942. float sx = (float) luaL_optnumber(L, startidx + 3, 1.0);
  943. float sy = (float) luaL_optnumber(L, startidx + 4, sx);
  944. float ox = (float) luaL_optnumber(L, startidx + 5, 0.0);
  945. float oy = (float) luaL_optnumber(L, startidx + 6, 0.0);
  946. float kx = (float) luaL_optnumber(L, startidx + 7, 0.0);
  947. float ky = (float) luaL_optnumber(L, startidx + 8, 0.0);
  948. if (texture && quad)
  949. texture->drawq(quad, x, y, a, sx, sy, ox, oy, kx, ky);
  950. else if (drawable)
  951. drawable->draw(x, y, a, sx, sy, ox, oy, kx, ky);
  952. return 0;
  953. }
  954. int w_print(lua_State *L)
  955. {
  956. std::string str = luax_checkstring(L, 1);
  957. float x = (float)luaL_optnumber(L, 2, 0.0);
  958. float y = (float)luaL_optnumber(L, 3, 0.0);
  959. float angle = (float)luaL_optnumber(L, 4, 0.0f);
  960. float sx = (float)luaL_optnumber(L, 5, 1.0f);
  961. float sy = (float)luaL_optnumber(L, 6, sx);
  962. float ox = (float)luaL_optnumber(L, 7, 0.0f);
  963. float oy = (float)luaL_optnumber(L, 8, 0.0f);
  964. float kx = (float)luaL_optnumber(L, 9, 0.0f);
  965. float ky = (float)luaL_optnumber(L, 10, 0.0f);
  966. luax_catchexcept(L,
  967. [&](){ instance()->print(str, x, y, angle, sx, sy, ox, oy, kx,ky); }
  968. );
  969. return 0;
  970. }
  971. int w_printf(lua_State *L)
  972. {
  973. std::string str = luax_checkstring(L, 1);
  974. float x = (float)luaL_checknumber(L, 2);
  975. float y = (float)luaL_checknumber(L, 3);
  976. float wrap = (float)luaL_checknumber(L, 4);
  977. float angle = 0.0f;
  978. float sx = 1.0f, sy = 1.0f;
  979. float ox = 0.0f, oy = 0.0f;
  980. float kx = 0.0f, ky = 0.0f;
  981. Font::AlignMode align = Font::ALIGN_LEFT;
  982. if (lua_gettop(L) >= 5)
  983. {
  984. if (!lua_isnil(L, 5))
  985. {
  986. const char *str = luaL_checkstring(L, 5);
  987. if (!Font::getConstant(str, align))
  988. return luaL_error(L, "Incorrect alignment: %s", str);
  989. }
  990. angle = (float) luaL_optnumber(L, 6, 0.0f);
  991. sx = (float) luaL_optnumber(L, 7, 1.0f);
  992. sy = (float) luaL_optnumber(L, 8, sx);
  993. ox = (float) luaL_optnumber(L, 9, 0.0f);
  994. oy = (float) luaL_optnumber(L, 10, 0.0f);
  995. kx = (float) luaL_optnumber(L, 11, 0.0f);
  996. ky = (float) luaL_optnumber(L, 12, 0.0f);
  997. }
  998. luax_catchexcept(L,
  999. [&](){ instance()->printf(str, x, y, wrap, align, angle, sx, sy, ox, oy, kx, ky); }
  1000. );
  1001. return 0;
  1002. }
  1003. int w_point(lua_State *L)
  1004. {
  1005. float x = (float)luaL_checknumber(L, 1);
  1006. float y = (float)luaL_checknumber(L, 2);
  1007. instance()->point(x, y);
  1008. return 0;
  1009. }
  1010. int w_line(lua_State *L)
  1011. {
  1012. int args = lua_gettop(L);
  1013. bool is_table = false;
  1014. if (args == 1 && lua_istable(L, 1))
  1015. {
  1016. args = lua_objlen(L, 1);
  1017. is_table = true;
  1018. }
  1019. if (args % 2 != 0)
  1020. return luaL_error(L, "Number of vertex components must be a multiple of two");
  1021. else if (args < 4)
  1022. return luaL_error(L, "Need at least two vertices to draw a line");
  1023. float *coords = new float[args];
  1024. if (is_table)
  1025. {
  1026. for (int i = 0; i < args; ++i)
  1027. {
  1028. lua_rawgeti(L, 1, i + 1);
  1029. coords[i] = luax_tofloat(L, -1);
  1030. lua_pop(L, 1);
  1031. }
  1032. }
  1033. else
  1034. {
  1035. for (int i = 0; i < args; ++i)
  1036. coords[i] = luax_tofloat(L, i + 1);
  1037. }
  1038. instance()->polyline(coords, args);
  1039. delete[] coords;
  1040. return 0;
  1041. }
  1042. int w_rectangle(lua_State *L)
  1043. {
  1044. Graphics::DrawMode mode;
  1045. const char *str = luaL_checkstring(L, 1);
  1046. if (!Graphics::getConstant(str, mode))
  1047. return luaL_error(L, "Incorrect draw mode %s", str);
  1048. float x = (float)luaL_checknumber(L, 2);
  1049. float y = (float)luaL_checknumber(L, 3);
  1050. float w = (float)luaL_checknumber(L, 4);
  1051. float h = (float)luaL_checknumber(L, 5);
  1052. instance()->rectangle(mode, x, y, w, h);
  1053. return 0;
  1054. }
  1055. int w_circle(lua_State *L)
  1056. {
  1057. Graphics::DrawMode mode;
  1058. const char *str = luaL_checkstring(L, 1);
  1059. if (!Graphics::getConstant(str, mode))
  1060. return luaL_error(L, "Incorrect draw mode %s", str);
  1061. float x = (float)luaL_checknumber(L, 2);
  1062. float y = (float)luaL_checknumber(L, 3);
  1063. float radius = (float)luaL_checknumber(L, 4);
  1064. int points;
  1065. if (lua_isnoneornil(L, 5))
  1066. points = radius > 10 ? (int)(radius) : 10;
  1067. else
  1068. points = luaL_checkint(L, 5);
  1069. instance()->circle(mode, x, y, radius, points);
  1070. return 0;
  1071. }
  1072. int w_arc(lua_State *L)
  1073. {
  1074. Graphics::DrawMode mode;
  1075. const char *str = luaL_checkstring(L, 1);
  1076. if (!Graphics::getConstant(str, mode))
  1077. return luaL_error(L, "Incorrect draw mode %s", str);
  1078. float x = (float)luaL_checknumber(L, 2);
  1079. float y = (float)luaL_checknumber(L, 3);
  1080. float radius = (float)luaL_checknumber(L, 4);
  1081. float angle1 = (float)luaL_checknumber(L, 5);
  1082. float angle2 = (float)luaL_checknumber(L, 6);
  1083. int points;
  1084. if (lua_isnoneornil(L, 7))
  1085. points = radius > 10 ? (int)(radius) : 10;
  1086. else
  1087. points = luaL_checkint(L, 7);
  1088. instance()->arc(mode, x, y, radius, angle1, angle2, points);
  1089. return 0;
  1090. }
  1091. int w_polygon(lua_State *L)
  1092. {
  1093. int args = lua_gettop(L) - 1;
  1094. Graphics::DrawMode mode;
  1095. const char *str = luaL_checkstring(L, 1);
  1096. if (!Graphics::getConstant(str, mode))
  1097. return luaL_error(L, "Invalid draw mode: %s", str);
  1098. bool is_table = false;
  1099. float *coords;
  1100. if (args == 1 && lua_istable(L, 2))
  1101. {
  1102. args = lua_objlen(L, 2);
  1103. is_table = true;
  1104. }
  1105. if (args % 2 != 0)
  1106. return luaL_error(L, "Number of vertex components must be a multiple of two");
  1107. else if (args < 6)
  1108. return luaL_error(L, "Need at least three vertices to draw a polygon");
  1109. // fetch coords
  1110. coords = new float[args + 2];
  1111. if (is_table)
  1112. {
  1113. for (int i = 0; i < args; ++i)
  1114. {
  1115. lua_rawgeti(L, 2, i + 1);
  1116. coords[i] = luax_tofloat(L, -1);
  1117. lua_pop(L, 1);
  1118. }
  1119. }
  1120. else
  1121. {
  1122. for (int i = 0; i < args; ++i)
  1123. coords[i] = luax_tofloat(L, i + 2);
  1124. }
  1125. // make a closed loop
  1126. coords[args] = coords[0];
  1127. coords[args+1] = coords[1];
  1128. instance()->polygon(mode, coords, args+2);
  1129. delete[] coords;
  1130. return 0;
  1131. }
  1132. int w_push(lua_State *L)
  1133. {
  1134. Graphics::StackType stype = Graphics::STACK_TRANSFORM;
  1135. const char *sname = lua_isnoneornil(L, 1) ? nullptr : luaL_checkstring(L, 1);
  1136. if (sname && !Graphics::getConstant(sname, stype))
  1137. return luaL_error(L, "Invalid graphics stack type: %s", sname);
  1138. luax_catchexcept(L, [&](){ instance()->push(stype); });
  1139. return 0;
  1140. }
  1141. int w_pop(lua_State *L)
  1142. {
  1143. luax_catchexcept(L, [&](){ instance()->pop(); });
  1144. return 0;
  1145. }
  1146. int w_rotate(lua_State *L)
  1147. {
  1148. float angle = (float)luaL_checknumber(L, 1);
  1149. instance()->rotate(angle);
  1150. return 0;
  1151. }
  1152. int w_scale(lua_State *L)
  1153. {
  1154. float sx = (float)luaL_optnumber(L, 1, 1.0f);
  1155. float sy = (float)luaL_optnumber(L, 2, sx);
  1156. instance()->scale(sx, sy);
  1157. return 0;
  1158. }
  1159. int w_translate(lua_State *L)
  1160. {
  1161. float x = (float)luaL_checknumber(L, 1);
  1162. float y = (float)luaL_checknumber(L, 2);
  1163. instance()->translate(x, y);
  1164. return 0;
  1165. }
  1166. int w_shear(lua_State *L)
  1167. {
  1168. float kx = (float)luaL_checknumber(L, 1);
  1169. float ky = (float)luaL_checknumber(L, 2);
  1170. instance()->shear(kx, ky);
  1171. return 0;
  1172. }
  1173. int w_origin(lua_State * /*L*/)
  1174. {
  1175. instance()->origin();
  1176. return 0;
  1177. }
  1178. // List of functions to wrap.
  1179. static const luaL_Reg functions[] =
  1180. {
  1181. { "reset", w_reset },
  1182. { "clear", w_clear },
  1183. { "present", w_present },
  1184. { "newImage", w_newImage },
  1185. { "newQuad", w_newQuad },
  1186. { "newFont", w_newFont },
  1187. { "newImageFont", w_newImageFont },
  1188. { "newSpriteBatch", w_newSpriteBatch },
  1189. { "newParticleSystem", w_newParticleSystem },
  1190. { "newCanvas", w_newCanvas },
  1191. { "newShader", w_newShader },
  1192. { "newMesh", w_newMesh },
  1193. { "newText", w_newText },
  1194. { "setColor", w_setColor },
  1195. { "getColor", w_getColor },
  1196. { "setBackgroundColor", w_setBackgroundColor },
  1197. { "getBackgroundColor", w_getBackgroundColor },
  1198. { "setFont", w_setFont },
  1199. { "getFont", w_getFont },
  1200. { "setColorMask", w_setColorMask },
  1201. { "getColorMask", w_getColorMask },
  1202. { "setBlendMode", w_setBlendMode },
  1203. { "getBlendMode", w_getBlendMode },
  1204. { "setDefaultFilter", w_setDefaultFilter },
  1205. { "getDefaultFilter", w_getDefaultFilter },
  1206. { "setDefaultMipmapFilter", w_setDefaultMipmapFilter },
  1207. { "getDefaultMipmapFilter", w_getDefaultMipmapFilter },
  1208. { "setLineWidth", w_setLineWidth },
  1209. { "setLineStyle", w_setLineStyle },
  1210. { "setLineJoin", w_setLineJoin },
  1211. { "getLineWidth", w_getLineWidth },
  1212. { "getLineStyle", w_getLineStyle },
  1213. { "getLineJoin", w_getLineJoin },
  1214. { "setPointSize", w_setPointSize },
  1215. { "getPointSize", w_getPointSize },
  1216. { "setWireframe", w_setWireframe },
  1217. { "isWireframe", w_isWireframe },
  1218. { "newScreenshot", w_newScreenshot },
  1219. { "setCanvas", w_setCanvas },
  1220. { "getCanvas", w_getCanvas },
  1221. { "setShader", w_setShader },
  1222. { "getShader", w_getShader },
  1223. { "_setDefaultShaderCode", w_setDefaultShaderCode },
  1224. { "getSupported", w_getSupported },
  1225. { "getCanvasFormats", w_getCanvasFormats },
  1226. { "getCompressedImageFormats", w_getCompressedImageFormats },
  1227. { "getRendererInfo", w_getRendererInfo },
  1228. { "getSystemLimits", w_getSystemLimits },
  1229. { "getStats", w_getStats },
  1230. { "draw", w_draw },
  1231. { "print", w_print },
  1232. { "printf", w_printf },
  1233. { "isCreated", w_isCreated },
  1234. { "getWidth", w_getWidth },
  1235. { "getHeight", w_getHeight },
  1236. { "getDimensions", w_getDimensions },
  1237. { "setScissor", w_setScissor },
  1238. { "getScissor", w_getScissor },
  1239. { "stencil", w_stencil },
  1240. { "setStencilTest", w_setStencilTest },
  1241. { "getStencilTest", w_getStencilTest },
  1242. { "point", w_point },
  1243. { "line", w_line },
  1244. { "rectangle", w_rectangle },
  1245. { "circle", w_circle },
  1246. { "arc", w_arc },
  1247. { "polygon", w_polygon },
  1248. { "push", w_push },
  1249. { "pop", w_pop },
  1250. { "rotate", w_rotate },
  1251. { "scale", w_scale },
  1252. { "translate", w_translate },
  1253. { "shear", w_shear },
  1254. { "origin", w_origin },
  1255. { 0, 0 }
  1256. };
  1257. // Types for this module.
  1258. static const lua_CFunction types[] =
  1259. {
  1260. luaopen_font,
  1261. luaopen_image,
  1262. luaopen_quad,
  1263. luaopen_spritebatch,
  1264. luaopen_particlesystem,
  1265. luaopen_canvas,
  1266. luaopen_shader,
  1267. luaopen_mesh,
  1268. luaopen_text,
  1269. 0
  1270. };
  1271. extern "C" int luaopen_love_graphics(lua_State *L)
  1272. {
  1273. Graphics *instance = instance();
  1274. if (instance == nullptr)
  1275. {
  1276. luax_catchexcept(L, [&](){ instance = new Graphics(); });
  1277. }
  1278. else
  1279. instance->retain();
  1280. WrappedModule w;
  1281. w.module = instance;
  1282. w.name = "graphics";
  1283. w.flags = MODULE_GRAPHICS_T;
  1284. w.functions = functions;
  1285. w.types = types;
  1286. int n = luax_register_module(L, w);
  1287. if (luaL_loadbuffer(L, (const char *)graphics_lua, sizeof(graphics_lua), "graphics.lua") == 0)
  1288. lua_call(L, 0, 0);
  1289. return n;
  1290. }
  1291. } // opengl
  1292. } // graphics
  1293. } // love