wrap_Graphics.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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_isSupported(lua_State *L)
  770. {
  771. bool supported = true;
  772. size_t len = lua_gettop(L);
  773. Graphics::Support support;
  774. for (unsigned int i = 1; i <= len; i++)
  775. {
  776. const char *str = luaL_checkstring(L, i);
  777. if (!Graphics::getConstant(str, support))
  778. return luaL_error(L, "Invalid graphics feature: %s", str);
  779. switch (support)
  780. {
  781. case Graphics::SUPPORT_CANVAS:
  782. if (!Canvas::isSupported())
  783. supported = false;
  784. break;
  785. case Graphics::SUPPORT_HDR_CANVAS:
  786. if (!Canvas::isHDRSupported())
  787. supported = false;
  788. break;
  789. case Graphics::SUPPORT_MULTI_CANVAS:
  790. if (!Canvas::isMultiCanvasSupported())
  791. supported = false;
  792. break;
  793. case Graphics::SUPPORT_SHADER:
  794. if (!Shader::isSupported())
  795. supported = false;
  796. break;
  797. case Graphics::SUPPORT_NPOT:
  798. if (!Image::hasNpot())
  799. supported = false;
  800. break;
  801. case Graphics::SUPPORT_SUBTRACTIVE:
  802. if (!((GLEE_VERSION_1_4 || GLEE_ARB_imaging) || (GLEE_EXT_blend_minmax && GLEE_EXT_blend_subtract)))
  803. supported = false;
  804. break;
  805. case Graphics::SUPPORT_MIPMAP:
  806. if (!Image::hasMipmapSupport())
  807. supported = false;
  808. break;
  809. case Graphics::SUPPORT_DXT:
  810. if (!Image::hasCompressedTextureSupport(image::CompressedData::FORMAT_DXT5))
  811. supported = false;
  812. break;
  813. case Graphics::SUPPORT_BC5:
  814. if (!Image::hasCompressedTextureSupport(image::CompressedData::FORMAT_BC5))
  815. supported = false;
  816. break;
  817. case Graphics::SUPPORT_INSTANCING:
  818. if (!GLEE_ARB_draw_instanced)
  819. supported = false;
  820. break;
  821. default:
  822. supported = false;
  823. }
  824. if (!supported)
  825. break;
  826. }
  827. lua_pushboolean(L, supported);
  828. return 1;
  829. }
  830. int w_getRendererInfo(lua_State *L)
  831. {
  832. std::string name, version, vendor, device;
  833. EXCEPT_GUARD(name = instance->getRendererInfo(Graphics::RENDERER_INFO_NAME);)
  834. EXCEPT_GUARD(version = instance->getRendererInfo(Graphics::RENDERER_INFO_VERSION);)
  835. EXCEPT_GUARD(vendor = instance->getRendererInfo(Graphics::RENDERER_INFO_VENDOR);)
  836. EXCEPT_GUARD(device = instance->getRendererInfo(Graphics::RENDERER_INFO_DEVICE);)
  837. luax_pushstring(L, name);
  838. luax_pushstring(L, version);
  839. luax_pushstring(L, vendor);
  840. luax_pushstring(L, device);
  841. return 4;
  842. }
  843. int w_draw(lua_State *L)
  844. {
  845. Drawable *drawable = nullptr;
  846. Texture *texture = nullptr;
  847. Quad *quad = nullptr;
  848. int startidx = 2;
  849. if (luax_istype(L, 2, GRAPHICS_QUAD_T))
  850. {
  851. texture = luax_checktexture(L, 1);
  852. quad = luax_totype<Quad>(L, 2, "Quad", GRAPHICS_QUAD_T);
  853. startidx = 3;
  854. }
  855. else if (lua_isnil(L, 2) && !lua_isnoneornil(L, 3))
  856. {
  857. return luax_typerror(L, 2, "Quad");
  858. }
  859. else
  860. {
  861. drawable = luax_checktype<Drawable>(L, 1, "Drawable", GRAPHICS_DRAWABLE_T);
  862. startidx = 2;
  863. }
  864. float x = (float) luaL_optnumber(L, startidx + 0, 0.0);
  865. float y = (float) luaL_optnumber(L, startidx + 1, 0.0);
  866. float a = (float) luaL_optnumber(L, startidx + 2, 0.0);
  867. float sx = (float) luaL_optnumber(L, startidx + 3, 1.0);
  868. float sy = (float) luaL_optnumber(L, startidx + 4, sx);
  869. float ox = (float) luaL_optnumber(L, startidx + 5, 0.0);
  870. float oy = (float) luaL_optnumber(L, startidx + 6, 0.0);
  871. float kx = (float) luaL_optnumber(L, startidx + 7, 0.0);
  872. float ky = (float) luaL_optnumber(L, startidx + 8, 0.0);
  873. if (texture && quad)
  874. texture->drawq(quad, x, y, a, sx, sy, ox, oy, kx, ky);
  875. else if (drawable)
  876. drawable->draw(x, y, a, sx, sy, ox, oy, kx, ky);
  877. return 0;
  878. }
  879. int w_print(lua_State *L)
  880. {
  881. std::string str = luax_checkstring(L, 1);
  882. float x = (float)luaL_optnumber(L, 2, 0.0);
  883. float y = (float)luaL_optnumber(L, 3, 0.0);
  884. float angle = (float)luaL_optnumber(L, 4, 0.0f);
  885. float sx = (float)luaL_optnumber(L, 5, 1.0f);
  886. float sy = (float)luaL_optnumber(L, 6, sx);
  887. float ox = (float)luaL_optnumber(L, 7, 0.0f);
  888. float oy = (float)luaL_optnumber(L, 8, 0.0f);
  889. float kx = (float)luaL_optnumber(L, 9, 0.0f);
  890. float ky = (float)luaL_optnumber(L, 10, 0.0f);
  891. EXCEPT_GUARD(instance->print(str, x, y, angle, sx, sy, ox, oy, kx,ky);)
  892. return 0;
  893. }
  894. int w_printf(lua_State *L)
  895. {
  896. std::string str = luax_checkstring(L, 1);
  897. float x = (float)luaL_checknumber(L, 2);
  898. float y = (float)luaL_checknumber(L, 3);
  899. float wrap = (float)luaL_checknumber(L, 4);
  900. float angle = 0.0f;
  901. float sx = 1.0f, sy = 1.0f;
  902. float ox = 0.0f, oy = 0.0f;
  903. float kx = 0.0f, ky = 0.0f;
  904. Graphics::AlignMode align = Graphics::ALIGN_LEFT;
  905. if (lua_gettop(L) >= 5)
  906. {
  907. if (!lua_isnil(L, 5))
  908. {
  909. const char *str = luaL_checkstring(L, 5);
  910. if (!Graphics::getConstant(str, align))
  911. return luaL_error(L, "Incorrect alignment: %s", str);
  912. }
  913. angle = (float) luaL_optnumber(L, 6, 0.0f);
  914. sx = (float) luaL_optnumber(L, 7, 1.0f);
  915. sy = (float) luaL_optnumber(L, 8, sx);
  916. ox = (float) luaL_optnumber(L, 9, 0.0f);
  917. oy = (float) luaL_optnumber(L, 10, 0.0f);
  918. kx = (float) luaL_optnumber(L, 11, 0.0f);
  919. ky = (float) luaL_optnumber(L, 12, 0.0f);
  920. }
  921. EXCEPT_GUARD(instance->printf(str, x, y, wrap, align, angle, sx, sy, ox, oy, kx, ky);)
  922. return 0;
  923. }
  924. int w_point(lua_State *L)
  925. {
  926. float x = (float)luaL_checknumber(L, 1);
  927. float y = (float)luaL_checknumber(L, 2);
  928. instance->point(x, y);
  929. return 0;
  930. }
  931. int w_line(lua_State *L)
  932. {
  933. int args = lua_gettop(L);
  934. bool is_table = false;
  935. if (args == 1 && lua_istable(L, 1))
  936. {
  937. args = lua_objlen(L, 1);
  938. is_table = true;
  939. }
  940. if (args % 2 != 0)
  941. return luaL_error(L, "Number of vertex components must be a multiple of two");
  942. else if (args < 4)
  943. return luaL_error(L, "Need at least two vertices to draw a line");
  944. float *coords = new float[args];
  945. if (is_table)
  946. {
  947. for (int i = 0; i < args; ++i)
  948. {
  949. lua_rawgeti(L, 1, i + 1);
  950. coords[i] = luax_tofloat(L, -1);
  951. lua_pop(L, 1);
  952. }
  953. }
  954. else
  955. {
  956. for (int i = 0; i < args; ++i)
  957. coords[i] = luax_tofloat(L, i + 1);
  958. }
  959. instance->polyline(coords, args);
  960. delete[] coords;
  961. return 0;
  962. }
  963. int w_rectangle(lua_State *L)
  964. {
  965. Graphics::DrawMode mode;
  966. const char *str = luaL_checkstring(L, 1);
  967. if (!Graphics::getConstant(str, mode))
  968. return luaL_error(L, "Incorrect draw mode %s", str);
  969. float x = (float)luaL_checknumber(L, 2);
  970. float y = (float)luaL_checknumber(L, 3);
  971. float w = (float)luaL_checknumber(L, 4);
  972. float h = (float)luaL_checknumber(L, 5);
  973. instance->rectangle(mode, x, y, w, h);
  974. return 0;
  975. }
  976. int w_circle(lua_State *L)
  977. {
  978. Graphics::DrawMode mode;
  979. const char *str = luaL_checkstring(L, 1);
  980. if (!Graphics::getConstant(str, mode))
  981. return luaL_error(L, "Incorrect draw mode %s", str);
  982. float x = (float)luaL_checknumber(L, 2);
  983. float y = (float)luaL_checknumber(L, 3);
  984. float radius = (float)luaL_checknumber(L, 4);
  985. int points;
  986. if (lua_isnoneornil(L, 5))
  987. points = radius > 10 ? (int)(radius) : 10;
  988. else
  989. points = luaL_checkint(L, 5);
  990. instance->circle(mode, x, y, radius, points);
  991. return 0;
  992. }
  993. int w_arc(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 radius = (float)luaL_checknumber(L, 4);
  1002. float angle1 = (float)luaL_checknumber(L, 5);
  1003. float angle2 = (float)luaL_checknumber(L, 6);
  1004. int points;
  1005. if (lua_isnoneornil(L, 7))
  1006. points = radius > 10 ? (int)(radius) : 10;
  1007. else
  1008. points = luaL_checkint(L, 7);
  1009. instance->arc(mode, x, y, radius, angle1, angle2, points);
  1010. return 0;
  1011. }
  1012. int w_polygon(lua_State *L)
  1013. {
  1014. int args = lua_gettop(L) - 1;
  1015. Graphics::DrawMode mode;
  1016. const char *str = luaL_checkstring(L, 1);
  1017. if (!Graphics::getConstant(str, mode))
  1018. return luaL_error(L, "Invalid draw mode: %s", str);
  1019. bool is_table = false;
  1020. float *coords;
  1021. if (args == 1 && lua_istable(L, 2))
  1022. {
  1023. args = lua_objlen(L, 2);
  1024. is_table = true;
  1025. }
  1026. if (args % 2 != 0)
  1027. return luaL_error(L, "Number of vertex components must be a multiple of two");
  1028. else if (args < 6)
  1029. return luaL_error(L, "Need at least three vertices to draw a polygon");
  1030. // fetch coords
  1031. coords = new float[args + 2];
  1032. if (is_table)
  1033. {
  1034. for (int i = 0; i < args; ++i)
  1035. {
  1036. lua_rawgeti(L, 2, i + 1);
  1037. coords[i] = luax_tofloat(L, -1);
  1038. lua_pop(L, 1);
  1039. }
  1040. }
  1041. else
  1042. {
  1043. for (int i = 0; i < args; ++i)
  1044. coords[i] = luax_tofloat(L, i + 2);
  1045. }
  1046. // make a closed loop
  1047. coords[args] = coords[0];
  1048. coords[args+1] = coords[1];
  1049. instance->polygon(mode, coords, args+2);
  1050. delete[] coords;
  1051. return 0;
  1052. }
  1053. int w_push(lua_State *L)
  1054. {
  1055. EXCEPT_GUARD(instance->push();)
  1056. return 0;
  1057. }
  1058. int w_pop(lua_State *L)
  1059. {
  1060. EXCEPT_GUARD(instance->pop();)
  1061. return 0;
  1062. }
  1063. int w_rotate(lua_State *L)
  1064. {
  1065. float angle = (float)luaL_checknumber(L, 1);
  1066. instance->rotate(angle);
  1067. return 0;
  1068. }
  1069. int w_scale(lua_State *L)
  1070. {
  1071. float sx = (float)luaL_optnumber(L, 1, 1.0f);
  1072. float sy = (float)luaL_optnumber(L, 2, sx);
  1073. instance->scale(sx, sy);
  1074. return 0;
  1075. }
  1076. int w_translate(lua_State *L)
  1077. {
  1078. float x = (float)luaL_checknumber(L, 1);
  1079. float y = (float)luaL_checknumber(L, 2);
  1080. instance->translate(x, y);
  1081. return 0;
  1082. }
  1083. int w_shear(lua_State *L)
  1084. {
  1085. float kx = (float)luaL_checknumber(L, 1);
  1086. float ky = (float)luaL_checknumber(L, 2);
  1087. instance->shear(kx, ky);
  1088. return 0;
  1089. }
  1090. int w_origin(lua_State * /*L*/)
  1091. {
  1092. instance->origin();
  1093. return 0;
  1094. }
  1095. // List of functions to wrap.
  1096. static const luaL_Reg functions[] =
  1097. {
  1098. { "reset", w_reset },
  1099. { "clear", w_clear },
  1100. { "present", w_present },
  1101. { "newImage", w_newImage },
  1102. { "newQuad", w_newQuad },
  1103. { "newFont", w_newFont },
  1104. { "newImageFont", w_newImageFont },
  1105. { "newSpriteBatch", w_newSpriteBatch },
  1106. { "newParticleSystem", w_newParticleSystem },
  1107. { "newCanvas", w_newCanvas },
  1108. { "newShader", w_newShader },
  1109. { "newMesh", w_newMesh },
  1110. { "setColor", w_setColor },
  1111. { "getColor", w_getColor },
  1112. { "setBackgroundColor", w_setBackgroundColor },
  1113. { "getBackgroundColor", w_getBackgroundColor },
  1114. { "setFont", w_setFont },
  1115. { "getFont", w_getFont },
  1116. { "setColorMask", w_setColorMask },
  1117. { "getColorMask", w_getColorMask },
  1118. { "setBlendMode", w_setBlendMode },
  1119. { "getBlendMode", w_getBlendMode },
  1120. { "setDefaultFilter", w_setDefaultFilter },
  1121. { "getDefaultFilter", w_getDefaultFilter },
  1122. { "setDefaultMipmapFilter", w_setDefaultMipmapFilter },
  1123. { "getDefaultMipmapFilter", w_getDefaultMipmapFilter },
  1124. { "setLineWidth", w_setLineWidth },
  1125. { "setLineStyle", w_setLineStyle },
  1126. { "setLineJoin", w_setLineJoin },
  1127. { "getLineWidth", w_getLineWidth },
  1128. { "getLineStyle", w_getLineStyle },
  1129. { "getLineJoin", w_getLineJoin },
  1130. { "setPointSize", w_setPointSize },
  1131. { "setPointStyle", w_setPointStyle },
  1132. { "getPointSize", w_getPointSize },
  1133. { "getPointStyle", w_getPointStyle },
  1134. { "getMaxPointSize", w_getMaxPointSize },
  1135. { "getMaxTextureSize", w_getMaxTextureSize },
  1136. { "newScreenshot", w_newScreenshot },
  1137. { "setCanvas", w_setCanvas },
  1138. { "getCanvas", w_getCanvas },
  1139. { "setShader", w_setShader },
  1140. { "getShader", w_getShader },
  1141. { "isSupported", w_isSupported },
  1142. { "getRendererInfo", w_getRendererInfo },
  1143. { "draw", w_draw },
  1144. { "print", w_print },
  1145. { "printf", w_printf },
  1146. { "isCreated", w_isCreated },
  1147. { "getWidth", w_getWidth },
  1148. { "getHeight", w_getHeight },
  1149. { "getDimensions", w_getDimensions },
  1150. { "setScissor", w_setScissor },
  1151. { "getScissor", w_getScissor },
  1152. { "setStencil", w_setStencil },
  1153. { "setInvertedStencil", w_setInvertedStencil },
  1154. { "point", w_point },
  1155. { "line", w_line },
  1156. { "rectangle", w_rectangle },
  1157. { "circle", w_circle },
  1158. { "arc", w_arc },
  1159. { "polygon", w_polygon },
  1160. { "push", w_push },
  1161. { "pop", w_pop },
  1162. { "rotate", w_rotate },
  1163. { "scale", w_scale },
  1164. { "translate", w_translate },
  1165. { "shear", w_shear },
  1166. { "origin", w_origin },
  1167. // Deprecated since 0.9.1.
  1168. { "getMaxImageSize", w_getMaxTextureSize },
  1169. { 0, 0 }
  1170. };
  1171. // Types for this module.
  1172. static const lua_CFunction types[] =
  1173. {
  1174. luaopen_font,
  1175. luaopen_image,
  1176. luaopen_quad,
  1177. luaopen_spritebatch,
  1178. luaopen_particlesystem,
  1179. luaopen_canvas,
  1180. luaopen_shader,
  1181. luaopen_mesh,
  1182. 0
  1183. };
  1184. extern "C" int luaopen_love_graphics(lua_State *L)
  1185. {
  1186. if (instance == 0)
  1187. {
  1188. EXCEPT_GUARD(instance = new Graphics();)
  1189. }
  1190. else
  1191. instance->retain();
  1192. WrappedModule w;
  1193. w.module = instance;
  1194. w.name = "graphics";
  1195. w.flags = MODULE_GRAPHICS_T;
  1196. w.functions = functions;
  1197. w.types = types;
  1198. int n = luax_register_module(L, w);
  1199. if (luaL_loadbuffer(L, (const char *)graphics_lua, sizeof(graphics_lua), "graphics.lua") == 0)
  1200. lua_call(L, 0, 0);
  1201. return n;
  1202. }
  1203. } // opengl
  1204. } // graphics
  1205. } // love