wrap_Graphics.cpp 34 KB

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