wrap_Graphics.cpp 35 KB

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