wrap_Graphics.cpp 68 KB

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