wrap_Graphics.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779
  1. /**
  2. * Copyright (c) 2006-2017 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 "common/config.h"
  21. #include "wrap_Graphics.h"
  22. #include "Texture.h"
  23. #include "image/ImageData.h"
  24. #include "image/Image.h"
  25. #include "font/Rasterizer.h"
  26. #include "filesystem/wrap_Filesystem.h"
  27. #include "video/VideoStream.h"
  28. #include "image/wrap_Image.h"
  29. #include "common/Reference.h"
  30. #include "math/wrap_Transform.h"
  31. #include "opengl/Graphics.h"
  32. #include <cassert>
  33. #include <cstring>
  34. #include <cstdlib>
  35. #include <algorithm>
  36. // Shove the wrap_Graphics.lua code directly into a raw string literal.
  37. static const char graphics_lua[] =
  38. #include "wrap_Graphics.lua"
  39. ;
  40. namespace love
  41. {
  42. namespace graphics
  43. {
  44. #define instance() (Module::getInstance<Graphics>(Module::M_GRAPHICS))
  45. static int luax_checkgraphicscreated(lua_State *L)
  46. {
  47. if (!instance()->isCreated())
  48. return luaL_error(L, "love.graphics cannot function without a window!");
  49. return 0;
  50. }
  51. int w_reset(lua_State *)
  52. {
  53. instance()->reset();
  54. return 0;
  55. }
  56. int w_clear(lua_State *L)
  57. {
  58. OptionalColorf color(Colorf(0.0f, 0.0f, 0.0f, 0.0f));
  59. std::vector<OptionalColorf> colors;
  60. OptionalInt stencil(0);
  61. OptionalDouble depth(1.0);
  62. int argtype = lua_type(L, 1);
  63. int startidx = -1;
  64. if (argtype == LUA_TTABLE)
  65. {
  66. int maxn = lua_gettop(L);
  67. colors.reserve(maxn);
  68. for (int i = 0; i < maxn; i++)
  69. {
  70. argtype = lua_type(L, i + 1);
  71. if (argtype == LUA_TNUMBER || argtype == LUA_TBOOLEAN)
  72. {
  73. startidx = i + 1;
  74. break;
  75. }
  76. else if (argtype == LUA_TNIL || argtype == LUA_TNONE || luax_objlen(L, i + 1) == 0)
  77. {
  78. colors.push_back(OptionalColorf());
  79. continue;
  80. }
  81. for (int j = 1; j <= 4; j++)
  82. lua_rawgeti(L, i + 1, j);
  83. OptionalColorf c;
  84. c.hasValue = true;
  85. c.value.r = (float) luaL_checknumber(L, -4);
  86. c.value.g = (float) luaL_checknumber(L, -3);
  87. c.value.b = (float) luaL_checknumber(L, -2);
  88. c.value.a = (float) luaL_optnumber(L, -1, 1.0);
  89. colors.push_back(c);
  90. lua_pop(L, 4);
  91. }
  92. }
  93. else if (argtype == LUA_TBOOLEAN)
  94. {
  95. color.hasValue = luax_toboolean(L, 1);
  96. startidx = 2;
  97. }
  98. else if (argtype != LUA_TNONE && argtype != LUA_TNIL)
  99. {
  100. color.hasValue = true;
  101. color.value.r = (float) luaL_checknumber(L, 1);
  102. color.value.g = (float) luaL_checknumber(L, 2);
  103. color.value.b = (float) luaL_checknumber(L, 3);
  104. color.value.a = (float) luaL_optnumber(L, 4, 1.0);
  105. startidx = 5;
  106. }
  107. if (startidx >= 0)
  108. {
  109. argtype = lua_type(L, startidx);
  110. if (argtype == LUA_TBOOLEAN)
  111. stencil.hasValue = luax_toboolean(L, startidx);
  112. else if (argtype == LUA_TNUMBER)
  113. stencil.value = (int) luaL_checkinteger(L, startidx);
  114. argtype = lua_type(L, startidx + 1);
  115. if (argtype == LUA_TBOOLEAN)
  116. depth.hasValue = luax_toboolean(L, startidx + 1);
  117. else if (argtype == LUA_TNUMBER)
  118. depth.value = luaL_checknumber(L, startidx + 1);
  119. }
  120. if (colors.empty())
  121. luax_catchexcept(L, [&]() { instance()->clear(color, stencil, depth); });
  122. else
  123. luax_catchexcept(L, [&]() { instance()->clear(colors, stencil, depth); });
  124. return 0;
  125. }
  126. int w_discard(lua_State *L)
  127. {
  128. std::vector<bool> colorbuffers;
  129. if (lua_istable(L, 1))
  130. {
  131. for (size_t i = 1; i <= luax_objlen(L, 1); i++)
  132. {
  133. lua_rawgeti(L, 1, i);
  134. colorbuffers.push_back(luax_optboolean(L, -1, true));
  135. lua_pop(L, 1);
  136. }
  137. }
  138. else
  139. {
  140. bool discardcolor = luax_optboolean(L, 1, true);
  141. size_t numbuffers = std::max((size_t) 1, instance()->getCanvas().colors.size());
  142. colorbuffers = std::vector<bool>(numbuffers, discardcolor);
  143. }
  144. bool depthstencil = luax_optboolean(L, 2, true);
  145. instance()->discard(colorbuffers, depthstencil);
  146. return 0;
  147. }
  148. int w_present(lua_State *L)
  149. {
  150. luax_catchexcept(L, [&]() { instance()->present(L); });
  151. return 0;
  152. }
  153. int w_isCreated(lua_State *L)
  154. {
  155. luax_pushboolean(L, instance()->isCreated());
  156. return 1;
  157. }
  158. int w_isActive(lua_State *L)
  159. {
  160. luax_pushboolean(L, instance()->isActive());
  161. return 1;
  162. }
  163. int w_isGammaCorrect(lua_State *L)
  164. {
  165. luax_pushboolean(L, graphics::isGammaCorrect());
  166. return 1;
  167. }
  168. int w_getWidth(lua_State *L)
  169. {
  170. lua_pushinteger(L, instance()->getWidth());
  171. return 1;
  172. }
  173. int w_getHeight(lua_State *L)
  174. {
  175. lua_pushinteger(L, instance()->getHeight());
  176. return 1;
  177. }
  178. int w_getDimensions(lua_State *L)
  179. {
  180. lua_pushinteger(L, instance()->getWidth());
  181. lua_pushinteger(L, instance()->getHeight());
  182. return 2;
  183. }
  184. int w_getPixelWidth(lua_State *L)
  185. {
  186. lua_pushinteger(L, instance()->getPixelWidth());
  187. return 1;
  188. }
  189. int w_getPixelHeight(lua_State *L)
  190. {
  191. lua_pushinteger(L, instance()->getPixelHeight());
  192. return 1;
  193. }
  194. int w_getPixelDimensions(lua_State *L)
  195. {
  196. lua_pushinteger(L, instance()->getPixelWidth());
  197. lua_pushinteger(L, instance()->getPixelHeight());
  198. return 2;
  199. }
  200. int w_getPixelDensity(lua_State *L)
  201. {
  202. lua_pushnumber(L, instance()->getScreenPixelDensity());
  203. return 1;
  204. }
  205. static Graphics::RenderTarget checkRenderTarget(lua_State *L, int idx)
  206. {
  207. lua_rawgeti(L, idx, 1);
  208. Graphics::RenderTarget target(luax_checkcanvas(L, -1), 0);
  209. lua_pop(L, 1);
  210. TextureType type = target.canvas->getTextureType();
  211. if (type == TEXTURE_2D_ARRAY || type == TEXTURE_VOLUME)
  212. target.slice = luax_checkintflag(L, idx, "layer") - 1;
  213. else if (type == TEXTURE_CUBE)
  214. target.slice = luax_checkintflag(L, idx, "face") - 1;
  215. target.mipmap = luax_intflag(L, idx, "mipmap", 1) - 1;
  216. return target;
  217. }
  218. int w_setCanvas(lua_State *L)
  219. {
  220. // Disable stencil writes.
  221. luax_catchexcept(L, [](){ instance()->stopDrawToStencilBuffer(); });
  222. // called with none -> reset to default buffer
  223. if (lua_isnoneornil(L, 1))
  224. {
  225. instance()->setCanvas();
  226. return 0;
  227. }
  228. bool is_table = lua_istable(L, 1);
  229. Graphics::RenderTargets targets;
  230. if (is_table)
  231. {
  232. lua_rawgeti(L, 1, 1);
  233. bool table_of_tables = lua_istable(L, -1);
  234. lua_pop(L, 1);
  235. for (int i = 1; i <= (int) luax_objlen(L, 1); i++)
  236. {
  237. lua_rawgeti(L, 1, i);
  238. if (table_of_tables)
  239. targets.colors.push_back(checkRenderTarget(L, -1));
  240. else
  241. {
  242. targets.colors.emplace_back(luax_checkcanvas(L, -1), 0);
  243. if (targets.colors.back().canvas->getTextureType() != TEXTURE_2D)
  244. return luaL_error(L, "The table-of-tables variant of setCanvas must be used with non-2D Canvases.");
  245. }
  246. lua_pop(L, 1);
  247. }
  248. lua_getfield(L, 1, "depthstencil");
  249. if (lua_istable(L, -1))
  250. targets.depthStencil = checkRenderTarget(L, -1);
  251. else if (!lua_isnoneornil(L, -1))
  252. targets.depthStencil.canvas = luax_checkcanvas(L, -1);
  253. lua_pop(L, 1);
  254. }
  255. else
  256. {
  257. for (int i = 1; i <= lua_gettop(L); i++)
  258. {
  259. Graphics::RenderTarget target(luax_checkcanvas(L, i), 0);
  260. TextureType type = target.canvas->getTextureType();
  261. if (i == 1 && type != TEXTURE_2D)
  262. {
  263. target.slice = (int) luaL_checknumber(L, i + 1) - 1;
  264. target.mipmap = (int) luaL_optinteger(L, i + 2, 1) - 1;
  265. targets.colors.push_back(target);
  266. break;
  267. }
  268. else if (type == TEXTURE_2D && lua_isnumber(L, i + 1))
  269. {
  270. target.mipmap = (int) luaL_optinteger(L, i + 1, 1) - 1;
  271. i++;
  272. }
  273. if (i > 1 && type != TEXTURE_2D)
  274. return luaL_error(L, "This variant of setCanvas only supports 2D texture types.");
  275. targets.colors.push_back(target);
  276. }
  277. }
  278. luax_catchexcept(L, [&]() {
  279. if (targets.colors.size() > 0)
  280. instance()->setCanvas(targets);
  281. else
  282. instance()->setCanvas();
  283. });
  284. return 0;
  285. }
  286. static void pushRenderTarget(lua_State *L, const Graphics::RenderTarget &rt)
  287. {
  288. lua_createtable(L, 1, 2);
  289. luax_pushtype(L, rt.canvas);
  290. lua_rawseti(L, -2, 1);
  291. TextureType type = rt.canvas->getTextureType();
  292. if (type == TEXTURE_2D_ARRAY || type == TEXTURE_VOLUME)
  293. {
  294. lua_pushnumber(L, rt.slice + 1);
  295. lua_setfield(L, -2, "layer");
  296. }
  297. else if (type == TEXTURE_VOLUME)
  298. {
  299. lua_pushnumber(L, rt.slice + 1);
  300. lua_setfield(L, -2, "face");
  301. }
  302. lua_pushnumber(L, rt.mipmap + 1);
  303. lua_setfield(L, -2, "mipmap");
  304. }
  305. int w_getCanvas(lua_State *L)
  306. {
  307. Graphics::RenderTargets targets = instance()->getCanvas();
  308. int ntargets = (int) targets.colors.size();
  309. if (ntargets == 0)
  310. {
  311. lua_pushnil(L);
  312. return 1;
  313. }
  314. bool shouldUseTablesVariant = targets.depthStencil.canvas != nullptr;
  315. if (!shouldUseTablesVariant)
  316. {
  317. for (const auto &rt : targets.colors)
  318. {
  319. if (rt.mipmap != 0 || rt.canvas->getTextureType() != TEXTURE_2D)
  320. {
  321. shouldUseTablesVariant = true;
  322. break;
  323. }
  324. }
  325. }
  326. if (shouldUseTablesVariant)
  327. {
  328. lua_createtable(L, ntargets, 0);
  329. for (int i = 0; i < ntargets; i++)
  330. {
  331. pushRenderTarget(L, targets.colors[i]);
  332. lua_rawseti(L, -2, i + 1);
  333. }
  334. if (targets.depthStencil.canvas != nullptr)
  335. {
  336. pushRenderTarget(L, targets.depthStencil);
  337. lua_setfield(L, -2, "depthstencil");
  338. }
  339. return 1;
  340. }
  341. else
  342. {
  343. for (const auto &rt : targets.colors)
  344. luax_pushtype(L, rt.canvas);
  345. return ntargets;
  346. }
  347. }
  348. static void screenshotCallback(love::image::ImageData *i, Reference *ref, void *gd)
  349. {
  350. if (i != nullptr)
  351. {
  352. lua_State *L = (lua_State *) gd;
  353. ref->push(L);
  354. delete ref;
  355. luax_pushtype(L, i);
  356. lua_call(L, 1, 0);
  357. }
  358. else
  359. delete ref;
  360. }
  361. int w_captureScreenshot(lua_State *L)
  362. {
  363. luaL_checktype(L, 1, LUA_TFUNCTION);
  364. Graphics::ScreenshotInfo info;
  365. info.callback = screenshotCallback;
  366. lua_pushvalue(L, 1);
  367. info.ref = luax_refif(L, LUA_TFUNCTION);
  368. lua_pop(L, 1);
  369. luax_catchexcept(L,
  370. [&]() { instance()->captureScreenshot(info); },
  371. [&](bool except) { if (except) delete info.ref; }
  372. );
  373. return 0;
  374. }
  375. int w_setScissor(lua_State *L)
  376. {
  377. int nargs = lua_gettop(L);
  378. if (nargs == 0 || (nargs == 4 && lua_isnil(L, 1) && lua_isnil(L, 2)
  379. && lua_isnil(L, 3) && lua_isnil(L, 4)))
  380. {
  381. instance()->setScissor();
  382. return 0;
  383. }
  384. Rect rect;
  385. rect.x = (int) luaL_checknumber(L, 1);
  386. rect.y = (int) luaL_checknumber(L, 2);
  387. rect.w = (int) luaL_checknumber(L, 3);
  388. rect.h = (int) luaL_checknumber(L, 4);
  389. if (rect.w < 0 || rect.h < 0)
  390. return luaL_error(L, "Can't set scissor with negative width and/or height.");
  391. instance()->setScissor(rect);
  392. return 0;
  393. }
  394. int w_intersectScissor(lua_State *L)
  395. {
  396. Rect rect;
  397. rect.x = (int) luaL_checknumber(L, 1);
  398. rect.y = (int) luaL_checknumber(L, 2);
  399. rect.w = (int) luaL_checknumber(L, 3);
  400. rect.h = (int) luaL_checknumber(L, 4);
  401. if (rect.w < 0 || rect.h < 0)
  402. return luaL_error(L, "Can't set scissor with negative width and/or height.");
  403. instance()->intersectScissor(rect);
  404. return 0;
  405. }
  406. int w_getScissor(lua_State *L)
  407. {
  408. Rect rect;
  409. if (!instance()->getScissor(rect))
  410. return 0;
  411. lua_pushinteger(L, rect.x);
  412. lua_pushinteger(L, rect.y);
  413. lua_pushinteger(L, rect.w);
  414. lua_pushinteger(L, rect.h);
  415. return 4;
  416. }
  417. int w_stencil(lua_State *L)
  418. {
  419. luaL_checktype(L, 1, LUA_TFUNCTION);
  420. StencilAction action = STENCIL_REPLACE;
  421. if (!lua_isnoneornil(L, 2))
  422. {
  423. const char *actionstr = luaL_checkstring(L, 2);
  424. if (!getConstant(actionstr, action))
  425. return luaL_error(L, "Invalid stencil draw action: %s", actionstr);
  426. }
  427. int stencilvalue = (int) luaL_optnumber(L, 3, 1);
  428. // Fourth argument: whether to keep the contents of the stencil buffer.
  429. int argtype = lua_type(L, 4);
  430. if (argtype == LUA_TNONE || argtype == LUA_TNIL || (argtype == LUA_TBOOLEAN && luax_toboolean(L, 4) == false))
  431. instance()->clearStencil(0);
  432. else if (argtype == LUA_TNUMBER)
  433. instance()->clearStencil((int) luaL_checkinteger(L, 4));
  434. else if (argtype != LUA_TBOOLEAN)
  435. luaL_checktype(L, 4, LUA_TBOOLEAN);
  436. luax_catchexcept(L, [&](){ instance()->drawToStencilBuffer(action, stencilvalue); });
  437. // Call stencilfunc()
  438. lua_pushvalue(L, 1);
  439. lua_call(L, 0, 0);
  440. luax_catchexcept(L, [&](){ instance()->stopDrawToStencilBuffer(); });
  441. return 0;
  442. }
  443. int w_setStencilTest(lua_State *L)
  444. {
  445. // COMPARE_ALWAYS effectively disables stencil testing.
  446. CompareMode compare = COMPARE_ALWAYS;
  447. int comparevalue = 0;
  448. if (!lua_isnoneornil(L, 1))
  449. {
  450. const char *comparestr = luaL_checkstring(L, 1);
  451. if (!getConstant(comparestr, compare))
  452. return luaL_error(L, "Invalid compare mode: %s", comparestr);
  453. comparevalue = (int) luaL_checknumber(L, 2);
  454. }
  455. luax_catchexcept(L, [&](){ instance()->setStencilTest(compare, comparevalue); });
  456. return 0;
  457. }
  458. int w_getStencilTest(lua_State *L)
  459. {
  460. CompareMode compare = COMPARE_ALWAYS;
  461. int comparevalue = 1;
  462. instance()->getStencilTest(compare, comparevalue);
  463. const char *comparestr;
  464. if (!getConstant(compare, comparestr))
  465. return luaL_error(L, "Unknown compare mode.");
  466. lua_pushstring(L, comparestr);
  467. lua_pushnumber(L, comparevalue);
  468. return 2;
  469. }
  470. static void parsePixelDensity(love::filesystem::FileData *d, float *pixeldensity)
  471. {
  472. // Parse a density scale of 2.0 from "[email protected]".
  473. const std::string &fname = d->getName();
  474. size_t namelen = fname.length();
  475. size_t atpos = fname.rfind('@');
  476. if (atpos != std::string::npos && atpos + 2 < namelen
  477. && (fname[namelen - 1] == 'x' || fname[namelen - 1] == 'X'))
  478. {
  479. char *end = nullptr;
  480. long density = strtol(fname.c_str() + atpos + 1, &end, 10);
  481. if (end != nullptr && density > 0 && pixeldensity != nullptr)
  482. *pixeldensity = (float) density;
  483. }
  484. }
  485. static Image::Settings w__optImageSettings(lua_State *L, int idx, const Image::Settings &s)
  486. {
  487. Image::Settings settings = s;
  488. if (!lua_isnoneornil(L, idx))
  489. {
  490. luaL_checktype(L, idx, LUA_TTABLE);
  491. settings.mipmaps = luax_boolflag(L, idx, "mipmaps", s.mipmaps);
  492. settings.linear = luax_boolflag(L, idx, "linear", s.linear);
  493. settings.pixeldensity = (float) luax_numberflag(L, idx, "pixeldensity", s.pixeldensity);
  494. }
  495. return settings;
  496. }
  497. static std::pair<StrongRef<love::image::ImageData>, StrongRef<love::image::CompressedImageData>>
  498. getImageData(lua_State *L, int idx, bool allowcompressed, float *density)
  499. {
  500. StrongRef<love::image::ImageData> idata;
  501. StrongRef<love::image::CompressedImageData> cdata;
  502. // Convert to ImageData / CompressedImageData, if necessary.
  503. if (lua_isstring(L, idx) || luax_istype(L, idx, love::filesystem::File::type) || luax_istype(L, idx, love::filesystem::FileData::type))
  504. {
  505. auto imagemodule = Module::getInstance<love::image::Image>(Module::M_IMAGE);
  506. if (imagemodule == nullptr)
  507. luaL_error(L, "Cannot load images without the love.image module.");
  508. StrongRef<love::filesystem::FileData> fdata(love::filesystem::luax_getfiledata(L, idx), Acquire::NORETAIN);
  509. if (density != nullptr)
  510. parsePixelDensity(fdata, density);
  511. if (allowcompressed && imagemodule->isCompressed(fdata))
  512. luax_catchexcept(L, [&]() { cdata.set(imagemodule->newCompressedData(fdata), Acquire::NORETAIN); });
  513. else
  514. luax_catchexcept(L, [&]() { idata.set(imagemodule->newImageData(fdata), Acquire::NORETAIN); });
  515. }
  516. else if (luax_istype(L, idx, love::image::CompressedImageData::type))
  517. cdata.set(love::image::luax_checkcompressedimagedata(L, idx));
  518. else
  519. idata.set(love::image::luax_checkimagedata(L, idx));
  520. return std::make_pair(idata, cdata);
  521. }
  522. static int w__pushNewImage(lua_State *L, Image::Slices &slices, const Image::Settings &settings)
  523. {
  524. StrongRef<Image> i;
  525. luax_catchexcept(L,
  526. [&]() { i.set(instance()->newImage(slices, settings), Acquire::NORETAIN); },
  527. [&](bool) { slices.clear(); }
  528. );
  529. luax_pushtype(L, i);
  530. return 1;
  531. }
  532. int w_newCubeImage(lua_State *L)
  533. {
  534. luax_checkgraphicscreated(L);
  535. Image::Slices slices(TEXTURE_CUBE);
  536. Image::Settings settings;
  537. auto imagemodule = Module::getInstance<love::image::Image>(Module::M_IMAGE);
  538. if (!lua_istable(L, 1))
  539. {
  540. auto data = getImageData(L, 1, true, &settings.pixeldensity);
  541. std::vector<StrongRef<love::image::ImageData>> faces;
  542. if (data.first.get())
  543. {
  544. luax_catchexcept(L, [&](){ faces = imagemodule->newCubeFaces(data.first); });
  545. for (int i = 0; i < (int) faces.size(); i++)
  546. slices.set(i, 0, faces[i]);
  547. }
  548. else
  549. slices.add(data.second, 0, 0, true, true);
  550. }
  551. else
  552. {
  553. int tlen = (int) luax_objlen(L, 1);
  554. if (luax_isarrayoftables(L, 1))
  555. {
  556. if (tlen != 6)
  557. return luaL_error(L, "Cubemap images must have 6 faces.");
  558. for (int face = 0; face < tlen; face++)
  559. {
  560. lua_rawgeti(L, 1, face + 1);
  561. luaL_checktype(L, -1, LUA_TTABLE);
  562. int miplen = std::max(1, (int) luax_objlen(L, -1));
  563. for (int mip = 0; mip < miplen; mip++)
  564. {
  565. lua_rawgeti(L, -1, mip + 1);
  566. auto data = getImageData(L, -1, true, face == 0 && mip == 0 ? &settings.pixeldensity : nullptr);
  567. if (data.first.get())
  568. slices.set(face, mip, data.first);
  569. else
  570. slices.set(face, mip, data.second->getSlice(0, 0));
  571. lua_pop(L, 1);
  572. }
  573. }
  574. }
  575. else
  576. {
  577. bool usemipmaps = false;
  578. for (int i = 0; i < tlen; i++)
  579. {
  580. lua_rawgeti(L, 1, i + 1);
  581. auto data = getImageData(L, -1, true, i == 0 ? &settings.pixeldensity : nullptr);
  582. if (data.first.get())
  583. {
  584. if (usemipmaps || data.first->getWidth() != data.first->getHeight())
  585. {
  586. usemipmaps = true;
  587. std::vector<StrongRef<love::image::ImageData>> faces;
  588. luax_catchexcept(L, [&](){ faces = imagemodule->newCubeFaces(data.first); });
  589. for (int face = 0; face < (int) faces.size(); face++)
  590. slices.set(face, i, faces[i]);
  591. }
  592. else
  593. slices.set(i, 0, data.first);
  594. }
  595. else
  596. slices.add(data.second, i, 0, false, true);
  597. }
  598. }
  599. lua_pop(L, tlen);
  600. }
  601. settings = w__optImageSettings(L, 2, settings);
  602. return w__pushNewImage(L, slices, settings);
  603. }
  604. int w_newArrayImage(lua_State *L)
  605. {
  606. luax_checkgraphicscreated(L);
  607. Image::Slices slices(TEXTURE_2D_ARRAY);
  608. Image::Settings settings;
  609. if (lua_istable(L, 1))
  610. {
  611. int tlen = std::max(1, (int) luax_objlen(L, 1));
  612. if (luax_isarrayoftables(L, 1))
  613. {
  614. for (int slice = 0; slice < tlen; slice++)
  615. {
  616. lua_rawgeti(L, 1, slice + 1);
  617. luaL_checktype(L, -1, LUA_TTABLE);
  618. int miplen = std::max(1, (int) luax_objlen(L, -1));
  619. for (int mip = 0; mip < miplen; mip++)
  620. {
  621. lua_rawgeti(L, -1, mip + 1);
  622. auto data = getImageData(L, -1, true, slice == 0 && mip == 0 ? &settings.pixeldensity : nullptr);
  623. if (data.first.get())
  624. slices.set(slice, mip, data.first);
  625. else
  626. slices.set(slice, mip, data.second->getSlice(0, 0));
  627. lua_pop(L, 1);
  628. }
  629. }
  630. }
  631. else
  632. {
  633. for (int slice = 0; slice < tlen; slice++)
  634. {
  635. lua_rawgeti(L, 1, slice + 1);
  636. auto data = getImageData(L, -1, true, slice == 0 ? &settings.pixeldensity : nullptr);
  637. if (data.first.get())
  638. slices.set(slice, 0, data.first);
  639. else
  640. slices.add(data.second, slice, 0, false, true);
  641. }
  642. }
  643. lua_pop(L, tlen);
  644. }
  645. else
  646. {
  647. auto data = getImageData(L, 1, true, &settings.pixeldensity);
  648. if (data.first.get())
  649. slices.set(0, 0, data.first);
  650. else
  651. slices.add(data.second, 0, 0, true, true);
  652. }
  653. settings = w__optImageSettings(L, 2, settings);
  654. return w__pushNewImage(L, slices, settings);
  655. }
  656. int w_newVolumeImage(lua_State *L)
  657. {
  658. luax_checkgraphicscreated(L);
  659. auto imagemodule = Module::getInstance<love::image::Image>(Module::M_IMAGE);
  660. Image::Slices slices(TEXTURE_VOLUME);
  661. Image::Settings settings;
  662. if (lua_istable(L, 1))
  663. {
  664. int tlen = std::max(1, (int) luax_objlen(L, 1));
  665. if (luax_isarrayoftables(L, 1))
  666. {
  667. for (int mip = 0; mip < tlen; mip++)
  668. {
  669. lua_rawgeti(L, 1, mip + 1);
  670. luaL_checktype(L, -1, LUA_TTABLE);
  671. int slicelen = std::max(1, (int) luax_objlen(L, -1));
  672. for (int slice = 0; slice < slicelen; slice++)
  673. {
  674. lua_rawgeti(L, -1, mip + 1);
  675. auto data = getImageData(L, -1, true, slice == 0 && mip == 0 ? &settings.pixeldensity : nullptr);
  676. if (data.first.get())
  677. slices.set(slice, mip, data.first);
  678. else
  679. slices.set(slice, mip, data.second->getSlice(0, 0));
  680. lua_pop(L, 1);
  681. }
  682. }
  683. }
  684. else
  685. {
  686. for (int layer = 0; layer < tlen; layer++)
  687. {
  688. lua_rawgeti(L, 1, layer + 1);
  689. auto data = getImageData(L, -1, true, layer == 0 ? &settings.pixeldensity : nullptr);
  690. if (data.first.get())
  691. slices.set(layer, 0, data.first);
  692. else
  693. slices.add(data.second, layer, 0, false, true);
  694. }
  695. }
  696. lua_pop(L, tlen);
  697. }
  698. else
  699. {
  700. auto data = getImageData(L, 1, true, &settings.pixeldensity);
  701. if (data.first.get())
  702. {
  703. std::vector<StrongRef<love::image::ImageData>> layers;
  704. luax_catchexcept(L, [&](){ layers = imagemodule->newVolumeLayers(data.first); });
  705. for (int i = 0; i < (int) layers.size(); i++)
  706. slices.set(i, 0, layers[i]);
  707. }
  708. else
  709. slices.add(data.second, 0, 0, true, true);
  710. }
  711. settings = w__optImageSettings(L, 2, settings);
  712. return w__pushNewImage(L, slices, settings);
  713. }
  714. int w_newImage(lua_State *L)
  715. {
  716. luax_checkgraphicscreated(L);
  717. Image::Slices slices(TEXTURE_2D);
  718. Image::Settings settings;
  719. if (lua_istable(L, 1))
  720. {
  721. int n = std::max(1, (int) luax_objlen(L, 1));
  722. for (int i = 0; i < n; i++)
  723. {
  724. lua_rawgeti(L, 1, i + 1);
  725. auto data = getImageData(L, -1, true, i == 0 ? &settings.pixeldensity : nullptr);
  726. if (data.first.get())
  727. slices.set(0, i, data.first);
  728. else
  729. slices.set(0, i, data.second->getSlice(0, 0));
  730. }
  731. lua_pop(L, n);
  732. }
  733. else
  734. {
  735. auto data = getImageData(L, 1, true, &settings.pixeldensity);
  736. if (data.first.get())
  737. slices.set(0, 0, data.first);
  738. else
  739. slices.add(data.second, 0, 0, false, true);
  740. }
  741. settings = w__optImageSettings(L, 2, settings);
  742. return w__pushNewImage(L, slices, settings);
  743. }
  744. int w_newQuad(lua_State *L)
  745. {
  746. luax_checkgraphicscreated(L);
  747. Quad::Viewport v;
  748. v.x = luaL_checknumber(L, 1);
  749. v.y = luaL_checknumber(L, 2);
  750. v.w = luaL_checknumber(L, 3);
  751. v.h = luaL_checknumber(L, 4);
  752. double sw = 0.0f;
  753. double sh = 0.0f;
  754. int layer = 0;
  755. if (luax_istype(L, 5, Texture::type))
  756. {
  757. Texture *texture = luax_checktexture(L, 5);
  758. sw = texture->getWidth();
  759. sh = texture->getHeight();
  760. }
  761. else if (luax_istype(L, 6, Texture::type))
  762. {
  763. layer = (int) luaL_checknumber(L, 5) - 1;
  764. Texture *texture = luax_checktexture(L, 6);
  765. sw = texture->getWidth();
  766. sh = texture->getHeight();
  767. }
  768. else if (!lua_isnoneornil(L, 7))
  769. {
  770. layer = (int) luaL_checknumber(L, 5) - 1;
  771. sw = luaL_checknumber(L, 6);
  772. sh = luaL_checknumber(L, 7);
  773. }
  774. else
  775. {
  776. sw = luaL_checknumber(L, 5);
  777. sh = luaL_checknumber(L, 6);
  778. }
  779. Quad *quad = instance()->newQuad(v, sw, sh);
  780. quad->setLayer(layer);
  781. luax_pushtype(L, quad);
  782. quad->release();
  783. return 1;
  784. }
  785. int w_newFont(lua_State *L)
  786. {
  787. luax_checkgraphicscreated(L);
  788. graphics::Font *font = nullptr;
  789. // Convert to Rasterizer, if necessary.
  790. if (!luax_istype(L, 1, love::font::Rasterizer::type))
  791. {
  792. std::vector<int> idxs;
  793. for (int i = 0; i < lua_gettop(L); i++)
  794. idxs.push_back(i + 1);
  795. luax_convobj(L, &idxs[0], (int) idxs.size(), "font", "newRasterizer");
  796. }
  797. love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1);
  798. luax_catchexcept(L, [&]() {
  799. font = instance()->newFont(rasterizer, instance()->getDefaultFilter()); }
  800. );
  801. // Push the type.
  802. luax_pushtype(L, font);
  803. font->release();
  804. return 1;
  805. }
  806. int w_newImageFont(lua_State *L)
  807. {
  808. luax_checkgraphicscreated(L);
  809. // filter for glyphs
  810. Texture::Filter filter = instance()->getDefaultFilter();
  811. // Convert to Rasterizer if necessary.
  812. if (!luax_istype(L, 1, love::font::Rasterizer::type))
  813. {
  814. luaL_checktype(L, 2, LUA_TSTRING);
  815. std::vector<int> idxs;
  816. for (int i = 0; i < lua_gettop(L); i++)
  817. idxs.push_back(i + 1);
  818. luax_convobj(L, &idxs[0], (int) idxs.size(), "font", "newImageRasterizer");
  819. }
  820. love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1);
  821. // Create the font.
  822. Font *font = instance()->newFont(rasterizer, filter);
  823. // Push the type.
  824. luax_pushtype(L, font);
  825. font->release();
  826. return 1;
  827. }
  828. int w_newSpriteBatch(lua_State *L)
  829. {
  830. luax_checkgraphicscreated(L);
  831. Texture *texture = luax_checktexture(L, 1);
  832. int size = (int) luaL_optnumber(L, 2, 1000);
  833. vertex::Usage usage = vertex::USAGE_DYNAMIC;
  834. if (lua_gettop(L) > 2)
  835. {
  836. const char *usagestr = luaL_checkstring(L, 3);
  837. if (!vertex::getConstant(usagestr, usage))
  838. return luaL_error(L, "Invalid usage hint: %s", usagestr);
  839. }
  840. SpriteBatch *t = nullptr;
  841. luax_catchexcept(L,
  842. [&](){ t = instance()->newSpriteBatch(texture, size, usage); }
  843. );
  844. luax_pushtype(L, t);
  845. t->release();
  846. return 1;
  847. }
  848. int w_newParticleSystem(lua_State *L)
  849. {
  850. luax_checkgraphicscreated(L);
  851. Texture *texture = luax_checktexture(L, 1);
  852. lua_Number size = luaL_optnumber(L, 2, 1000);
  853. ParticleSystem *t = nullptr;
  854. if (size < 1.0 || size > ParticleSystem::MAX_PARTICLES)
  855. return luaL_error(L, "Invalid ParticleSystem size");
  856. luax_catchexcept(L,
  857. [&](){ t = instance()->newParticleSystem(texture, int(size)); }
  858. );
  859. luax_pushtype(L, t);
  860. t->release();
  861. return 1;
  862. }
  863. int w_newCanvas(lua_State *L)
  864. {
  865. luax_checkgraphicscreated(L);
  866. Canvas::Settings settings;
  867. // check if width and height are given. else default to screen dimensions.
  868. settings.width = (int) luaL_optnumber(L, 1, instance()->getWidth());
  869. settings.height = (int) luaL_optnumber(L, 2, instance()->getHeight());
  870. // Default to the screen's current pixel density scale.
  871. settings.pixeldensity = instance()->getScreenPixelDensity();
  872. int startidx = 3;
  873. if (lua_isnumber(L, 3))
  874. {
  875. settings.layers = (int) luaL_checknumber(L, 3);
  876. settings.type = TEXTURE_2D_ARRAY;
  877. startidx = 4;
  878. }
  879. if (!lua_isnoneornil(L, startidx))
  880. {
  881. settings.pixeldensity = (float) luax_numberflag(L, startidx, "pixeldensity", settings.pixeldensity);
  882. settings.msaa = luax_intflag(L, startidx, "msaa", settings.msaa);
  883. lua_getfield(L, startidx, "format");
  884. if (!lua_isnoneornil(L, -1))
  885. {
  886. const char *str = luaL_checkstring(L, -1);
  887. if (!getConstant(str, settings.format))
  888. return luaL_error(L, "Invalid pixel format: %s", str);
  889. }
  890. lua_pop(L, 1);
  891. lua_getfield(L, startidx, "type");
  892. if (!lua_isnoneornil(L, -1))
  893. {
  894. const char *str = luaL_checkstring(L, -1);
  895. if (!Texture::getConstant(str, settings.type))
  896. return luaL_error(L, "Invalid texture type: %s", str);
  897. }
  898. lua_pop(L, 1);
  899. lua_getfield(L, startidx, "readable");
  900. if (!lua_isnoneornil(L, -1))
  901. {
  902. luaL_checktype(L, -1, LUA_TBOOLEAN);
  903. settings.readable.hasValue = true;
  904. settings.readable.value = luax_toboolean(L, -1);
  905. }
  906. lua_pop(L, 1);
  907. lua_getfield(L, startidx, "mipmaps");
  908. if (!lua_isnoneornil(L, -1))
  909. {
  910. const char *str = luaL_checkstring(L, -1);
  911. if (!Canvas::getConstant(str, settings.mipmaps))
  912. return luaL_error(L, "Invalid Canvas mipmap mode: %s", str);
  913. }
  914. lua_pop(L, 1);
  915. }
  916. Canvas *canvas = nullptr;
  917. luax_catchexcept(L, [&](){ canvas = instance()->newCanvas(settings); });
  918. luax_pushtype(L, canvas);
  919. canvas->release();
  920. return 1;
  921. }
  922. static int w_getShaderSource(lua_State *L, int startidx, bool gles, Shader::ShaderSource &source)
  923. {
  924. luax_checkgraphicscreated(L);
  925. // read any filepath arguments
  926. for (int i = startidx; i < startidx + 2; i++)
  927. {
  928. if (!lua_isstring(L, i))
  929. continue;
  930. // call love.filesystem.isFile(arg_i)
  931. luax_getfunction(L, "filesystem", "isFile");
  932. lua_pushvalue(L, i);
  933. lua_call(L, 1, 1);
  934. bool isFile = luax_toboolean(L, -1);
  935. lua_pop(L, 1);
  936. if (isFile)
  937. {
  938. luax_getfunction(L, "filesystem", "read");
  939. lua_pushvalue(L, i);
  940. lua_call(L, 1, 1);
  941. lua_replace(L, i);
  942. }
  943. else
  944. {
  945. // Check if the argument looks like a filepath - we want a nicer
  946. // error for misspelled filepath arguments.
  947. size_t slen = 0;
  948. const char *str = lua_tolstring(L, i, &slen);
  949. if (slen > 0 && slen < 256 && !strchr(str, '\n'))
  950. {
  951. const char *ext = strchr(str, '.');
  952. if (ext != nullptr && !strchr(ext, ';') && !strchr(ext, ' '))
  953. return luaL_error(L, "Could not open file %s. Does not exist.", str);
  954. }
  955. }
  956. }
  957. bool has_arg1 = lua_isstring(L, startidx + 0) != 0;
  958. bool has_arg2 = lua_isstring(L, startidx + 1) != 0;
  959. // require at least one string argument
  960. if (!(has_arg1 || has_arg2))
  961. luaL_checkstring(L, startidx);
  962. luax_getfunction(L, "graphics", "_shaderCodeToGLSL");
  963. // push vertexcode and pixelcode strings to the top of the stack
  964. lua_pushboolean(L, gles);
  965. if (has_arg1)
  966. lua_pushvalue(L, startidx + 0);
  967. else
  968. lua_pushnil(L);
  969. if (has_arg2)
  970. lua_pushvalue(L, startidx + 1);
  971. else
  972. lua_pushnil(L);
  973. // call effectCodeToGLSL, returned values will be at the top of the stack
  974. if (lua_pcall(L, 3, 2, 0) != 0)
  975. return luaL_error(L, "%s", lua_tostring(L, -1));
  976. // vertex shader code
  977. if (lua_isstring(L, -2))
  978. source.vertex = luax_checkstring(L, -2);
  979. else if (has_arg1 && has_arg2)
  980. return luaL_error(L, "Could not parse vertex shader code (missing 'position' function?)");
  981. // pixel shader code
  982. if (lua_isstring(L, -1))
  983. source.pixel = luax_checkstring(L, -1);
  984. else if (has_arg1 && has_arg2)
  985. return luaL_error(L, "Could not parse pixel shader code (missing 'effect' function?)");
  986. if (source.vertex.empty() && source.pixel.empty())
  987. {
  988. // Original args had source code, but effectCodeToGLSL couldn't translate it
  989. for (int i = startidx; i < startidx + 2; i++)
  990. {
  991. if (lua_isstring(L, i))
  992. return luaL_argerror(L, i, "missing 'position' or 'effect' function?");
  993. }
  994. }
  995. return 0;
  996. }
  997. int w_newShader(lua_State *L)
  998. {
  999. bool gles = instance()->getRenderer() == Graphics::RENDERER_OPENGLES;
  1000. Shader::ShaderSource source;
  1001. w_getShaderSource(L, 1, gles, source);
  1002. bool should_error = false;
  1003. try
  1004. {
  1005. Shader *shader = instance()->newShader(source);
  1006. luax_pushtype(L, shader);
  1007. shader->release();
  1008. }
  1009. catch (love::Exception &e)
  1010. {
  1011. luax_getfunction(L, "graphics", "_transformGLSLErrorMessages");
  1012. lua_pushstring(L, e.what());
  1013. // Function pushes the new error string onto the stack.
  1014. lua_pcall(L, 1, 1, 0);
  1015. should_error = true;
  1016. }
  1017. if (should_error)
  1018. return lua_error(L);
  1019. return 1;
  1020. }
  1021. int w_validateShader(lua_State *L)
  1022. {
  1023. luaL_checktype(L, 1, LUA_TBOOLEAN);
  1024. bool gles = luax_toboolean(L, 1);
  1025. Shader::ShaderSource source;
  1026. w_getShaderSource(L, 2, gles, source);
  1027. std::string err;
  1028. bool success = instance()->validateShader(gles, source, err);
  1029. luax_pushboolean(L, success);
  1030. if (!success)
  1031. {
  1032. luax_pushstring(L, err);
  1033. return 2;
  1034. }
  1035. return 1;
  1036. }
  1037. static vertex::Usage luax_optmeshusage(lua_State *L, int idx, vertex::Usage def)
  1038. {
  1039. const char *usagestr = lua_isnoneornil(L, idx) ? nullptr : luaL_checkstring(L, idx);
  1040. if (usagestr && !vertex::getConstant(usagestr, def))
  1041. luaL_error(L, "Invalid usage hint: %s", usagestr);
  1042. return def;
  1043. }
  1044. static Mesh::DrawMode luax_optmeshdrawmode(lua_State *L, int idx, Mesh::DrawMode def)
  1045. {
  1046. const char *modestr = lua_isnoneornil(L, idx) ? nullptr : luaL_checkstring(L, idx);
  1047. if (modestr && !Mesh::getConstant(modestr, def))
  1048. luaL_error(L, "Invalid mesh draw mode: %s", modestr);
  1049. return def;
  1050. }
  1051. static Mesh *newStandardMesh(lua_State *L)
  1052. {
  1053. Mesh *t = nullptr;
  1054. Mesh::DrawMode drawmode = luax_optmeshdrawmode(L, 2, Mesh::DRAWMODE_FAN);
  1055. vertex::Usage usage = luax_optmeshusage(L, 3, vertex::USAGE_DYNAMIC);
  1056. // First argument is a table of standard vertices, or the number of
  1057. // standard vertices.
  1058. if (lua_istable(L, 1))
  1059. {
  1060. size_t vertexcount = luax_objlen(L, 1);
  1061. std::vector<Vertex> vertices;
  1062. vertices.reserve(vertexcount);
  1063. // Get the vertices from the table.
  1064. for (size_t i = 1; i <= vertexcount; i++)
  1065. {
  1066. lua_rawgeti(L, 1, (int) i);
  1067. if (lua_type(L, -1) != LUA_TTABLE)
  1068. {
  1069. luax_typerror(L, 1, "table of tables");
  1070. return nullptr;
  1071. }
  1072. for (int j = 1; j <= 8; j++)
  1073. lua_rawgeti(L, -j, j);
  1074. Vertex v;
  1075. v.x = (float) luaL_checknumber(L, -8);
  1076. v.y = (float) luaL_checknumber(L, -7);
  1077. v.s = (float) luaL_optnumber(L, -6, 0.0);
  1078. v.t = (float) luaL_optnumber(L, -5, 0.0);
  1079. v.color.r = (unsigned char) (luaL_optnumber(L, -4, 1.0) * 255.0);
  1080. v.color.g = (unsigned char) (luaL_optnumber(L, -3, 1.0) * 255.0);
  1081. v.color.b = (unsigned char) (luaL_optnumber(L, -2, 1.0) * 255.0);
  1082. v.color.a = (unsigned char) (luaL_optnumber(L, -1, 1.0) * 255.0);
  1083. lua_pop(L, 9);
  1084. vertices.push_back(v);
  1085. }
  1086. luax_catchexcept(L, [&](){ t = instance()->newMesh(vertices, drawmode, usage); });
  1087. }
  1088. else
  1089. {
  1090. int count = (int) luaL_checknumber(L, 1);
  1091. luax_catchexcept(L, [&](){ t = instance()->newMesh(count, drawmode, usage); });
  1092. }
  1093. return t;
  1094. }
  1095. static Mesh *newCustomMesh(lua_State *L)
  1096. {
  1097. Mesh *t = nullptr;
  1098. // First argument is the vertex format, second is a table of vertices or
  1099. // the number of vertices.
  1100. std::vector<Mesh::AttribFormat> vertexformat;
  1101. Mesh::DrawMode drawmode = luax_optmeshdrawmode(L, 3, Mesh::DRAWMODE_FAN);
  1102. vertex::Usage usage = luax_optmeshusage(L, 4, vertex::USAGE_DYNAMIC);
  1103. lua_rawgeti(L, 1, 1);
  1104. if (!lua_istable(L, -1))
  1105. {
  1106. luaL_argerror(L, 1, "table of tables expected");
  1107. return nullptr;
  1108. }
  1109. lua_pop(L, 1);
  1110. // Per-vertex attribute formats.
  1111. for (int i = 1; i <= (int) luax_objlen(L, 1); i++)
  1112. {
  1113. lua_rawgeti(L, 1, i);
  1114. // {name, datatype, components}
  1115. for (int j = 1; j <= 3; j++)
  1116. lua_rawgeti(L, -j, j);
  1117. Mesh::AttribFormat format;
  1118. format.name = luaL_checkstring(L, -3);
  1119. const char *tname = luaL_checkstring(L, -2);
  1120. if (!Mesh::getConstant(tname, format.type))
  1121. {
  1122. luaL_error(L, "Invalid Mesh vertex data type name: %s", tname);
  1123. return nullptr;
  1124. }
  1125. format.components = (int) luaL_checknumber(L, -1);
  1126. if (format.components <= 0 || format.components > 4)
  1127. {
  1128. luaL_error(L, "Number of vertex attribute components must be between 1 and 4 (got %d)", format.components);
  1129. return nullptr;
  1130. }
  1131. lua_pop(L, 4);
  1132. vertexformat.push_back(format);
  1133. }
  1134. if (lua_isnumber(L, 2))
  1135. {
  1136. int vertexcount = (int) luaL_checknumber(L, 2);
  1137. luax_catchexcept(L, [&](){ t = instance()->newMesh(vertexformat, vertexcount, drawmode, usage); });
  1138. }
  1139. else if (luax_istype(L, 2, Data::type))
  1140. {
  1141. // Vertex data comes directly from a Data object.
  1142. Data *data = luax_checktype<Data>(L, 2);
  1143. luax_catchexcept(L, [&](){ t = instance()->newMesh(vertexformat, data->getData(), data->getSize(), drawmode, usage); });
  1144. }
  1145. else
  1146. {
  1147. // Table of vertices.
  1148. lua_rawgeti(L, 2, 1);
  1149. if (!lua_istable(L, -1))
  1150. {
  1151. luaL_argerror(L, 2, "expected table of tables");
  1152. return nullptr;
  1153. }
  1154. lua_pop(L, 1);
  1155. int vertexcomponents = 0;
  1156. for (const Mesh::AttribFormat &format : vertexformat)
  1157. vertexcomponents += format.components;
  1158. size_t numvertices = luax_objlen(L, 2);
  1159. luax_catchexcept(L, [&](){ t = instance()->newMesh(vertexformat, numvertices, drawmode, usage); });
  1160. // Maximum possible data size for a single vertex attribute.
  1161. char data[sizeof(float) * 4];
  1162. for (size_t vertindex = 0; vertindex < numvertices; vertindex++)
  1163. {
  1164. // get vertices[vertindex]
  1165. lua_rawgeti(L, 2, vertindex + 1);
  1166. luaL_checktype(L, -1, LUA_TTABLE);
  1167. int n = 0;
  1168. for (size_t i = 0; i < vertexformat.size(); i++)
  1169. {
  1170. int components = vertexformat[i].components;
  1171. // get vertices[vertindex][n]
  1172. for (int c = 0; c < components; c++)
  1173. {
  1174. n++;
  1175. lua_rawgeti(L, -(c + 1), n);
  1176. }
  1177. // Fetch the values from Lua and store them in data buffer.
  1178. luax_writeAttributeData(L, -components, vertexformat[i].type, components, data);
  1179. lua_pop(L, components);
  1180. luax_catchexcept(L,
  1181. [&](){ t->setVertexAttribute(vertindex, i, data, sizeof(float) * 4); },
  1182. [&](bool diderror){ if (diderror) t->release(); }
  1183. );
  1184. }
  1185. lua_pop(L, 1); // pop vertices[vertindex]
  1186. }
  1187. t->flush();
  1188. }
  1189. return t;
  1190. }
  1191. int w_newMesh(lua_State *L)
  1192. {
  1193. luax_checkgraphicscreated(L);
  1194. // Check first argument: table or number of vertices.
  1195. int arg1type = lua_type(L, 1);
  1196. if (arg1type != LUA_TTABLE && arg1type != LUA_TNUMBER)
  1197. luaL_argerror(L, 1, "table or number expected");
  1198. Mesh *t = nullptr;
  1199. int arg2type = lua_type(L, 2);
  1200. if (arg1type == LUA_TTABLE && (arg2type == LUA_TTABLE || arg2type == LUA_TNUMBER || arg2type == LUA_TUSERDATA))
  1201. t = newCustomMesh(L);
  1202. else
  1203. t = newStandardMesh(L);
  1204. luax_pushtype(L, t);
  1205. t->release();
  1206. return 1;
  1207. }
  1208. int w_newText(lua_State *L)
  1209. {
  1210. luax_checkgraphicscreated(L);
  1211. graphics::Font *font = luax_checkfont(L, 1);
  1212. Text *t = nullptr;
  1213. if (lua_isnoneornil(L, 2))
  1214. luax_catchexcept(L, [&](){ t = instance()->newText(font); });
  1215. else
  1216. {
  1217. std::vector<Font::ColoredString> text;
  1218. luax_checkcoloredstring(L, 2, text);
  1219. luax_catchexcept(L, [&](){ t = instance()->newText(font, text); });
  1220. }
  1221. luax_pushtype(L, t);
  1222. t->release();
  1223. return 1;
  1224. }
  1225. int w_newVideo(lua_State *L)
  1226. {
  1227. luax_checkgraphicscreated(L);
  1228. if (!luax_istype(L, 1, love::video::VideoStream::type))
  1229. luax_convobj(L, 1, "video", "newVideoStream");
  1230. auto stream = luax_checktype<love::video::VideoStream>(L, 1);
  1231. float pixeldensity = (float) luaL_optnumber(L, 2, 1.0);
  1232. Video *video = nullptr;
  1233. luax_catchexcept(L, [&]() { video = instance()->newVideo(stream, pixeldensity); });
  1234. luax_pushtype(L, video);
  1235. video->release();
  1236. return 1;
  1237. }
  1238. int w_setColor(lua_State *L)
  1239. {
  1240. Colorf c;
  1241. if (lua_istable(L, 1))
  1242. {
  1243. for (int i = 1; i <= 4; i++)
  1244. lua_rawgeti(L, 1, i);
  1245. c.r = (float) luaL_checknumber(L, -4);
  1246. c.g = (float) luaL_checknumber(L, -3);
  1247. c.b = (float) luaL_checknumber(L, -2);
  1248. c.a = (float) luaL_optnumber(L, -1, 1.0);
  1249. lua_pop(L, 4);
  1250. }
  1251. else
  1252. {
  1253. c.r = (float) luaL_checknumber(L, 1);
  1254. c.g = (float) luaL_checknumber(L, 2);
  1255. c.b = (float) luaL_checknumber(L, 3);
  1256. c.a = (float) luaL_optnumber(L, 4, 1.0);
  1257. }
  1258. instance()->setColor(c);
  1259. return 0;
  1260. }
  1261. int w_getColor(lua_State *L)
  1262. {
  1263. Colorf c = instance()->getColor();
  1264. lua_pushnumber(L, c.r);
  1265. lua_pushnumber(L, c.g);
  1266. lua_pushnumber(L, c.b);
  1267. lua_pushnumber(L, c.a);
  1268. return 4;
  1269. }
  1270. int w_setBackgroundColor(lua_State *L)
  1271. {
  1272. Colorf c;
  1273. if (lua_istable(L, 1))
  1274. {
  1275. for (int i = 1; i <= 4; i++)
  1276. lua_rawgeti(L, 1, i);
  1277. c.r = (float) luaL_checknumber(L, -4);
  1278. c.g = (float) luaL_checknumber(L, -3);
  1279. c.b = (float) luaL_checknumber(L, -2);
  1280. c.a = (float) luaL_optnumber(L, -1, 1.0);
  1281. lua_pop(L, 4);
  1282. }
  1283. else
  1284. {
  1285. c.r = (float) luaL_checknumber(L, 1);
  1286. c.g = (float) luaL_checknumber(L, 2);
  1287. c.b = (float) luaL_checknumber(L, 3);
  1288. c.a = (float) luaL_optnumber(L, 4, 1.0);
  1289. }
  1290. instance()->setBackgroundColor(c);
  1291. return 0;
  1292. }
  1293. int w_getBackgroundColor(lua_State *L)
  1294. {
  1295. Colorf c = instance()->getBackgroundColor();
  1296. lua_pushnumber(L, c.r);
  1297. lua_pushnumber(L, c.g);
  1298. lua_pushnumber(L, c.b);
  1299. lua_pushnumber(L, c.a);
  1300. return 4;
  1301. }
  1302. int w_setNewFont(lua_State *L)
  1303. {
  1304. int ret = w_newFont(L);
  1305. Font *font = luax_checktype<Font>(L, -1);
  1306. instance()->setFont(font);
  1307. return ret;
  1308. }
  1309. int w_setFont(lua_State *L)
  1310. {
  1311. Font *font = luax_checktype<Font>(L, 1);
  1312. instance()->setFont(font);
  1313. return 0;
  1314. }
  1315. int w_getFont(lua_State *L)
  1316. {
  1317. Font *f = nullptr;
  1318. luax_catchexcept(L, [&](){ f = instance()->getFont(); });
  1319. luax_pushtype(L, f);
  1320. return 1;
  1321. }
  1322. int w_setColorMask(lua_State *L)
  1323. {
  1324. Graphics::ColorMask mask;
  1325. if (lua_gettop(L) <= 1 && lua_isnoneornil(L, 1))
  1326. {
  1327. // Enable all color components if no argument is given.
  1328. mask.r = mask.g = mask.b = mask.a = true;
  1329. }
  1330. else
  1331. {
  1332. mask.r = luax_toboolean(L, 1);
  1333. mask.g = luax_toboolean(L, 2);
  1334. mask.b = luax_toboolean(L, 3);
  1335. mask.a = luax_toboolean(L, 4);
  1336. }
  1337. instance()->setColorMask(mask);
  1338. return 0;
  1339. }
  1340. int w_getColorMask(lua_State *L)
  1341. {
  1342. Graphics::ColorMask mask = instance()->getColorMask();
  1343. luax_pushboolean(L, mask.r);
  1344. luax_pushboolean(L, mask.g);
  1345. luax_pushboolean(L, mask.b);
  1346. luax_pushboolean(L, mask.a);
  1347. return 4;
  1348. }
  1349. int w_setBlendMode(lua_State *L)
  1350. {
  1351. Graphics::BlendMode mode;
  1352. const char *str = luaL_checkstring(L, 1);
  1353. if (!Graphics::getConstant(str, mode))
  1354. return luaL_error(L, "Invalid blend mode: %s", str);
  1355. Graphics::BlendAlpha alphamode = Graphics::BLENDALPHA_MULTIPLY;
  1356. if (!lua_isnoneornil(L, 2))
  1357. {
  1358. const char *alphastr = luaL_checkstring(L, 2);
  1359. if (!Graphics::getConstant(alphastr, alphamode))
  1360. return luaL_error(L, "Invalid blend alpha mode: %s", alphastr);
  1361. }
  1362. luax_catchexcept(L, [&](){ instance()->setBlendMode(mode, alphamode); });
  1363. return 0;
  1364. }
  1365. int w_getBlendMode(lua_State *L)
  1366. {
  1367. const char *str;
  1368. const char *alphastr;
  1369. Graphics::BlendAlpha alphamode;
  1370. Graphics::BlendMode mode = instance()->getBlendMode(alphamode);
  1371. if (!Graphics::getConstant(mode, str))
  1372. return luaL_error(L, "Unknown blend mode");
  1373. if (!Graphics::getConstant(alphamode, alphastr))
  1374. return luaL_error(L, "Unknown blend alpha mode");
  1375. lua_pushstring(L, str);
  1376. lua_pushstring(L, alphastr);
  1377. return 2;
  1378. }
  1379. int w_setDefaultFilter(lua_State *L)
  1380. {
  1381. Texture::Filter f;
  1382. const char *minstr = luaL_checkstring(L, 1);
  1383. const char *magstr = luaL_optstring(L, 2, minstr);
  1384. if (!Texture::getConstant(minstr, f.min))
  1385. return luaL_error(L, "Invalid filter mode: %s", minstr);
  1386. if (!Texture::getConstant(magstr, f.mag))
  1387. return luaL_error(L, "Invalid filter mode: %s", magstr);
  1388. f.anisotropy = (float) luaL_optnumber(L, 3, 1.0);
  1389. instance()->setDefaultFilter(f);
  1390. return 0;
  1391. }
  1392. int w_getDefaultFilter(lua_State *L)
  1393. {
  1394. const Texture::Filter &f = instance()->getDefaultFilter();
  1395. const char *minstr;
  1396. const char *magstr;
  1397. if (!Texture::getConstant(f.min, minstr))
  1398. return luaL_error(L, "Unknown minification filter mode");
  1399. if (!Texture::getConstant(f.mag, magstr))
  1400. return luaL_error(L, "Unknown magnification filter mode");
  1401. lua_pushstring(L, minstr);
  1402. lua_pushstring(L, magstr);
  1403. lua_pushnumber(L, f.anisotropy);
  1404. return 3;
  1405. }
  1406. int w_setDefaultMipmapFilter(lua_State *L)
  1407. {
  1408. Texture::FilterMode filter = Texture::FILTER_NONE;
  1409. if (!lua_isnoneornil(L, 1))
  1410. {
  1411. const char *str = luaL_checkstring(L, 1);
  1412. if (!Texture::getConstant(str, filter))
  1413. return luaL_error(L, "Invalid filter mode: %s", str);
  1414. }
  1415. float sharpness = (float) luaL_optnumber(L, 2, 0);
  1416. instance()->setDefaultMipmapFilter(filter, sharpness);
  1417. return 0;
  1418. }
  1419. int w_getDefaultMipmapFilter(lua_State *L)
  1420. {
  1421. Texture::FilterMode filter;
  1422. float sharpness;
  1423. instance()->getDefaultMipmapFilter(&filter, &sharpness);
  1424. const char *str;
  1425. if (Texture::getConstant(filter, str))
  1426. lua_pushstring(L, str);
  1427. else
  1428. lua_pushnil(L);
  1429. lua_pushnumber(L, sharpness);
  1430. return 2;
  1431. }
  1432. int w_setLineWidth(lua_State *L)
  1433. {
  1434. float width = (float)luaL_checknumber(L, 1);
  1435. instance()->setLineWidth(width);
  1436. return 0;
  1437. }
  1438. int w_setLineStyle(lua_State *L)
  1439. {
  1440. Graphics::LineStyle style;
  1441. const char *str = luaL_checkstring(L, 1);
  1442. if (!Graphics::getConstant(str, style))
  1443. return luaL_error(L, "Invalid line style: %s", str);
  1444. instance()->setLineStyle(style);
  1445. return 0;
  1446. }
  1447. int w_setLineJoin(lua_State *L)
  1448. {
  1449. Graphics::LineJoin join;
  1450. const char *str = luaL_checkstring(L, 1);
  1451. if (!Graphics::getConstant(str, join))
  1452. return luaL_error(L, "Invalid line join mode: %s", str);
  1453. instance()->setLineJoin(join);
  1454. return 0;
  1455. }
  1456. int w_getLineWidth(lua_State *L)
  1457. {
  1458. lua_pushnumber(L, instance()->getLineWidth());
  1459. return 1;
  1460. }
  1461. int w_getLineStyle(lua_State *L)
  1462. {
  1463. Graphics::LineStyle style = instance()->getLineStyle();
  1464. const char *str;
  1465. if (!Graphics::getConstant(style, str))
  1466. return luaL_error(L, "Unknown line style");
  1467. lua_pushstring(L, str);
  1468. return 1;
  1469. }
  1470. int w_getLineJoin(lua_State *L)
  1471. {
  1472. Graphics::LineJoin join = instance()->getLineJoin();
  1473. const char *str;
  1474. if (!Graphics::getConstant(join, str))
  1475. return luaL_error(L, "Unknown line join");
  1476. lua_pushstring(L, str);
  1477. return 1;
  1478. }
  1479. int w_setPointSize(lua_State *L)
  1480. {
  1481. float size = (float)luaL_checknumber(L, 1);
  1482. instance()->setPointSize(size);
  1483. return 0;
  1484. }
  1485. int w_getPointSize(lua_State *L)
  1486. {
  1487. lua_pushnumber(L, instance()->getPointSize());
  1488. return 1;
  1489. }
  1490. int w_setWireframe(lua_State *L)
  1491. {
  1492. instance()->setWireframe(luax_toboolean(L, 1));
  1493. return 0;
  1494. }
  1495. int w_isWireframe(lua_State *L)
  1496. {
  1497. luax_pushboolean(L, instance()->isWireframe());
  1498. return 1;
  1499. }
  1500. int w_setShader(lua_State *L)
  1501. {
  1502. if (lua_isnoneornil(L,1))
  1503. {
  1504. instance()->setShader();
  1505. return 0;
  1506. }
  1507. Shader *shader = luax_checkshader(L, 1);
  1508. instance()->setShader(shader);
  1509. return 0;
  1510. }
  1511. int w_getShader(lua_State *L)
  1512. {
  1513. Shader *shader = instance()->getShader();
  1514. if (shader)
  1515. luax_pushtype(L, shader);
  1516. else
  1517. lua_pushnil(L);
  1518. return 1;
  1519. }
  1520. int w_setDefaultShaderCode(lua_State *L)
  1521. {
  1522. for (int i = 0; i < 2; i++)
  1523. {
  1524. luaL_checktype(L, i + 1, LUA_TTABLE);
  1525. for (int lang = 0; lang < Shader::LANGUAGE_MAX_ENUM; lang++)
  1526. {
  1527. const char *langname;
  1528. if (!Shader::getConstant((Shader::Language) lang, langname))
  1529. continue;
  1530. lua_getfield(L, i + 1, langname);
  1531. lua_getfield(L, -1, "vertex");
  1532. lua_getfield(L, -2, "pixel");
  1533. lua_getfield(L, -3, "videopixel");
  1534. lua_getfield(L, -4, "arraypixel");
  1535. Shader::ShaderSource code;
  1536. code.vertex = luax_checkstring(L, -4);
  1537. code.pixel = luax_checkstring(L, -3);
  1538. Shader::ShaderSource videocode;
  1539. videocode.vertex = luax_checkstring(L, -4);
  1540. videocode.pixel = luax_checkstring(L, -2);
  1541. Shader::ShaderSource arraycode;
  1542. arraycode.vertex = luax_checkstring(L, -4);
  1543. arraycode.pixel = luax_checkstring(L, -1);
  1544. lua_pop(L, 5);
  1545. Graphics::defaultShaderCode[Shader::STANDARD_DEFAULT][lang][i] = code;
  1546. Graphics::defaultShaderCode[Shader::STANDARD_VIDEO][lang][i] = videocode;
  1547. Graphics::defaultShaderCode[Shader::STANDARD_ARRAY][lang][i] = arraycode;
  1548. }
  1549. }
  1550. return 0;
  1551. }
  1552. int w_getSupported(lua_State *L)
  1553. {
  1554. lua_createtable(L, 0, (int) Graphics::FEATURE_MAX_ENUM);
  1555. for (int i = 0; i < (int) Graphics::FEATURE_MAX_ENUM; i++)
  1556. {
  1557. auto feature = (Graphics::Feature) i;
  1558. const char *name = nullptr;
  1559. if (!Graphics::getConstant(feature, name))
  1560. continue;
  1561. luax_pushboolean(L, instance()->isSupported(feature));
  1562. lua_setfield(L, -2, name);
  1563. }
  1564. return 1;
  1565. }
  1566. static int w__getFormats(lua_State *L, bool (*isFormatSupported)(PixelFormat), bool (*ignore)(PixelFormat))
  1567. {
  1568. lua_createtable(L, 0, (int) PIXELFORMAT_MAX_ENUM);
  1569. for (int i = 0; i < (int) PIXELFORMAT_MAX_ENUM; i++)
  1570. {
  1571. PixelFormat format = (PixelFormat) i;
  1572. const char *name = nullptr;
  1573. if (format == PIXELFORMAT_UNKNOWN || !love::getConstant(format, name) || ignore(format))
  1574. continue;
  1575. luax_pushboolean(L, isFormatSupported(format));
  1576. lua_setfield(L, -2, name);
  1577. }
  1578. return 1;
  1579. }
  1580. int w_getCanvasFormats(lua_State *L)
  1581. {
  1582. bool (*supported)(PixelFormat);
  1583. if (!lua_isnoneornil(L, 1))
  1584. {
  1585. luaL_checktype(L, 1, LUA_TBOOLEAN);
  1586. if (luax_toboolean(L, 1))
  1587. {
  1588. supported = [](PixelFormat format) -> bool
  1589. {
  1590. return instance()->isCanvasFormatSupported(format, true);
  1591. };
  1592. }
  1593. else
  1594. {
  1595. supported = [](PixelFormat format) -> bool
  1596. {
  1597. return instance()->isCanvasFormatSupported(format, false);
  1598. };
  1599. }
  1600. }
  1601. else
  1602. {
  1603. supported = [](PixelFormat format) -> bool
  1604. {
  1605. return instance()->isCanvasFormatSupported(format);
  1606. };
  1607. }
  1608. return w__getFormats(L, supported, isPixelFormatCompressed);
  1609. }
  1610. int w_getImageFormats(lua_State *L)
  1611. {
  1612. const auto supported = [](PixelFormat format) -> bool
  1613. {
  1614. return instance()->isImageFormatSupported(format);
  1615. };
  1616. const auto ignore = [](PixelFormat format) -> bool
  1617. {
  1618. return !(image::ImageData::validPixelFormat(format) || isPixelFormatCompressed(format));
  1619. };
  1620. return w__getFormats(L, supported, ignore);
  1621. }
  1622. int w_getRendererInfo(lua_State *L)
  1623. {
  1624. Graphics::RendererInfo info;
  1625. luax_catchexcept(L, [&](){ info = instance()->getRendererInfo(); });
  1626. luax_pushstring(L, info.name);
  1627. luax_pushstring(L, info.version);
  1628. luax_pushstring(L, info.vendor);
  1629. luax_pushstring(L, info.device);
  1630. return 4;
  1631. }
  1632. int w_getSystemLimits(lua_State *L)
  1633. {
  1634. lua_createtable(L, 0, (int) Graphics::LIMIT_MAX_ENUM);
  1635. for (int i = 0; i < (int) Graphics::LIMIT_MAX_ENUM; i++)
  1636. {
  1637. Graphics::SystemLimit limittype = (Graphics::SystemLimit) i;
  1638. const char *name = nullptr;
  1639. if (!Graphics::getConstant(limittype, name))
  1640. continue;
  1641. lua_pushnumber(L, instance()->getSystemLimit(limittype));
  1642. lua_setfield(L, -2, name);
  1643. }
  1644. return 1;
  1645. }
  1646. int w_getStats(lua_State *L)
  1647. {
  1648. Graphics::Stats stats = instance()->getStats();
  1649. lua_createtable(L, 0, 7);
  1650. lua_pushinteger(L, stats.drawCalls);
  1651. lua_setfield(L, -2, "drawcalls");
  1652. lua_pushinteger(L, stats.canvasSwitches);
  1653. lua_setfield(L, -2, "canvasswitches");
  1654. lua_pushinteger(L, stats.shaderSwitches);
  1655. lua_setfield(L, -2, "shaderswitches");
  1656. lua_pushinteger(L, stats.canvases);
  1657. lua_setfield(L, -2, "canvases");
  1658. lua_pushinteger(L, stats.images);
  1659. lua_setfield(L, -2, "images");
  1660. lua_pushinteger(L, stats.fonts);
  1661. lua_setfield(L, -2, "fonts");
  1662. lua_pushinteger(L, stats.textureMemory);
  1663. lua_setfield(L, -2, "texturememory");
  1664. return 1;
  1665. }
  1666. int w_draw(lua_State *L)
  1667. {
  1668. Drawable *drawable = nullptr;
  1669. Texture *texture = nullptr;
  1670. Quad *quad = nullptr;
  1671. int startidx = 2;
  1672. if (luax_istype(L, 2, Quad::type))
  1673. {
  1674. texture = luax_checktexture(L, 1);
  1675. quad = luax_totype<Quad>(L, 2);
  1676. startidx = 3;
  1677. }
  1678. else if (lua_isnil(L, 2) && !lua_isnoneornil(L, 3))
  1679. {
  1680. return luax_typerror(L, 2, "Quad");
  1681. }
  1682. else
  1683. {
  1684. drawable = luax_checktype<Drawable>(L, 1);
  1685. startidx = 2;
  1686. }
  1687. luax_checkstandardtransform(L, startidx, [&](const Matrix4 &m)
  1688. {
  1689. luax_catchexcept(L, [&]()
  1690. {
  1691. if (texture && quad)
  1692. instance()->draw(texture, quad, m);
  1693. else
  1694. instance()->draw(drawable, m);
  1695. });
  1696. });
  1697. return 0;
  1698. }
  1699. int w_drawLayer(lua_State *L)
  1700. {
  1701. Texture *texture = luax_checktexture(L, 1);
  1702. Quad *quad = nullptr;
  1703. int layer = (int) luaL_checknumber(L, 2) - 1;
  1704. int startidx = 3;
  1705. if (luax_istype(L, startidx, Quad::type))
  1706. {
  1707. texture = luax_checktexture(L, 1);
  1708. quad = luax_totype<Quad>(L, startidx);
  1709. startidx++;
  1710. }
  1711. else if (lua_isnil(L, startidx) && !lua_isnoneornil(L, startidx + 1))
  1712. {
  1713. return luax_typerror(L, startidx, "Quad");
  1714. }
  1715. luax_checkstandardtransform(L, startidx, [&](const Matrix4 &m)
  1716. {
  1717. luax_catchexcept(L, [&]()
  1718. {
  1719. if (quad)
  1720. instance()->drawLayer(texture, layer, quad, m);
  1721. else
  1722. instance()->drawLayer(texture, layer, m);
  1723. });
  1724. });
  1725. return 0;
  1726. }
  1727. int w_drawInstanced(lua_State *L)
  1728. {
  1729. Mesh *t = luax_checkmesh(L, 1);
  1730. int instancecount = (int) luaL_checkinteger(L, 2);
  1731. luax_checkstandardtransform(L, 3, [&](const Matrix4 &m)
  1732. {
  1733. luax_catchexcept(L, [&]() { instance()->drawInstanced(t, m, instancecount); });
  1734. });
  1735. return 0;
  1736. }
  1737. int w_print(lua_State *L)
  1738. {
  1739. std::vector<Font::ColoredString> str;
  1740. luax_checkcoloredstring(L, 1, str);
  1741. if (luax_istype(L, 2, Font::type))
  1742. {
  1743. Font *font = luax_checkfont(L, 2);
  1744. luax_checkstandardtransform(L, 3, [&](const Matrix4 &m)
  1745. {
  1746. luax_catchexcept(L, [&](){ instance()->print(str, font, m); });
  1747. });
  1748. }
  1749. else
  1750. {
  1751. luax_checkstandardtransform(L, 2, [&](const Matrix4 &m)
  1752. {
  1753. luax_catchexcept(L, [&](){ instance()->print(str, m); });
  1754. });
  1755. }
  1756. return 0;
  1757. }
  1758. int w_printf(lua_State *L)
  1759. {
  1760. std::vector<Font::ColoredString> str;
  1761. luax_checkcoloredstring(L, 1, str);
  1762. Font *font = nullptr;
  1763. int startidx = 2;
  1764. if (luax_istype(L, startidx, Font::type))
  1765. {
  1766. font = luax_checkfont(L, startidx);
  1767. startidx++;
  1768. }
  1769. Font::AlignMode align = Font::ALIGN_LEFT;
  1770. Matrix4 m;
  1771. int formatidx = startidx + 2;
  1772. if (luax_istype(L, startidx, math::Transform::type))
  1773. {
  1774. math::Transform *tf = luax_totype<math::Transform>(L, startidx);
  1775. m = tf->getMatrix();
  1776. formatidx = startidx + 1;
  1777. }
  1778. else
  1779. {
  1780. float x = (float)luaL_checknumber(L, startidx + 0);
  1781. float y = (float)luaL_checknumber(L, startidx + 1);
  1782. float angle = (float) luaL_optnumber(L, startidx + 4, 0.0f);
  1783. float sx = (float) luaL_optnumber(L, startidx + 5, 1.0f);
  1784. float sy = (float) luaL_optnumber(L, startidx + 6, sx);
  1785. float ox = (float) luaL_optnumber(L, startidx + 7, 0.0f);
  1786. float oy = (float) luaL_optnumber(L, startidx + 8, 0.0f);
  1787. float kx = (float) luaL_optnumber(L, startidx + 9, 0.0f);
  1788. float ky = (float) luaL_optnumber(L, startidx + 10, 0.0f);
  1789. m = Matrix4(x, y, angle, sx, sy, ox, oy, kx, ky);
  1790. }
  1791. float wrap = (float)luaL_checknumber(L, formatidx);
  1792. const char *astr = lua_isnoneornil(L, formatidx + 1) ? nullptr : luaL_checkstring(L, formatidx + 1);
  1793. if (astr != nullptr && !Font::getConstant(astr, align))
  1794. return luaL_error(L, "Incorrect alignment: %s", astr);
  1795. if (font != nullptr)
  1796. luax_catchexcept(L, [&](){ instance()->printf(str, font, wrap, align, m); });
  1797. else
  1798. luax_catchexcept(L, [&](){ instance()->printf(str, wrap, align, m); });
  1799. return 0;
  1800. }
  1801. int w_points(lua_State *L)
  1802. {
  1803. // love.graphics.points has 3 variants:
  1804. // - points(x1, y1, x2, y2, ...)
  1805. // - points({x1, y1, x2, y2, ...})
  1806. // - points({{x1, y1 [, r, g, b, a]}, {x2, y2 [, r, g, b, a]}, ...})
  1807. int args = lua_gettop(L);
  1808. bool is_table = false;
  1809. bool is_table_of_tables = false;
  1810. if (args == 1 && lua_istable(L, 1))
  1811. {
  1812. is_table = true;
  1813. args = (int) luax_objlen(L, 1);
  1814. lua_rawgeti(L, 1, 1);
  1815. is_table_of_tables = lua_istable(L, -1);
  1816. lua_pop(L, 1);
  1817. }
  1818. if (args % 2 != 0 && !is_table_of_tables)
  1819. return luaL_error(L, "Number of vertex components must be a multiple of two");
  1820. int numpoints = args / 2;
  1821. if (is_table_of_tables)
  1822. numpoints = args;
  1823. float *coords = nullptr;
  1824. Colorf *colors = nullptr;
  1825. if (is_table_of_tables)
  1826. {
  1827. size_t datasize = (sizeof(float) * 2 + sizeof(Colorf)) * numpoints;
  1828. uint8 *data = instance()->getScratchBuffer<uint8>(datasize);
  1829. coords = (float *) data;
  1830. colors = (Colorf *) (data + sizeof(float) * numpoints * 2);
  1831. }
  1832. else
  1833. coords = instance()->getScratchBuffer<float>(numpoints * 2);
  1834. if (is_table)
  1835. {
  1836. if (is_table_of_tables)
  1837. {
  1838. // points({{x1, y1 [, r, g, b, a]}, {x2, y2 [, r, g, b, a]}, ...})
  1839. for (int i = 0; i < args; i++)
  1840. {
  1841. lua_rawgeti(L, 1, i + 1);
  1842. for (int j = 1; j <= 6; j++)
  1843. lua_rawgeti(L, -j, j);
  1844. coords[i * 2 + 0] = luax_tofloat(L, -6);
  1845. coords[i * 2 + 1] = luax_tofloat(L, -5);
  1846. colors[i].r = luaL_optnumber(L, -4, 1.0);
  1847. colors[i].g = luaL_optnumber(L, -3, 1.0);
  1848. colors[i].b = luaL_optnumber(L, -2, 1.0);
  1849. colors[i].a = luaL_optnumber(L, -1, 1.0);
  1850. lua_pop(L, 7);
  1851. }
  1852. }
  1853. else
  1854. {
  1855. // points({x1, y1, x2, y2, ...})
  1856. for (int i = 0; i < args; i++)
  1857. {
  1858. lua_rawgeti(L, 1, i + 1);
  1859. coords[i] = luax_tofloat(L, -1);
  1860. lua_pop(L, 1);
  1861. }
  1862. }
  1863. }
  1864. else
  1865. {
  1866. for (int i = 0; i < args; i++)
  1867. coords[i] = luax_tofloat(L, i + 1);
  1868. }
  1869. luax_catchexcept(L, [&](){ instance()->points(coords, colors, numpoints); });
  1870. return 0;
  1871. }
  1872. int w_line(lua_State *L)
  1873. {
  1874. int args = lua_gettop(L);
  1875. bool is_table = false;
  1876. if (args == 1 && lua_istable(L, 1))
  1877. {
  1878. args = (int) luax_objlen(L, 1);
  1879. is_table = true;
  1880. }
  1881. if (args % 2 != 0)
  1882. return luaL_error(L, "Number of vertex components must be a multiple of two");
  1883. else if (args < 4)
  1884. return luaL_error(L, "Need at least two vertices to draw a line");
  1885. float *coords = instance()->getScratchBuffer<float>(args);
  1886. if (is_table)
  1887. {
  1888. for (int i = 0; i < args; ++i)
  1889. {
  1890. lua_rawgeti(L, 1, i + 1);
  1891. coords[i] = luax_tofloat(L, -1);
  1892. lua_pop(L, 1);
  1893. }
  1894. }
  1895. else
  1896. {
  1897. for (int i = 0; i < args; ++i)
  1898. coords[i] = luax_tofloat(L, i + 1);
  1899. }
  1900. luax_catchexcept(L,
  1901. [&](){ instance()->polyline(coords, args); }
  1902. );
  1903. return 0;
  1904. }
  1905. int w_rectangle(lua_State *L)
  1906. {
  1907. Graphics::DrawMode mode;
  1908. const char *str = luaL_checkstring(L, 1);
  1909. if (!Graphics::getConstant(str, mode))
  1910. return luaL_error(L, "Invalid draw mode: %s", str);
  1911. float x = (float)luaL_checknumber(L, 2);
  1912. float y = (float)luaL_checknumber(L, 3);
  1913. float w = (float)luaL_checknumber(L, 4);
  1914. float h = (float)luaL_checknumber(L, 5);
  1915. if (lua_isnoneornil(L, 6))
  1916. {
  1917. instance()->rectangle(mode, x, y, w, h);
  1918. return 0;
  1919. }
  1920. float rx = (float)luaL_optnumber(L, 6, 0.0);
  1921. float ry = (float)luaL_optnumber(L, 7, rx);
  1922. if (lua_isnoneornil(L, 8))
  1923. luax_catchexcept(L, [&](){ instance()->rectangle(mode, x, y, w, h, rx, ry); });
  1924. else
  1925. {
  1926. int points = (int) luaL_checknumber(L, 8);
  1927. luax_catchexcept(L, [&](){ instance()->rectangle(mode, x, y, w, h, rx, ry, points); });
  1928. }
  1929. return 0;
  1930. }
  1931. int w_circle(lua_State *L)
  1932. {
  1933. Graphics::DrawMode mode;
  1934. const char *str = luaL_checkstring(L, 1);
  1935. if (!Graphics::getConstant(str, mode))
  1936. return luaL_error(L, "Invalid draw mode: %s", str);
  1937. float x = (float)luaL_checknumber(L, 2);
  1938. float y = (float)luaL_checknumber(L, 3);
  1939. float radius = (float)luaL_checknumber(L, 4);
  1940. if (lua_isnoneornil(L, 5))
  1941. luax_catchexcept(L, [&](){ instance()->circle(mode, x, y, radius); });
  1942. else
  1943. {
  1944. int points = (int) luaL_checknumber(L, 5);
  1945. luax_catchexcept(L, [&](){ instance()->circle(mode, x, y, radius, points); });
  1946. }
  1947. return 0;
  1948. }
  1949. int w_ellipse(lua_State *L)
  1950. {
  1951. Graphics::DrawMode mode;
  1952. const char *str = luaL_checkstring(L, 1);
  1953. if (!Graphics::getConstant(str, mode))
  1954. return luaL_error(L, "Invalid draw mode: %s", str);
  1955. float x = (float)luaL_checknumber(L, 2);
  1956. float y = (float)luaL_checknumber(L, 3);
  1957. float a = (float)luaL_checknumber(L, 4);
  1958. float b = (float)luaL_optnumber(L, 5, a);
  1959. if (lua_isnoneornil(L, 6))
  1960. luax_catchexcept(L, [&](){ instance()->ellipse(mode, x, y, a, b); });
  1961. else
  1962. {
  1963. int points = (int) luaL_checknumber(L, 6);
  1964. luax_catchexcept(L, [&](){ instance()->ellipse(mode, x, y, a, b, points); });
  1965. }
  1966. return 0;
  1967. }
  1968. int w_arc(lua_State *L)
  1969. {
  1970. Graphics::DrawMode drawmode;
  1971. const char *drawstr = luaL_checkstring(L, 1);
  1972. if (!Graphics::getConstant(drawstr, drawmode))
  1973. return luaL_error(L, "Invalid draw mode: %s", drawstr);
  1974. int startidx = 2;
  1975. Graphics::ArcMode arcmode = Graphics::ARC_PIE;
  1976. if (lua_type(L, 2) == LUA_TSTRING)
  1977. {
  1978. const char *arcstr = luaL_checkstring(L, 2);
  1979. if (!Graphics::getConstant(arcstr, arcmode))
  1980. return luaL_error(L, "Invalid arc mode: %s", arcstr);
  1981. startidx = 3;
  1982. }
  1983. float x = (float) luaL_checknumber(L, startidx + 0);
  1984. float y = (float) luaL_checknumber(L, startidx + 1);
  1985. float radius = (float) luaL_checknumber(L, startidx + 2);
  1986. float angle1 = (float) luaL_checknumber(L, startidx + 3);
  1987. float angle2 = (float) luaL_checknumber(L, startidx + 4);
  1988. if (lua_isnoneornil(L, startidx + 5))
  1989. luax_catchexcept(L, [&](){ instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2); });
  1990. else
  1991. {
  1992. int points = (int) luaL_checknumber(L, startidx + 5);
  1993. luax_catchexcept(L, [&](){ instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2, points); });
  1994. }
  1995. return 0;
  1996. }
  1997. int w_polygon(lua_State *L)
  1998. {
  1999. int args = lua_gettop(L) - 1;
  2000. Graphics::DrawMode mode;
  2001. const char *str = luaL_checkstring(L, 1);
  2002. if (!Graphics::getConstant(str, mode))
  2003. return luaL_error(L, "Invalid draw mode: %s", str);
  2004. bool is_table = false;
  2005. if (args == 1 && lua_istable(L, 2))
  2006. {
  2007. args = (int) luax_objlen(L, 2);
  2008. is_table = true;
  2009. }
  2010. if (args % 2 != 0)
  2011. return luaL_error(L, "Number of vertex components must be a multiple of two");
  2012. else if (args < 6)
  2013. return luaL_error(L, "Need at least three vertices to draw a polygon");
  2014. // fetch coords
  2015. float *coords = instance()->getScratchBuffer<float>(args + 2);
  2016. if (is_table)
  2017. {
  2018. for (int i = 0; i < args; ++i)
  2019. {
  2020. lua_rawgeti(L, 2, i + 1);
  2021. coords[i] = luax_tofloat(L, -1);
  2022. lua_pop(L, 1);
  2023. }
  2024. }
  2025. else
  2026. {
  2027. for (int i = 0; i < args; ++i)
  2028. coords[i] = luax_tofloat(L, i + 2);
  2029. }
  2030. // make a closed loop
  2031. coords[args] = coords[0];
  2032. coords[args+1] = coords[1];
  2033. luax_catchexcept(L, [&](){ instance()->polygon(mode, coords, args+2); });
  2034. return 0;
  2035. }
  2036. int w_flushBatch(lua_State *)
  2037. {
  2038. instance()->flushStreamDraws();
  2039. return 0;
  2040. }
  2041. int w_push(lua_State *L)
  2042. {
  2043. Graphics::StackType stype = Graphics::STACK_TRANSFORM;
  2044. const char *sname = lua_isnoneornil(L, 1) ? nullptr : luaL_checkstring(L, 1);
  2045. if (sname && !Graphics::getConstant(sname, stype))
  2046. return luaL_error(L, "Invalid graphics stack type: %s", sname);
  2047. luax_catchexcept(L, [&](){ instance()->push(stype); });
  2048. if (luax_istype(L, 2, math::Transform::type))
  2049. {
  2050. math::Transform *t = luax_totype<math::Transform>(L, 2);
  2051. instance()->applyTransform(t);
  2052. }
  2053. return 0;
  2054. }
  2055. int w_pop(lua_State *L)
  2056. {
  2057. luax_catchexcept(L, [&](){ instance()->pop(); });
  2058. return 0;
  2059. }
  2060. int w_rotate(lua_State *L)
  2061. {
  2062. float angle = (float)luaL_checknumber(L, 1);
  2063. instance()->rotate(angle);
  2064. return 0;
  2065. }
  2066. int w_scale(lua_State *L)
  2067. {
  2068. float sx = (float)luaL_optnumber(L, 1, 1.0f);
  2069. float sy = (float)luaL_optnumber(L, 2, sx);
  2070. instance()->scale(sx, sy);
  2071. return 0;
  2072. }
  2073. int w_translate(lua_State *L)
  2074. {
  2075. float x = (float)luaL_checknumber(L, 1);
  2076. float y = (float)luaL_checknumber(L, 2);
  2077. instance()->translate(x, y);
  2078. return 0;
  2079. }
  2080. int w_shear(lua_State *L)
  2081. {
  2082. float kx = (float)luaL_checknumber(L, 1);
  2083. float ky = (float)luaL_checknumber(L, 2);
  2084. instance()->shear(kx, ky);
  2085. return 0;
  2086. }
  2087. int w_origin(lua_State * /*L*/)
  2088. {
  2089. instance()->origin();
  2090. return 0;
  2091. }
  2092. int w_applyTransform(lua_State *L)
  2093. {
  2094. math::Transform *t = math::luax_checktransform(L, 1);
  2095. instance()->applyTransform(t);
  2096. return 0;
  2097. }
  2098. int w_replaceTransform(lua_State *L)
  2099. {
  2100. math::Transform *t = math::luax_checktransform(L, 1);
  2101. instance()->replaceTransform(t);
  2102. return 0;
  2103. }
  2104. int w_transformPoint(lua_State *L)
  2105. {
  2106. Vector p;
  2107. p.x = (float) luaL_checknumber(L, 1);
  2108. p.y = (float) luaL_checknumber(L, 2);
  2109. p = instance()->transformPoint(p);
  2110. lua_pushnumber(L, p.x);
  2111. lua_pushnumber(L, p.y);
  2112. return 2;
  2113. }
  2114. int w_inverseTransformPoint(lua_State *L)
  2115. {
  2116. Vector p;
  2117. p.x = (float) luaL_checknumber(L, 1);
  2118. p.y = (float) luaL_checknumber(L, 2);
  2119. p = instance()->inverseTransformPoint(p);
  2120. lua_pushnumber(L, p.x);
  2121. lua_pushnumber(L, p.y);
  2122. return 2;
  2123. }
  2124. // List of functions to wrap.
  2125. static const luaL_Reg functions[] =
  2126. {
  2127. { "reset", w_reset },
  2128. { "clear", w_clear },
  2129. { "discard", w_discard },
  2130. { "present", w_present },
  2131. { "newImage", w_newImage },
  2132. { "newArrayImage", w_newArrayImage },
  2133. { "newVolumeImage", w_newVolumeImage },
  2134. { "newCubeImage", w_newCubeImage },
  2135. { "newQuad", w_newQuad },
  2136. { "newFont", w_newFont },
  2137. { "newImageFont", w_newImageFont },
  2138. { "newSpriteBatch", w_newSpriteBatch },
  2139. { "newParticleSystem", w_newParticleSystem },
  2140. { "newCanvas", w_newCanvas },
  2141. { "newShader", w_newShader },
  2142. { "newMesh", w_newMesh },
  2143. { "newText", w_newText },
  2144. { "_newVideo", w_newVideo },
  2145. { "validateShader", w_validateShader },
  2146. { "setCanvas", w_setCanvas },
  2147. { "getCanvas", w_getCanvas },
  2148. { "setColor", w_setColor },
  2149. { "getColor", w_getColor },
  2150. { "setBackgroundColor", w_setBackgroundColor },
  2151. { "getBackgroundColor", w_getBackgroundColor },
  2152. { "setNewFont", w_setNewFont },
  2153. { "setFont", w_setFont },
  2154. { "getFont", w_getFont },
  2155. { "setColorMask", w_setColorMask },
  2156. { "getColorMask", w_getColorMask },
  2157. { "setBlendMode", w_setBlendMode },
  2158. { "getBlendMode", w_getBlendMode },
  2159. { "setDefaultFilter", w_setDefaultFilter },
  2160. { "getDefaultFilter", w_getDefaultFilter },
  2161. { "setDefaultMipmapFilter", w_setDefaultMipmapFilter },
  2162. { "getDefaultMipmapFilter", w_getDefaultMipmapFilter },
  2163. { "setLineWidth", w_setLineWidth },
  2164. { "setLineStyle", w_setLineStyle },
  2165. { "setLineJoin", w_setLineJoin },
  2166. { "getLineWidth", w_getLineWidth },
  2167. { "getLineStyle", w_getLineStyle },
  2168. { "getLineJoin", w_getLineJoin },
  2169. { "setPointSize", w_setPointSize },
  2170. { "getPointSize", w_getPointSize },
  2171. { "setWireframe", w_setWireframe },
  2172. { "isWireframe", w_isWireframe },
  2173. { "setShader", w_setShader },
  2174. { "getShader", w_getShader },
  2175. { "_setDefaultShaderCode", w_setDefaultShaderCode },
  2176. { "getSupported", w_getSupported },
  2177. { "getCanvasFormats", w_getCanvasFormats },
  2178. { "getImageFormats", w_getImageFormats },
  2179. { "getRendererInfo", w_getRendererInfo },
  2180. { "getSystemLimits", w_getSystemLimits },
  2181. { "getStats", w_getStats },
  2182. { "captureScreenshot", w_captureScreenshot },
  2183. { "draw", w_draw },
  2184. { "drawLayer", w_drawLayer },
  2185. { "drawInstanced", w_drawInstanced },
  2186. { "print", w_print },
  2187. { "printf", w_printf },
  2188. { "isCreated", w_isCreated },
  2189. { "isActive", w_isActive },
  2190. { "isGammaCorrect", w_isGammaCorrect },
  2191. { "getWidth", w_getWidth },
  2192. { "getHeight", w_getHeight },
  2193. { "getDimensions", w_getDimensions },
  2194. { "getPixelWidth", w_getPixelWidth },
  2195. { "getPixelHeight", w_getPixelHeight },
  2196. { "getPixelDimensions", w_getPixelDimensions },
  2197. { "getPixelDensity", w_getPixelDensity },
  2198. { "setScissor", w_setScissor },
  2199. { "intersectScissor", w_intersectScissor },
  2200. { "getScissor", w_getScissor },
  2201. { "stencil", w_stencil },
  2202. { "setStencilTest", w_setStencilTest },
  2203. { "getStencilTest", w_getStencilTest },
  2204. { "points", w_points },
  2205. { "line", w_line },
  2206. { "rectangle", w_rectangle },
  2207. { "circle", w_circle },
  2208. { "ellipse", w_ellipse },
  2209. { "arc", w_arc },
  2210. { "polygon", w_polygon },
  2211. { "flushBatch", w_flushBatch },
  2212. { "push", w_push },
  2213. { "pop", w_pop },
  2214. { "rotate", w_rotate },
  2215. { "scale", w_scale },
  2216. { "translate", w_translate },
  2217. { "shear", w_shear },
  2218. { "origin", w_origin },
  2219. { "applyTransform", w_applyTransform },
  2220. { "replaceTransform", w_replaceTransform },
  2221. { "transformPoint", w_transformPoint },
  2222. { "inverseTransformPoint", w_inverseTransformPoint },
  2223. { 0, 0 }
  2224. };
  2225. static int luaopen_drawable(lua_State *L)
  2226. {
  2227. return luax_register_type(L, &Drawable::type, nullptr);
  2228. }
  2229. // Types for this module.
  2230. static const lua_CFunction types[] =
  2231. {
  2232. luaopen_drawable,
  2233. luaopen_texture,
  2234. luaopen_font,
  2235. luaopen_image,
  2236. luaopen_quad,
  2237. luaopen_spritebatch,
  2238. luaopen_particlesystem,
  2239. luaopen_canvas,
  2240. luaopen_shader,
  2241. luaopen_mesh,
  2242. luaopen_text,
  2243. luaopen_video,
  2244. 0
  2245. };
  2246. extern "C" int luaopen_love_graphics(lua_State *L)
  2247. {
  2248. Graphics *instance = instance();
  2249. if (instance == nullptr)
  2250. {
  2251. luax_catchexcept(L, [&](){ instance = new love::graphics::opengl::Graphics(); });
  2252. }
  2253. else
  2254. instance->retain();
  2255. WrappedModule w;
  2256. w.module = instance;
  2257. w.name = "graphics";
  2258. w.type = &Graphics::type;
  2259. w.functions = functions;
  2260. w.types = types;
  2261. int n = luax_register_module(L, w);
  2262. if (luaL_loadbuffer(L, (const char *)graphics_lua, sizeof(graphics_lua), "wrap_Graphics.lua") == 0)
  2263. lua_call(L, 0, 0);
  2264. else
  2265. lua_error(L);
  2266. return n;
  2267. }
  2268. } // graphics
  2269. } // love