wrap_Graphics.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  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_pushinteger(L, 1);
  498. lua_gettable(L, -2);
  499. c.r = (unsigned char)luaL_checkint(L, -1);
  500. lua_pop(L, 1);
  501. lua_pushinteger(L, 2);
  502. lua_gettable(L, -2);
  503. c.g = (unsigned char)luaL_checkint(L, -1);
  504. lua_pop(L, 1);
  505. lua_pushinteger(L, 3);
  506. lua_gettable(L, -2);
  507. c.b = (unsigned char)luaL_checkint(L, -1);
  508. lua_pop(L, 1);
  509. lua_pushinteger(L, 4);
  510. lua_gettable(L, -2);
  511. c.a = (unsigned char)luaL_optint(L, -1, 255);
  512. lua_pop(L, 1);
  513. }
  514. else
  515. {
  516. c.r = (unsigned char)luaL_checkint(L, 1);
  517. c.g = (unsigned char)luaL_checkint(L, 2);
  518. c.b = (unsigned char)luaL_checkint(L, 3);
  519. c.a = (unsigned char)luaL_optint(L, 4, 255);
  520. }
  521. instance->setColor(c);
  522. return 0;
  523. }
  524. int w_getColor(lua_State *L)
  525. {
  526. Color c = instance->getColor();
  527. lua_pushinteger(L, c.r);
  528. lua_pushinteger(L, c.g);
  529. lua_pushinteger(L, c.b);
  530. lua_pushinteger(L, c.a);
  531. return 4;
  532. }
  533. int w_setBackgroundColor(lua_State *L)
  534. {
  535. Color c;
  536. if (lua_istable(L, 1))
  537. {
  538. lua_pushinteger(L, 1);
  539. lua_gettable(L, -2);
  540. c.r = (unsigned char)luaL_checkint(L, -1);
  541. lua_pop(L, 1);
  542. lua_pushinteger(L, 2);
  543. lua_gettable(L, -2);
  544. c.g = (unsigned char)luaL_checkint(L, -1);
  545. lua_pop(L, 1);
  546. lua_pushinteger(L, 3);
  547. lua_gettable(L, -2);
  548. c.b = (unsigned char)luaL_checkint(L, -1);
  549. lua_pop(L, 1);
  550. lua_pushinteger(L, 4);
  551. lua_gettable(L, -2);
  552. c.a = (unsigned char)luaL_optint(L, -1, 255);
  553. lua_pop(L, 1);
  554. }
  555. else
  556. {
  557. c.r = (unsigned char)luaL_checkint(L, 1);
  558. c.g = (unsigned char)luaL_checkint(L, 2);
  559. c.b = (unsigned char)luaL_checkint(L, 3);
  560. c.a = (unsigned char)luaL_optint(L, 4, 255);
  561. }
  562. instance->setBackgroundColor(c);
  563. return 0;
  564. }
  565. int w_getBackgroundColor(lua_State *L)
  566. {
  567. Color c = instance->getBackgroundColor();
  568. lua_pushinteger(L, c.r);
  569. lua_pushinteger(L, c.g);
  570. lua_pushinteger(L, c.b);
  571. lua_pushinteger(L, c.a);
  572. return 4;
  573. }
  574. int w_setFont(lua_State *L)
  575. {
  576. Font *font = luax_checktype<Font>(L, 1, "Font", GRAPHICS_FONT_T);
  577. instance->setFont(font);
  578. return 0;
  579. }
  580. int w_getFont(lua_State *L)
  581. {
  582. Font *f = instance->getFont();
  583. if (f == 0)
  584. return 0;
  585. f->retain();
  586. luax_newtype(L, "Font", GRAPHICS_FONT_T, (void *)f);
  587. return 1;
  588. }
  589. int w_setBlendMode(lua_State *L)
  590. {
  591. Graphics::BlendMode mode;
  592. const char *str = luaL_checkstring(L, 1);
  593. if (!Graphics::getConstant(str, mode))
  594. return luaL_error(L, "Invalid blend mode: %s", str);
  595. try
  596. {
  597. instance->setBlendMode(mode);
  598. }
  599. catch(love::Exception &e)
  600. {
  601. return luaL_error(L, e.what());
  602. }
  603. return 0;
  604. }
  605. int w_getBlendMode(lua_State *L)
  606. {
  607. try
  608. {
  609. Graphics::BlendMode mode = instance->getBlendMode();
  610. const char *str;
  611. if (!Graphics::getConstant(mode, str))
  612. return luaL_error(L, "Unknown blend mode");
  613. lua_pushstring(L, str);
  614. return 1;
  615. }
  616. catch (love::Exception &e)
  617. {
  618. return luaL_error(L, "%s", e.what());
  619. }
  620. }
  621. int w_setDefaultFilter(lua_State *L)
  622. {
  623. Image::FilterMode min;
  624. Image::FilterMode mag;
  625. const char *minstr = luaL_checkstring(L, 1);
  626. const char *magstr = luaL_optstring(L, 2, minstr);
  627. if (!Image::getConstant(minstr, min))
  628. return luaL_error(L, "Invalid filter mode: %s", minstr);
  629. if (!Image::getConstant(magstr, mag))
  630. return luaL_error(L, "Invalid filter mode: %s", magstr);
  631. float anisotropy = (float) luaL_optnumber(L, 3, 1.0);
  632. Image::Filter f;
  633. f.min = min;
  634. f.mag = mag;
  635. f.anisotropy = anisotropy;
  636. instance->setDefaultFilter(f);
  637. return 0;
  638. }
  639. int w_getDefaultFilter(lua_State *L)
  640. {
  641. const Image::Filter &f = instance->getDefaultFilter();
  642. const char *minstr;
  643. const char *magstr;
  644. if (!Image::getConstant(f.min, minstr))
  645. return luaL_error(L, "Unknown minification filter mode");
  646. if (!Image::getConstant(f.mag, magstr))
  647. return luaL_error(L, "Unknown magnification filter mode");
  648. lua_pushstring(L, minstr);
  649. lua_pushstring(L, magstr);
  650. lua_pushnumber(L, f.anisotropy);
  651. return 3;
  652. }
  653. int w_setDefaultMipmapFilter(lua_State *L)
  654. {
  655. Image::FilterMode filter = Image::FILTER_NONE;
  656. if (!lua_isnoneornil(L, 1))
  657. {
  658. const char *str = luaL_checkstring(L, 1);
  659. if (!Image::getConstant(str, filter))
  660. return luaL_error(L, "Invalid filter mode: %s", str);
  661. }
  662. float sharpness = luaL_optnumber(L, 2, 0);
  663. instance->setDefaultMipmapFilter(filter, sharpness);
  664. return 0;
  665. }
  666. int w_getDefaultMipmapFilter(lua_State *L)
  667. {
  668. Image::FilterMode filter;
  669. float sharpness;
  670. instance->getDefaultMipmapFilter(&filter, &sharpness);
  671. const char *str;
  672. if (Image::getConstant(filter, str))
  673. lua_pushstring(L, str);
  674. else
  675. lua_pushnil(L);
  676. lua_pushnumber(L, sharpness);
  677. return 2;
  678. }
  679. int w_setLineWidth(lua_State *L)
  680. {
  681. float width = (float)luaL_checknumber(L, 1);
  682. instance->setLineWidth(width);
  683. return 0;
  684. }
  685. int w_setLineStyle(lua_State *L)
  686. {
  687. Graphics::LineStyle style;
  688. const char *str = luaL_checkstring(L, 1);
  689. if (!Graphics::getConstant(str, style))
  690. return luaL_error(L, "Invalid line style: %s", str);
  691. instance->setLineStyle(style);
  692. return 0;
  693. }
  694. int w_setLine(lua_State *L)
  695. {
  696. float width = (float)luaL_checknumber(L, 1);
  697. Graphics::LineStyle style = Graphics::LINE_SMOOTH;
  698. if (lua_gettop(L) >= 2)
  699. {
  700. const char *str = luaL_checkstring(L, 2);
  701. if (!Graphics::getConstant(str, style))
  702. return luaL_error(L, "Invalid line style: %s", str);
  703. }
  704. instance->setLine(width, style);
  705. return 0;
  706. }
  707. int w_getLineWidth(lua_State *L)
  708. {
  709. lua_pushnumber(L, instance->getLineWidth());
  710. return 1;
  711. }
  712. int w_getLineStyle(lua_State *L)
  713. {
  714. Graphics::LineStyle style = instance->getLineStyle();
  715. const char *str;
  716. if (!Graphics::getConstant(style, str))
  717. return luaL_error(L, "Unknown line style");
  718. lua_pushstring(L, str);
  719. return 1;
  720. }
  721. int w_setPointSize(lua_State *L)
  722. {
  723. float size = (float)luaL_checknumber(L, 1);
  724. instance->setPointSize(size);
  725. return 0;
  726. }
  727. int w_setPointStyle(lua_State *L)
  728. {
  729. Graphics::PointStyle style;
  730. const char *str = luaL_checkstring(L, 1);
  731. if (!Graphics::getConstant(str, style))
  732. return luaL_error(L, "Invalid point style: %s", str);
  733. instance->setPointStyle(style);
  734. return 0;
  735. }
  736. int w_setPoint(lua_State *L)
  737. {
  738. float size = (float)luaL_checknumber(L, 1);
  739. Graphics::PointStyle style = Graphics::POINT_SMOOTH;
  740. if (lua_gettop(L) >= 2)
  741. {
  742. const char *str = luaL_checkstring(L, 2);
  743. if (!Graphics::getConstant(str, style))
  744. return luaL_error(L, "Invalid point style: %s", str);
  745. }
  746. instance->setPoint(size, style);
  747. return 0;
  748. }
  749. int w_getPointSize(lua_State *L)
  750. {
  751. lua_pushnumber(L, instance->getPointSize());
  752. return 1;
  753. }
  754. int w_getPointStyle(lua_State *L)
  755. {
  756. Graphics::PointStyle style = instance->getPointStyle();
  757. const char *str;
  758. if (!Graphics::getConstant(style, str))
  759. return luaL_error(L, "Unknown point style");
  760. lua_pushstring(L, str);
  761. return 1;
  762. }
  763. int w_getMaxPointSize(lua_State *L)
  764. {
  765. lua_pushnumber(L, instance->getMaxPointSize());
  766. return 1;
  767. }
  768. int w_newScreenshot(lua_State *L)
  769. {
  770. love::image::Image *image = luax_getmodule<love::image::Image>(L, "image", MODULE_IMAGE_T);
  771. bool copyAlpha = luax_optboolean(L, 1, false);
  772. love::image::ImageData *i = instance->newScreenshot(image, copyAlpha);
  773. luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)i);
  774. return 1;
  775. }
  776. int w_setCanvas(lua_State *L)
  777. {
  778. // discard stencil testing
  779. instance->discardStencil();
  780. // called with none -> reset to default buffer
  781. // nil is an error, to help people with typoes
  782. if (lua_isnone(L,1))
  783. {
  784. Canvas::bindDefaultCanvas();
  785. return 0;
  786. }
  787. Canvas *canvas = luax_checkcanvas(L, 1);
  788. try
  789. {
  790. // this unbinds the previous fbo
  791. canvas->startGrab();
  792. }
  793. catch (love::Exception &e)
  794. {
  795. return luaL_error(L, "%s", e.what());
  796. }
  797. return 0;
  798. }
  799. int w_setCanvases(lua_State *L)
  800. {
  801. // discard stencil testing
  802. instance->discardStencil();
  803. // called with none -> reset to default buffer
  804. // nil is an error, to help people with typoes
  805. if (lua_isnone(L,1))
  806. {
  807. Canvas::bindDefaultCanvas();
  808. return 0;
  809. }
  810. bool is_table = lua_istable(L, 1);
  811. std::vector<Canvas *> attachments;
  812. Canvas *canvas = 0;
  813. if (is_table)
  814. {
  815. lua_pushinteger(L, 1);
  816. lua_gettable(L, 1);
  817. canvas = luax_checkcanvas(L, -1);
  818. lua_pop(L, 1);
  819. for (int i = 2; i <= lua_objlen(L, 1); i++)
  820. {
  821. lua_pushinteger(L, i);
  822. lua_gettable(L, 1);
  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_pushnumber(L, i + 1);
  1082. lua_rawget(L, 1);
  1083. coords[i] = luax_tofloat(L, -1);
  1084. lua_pop(L, 1);
  1085. }
  1086. }
  1087. else
  1088. {
  1089. for (int i = 0; i < args; ++i)
  1090. coords[i] = luax_tofloat(L, i + 1);
  1091. }
  1092. instance->polyline(coords, args);
  1093. delete[] coords;
  1094. return 0;
  1095. }
  1096. int w_rectangle(lua_State *L)
  1097. {
  1098. Graphics::DrawMode mode;
  1099. const char *str = luaL_checkstring(L, 1);
  1100. if (!Graphics::getConstant(str, mode))
  1101. return luaL_error(L, "Incorrect draw mode %s", str);
  1102. float x = (float)luaL_checknumber(L, 2);
  1103. float y = (float)luaL_checknumber(L, 3);
  1104. float w = (float)luaL_checknumber(L, 4);
  1105. float h = (float)luaL_checknumber(L, 5);
  1106. instance->rectangle(mode, x, y, w, h);
  1107. return 0;
  1108. }
  1109. int w_circle(lua_State *L)
  1110. {
  1111. Graphics::DrawMode mode;
  1112. const char *str = luaL_checkstring(L, 1);
  1113. if (!Graphics::getConstant(str, mode))
  1114. return luaL_error(L, "Incorrect draw mode %s", str);
  1115. float x = (float)luaL_checknumber(L, 2);
  1116. float y = (float)luaL_checknumber(L, 3);
  1117. float radius = (float)luaL_checknumber(L, 4);
  1118. int points;
  1119. if (lua_isnoneornil(L, 5))
  1120. points = radius > 10 ? (int)(radius) : 10;
  1121. else
  1122. points = luaL_checkint(L, 5);
  1123. instance->circle(mode, x, y, radius, points);
  1124. return 0;
  1125. }
  1126. int w_arc(lua_State *L)
  1127. {
  1128. Graphics::DrawMode mode;
  1129. const char *str = luaL_checkstring(L, 1);
  1130. if (!Graphics::getConstant(str, mode))
  1131. return luaL_error(L, "Incorrect draw mode %s", str);
  1132. float x = (float)luaL_checknumber(L, 2);
  1133. float y = (float)luaL_checknumber(L, 3);
  1134. float radius = (float)luaL_checknumber(L, 4);
  1135. float angle1 = (float)luaL_checknumber(L, 5);
  1136. float angle2 = (float)luaL_checknumber(L, 6);
  1137. int points;
  1138. if (lua_isnoneornil(L, 7))
  1139. points = radius > 10 ? (int)(radius) : 10;
  1140. else
  1141. points = luaL_checkint(L, 7);
  1142. instance->arc(mode, x, y, radius, angle1, angle2, points);
  1143. return 0;
  1144. }
  1145. int w_polygon(lua_State *L)
  1146. {
  1147. int args = lua_gettop(L) - 1;
  1148. Graphics::DrawMode mode;
  1149. const char *str = luaL_checkstring(L, 1);
  1150. if (!Graphics::getConstant(str, mode))
  1151. return luaL_error(L, "Invalid draw mode: %s", str);
  1152. bool is_table = false;
  1153. float *coords;
  1154. if (args == 1 && lua_istable(L, 2))
  1155. {
  1156. args = lua_objlen(L, 2);
  1157. is_table = true;
  1158. }
  1159. if (args % 2 != 0)
  1160. return luaL_error(L, "Number of vertices must be a multiple of two");
  1161. else if (args < 6)
  1162. return luaL_error(L, "Need at least three vertices to draw a polygon");
  1163. // fetch coords
  1164. coords = new float[args + 2];
  1165. if (is_table)
  1166. {
  1167. for (int i = 0; i < args; ++i)
  1168. {
  1169. lua_pushnumber(L, i + 1);
  1170. lua_rawget(L, 2);
  1171. coords[i] = luax_tofloat(L, -1);
  1172. lua_pop(L, 1);
  1173. }
  1174. }
  1175. else
  1176. {
  1177. for (int i = 0; i < args; ++i)
  1178. coords[i] = luax_tofloat(L, i + 2);
  1179. }
  1180. // make a closed loop
  1181. coords[args] = coords[0];
  1182. coords[args+1] = coords[1];
  1183. instance->polygon(mode, coords, args+2);
  1184. delete[] coords;
  1185. return 0;
  1186. }
  1187. int w_push(lua_State *L)
  1188. {
  1189. try
  1190. {
  1191. instance->push();
  1192. }
  1193. catch(love::Exception e)
  1194. {
  1195. return luaL_error(L, e.what());
  1196. }
  1197. return 0;
  1198. }
  1199. int w_pop(lua_State *L)
  1200. {
  1201. try
  1202. {
  1203. instance->pop();
  1204. }
  1205. catch(love::Exception e)
  1206. {
  1207. return luaL_error(L, e.what());
  1208. }
  1209. return 0;
  1210. }
  1211. int w_rotate(lua_State *L)
  1212. {
  1213. float deg = (float)luaL_checknumber(L, 1);
  1214. instance->rotate(deg);
  1215. return 0;
  1216. }
  1217. int w_scale(lua_State *L)
  1218. {
  1219. float sx = (float)luaL_optnumber(L, 1, 1.0f);
  1220. float sy = (float)luaL_optnumber(L, 2, sx);
  1221. instance->scale(sx, sy);
  1222. return 0;
  1223. }
  1224. int w_translate(lua_State *L)
  1225. {
  1226. float x = (float)luaL_checknumber(L, 1);
  1227. float y = (float)luaL_checknumber(L, 2);
  1228. instance->translate(x, y);
  1229. return 0;
  1230. }
  1231. int w_shear(lua_State *L)
  1232. {
  1233. float kx = (float)luaL_checknumber(L, 1);
  1234. float ky = (float)luaL_checknumber(L, 2);
  1235. instance->shear(kx, ky);
  1236. return 0;
  1237. }
  1238. int w_hasFocus(lua_State *L)
  1239. {
  1240. luax_pushboolean(L, instance->hasFocus());
  1241. return 1;
  1242. }
  1243. // List of functions to wrap.
  1244. static const luaL_Reg functions[] =
  1245. {
  1246. { "checkMode", w_checkMode },
  1247. { "setMode", w_setMode },
  1248. { "getMode", w_getMode },
  1249. { "toggleFullscreen", w_toggleFullscreen },
  1250. { "reset", w_reset },
  1251. { "clear", w_clear },
  1252. { "present", w_present },
  1253. { "newImage", w_newImage },
  1254. { "newQuad", w_newQuad },
  1255. { "newFont", w_newFont },
  1256. { "newImageFont", w_newImageFont },
  1257. { "newSpriteBatch", w_newSpriteBatch },
  1258. { "newParticleSystem", w_newParticleSystem },
  1259. { "newCanvas", w_newCanvas },
  1260. { "newShader", w_newShader },
  1261. { "setColor", w_setColor },
  1262. { "getColor", w_getColor },
  1263. { "setBackgroundColor", w_setBackgroundColor },
  1264. { "getBackgroundColor", w_getBackgroundColor },
  1265. { "setFont", w_setFont },
  1266. { "getFont", w_getFont },
  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