wrap_Graphics.cpp 35 KB

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