wrap_Graphics.cpp 34 KB

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