wrap_Graphics.cpp 67 KB

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