wrap_Graphics.cpp 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019
  1. /**
  2. * Copyright (c) 2006-2023 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 "thread/wrap_Channel.h"
  33. #include "opengl/Graphics.h"
  34. #include <cassert>
  35. #include <cstring>
  36. #include <cstdlib>
  37. #include <sstream>
  38. #include <algorithm>
  39. // Shove the wrap_Graphics.lua code directly into a raw string literal.
  40. static const char graphics_lua[] =
  41. #include "wrap_Graphics.lua"
  42. ;
  43. namespace love
  44. {
  45. namespace graphics
  46. {
  47. #define instance() (Module::getInstance<Graphics>(Module::M_GRAPHICS))
  48. static int luax_checkgraphicscreated(lua_State *L)
  49. {
  50. if (!instance()->isCreated())
  51. return luaL_error(L, "love.graphics cannot function without a window!");
  52. return 0;
  53. }
  54. int w_reset(lua_State *)
  55. {
  56. instance()->reset();
  57. return 0;
  58. }
  59. int w_clear(lua_State *L)
  60. {
  61. OptionalColorD color(ColorD(0.0, 0.0, 0.0, 0.0));
  62. std::vector<OptionalColorD> colors;
  63. OptionalInt stencil(0);
  64. OptionalDouble depth(1.0);
  65. int argtype = lua_type(L, 1);
  66. int startidx = -1;
  67. if (argtype == LUA_TTABLE)
  68. {
  69. int maxn = lua_gettop(L);
  70. colors.reserve(maxn);
  71. for (int i = 0; i < maxn; i++)
  72. {
  73. argtype = lua_type(L, i + 1);
  74. if (argtype == LUA_TNUMBER || argtype == LUA_TBOOLEAN)
  75. {
  76. startidx = i + 1;
  77. break;
  78. }
  79. else if (argtype == LUA_TNIL || argtype == LUA_TNONE || luax_objlen(L, i + 1) == 0)
  80. {
  81. colors.push_back(OptionalColorD());
  82. continue;
  83. }
  84. for (int j = 1; j <= 4; j++)
  85. lua_rawgeti(L, i + 1, j);
  86. OptionalColorD c;
  87. c.hasValue = true;
  88. c.value.r = luaL_checknumber(L, -4);
  89. c.value.g = luaL_checknumber(L, -3);
  90. c.value.b = luaL_checknumber(L, -2);
  91. c.value.a = luaL_optnumber(L, -1, 1.0);
  92. colors.push_back(c);
  93. lua_pop(L, 4);
  94. }
  95. }
  96. else if (argtype == LUA_TBOOLEAN)
  97. {
  98. color.hasValue = luax_toboolean(L, 1);
  99. startidx = 2;
  100. }
  101. else if (argtype != LUA_TNONE && argtype != LUA_TNIL)
  102. {
  103. color.hasValue = true;
  104. color.value.r = (float) luaL_checknumber(L, 1);
  105. color.value.g = (float) luaL_checknumber(L, 2);
  106. color.value.b = (float) luaL_checknumber(L, 3);
  107. color.value.a = (float) luaL_optnumber(L, 4, 1.0);
  108. startidx = 5;
  109. }
  110. if (startidx >= 0)
  111. {
  112. argtype = lua_type(L, startidx);
  113. if (argtype == LUA_TBOOLEAN)
  114. stencil.hasValue = luax_toboolean(L, startidx);
  115. else if (argtype == LUA_TNUMBER)
  116. stencil.value = (int) luaL_checkinteger(L, startidx);
  117. argtype = lua_type(L, startidx + 1);
  118. if (argtype == LUA_TBOOLEAN)
  119. depth.hasValue = luax_toboolean(L, startidx + 1);
  120. else if (argtype == LUA_TNUMBER)
  121. depth.value = luaL_checknumber(L, startidx + 1);
  122. }
  123. if (colors.empty())
  124. luax_catchexcept(L, [&]() { instance()->clear(color, stencil, depth); });
  125. else
  126. luax_catchexcept(L, [&]() { instance()->clear(colors, stencil, depth); });
  127. return 0;
  128. }
  129. int w_discard(lua_State *L)
  130. {
  131. std::vector<bool> colorbuffers;
  132. if (lua_istable(L, 1))
  133. {
  134. for (size_t i = 1; i <= luax_objlen(L, 1); i++)
  135. {
  136. lua_rawgeti(L, 1, i);
  137. colorbuffers.push_back(luax_optboolean(L, -1, true));
  138. lua_pop(L, 1);
  139. }
  140. }
  141. else
  142. {
  143. bool discardcolor = luax_optboolean(L, 1, true);
  144. size_t numbuffers = std::max((size_t) 1, instance()->getRenderTargets().colors.size());
  145. colorbuffers = std::vector<bool>(numbuffers, discardcolor);
  146. }
  147. bool depthstencil = luax_optboolean(L, 2, true);
  148. instance()->discard(colorbuffers, depthstencil);
  149. return 0;
  150. }
  151. int w_present(lua_State *L)
  152. {
  153. luax_catchexcept(L, [&]() { instance()->present(L); });
  154. return 0;
  155. }
  156. int w_isCreated(lua_State *L)
  157. {
  158. luax_pushboolean(L, instance()->isCreated());
  159. return 1;
  160. }
  161. int w_isActive(lua_State *L)
  162. {
  163. luax_pushboolean(L, instance()->isActive());
  164. return 1;
  165. }
  166. int w_isGammaCorrect(lua_State *L)
  167. {
  168. luax_pushboolean(L, graphics::isGammaCorrect());
  169. return 1;
  170. }
  171. int w_getWidth(lua_State *L)
  172. {
  173. lua_pushinteger(L, instance()->getWidth());
  174. return 1;
  175. }
  176. int w_getHeight(lua_State *L)
  177. {
  178. lua_pushinteger(L, instance()->getHeight());
  179. return 1;
  180. }
  181. int w_getDimensions(lua_State *L)
  182. {
  183. lua_pushinteger(L, instance()->getWidth());
  184. lua_pushinteger(L, instance()->getHeight());
  185. return 2;
  186. }
  187. int w_getPixelWidth(lua_State *L)
  188. {
  189. lua_pushinteger(L, instance()->getPixelWidth());
  190. return 1;
  191. }
  192. int w_getPixelHeight(lua_State *L)
  193. {
  194. lua_pushinteger(L, instance()->getPixelHeight());
  195. return 1;
  196. }
  197. int w_getPixelDimensions(lua_State *L)
  198. {
  199. lua_pushinteger(L, instance()->getPixelWidth());
  200. lua_pushinteger(L, instance()->getPixelHeight());
  201. return 2;
  202. }
  203. int w_getDPIScale(lua_State *L)
  204. {
  205. lua_pushnumber(L, instance()->getScreenDPIScale());
  206. return 1;
  207. }
  208. int w_getQuadIndexBuffer(lua_State *L)
  209. {
  210. luax_pushtype(L, instance()->getQuadIndexBuffer());
  211. return 1;
  212. }
  213. static Graphics::RenderTarget checkRenderTarget(lua_State *L, int idx)
  214. {
  215. lua_rawgeti(L, idx, 1);
  216. Graphics::RenderTarget target(luax_checktexture(L, -1), 0);
  217. lua_pop(L, 1);
  218. TextureType type = target.texture->getTextureType();
  219. if (type == TEXTURE_2D_ARRAY || type == TEXTURE_VOLUME)
  220. target.slice = luax_checkintflag(L, idx, "layer") - 1;
  221. else if (type == TEXTURE_CUBE)
  222. target.slice = luax_checkintflag(L, idx, "face") - 1;
  223. target.mipmap = luax_intflag(L, idx, "mipmap", 1) - 1;
  224. return target;
  225. }
  226. int w_setCanvas(lua_State *L)
  227. {
  228. // called with none -> reset to default buffer
  229. if (lua_isnoneornil(L, 1))
  230. {
  231. instance()->setRenderTarget();
  232. return 0;
  233. }
  234. bool is_table = lua_istable(L, 1);
  235. Graphics::RenderTargets targets;
  236. if (is_table)
  237. {
  238. lua_rawgeti(L, 1, 1);
  239. bool table_of_tables = lua_istable(L, -1);
  240. lua_pop(L, 1);
  241. for (int i = 1; i <= (int) luax_objlen(L, 1); i++)
  242. {
  243. lua_rawgeti(L, 1, i);
  244. if (table_of_tables)
  245. targets.colors.push_back(checkRenderTarget(L, -1));
  246. else
  247. {
  248. targets.colors.emplace_back(luax_checktexture(L, -1), 0);
  249. if (targets.colors.back().texture->getTextureType() != TEXTURE_2D)
  250. return luaL_error(L, "Non-2D textures must use the table-of-tables variant of setCanvas.");
  251. }
  252. lua_pop(L, 1);
  253. }
  254. uint32 tempdepthflag = Graphics::TEMPORARY_RT_DEPTH;
  255. uint32 tempstencilflag = Graphics::TEMPORARY_RT_STENCIL;
  256. lua_getfield(L, 1, "depthstencil");
  257. int dstype = lua_type(L, -1);
  258. if (dstype == LUA_TTABLE)
  259. targets.depthStencil = checkRenderTarget(L, -1);
  260. else if (dstype == LUA_TBOOLEAN)
  261. targets.temporaryRTFlags |= luax_toboolean(L, -1) ? (tempdepthflag | tempstencilflag) : 0;
  262. else if (dstype != LUA_TNONE && dstype != LUA_TNIL)
  263. targets.depthStencil.texture = luax_checktexture(L, -1);
  264. lua_pop(L, 1);
  265. if (targets.depthStencil.texture == nullptr && (targets.temporaryRTFlags & tempdepthflag) == 0)
  266. targets.temporaryRTFlags |= luax_boolflag(L, 1, "depth", false) ? tempdepthflag : 0;
  267. if (targets.depthStencil.texture == nullptr && (targets.temporaryRTFlags & tempstencilflag) == 0)
  268. targets.temporaryRTFlags |= luax_boolflag(L, 1, "stencil", false) ? tempstencilflag : 0;
  269. }
  270. else
  271. {
  272. for (int i = 1; i <= lua_gettop(L); i++)
  273. {
  274. Graphics::RenderTarget target(luax_checktexture(L, i), 0);
  275. TextureType type = target.texture->getTextureType();
  276. if (i == 1 && type != TEXTURE_2D)
  277. {
  278. target.slice = (int) luaL_checkinteger(L, i + 1) - 1;
  279. target.mipmap = (int) luaL_optinteger(L, i + 2, 1) - 1;
  280. targets.colors.push_back(target);
  281. break;
  282. }
  283. else if (type == TEXTURE_2D && lua_isnumber(L, i + 1))
  284. {
  285. target.mipmap = (int) luaL_optinteger(L, i + 1, 1) - 1;
  286. i++;
  287. }
  288. if (i > 1 && type != TEXTURE_2D)
  289. return luaL_error(L, "This variant of setCanvas only supports 2D texture types.");
  290. targets.colors.push_back(target);
  291. }
  292. }
  293. luax_catchexcept(L, [&]() {
  294. if (targets.getFirstTarget().texture != nullptr)
  295. instance()->setRenderTargets(targets);
  296. else
  297. instance()->setRenderTarget();
  298. });
  299. return 0;
  300. }
  301. static void pushRenderTarget(lua_State *L, const Graphics::RenderTarget &rt)
  302. {
  303. lua_createtable(L, 1, 2);
  304. luax_pushtype(L, rt.texture);
  305. lua_rawseti(L, -2, 1);
  306. TextureType type = rt.texture->getTextureType();
  307. if (type == TEXTURE_2D_ARRAY || type == TEXTURE_VOLUME)
  308. {
  309. lua_pushnumber(L, rt.slice + 1);
  310. lua_setfield(L, -2, "layer");
  311. }
  312. else if (type == TEXTURE_CUBE)
  313. {
  314. lua_pushnumber(L, rt.slice + 1);
  315. lua_setfield(L, -2, "face");
  316. }
  317. lua_pushnumber(L, rt.mipmap + 1);
  318. lua_setfield(L, -2, "mipmap");
  319. }
  320. int w_getCanvas(lua_State *L)
  321. {
  322. Graphics::RenderTargets targets = instance()->getRenderTargets();
  323. int ntargets = (int) targets.colors.size();
  324. if (ntargets == 0)
  325. {
  326. lua_pushnil(L);
  327. return 1;
  328. }
  329. bool shouldUseTablesVariant = targets.depthStencil.texture != nullptr;
  330. if (!shouldUseTablesVariant)
  331. {
  332. for (const auto &rt : targets.colors)
  333. {
  334. if (rt.mipmap != 0 || rt.texture->getTextureType() != TEXTURE_2D)
  335. {
  336. shouldUseTablesVariant = true;
  337. break;
  338. }
  339. }
  340. }
  341. if (shouldUseTablesVariant)
  342. {
  343. lua_createtable(L, ntargets, 0);
  344. for (int i = 0; i < ntargets; i++)
  345. {
  346. pushRenderTarget(L, targets.colors[i]);
  347. lua_rawseti(L, -2, i + 1);
  348. }
  349. if (targets.depthStencil.texture != nullptr)
  350. {
  351. pushRenderTarget(L, targets.depthStencil);
  352. lua_setfield(L, -2, "depthstencil");
  353. }
  354. return 1;
  355. }
  356. else
  357. {
  358. for (const auto &rt : targets.colors)
  359. luax_pushtype(L, rt.texture);
  360. return ntargets;
  361. }
  362. }
  363. static void screenshotFunctionCallback(const Graphics::ScreenshotInfo *info, love::image::ImageData *i, void *gd)
  364. {
  365. if (info == nullptr)
  366. return;
  367. lua_State *L = (lua_State *) gd;
  368. Reference *ref = (Reference *) info->data;
  369. if (i != nullptr && L != nullptr)
  370. {
  371. if (ref == nullptr)
  372. luaL_error(L, "Internal error in screenshot callback.");
  373. ref->push(L);
  374. delete ref;
  375. luax_pushtype(L, i);
  376. lua_call(L, 1, 0);
  377. }
  378. else
  379. delete ref;
  380. }
  381. struct ScreenshotFileInfo
  382. {
  383. std::string filename;
  384. image::FormatHandler::EncodedFormat format;
  385. };
  386. static void screenshotFileCallback(const Graphics::ScreenshotInfo *info, love::image::ImageData *i, void * /*gd*/)
  387. {
  388. if (info == nullptr)
  389. return;
  390. ScreenshotFileInfo *fileinfo = (ScreenshotFileInfo *) info->data;
  391. if (i != nullptr && fileinfo != nullptr)
  392. {
  393. try
  394. {
  395. i->encode(fileinfo->format, fileinfo->filename.c_str(), true);
  396. }
  397. catch (love::Exception &e)
  398. {
  399. printf("Screenshot encoding or saving failed: %s", e.what());
  400. // Do nothing...
  401. }
  402. }
  403. delete fileinfo;
  404. }
  405. static void screenshotChannelCallback(const Graphics::ScreenshotInfo *info, love::image::ImageData *i, void * /*gd*/)
  406. {
  407. if (info == nullptr)
  408. return;
  409. auto *channel = (love::thread::Channel *) info->data;
  410. if (channel != nullptr)
  411. {
  412. if (i != nullptr)
  413. channel->push(Variant(&love::image::ImageData::type, i));
  414. channel->release();
  415. }
  416. }
  417. int w_captureScreenshot(lua_State *L)
  418. {
  419. Graphics::ScreenshotInfo info;
  420. if (lua_isfunction(L, 1))
  421. {
  422. lua_pushvalue(L, 1);
  423. info.data = luax_refif(L, LUA_TFUNCTION);
  424. lua_pop(L, 1);
  425. info.callback = screenshotFunctionCallback;
  426. }
  427. else if (lua_isstring(L, 1))
  428. {
  429. std::string filename = luax_checkstring(L, 1);
  430. std::string ext;
  431. size_t dotpos = filename.rfind('.');
  432. if (dotpos != std::string::npos)
  433. ext = filename.substr(dotpos + 1);
  434. std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
  435. image::FormatHandler::EncodedFormat format;
  436. if (!image::ImageData::getConstant(ext.c_str(), format))
  437. return luax_enumerror(L, "encoded image format", image::ImageData::getConstants(format), ext.c_str());
  438. ScreenshotFileInfo *fileinfo = new ScreenshotFileInfo;
  439. fileinfo->filename = filename;
  440. fileinfo->format = format;
  441. info.data = fileinfo;
  442. info.callback = screenshotFileCallback;
  443. }
  444. else if (luax_istype(L, 1, love::thread::Channel::type))
  445. {
  446. auto *channel = love::thread::luax_checkchannel(L, 1);
  447. channel->retain();
  448. info.data = channel;
  449. info.callback = screenshotChannelCallback;
  450. }
  451. else
  452. return luax_typerror(L, 1, "function, string, or Channel");
  453. luax_catchexcept(L,
  454. [&]() { instance()->captureScreenshot(info); },
  455. [&](bool except) { if (except) info.callback(&info, nullptr, nullptr); }
  456. );
  457. return 0;
  458. }
  459. int w_setScissor(lua_State *L)
  460. {
  461. int nargs = lua_gettop(L);
  462. if (nargs == 0 || (nargs == 4 && lua_isnil(L, 1) && lua_isnil(L, 2)
  463. && lua_isnil(L, 3) && lua_isnil(L, 4)))
  464. {
  465. instance()->setScissor();
  466. return 0;
  467. }
  468. Rect rect;
  469. rect.x = (int) luaL_checkinteger(L, 1);
  470. rect.y = (int) luaL_checkinteger(L, 2);
  471. rect.w = (int) luaL_checkinteger(L, 3);
  472. rect.h = (int) luaL_checkinteger(L, 4);
  473. if (rect.w < 0 || rect.h < 0)
  474. return luaL_error(L, "Can't set scissor with negative width and/or height.");
  475. instance()->setScissor(rect);
  476. return 0;
  477. }
  478. int w_intersectScissor(lua_State *L)
  479. {
  480. Rect rect;
  481. rect.x = (int) luaL_checkinteger(L, 1);
  482. rect.y = (int) luaL_checkinteger(L, 2);
  483. rect.w = (int) luaL_checkinteger(L, 3);
  484. rect.h = (int) luaL_checkinteger(L, 4);
  485. if (rect.w < 0 || rect.h < 0)
  486. return luaL_error(L, "Can't set scissor with negative width and/or height.");
  487. instance()->intersectScissor(rect);
  488. return 0;
  489. }
  490. int w_getScissor(lua_State *L)
  491. {
  492. Rect rect;
  493. if (!instance()->getScissor(rect))
  494. return 0;
  495. lua_pushinteger(L, rect.x);
  496. lua_pushinteger(L, rect.y);
  497. lua_pushinteger(L, rect.w);
  498. lua_pushinteger(L, rect.h);
  499. return 4;
  500. }
  501. int w_setStencilMode(lua_State *L)
  502. {
  503. if (lua_gettop(L) <= 1 && lua_isnoneornil(L, 1))
  504. {
  505. luax_catchexcept(L, [&](){ instance()->setStencilMode(); });
  506. return 0;
  507. }
  508. StencilAction action = STENCIL_KEEP;
  509. const char *actionstr = luaL_checkstring(L, 1);
  510. if (!getConstant(actionstr, action))
  511. return luax_enumerror(L, "stencil draw action", getConstants(action), actionstr);
  512. CompareMode compare = COMPARE_ALWAYS;
  513. const char *comparestr = luaL_checkstring(L, 2);
  514. if (!getConstant(comparestr, compare))
  515. return luax_enumerror(L, "compare mode", getConstants(compare), comparestr);
  516. int value = (int) luaL_optinteger(L, 3, 0);
  517. uint32 readmask = (uint32) luaL_optnumber(L, 4, LOVE_UINT32_MAX);
  518. uint32 writemask = (uint32) luaL_optnumber(L, 5, LOVE_UINT32_MAX);
  519. luax_catchexcept(L, [&](){ instance()->setStencilMode(action, compare, value, readmask, writemask); });
  520. return 0;
  521. }
  522. int w_getStencilMode(lua_State *L)
  523. {
  524. StencilAction action = STENCIL_KEEP;
  525. CompareMode compare = COMPARE_ALWAYS;
  526. int value = 1;
  527. uint32 readmask = LOVE_UINT32_MAX;
  528. uint32 writemask = LOVE_UINT32_MAX;
  529. instance()->getStencilMode(action, compare, value, readmask, writemask);
  530. const char *actionstr;
  531. if (!getConstant(action, actionstr))
  532. return luaL_error(L, "Unknown stencil draw action.");
  533. const char *comparestr;
  534. if (!getConstant(compare, comparestr))
  535. return luaL_error(L, "Unknown compare mode.");
  536. lua_pushstring(L, actionstr);
  537. lua_pushstring(L, comparestr);
  538. lua_pushnumber(L, value);
  539. lua_pushnumber(L, readmask);
  540. lua_pushnumber(L, writemask);
  541. return 5;
  542. }
  543. static void parseDPIScale(Data *d, float *dpiscale)
  544. {
  545. auto fd = dynamic_cast<love::filesystem::FileData *>(d);
  546. if (fd == nullptr)
  547. return;
  548. // Parse a density scale of 2.0 from "[email protected]".
  549. const std::string &fname = fd->getName();
  550. size_t namelen = fname.length();
  551. size_t atpos = fname.rfind('@');
  552. if (atpos != std::string::npos && atpos + 2 < namelen
  553. && (fname[namelen - 1] == 'x' || fname[namelen - 1] == 'X'))
  554. {
  555. char *end = nullptr;
  556. long density = strtol(fname.c_str() + atpos + 1, &end, 10);
  557. if (end != nullptr && density > 0 && dpiscale != nullptr)
  558. *dpiscale = (float) density;
  559. }
  560. }
  561. static std::pair<StrongRef<image::ImageData>, StrongRef<image::CompressedImageData>>
  562. getImageData(lua_State *L, int idx, bool allowcompressed, float *dpiscale)
  563. {
  564. StrongRef<image::ImageData> idata;
  565. StrongRef<image::CompressedImageData> cdata;
  566. if (luax_istype(L, idx, image::ImageData::type))
  567. idata.set(image::luax_checkimagedata(L, idx));
  568. else if (luax_istype(L, idx, image::CompressedImageData::type))
  569. cdata.set(image::luax_checkcompressedimagedata(L, idx));
  570. else if (filesystem::luax_cangetdata(L, idx))
  571. {
  572. // Convert to ImageData / CompressedImageData.
  573. auto imagemodule = Module::getInstance<image::Image>(Module::M_IMAGE);
  574. if (imagemodule == nullptr)
  575. luaL_error(L, "Cannot load images without the love.image module.");
  576. StrongRef<Data> fdata(filesystem::luax_getdata(L, idx), Acquire::NORETAIN);
  577. if (dpiscale != nullptr)
  578. parseDPIScale(fdata, dpiscale);
  579. if (allowcompressed && imagemodule->isCompressed(fdata))
  580. luax_catchexcept(L, [&]() { cdata.set(imagemodule->newCompressedData(fdata), Acquire::NORETAIN); });
  581. else
  582. luax_catchexcept(L, [&]() { idata.set(imagemodule->newImageData(fdata), Acquire::NORETAIN); });
  583. }
  584. else
  585. idata.set(image::luax_checkimagedata(L, idx));
  586. return std::make_pair(idata, cdata);
  587. }
  588. static int w__pushNewTexture(lua_State *L, Texture::Slices *slices, const Texture::Settings &settings)
  589. {
  590. StrongRef<Texture> i;
  591. luax_catchexcept(L,
  592. [&]() { i.set(instance()->newTexture(settings, slices), Acquire::NORETAIN); },
  593. [&](bool) { if (slices) slices->clear(); }
  594. );
  595. luax_pushtype(L, i);
  596. return 1;
  597. }
  598. static void luax_checktexturesettings(lua_State *L, int idx, bool opt, bool checkType, bool checkDimensions, OptionalBool forceRenderTarget, Texture::Settings &s, bool &setdpiscale)
  599. {
  600. setdpiscale = false;
  601. if (forceRenderTarget.hasValue)
  602. s.renderTarget = forceRenderTarget.value;
  603. if (opt && lua_isnoneornil(L, idx))
  604. return;
  605. luax_checktablefields<Texture::SettingType>(L, idx, "texture setting name", Texture::getConstant);
  606. if (!forceRenderTarget.hasValue)
  607. s.renderTarget = luax_boolflag(L, idx, Texture::getConstant(Texture::SETTING_RENDER_TARGET), s.renderTarget);
  608. lua_getfield(L, idx, Texture::getConstant(Texture::SETTING_DEBUGNAME));
  609. if (!lua_isnoneornil(L, -1))
  610. {
  611. s.debugName.set(luaL_checkstring(L, -1));
  612. }
  613. lua_pop(L, 1);
  614. lua_getfield(L, idx, Texture::getConstant(Texture::SETTING_FORMAT));
  615. if (!lua_isnoneornil(L, -1))
  616. {
  617. const char *str = luaL_checkstring(L, -1);
  618. if (!getConstant(str, s.format))
  619. luax_enumerror(L, "pixel format", str);
  620. }
  621. lua_pop(L, 1);
  622. if (checkType)
  623. {
  624. lua_getfield(L, idx, Texture::getConstant(Texture::SETTING_TYPE));
  625. if (!lua_isnoneornil(L, -1))
  626. {
  627. const char *str = luaL_checkstring(L, -1);
  628. if (!Texture::getConstant(str, s.type))
  629. luax_enumerror(L, "texture type", Texture::getConstants(s.type), str);
  630. }
  631. lua_pop(L, 1);
  632. }
  633. if (checkDimensions)
  634. {
  635. s.width = luax_checkintflag(L, idx, Texture::getConstant(Texture::SETTING_WIDTH));
  636. s.height = luax_checkintflag(L, idx, Texture::getConstant(Texture::SETTING_HEIGHT));
  637. if (s.type == TEXTURE_2D_ARRAY || s.type == TEXTURE_VOLUME)
  638. s.layers = luax_checkintflag(L, idx, Texture::getConstant(Texture::SETTING_LAYERS));
  639. }
  640. lua_getfield(L, idx, Texture::getConstant(Texture::SETTING_MIPMAPS));
  641. if (!lua_isnoneornil(L, -1))
  642. {
  643. if (lua_type(L, -1) == LUA_TBOOLEAN)
  644. s.mipmaps = luax_toboolean(L, -1) ? Texture::MIPMAPS_MANUAL : Texture::MIPMAPS_NONE;
  645. else
  646. {
  647. const char *str = luaL_checkstring(L, -1);
  648. if (!Texture::getConstant(str, s.mipmaps))
  649. luax_enumerror(L, "Texture mipmap mode", Texture::getConstants(s.mipmaps), str);
  650. }
  651. }
  652. lua_pop(L, 1);
  653. lua_getfield(L, idx, Texture::getConstant(Texture::SETTING_MIPMAP_COUNT));
  654. if (!lua_isnoneornil(L, -1))
  655. s.mipmapCount = (int) luaL_checkinteger(L, -1);
  656. lua_pop(L, 1);
  657. s.linear = luax_boolflag(L, idx, Texture::getConstant(Texture::SETTING_LINEAR), s.linear);
  658. s.msaa = luax_intflag(L, idx, Texture::getConstant(Texture::SETTING_MSAA), s.msaa);
  659. s.computeWrite = luax_boolflag(L, idx, Texture::getConstant(Texture::SETTING_COMPUTE_WRITE), s.computeWrite);
  660. lua_getfield(L, idx, Texture::getConstant(Texture::SETTING_READABLE));
  661. if (!lua_isnoneornil(L, -1))
  662. s.readable.set(luax_checkboolean(L, -1));
  663. lua_pop(L, 1);
  664. lua_getfield(L, idx, Texture::getConstant(Texture::SETTING_DPI_SCALE));
  665. if (lua_isnumber(L, -1))
  666. {
  667. s.dpiScale = (float) lua_tonumber(L, -1);
  668. setdpiscale = true;
  669. }
  670. lua_pop(L, 1);
  671. }
  672. int w_newCanvas(lua_State *L)
  673. {
  674. luax_checkgraphicscreated(L);
  675. Texture::Settings s;
  676. OptionalBool forceRenderTarget(true);
  677. bool setDPIScale = false;
  678. if (lua_istable(L, 1))
  679. {
  680. luax_checktexturesettings(L, 1, false, true, true, forceRenderTarget, s, setDPIScale);
  681. }
  682. else
  683. {
  684. // check if width and height are given. else default to screen dimensions.
  685. s.width = (int) luaL_optinteger(L, 1, instance()->getWidth());
  686. s.height = (int) luaL_optinteger(L, 2, instance()->getHeight());
  687. int startidx = 3;
  688. if (lua_isnumber(L, 3))
  689. {
  690. s.layers = (int) luaL_checkinteger(L, 3);
  691. s.type = TEXTURE_2D_ARRAY;
  692. startidx = 4;
  693. }
  694. luax_checktexturesettings(L, startidx, true, true, false, forceRenderTarget, s, setDPIScale);
  695. }
  696. // Default to the screen's current pixel density scale.
  697. if (!setDPIScale)
  698. s.dpiScale = instance()->getScreenDPIScale();
  699. Texture *texture = nullptr;
  700. luax_catchexcept(L, [&](){ texture = instance()->newTexture(s); });
  701. luax_pushtype(L, texture);
  702. texture->release();
  703. return 1;
  704. }
  705. int w_newTexture(lua_State *L)
  706. {
  707. luax_checkgraphicscreated(L);
  708. Texture::Slices slices(TEXTURE_2D);
  709. Texture::Slices *slicesref = &slices;
  710. Texture::Settings settings;
  711. settings.type = TEXTURE_2D;
  712. bool dpiscaleset = false;
  713. if (lua_type(L, 1) == LUA_TNUMBER)
  714. {
  715. slicesref = nullptr;
  716. settings.width = (int) luaL_checkinteger(L, 1);
  717. settings.height = (int) luaL_checkinteger(L, 2);
  718. int startidx = 3;
  719. if (lua_type(L, 3) == LUA_TNUMBER)
  720. {
  721. settings.layers = (int) luaL_checkinteger(L, 3);
  722. settings.type = TEXTURE_2D_ARRAY;
  723. startidx = 4;
  724. }
  725. luax_checktexturesettings(L, startidx, true, true, false, OptionalBool(), settings, dpiscaleset);
  726. }
  727. else
  728. {
  729. luax_checktexturesettings(L, 2, true, false, false, OptionalBool(), settings, dpiscaleset);
  730. float *autodpiscale = dpiscaleset ? nullptr : &settings.dpiScale;
  731. if (lua_istable(L, 1))
  732. {
  733. int n = std::max(1, (int) luax_objlen(L, 1));
  734. for (int i = 0; i < n; i++)
  735. {
  736. lua_rawgeti(L, 1, i + 1);
  737. auto data = getImageData(L, -1, true, i == 0 ? autodpiscale : nullptr);
  738. if (data.first.get())
  739. slices.set(0, i, data.first);
  740. else
  741. slices.set(0, i, data.second->getSlice(0, 0));
  742. }
  743. lua_pop(L, n);
  744. }
  745. else
  746. {
  747. auto data = getImageData(L, 1, true, autodpiscale);
  748. if (data.first.get())
  749. slices.set(0, 0, data.first);
  750. else
  751. slices.add(data.second, 0, 0, false, settings.mipmaps != Texture::MIPMAPS_NONE);
  752. }
  753. }
  754. return w__pushNewTexture(L, slicesref, settings);
  755. }
  756. int w_newCubeTexture(lua_State *L)
  757. {
  758. luax_checkgraphicscreated(L);
  759. Texture::Slices slices(TEXTURE_CUBE);
  760. Texture::Slices *slicesref = &slices;
  761. Texture::Settings settings;
  762. settings.type = TEXTURE_CUBE;
  763. bool dpiscaleset = false;
  764. if (lua_type(L, 1) == LUA_TNUMBER)
  765. {
  766. slicesref = nullptr;
  767. settings.width = settings.height = (int) luaL_checkinteger(L, 1);
  768. luax_checktexturesettings(L, 2, true, false, false, OptionalBool(), settings, dpiscaleset);
  769. }
  770. else
  771. {
  772. luax_checktexturesettings(L, 2, true, false, false, OptionalBool(), settings, dpiscaleset);
  773. float *autodpiscale = dpiscaleset ? nullptr : &settings.dpiScale;
  774. auto imagemodule = Module::getInstance<love::image::Image>(Module::M_IMAGE);
  775. if (!lua_istable(L, 1))
  776. {
  777. auto data = getImageData(L, 1, true, autodpiscale);
  778. std::vector<StrongRef<love::image::ImageData>> faces;
  779. if (data.first.get())
  780. {
  781. luax_catchexcept(L, [&](){ faces = imagemodule->newCubeFaces(data.first); });
  782. for (int i = 0; i < (int) faces.size(); i++)
  783. slices.set(i, 0, faces[i]);
  784. }
  785. else
  786. slices.add(data.second, 0, 0, true, settings.mipmaps != Texture::MIPMAPS_NONE);
  787. }
  788. else
  789. {
  790. int tlen = (int) luax_objlen(L, 1);
  791. if (luax_isarrayoftables(L, 1))
  792. {
  793. if (tlen != 6)
  794. return luaL_error(L, "Cubemap images must have 6 faces.");
  795. for (int face = 0; face < tlen; face++)
  796. {
  797. lua_rawgeti(L, 1, face + 1);
  798. luaL_checktype(L, -1, LUA_TTABLE);
  799. int miplen = std::max(1, (int) luax_objlen(L, -1));
  800. for (int mip = 0; mip < miplen; mip++)
  801. {
  802. lua_rawgeti(L, -1, mip + 1);
  803. auto data = getImageData(L, -1, true, face == 0 && mip == 0 ? autodpiscale : nullptr);
  804. if (data.first.get())
  805. slices.set(face, mip, data.first);
  806. else
  807. slices.set(face, mip, data.second->getSlice(0, 0));
  808. lua_pop(L, 1);
  809. }
  810. }
  811. }
  812. else
  813. {
  814. bool usemipmaps = false;
  815. for (int i = 0; i < tlen; i++)
  816. {
  817. lua_rawgeti(L, 1, i + 1);
  818. auto data = getImageData(L, -1, true, i == 0 ? autodpiscale : nullptr);
  819. if (data.first.get())
  820. {
  821. if (usemipmaps || data.first->getWidth() != data.first->getHeight())
  822. {
  823. usemipmaps = true;
  824. std::vector<StrongRef<love::image::ImageData>> faces;
  825. luax_catchexcept(L, [&](){ faces = imagemodule->newCubeFaces(data.first); });
  826. for (int face = 0; face < (int) faces.size(); face++)
  827. slices.set(face, i, faces[i]);
  828. }
  829. else
  830. slices.set(i, 0, data.first);
  831. }
  832. else
  833. slices.add(data.second, i, 0, false, settings.mipmaps != Texture::MIPMAPS_NONE);
  834. }
  835. }
  836. lua_pop(L, tlen);
  837. }
  838. }
  839. return w__pushNewTexture(L, slicesref, settings);
  840. }
  841. int w_newArrayTexture(lua_State *L)
  842. {
  843. luax_checkgraphicscreated(L);
  844. Texture::Slices slices(TEXTURE_2D_ARRAY);
  845. Texture::Slices *slicesref = &slices;
  846. Texture::Settings settings;
  847. settings.type = TEXTURE_2D_ARRAY;
  848. bool dpiscaleset = false;
  849. if (lua_type(L, 1) == LUA_TNUMBER)
  850. {
  851. slicesref = nullptr;
  852. settings.width = (int) luaL_checkinteger(L, 1);
  853. settings.height = (int) luaL_checkinteger(L, 2);
  854. settings.layers = (int) luaL_checkinteger(L, 3);
  855. luax_checktexturesettings(L, 4, true, false, false, OptionalBool(), settings, dpiscaleset);
  856. }
  857. else
  858. {
  859. luax_checktexturesettings(L, 2, true, false, false, OptionalBool(), settings, dpiscaleset);
  860. float *autodpiscale = dpiscaleset ? nullptr : &settings.dpiScale;
  861. if (lua_istable(L, 1))
  862. {
  863. int tlen = std::max(1, (int) luax_objlen(L, 1));
  864. if (luax_isarrayoftables(L, 1))
  865. {
  866. for (int slice = 0; slice < tlen; slice++)
  867. {
  868. lua_rawgeti(L, 1, slice + 1);
  869. luaL_checktype(L, -1, LUA_TTABLE);
  870. int miplen = std::max(1, (int) luax_objlen(L, -1));
  871. for (int mip = 0; mip < miplen; mip++)
  872. {
  873. lua_rawgeti(L, -1, mip + 1);
  874. auto data = getImageData(L, -1, true, slice == 0 && mip == 0 ? autodpiscale : nullptr);
  875. if (data.first.get())
  876. slices.set(slice, mip, data.first);
  877. else
  878. slices.set(slice, mip, data.second->getSlice(0, 0));
  879. lua_pop(L, 1);
  880. }
  881. }
  882. }
  883. else
  884. {
  885. for (int slice = 0; slice < tlen; slice++)
  886. {
  887. lua_rawgeti(L, 1, slice + 1);
  888. auto data = getImageData(L, -1, true, slice == 0 ? autodpiscale : nullptr);
  889. if (data.first.get())
  890. slices.set(slice, 0, data.first);
  891. else
  892. slices.add(data.second, slice, 0, false, settings.mipmaps != Texture::MIPMAPS_NONE);
  893. }
  894. }
  895. lua_pop(L, tlen);
  896. }
  897. else
  898. {
  899. auto data = getImageData(L, 1, true, autodpiscale);
  900. if (data.first.get())
  901. slices.set(0, 0, data.first);
  902. else
  903. slices.add(data.second, 0, 0, true, settings.mipmaps != Texture::MIPMAPS_NONE);
  904. }
  905. }
  906. return w__pushNewTexture(L, slicesref, settings);
  907. }
  908. int w_newVolumeTexture(lua_State *L)
  909. {
  910. luax_checkgraphicscreated(L);
  911. auto imagemodule = Module::getInstance<love::image::Image>(Module::M_IMAGE);
  912. Texture::Slices slices(TEXTURE_VOLUME);
  913. Texture::Slices *slicesref = &slices;
  914. Texture::Settings settings;
  915. settings.type = TEXTURE_VOLUME;
  916. bool dpiscaleset = false;
  917. if (lua_type(L, 1) == LUA_TNUMBER)
  918. {
  919. slicesref = nullptr;
  920. settings.width = (int) luaL_checkinteger(L, 1);
  921. settings.height = (int) luaL_checkinteger(L, 2);
  922. settings.layers = (int) luaL_checkinteger(L, 3);
  923. luax_checktexturesettings(L, 4, true, false, false, OptionalBool(), settings, dpiscaleset);
  924. }
  925. else
  926. {
  927. luax_checktexturesettings(L, 2, true, false, false, OptionalBool(), settings, dpiscaleset);
  928. float *autodpiscale = dpiscaleset ? nullptr : &settings.dpiScale;
  929. if (lua_istable(L, 1))
  930. {
  931. int tlen = std::max(1, (int) luax_objlen(L, 1));
  932. if (luax_isarrayoftables(L, 1))
  933. {
  934. for (int mip = 0; mip < tlen; mip++)
  935. {
  936. lua_rawgeti(L, 1, mip + 1);
  937. luaL_checktype(L, -1, LUA_TTABLE);
  938. int slicelen = std::max(1, (int) luax_objlen(L, -1));
  939. for (int slice = 0; slice < slicelen; slice++)
  940. {
  941. lua_rawgeti(L, -1, slice + 1);
  942. auto data = getImageData(L, -1, true, slice == 0 && mip == 0 ? autodpiscale : nullptr);
  943. if (data.first.get())
  944. slices.set(slice, mip, data.first);
  945. else
  946. slices.set(slice, mip, data.second->getSlice(0, 0));
  947. lua_pop(L, 1);
  948. }
  949. }
  950. }
  951. else
  952. {
  953. for (int layer = 0; layer < tlen; layer++)
  954. {
  955. lua_rawgeti(L, 1, layer + 1);
  956. auto data = getImageData(L, -1, true, layer == 0 ? autodpiscale : nullptr);
  957. if (data.first.get())
  958. slices.set(layer, 0, data.first);
  959. else
  960. slices.add(data.second, layer, 0, false, settings.mipmaps != Texture::MIPMAPS_NONE);
  961. }
  962. }
  963. lua_pop(L, tlen);
  964. }
  965. else
  966. {
  967. auto data = getImageData(L, 1, true, autodpiscale);
  968. if (data.first.get())
  969. {
  970. std::vector<StrongRef<love::image::ImageData>> layers;
  971. luax_catchexcept(L, [&](){ layers = imagemodule->newVolumeLayers(data.first); });
  972. for (int i = 0; i < (int) layers.size(); i++)
  973. slices.set(i, 0, layers[i]);
  974. }
  975. else
  976. slices.add(data.second, 0, 0, true, settings.mipmaps != Texture::MIPMAPS_NONE);
  977. }
  978. }
  979. return w__pushNewTexture(L, slicesref, settings);
  980. }
  981. int w_newImage(lua_State *L)
  982. {
  983. //luax_markdeprecated(L, 1, "love.graphics.newImage", API_FUNCTION, DEPRECATED_RENAMED, "love.graphics.newTexture");
  984. return w_newTexture(L);
  985. }
  986. int w_newCubeImage(lua_State *L)
  987. {
  988. //luax_markdeprecated(L, 1, "love.graphics.newCubeImage", API_FUNCTION, DEPRECATED_RENAMED, "love.graphics.newCubeTexture");
  989. return w_newCubeTexture(L);
  990. }
  991. int w_newArrayImage(lua_State *L)
  992. {
  993. //luax_markdeprecated(L, 1, "love.graphics.newArrayImage", API_FUNCTION, DEPRECATED_RENAMED, "love.graphics.newArrayTexture");
  994. return w_newArrayTexture(L);
  995. }
  996. int w_newVolumeImage(lua_State *L)
  997. {
  998. //luax_markdeprecated(L, 1, "love.graphics.newVolumeImage", API_FUNCTION, DEPRECATED_RENAMED, "love.graphics.newVolumeTexture");
  999. return w_newVolumeTexture(L);
  1000. }
  1001. int w_newQuad(lua_State *L)
  1002. {
  1003. luax_checkgraphicscreated(L);
  1004. Quad::Viewport v;
  1005. v.x = luaL_checknumber(L, 1);
  1006. v.y = luaL_checknumber(L, 2);
  1007. v.w = luaL_checknumber(L, 3);
  1008. v.h = luaL_checknumber(L, 4);
  1009. double sw = 0.0f;
  1010. double sh = 0.0f;
  1011. int layer = 0;
  1012. if (luax_istype(L, 5, Texture::type))
  1013. {
  1014. Texture *texture = luax_checktexture(L, 5);
  1015. sw = texture->getWidth();
  1016. sh = texture->getHeight();
  1017. }
  1018. else if (luax_istype(L, 6, Texture::type))
  1019. {
  1020. layer = (int) luaL_checkinteger(L, 5) - 1;
  1021. Texture *texture = luax_checktexture(L, 6);
  1022. sw = texture->getWidth();
  1023. sh = texture->getHeight();
  1024. }
  1025. else if (!lua_isnoneornil(L, 7))
  1026. {
  1027. layer = (int) luaL_checkinteger(L, 5) - 1;
  1028. sw = luaL_checknumber(L, 6);
  1029. sh = luaL_checknumber(L, 7);
  1030. }
  1031. else
  1032. {
  1033. sw = luaL_checknumber(L, 5);
  1034. sh = luaL_checknumber(L, 6);
  1035. }
  1036. Quad *quad = instance()->newQuad(v, sw, sh);
  1037. quad->setLayer(layer);
  1038. luax_pushtype(L, quad);
  1039. quad->release();
  1040. return 1;
  1041. }
  1042. int w_newFont(lua_State *L)
  1043. {
  1044. luax_checkgraphicscreated(L);
  1045. graphics::Font *font = nullptr;
  1046. // Convert to Rasterizer, if necessary.
  1047. if (!luax_istype(L, 1, love::font::Rasterizer::type))
  1048. {
  1049. std::vector<int> idxs;
  1050. for (int i = 0; i < lua_gettop(L); i++)
  1051. idxs.push_back(i + 1);
  1052. luax_convobj(L, idxs, "font", "newRasterizer");
  1053. }
  1054. love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1);
  1055. luax_catchexcept(L, [&]() {
  1056. font = instance()->newFont(rasterizer); }
  1057. );
  1058. // Push the type.
  1059. luax_pushtype(L, font);
  1060. font->release();
  1061. return 1;
  1062. }
  1063. int w_newImageFont(lua_State *L)
  1064. {
  1065. luax_checkgraphicscreated(L);
  1066. // Convert to Rasterizer if necessary.
  1067. if (!luax_istype(L, 1, love::font::Rasterizer::type))
  1068. {
  1069. luaL_checktype(L, 2, LUA_TSTRING);
  1070. std::vector<int> idxs;
  1071. for (int i = 0; i < lua_gettop(L); i++)
  1072. idxs.push_back(i + 1);
  1073. luax_convobj(L, idxs, "font", "newImageRasterizer");
  1074. }
  1075. love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1);
  1076. // Create the font.
  1077. Font *font = instance()->newFont(rasterizer);
  1078. // Push the type.
  1079. luax_pushtype(L, font);
  1080. font->release();
  1081. return 1;
  1082. }
  1083. int w_newSpriteBatch(lua_State *L)
  1084. {
  1085. luax_checkgraphicscreated(L);
  1086. Texture *texture = luax_checktexture(L, 1);
  1087. int size = (int) luaL_optinteger(L, 2, 1000);
  1088. BufferDataUsage usage = BUFFERDATAUSAGE_DYNAMIC;
  1089. if (lua_gettop(L) > 2)
  1090. {
  1091. const char *usagestr = luaL_checkstring(L, 3);
  1092. if (!getConstant(usagestr, usage))
  1093. return luax_enumerror(L, "usage hint", getConstants(usage), usagestr);
  1094. }
  1095. SpriteBatch *t = nullptr;
  1096. luax_catchexcept(L,
  1097. [&](){ t = instance()->newSpriteBatch(texture, size, usage); }
  1098. );
  1099. luax_pushtype(L, t);
  1100. t->release();
  1101. return 1;
  1102. }
  1103. int w_newParticleSystem(lua_State *L)
  1104. {
  1105. luax_checkgraphicscreated(L);
  1106. Texture *texture = luax_checktexture(L, 1);
  1107. lua_Number size = luaL_optnumber(L, 2, 1000);
  1108. ParticleSystem *t = nullptr;
  1109. if (size < 1.0 || size > ParticleSystem::MAX_PARTICLES)
  1110. return luaL_error(L, "Invalid ParticleSystem size");
  1111. luax_catchexcept(L,
  1112. [&](){ t = instance()->newParticleSystem(texture, int(size)); }
  1113. );
  1114. luax_pushtype(L, t);
  1115. t->release();
  1116. return 1;
  1117. }
  1118. static int w_getShaderSource(lua_State *L, int startidx, std::vector<std::string> &stages, Shader::CompileOptions &options)
  1119. {
  1120. using namespace love::filesystem;
  1121. luax_checkgraphicscreated(L);
  1122. auto fs = Module::getInstance<Filesystem>(Module::M_FILESYSTEM);
  1123. // read any filepath arguments
  1124. for (int i = startidx; i < startidx + 2; i++)
  1125. {
  1126. if (!lua_isstring(L, i))
  1127. {
  1128. if (luax_cangetfiledata(L, i))
  1129. {
  1130. FileData *fd = luax_getfiledata(L, i);
  1131. lua_pushlstring(L, (const char *) fd->getData(), fd->getSize());
  1132. fd->release();
  1133. lua_replace(L, i);
  1134. }
  1135. continue;
  1136. }
  1137. size_t slen = 0;
  1138. const char *str = lua_tolstring(L, i, &slen);
  1139. Filesystem::Info info = {};
  1140. if (fs != nullptr && fs->getInfo(str, info))
  1141. {
  1142. FileData *fd = nullptr;
  1143. luax_catchexcept(L, [&](){ fd = fs->read(str); });
  1144. lua_pushlstring(L, (const char *) fd->getData(), fd->getSize());
  1145. fd->release();
  1146. lua_replace(L, i);
  1147. }
  1148. else
  1149. {
  1150. // Check if the argument looks like a filepath - we want a nicer
  1151. // error for misspelled filepath arguments.
  1152. if (slen > 0 && slen < 64 && !strchr(str, '\n'))
  1153. {
  1154. const char *ext = strchr(str, '.');
  1155. if (ext != nullptr && !strchr(ext, ';') && !strchr(ext, ' '))
  1156. return luaL_error(L, "Could not open file %s. Does not exist.", str);
  1157. }
  1158. }
  1159. }
  1160. bool has_arg1 = lua_isstring(L, startidx + 0) != 0;
  1161. bool has_arg2 = lua_isstring(L, startidx + 1) != 0;
  1162. // require at least one string argument
  1163. if (!(has_arg1 || has_arg2))
  1164. luaL_checkstring(L, startidx);
  1165. if (has_arg1)
  1166. stages.push_back(luax_checkstring(L, startidx + 0));
  1167. if (has_arg2)
  1168. stages.push_back(luax_checkstring(L, startidx + 1));
  1169. int optionsidx = has_arg2 ? startidx + 2 : startidx + 1;
  1170. if (!lua_isnoneornil(L, optionsidx))
  1171. {
  1172. luaL_checktype(L, optionsidx, LUA_TTABLE);
  1173. lua_getfield(L, optionsidx, "defines");
  1174. if (!lua_isnoneornil(L, -1))
  1175. {
  1176. if (!lua_istable(L, -1))
  1177. luaL_argerror(L, optionsidx, "expected 'defines' field to be a table");
  1178. lua_pushnil(L);
  1179. while (lua_next(L, -2))
  1180. {
  1181. std::string defname;
  1182. std::string defval;
  1183. if (lua_type(L, -2) == LUA_TNUMBER && lua_type(L, -1) == LUA_TSTRING)
  1184. defname = luaL_checkstring(L, -1);
  1185. else if (lua_type(L, -2) != LUA_TSTRING)
  1186. luaL_argerror(L, optionsidx, "all fields in the 'defines' table must use string keys.");
  1187. else
  1188. {
  1189. defname = luaL_checkstring(L, -2);
  1190. if (lua_type(L, -1) == LUA_TBOOLEAN)
  1191. defval = luax_toboolean(L, -1) ? "1" : "0";
  1192. else
  1193. {
  1194. const char *val = lua_tostring(L, -1);
  1195. if (val == nullptr)
  1196. luaL_argerror(L, optionsidx, "'defines' table values must be strings, numbers, or booleans.");
  1197. defval = val;
  1198. }
  1199. }
  1200. options.defines[defname] = defval;
  1201. lua_pop(L, 1);
  1202. }
  1203. }
  1204. lua_pop(L, 1);
  1205. }
  1206. return 0;
  1207. }
  1208. int w_newShader(lua_State *L)
  1209. {
  1210. std::vector<std::string> stages;
  1211. Shader::CompileOptions options;
  1212. w_getShaderSource(L, 1, stages, options);
  1213. bool should_error = false;
  1214. try
  1215. {
  1216. Shader *shader = instance()->newShader(stages, options);
  1217. luax_pushtype(L, shader);
  1218. shader->release();
  1219. }
  1220. catch (love::Exception &e)
  1221. {
  1222. luax_getfunction(L, "graphics", "_transformGLSLErrorMessages");
  1223. lua_pushstring(L, e.what());
  1224. // Function pushes the new error string onto the stack.
  1225. lua_pcall(L, 1, 1, 0);
  1226. should_error = true;
  1227. }
  1228. if (should_error)
  1229. return lua_error(L);
  1230. return 1;
  1231. }
  1232. int w_newComputeShader(lua_State* L)
  1233. {
  1234. std::vector<std::string> stages;
  1235. Shader::CompileOptions options;
  1236. w_getShaderSource(L, 1, stages, options);
  1237. bool should_error = false;
  1238. try
  1239. {
  1240. Shader *shader = instance()->newComputeShader(stages[0], options);
  1241. luax_pushtype(L, shader);
  1242. shader->release();
  1243. }
  1244. catch (love::Exception &e)
  1245. {
  1246. luax_getfunction(L, "graphics", "_transformGLSLErrorMessages");
  1247. lua_pushstring(L, e.what());
  1248. // Function pushes the new error string onto the stack.
  1249. lua_pcall(L, 1, 1, 0);
  1250. should_error = true;
  1251. }
  1252. if (should_error)
  1253. return lua_error(L);
  1254. return 1;
  1255. }
  1256. int w_validateShader(lua_State *L)
  1257. {
  1258. bool gles = luax_checkboolean(L, 1);
  1259. std::vector<std::string> stages;
  1260. Shader::CompileOptions options;
  1261. w_getShaderSource(L, 2, stages, options);
  1262. bool success = true;
  1263. std::string err;
  1264. try
  1265. {
  1266. success = instance()->validateShader(gles, stages, options, err);
  1267. }
  1268. catch (love::Exception &e)
  1269. {
  1270. success = false;
  1271. err = e.what();
  1272. }
  1273. luax_pushboolean(L, success);
  1274. if (!success)
  1275. {
  1276. luax_pushstring(L, err);
  1277. return 2;
  1278. }
  1279. return 1;
  1280. }
  1281. static BufferDataUsage luax_optdatausage(lua_State *L, int idx, BufferDataUsage def)
  1282. {
  1283. const char *usagestr = lua_isnoneornil(L, idx) ? nullptr : luaL_checkstring(L, idx);
  1284. if (usagestr && !getConstant(usagestr, def))
  1285. luax_enumerror(L, "usage hint", getConstants(def), usagestr);
  1286. return def;
  1287. }
  1288. static void luax_optbuffersettings(lua_State *L, int idx, Buffer::Settings &settings)
  1289. {
  1290. if (lua_isnoneornil(L, idx))
  1291. return;
  1292. luaL_checktype(L, idx, LUA_TTABLE);
  1293. lua_getfield(L, idx, "usage");
  1294. settings.dataUsage = luax_optdatausage(L, -1, settings.dataUsage);
  1295. lua_pop(L, 1);
  1296. lua_getfield(L, idx, "debugname");
  1297. if (!lua_isnoneornil(L, -1))
  1298. settings.debugName = luax_checkstring(L, -1);
  1299. lua_pop(L, 1);
  1300. }
  1301. static Buffer::DataDeclaration luax_checkdatadeclaration(lua_State* L, int formattableidx, int arrayindex, int declindex, bool requirename)
  1302. {
  1303. Buffer::DataDeclaration decl("", DATAFORMAT_MAX_ENUM);
  1304. lua_getfield(L, declindex, "name");
  1305. if (requirename && lua_type(L, -1) != LUA_TSTRING)
  1306. {
  1307. std::ostringstream ss;
  1308. ss << "'name' field expected in array element #";
  1309. ss << arrayindex;
  1310. ss << " of format table";
  1311. std::string str = ss.str();
  1312. luaL_argerror(L, formattableidx, str.c_str());
  1313. }
  1314. else if (!lua_isnoneornil(L, -1))
  1315. decl.name = luax_checkstring(L, -1);
  1316. lua_pop(L, 1);
  1317. lua_getfield(L, declindex, "format");
  1318. if (lua_type(L, -1) != LUA_TSTRING)
  1319. {
  1320. std::ostringstream ss;
  1321. ss << "'format' field expected in array element #";
  1322. ss << arrayindex;
  1323. ss << " of format table";
  1324. std::string str = ss.str();
  1325. luaL_argerror(L, formattableidx, str.c_str());
  1326. }
  1327. const char* formatstr = luaL_checkstring(L, -1);
  1328. if (!getConstant(formatstr, decl.format))
  1329. luax_enumerror(L, "data format", getConstants(decl.format), formatstr);
  1330. lua_pop(L, 1);
  1331. decl.arrayLength = luax_intflag(L, declindex, "arraylength", 0);
  1332. return decl;
  1333. }
  1334. static void luax_checkbufferformat(lua_State *L, int idx, std::vector<Buffer::DataDeclaration> &format)
  1335. {
  1336. if (lua_type(L, idx) == LUA_TSTRING)
  1337. {
  1338. Buffer::DataDeclaration decl("", DATAFORMAT_MAX_ENUM);
  1339. const char *formatstr = luaL_checkstring(L, idx);
  1340. if (!getConstant(formatstr, decl.format))
  1341. luax_enumerror(L, "data format", getConstants(decl.format), formatstr);
  1342. format.push_back(decl);
  1343. return;
  1344. }
  1345. luaL_checktype(L, idx, LUA_TTABLE);
  1346. int tablelen = luax_objlen(L, idx);
  1347. for (int i = 1; i <= tablelen; i++)
  1348. {
  1349. lua_rawgeti(L, idx, i);
  1350. luaL_checktype(L, -1, LUA_TTABLE);
  1351. Buffer::DataDeclaration decl = luax_checkdatadeclaration(L, idx, i, -1, false);
  1352. format.push_back(decl);
  1353. lua_pop(L, 1);
  1354. }
  1355. }
  1356. static Buffer *luax_newbuffer(lua_State *L, int idx, Buffer::Settings settings, const std::vector<Buffer::DataDeclaration> &format)
  1357. {
  1358. size_t arraylength = 0;
  1359. size_t bytesize = 0;
  1360. Data *data = nullptr;
  1361. const void *initialdata = nullptr;
  1362. int ncomponents = 0;
  1363. for (const Buffer::DataDeclaration &decl : format)
  1364. ncomponents += getDataFormatInfo(decl.format).components;
  1365. if (luax_istype(L, idx, Data::type))
  1366. {
  1367. data = luax_checktype<Data>(L, idx);
  1368. initialdata = data->getData();
  1369. bytesize = data->getSize();
  1370. }
  1371. bool tableoftables = false;
  1372. if (lua_istable(L, idx))
  1373. {
  1374. arraylength = luax_objlen(L, idx);
  1375. lua_rawgeti(L, idx, 1);
  1376. tableoftables = lua_istable(L, -1);
  1377. lua_pop(L, 1);
  1378. if (!tableoftables)
  1379. {
  1380. if (arraylength % ncomponents != 0)
  1381. luaL_error(L, "Array length in flat array variant of newBuffer must be a multiple of the total number of components (%d)", ncomponents);
  1382. arraylength /= ncomponents;
  1383. }
  1384. }
  1385. else if (data == nullptr)
  1386. {
  1387. lua_Integer len = luaL_checkinteger(L, idx);
  1388. if (len <= 0)
  1389. luaL_argerror(L, idx, "number of elements must be greater than 0");
  1390. arraylength = (size_t) len;
  1391. settings.zeroInitialize = true;
  1392. }
  1393. Buffer *b = nullptr;
  1394. luax_catchexcept(L, [&] { b = instance()->newBuffer(settings, format, initialdata, bytesize, arraylength); });
  1395. if (lua_istable(L, idx))
  1396. {
  1397. Buffer::Mapper mapper(*b);
  1398. char *data = (char *) mapper.data;
  1399. const auto &members = b->getDataMembers();
  1400. size_t stride = b->getArrayStride();
  1401. if (tableoftables)
  1402. {
  1403. for (size_t i = 0; i < arraylength; i++)
  1404. {
  1405. // get arraydata[index]
  1406. lua_rawgeti(L, 2, i + 1);
  1407. luaL_checktype(L, -1, LUA_TTABLE);
  1408. // get arraydata[index][j]
  1409. for (int j = 1; j <= ncomponents; j++)
  1410. lua_rawgeti(L, -j, j);
  1411. int idx = -ncomponents;
  1412. for (const Buffer::DataMember &member : members)
  1413. {
  1414. luax_writebufferdata(L, idx, member.decl.format, data + member.offset);
  1415. idx += member.info.components;
  1416. }
  1417. lua_pop(L, ncomponents + 1);
  1418. data += stride;
  1419. }
  1420. }
  1421. else // Flat array
  1422. {
  1423. for (size_t i = 0; i < arraylength; i++)
  1424. {
  1425. // get arraydata[arrayindex * ncomponents + componentindex]
  1426. for (int componentindex = 1; componentindex <= ncomponents; componentindex++)
  1427. lua_rawgeti(L, 2, i * ncomponents + componentindex);
  1428. int idx = -ncomponents;
  1429. for (const Buffer::DataMember &member : members)
  1430. {
  1431. luax_writebufferdata(L, idx, member.decl.format, data + member.offset);
  1432. idx += member.info.components;
  1433. }
  1434. lua_pop(L, ncomponents);
  1435. data += stride;
  1436. }
  1437. }
  1438. }
  1439. return b;
  1440. }
  1441. int w_newBuffer(lua_State *L)
  1442. {
  1443. Buffer::Settings settings(0, BUFFERDATAUSAGE_DYNAMIC);
  1444. luaL_checktype(L, 3, LUA_TTABLE);
  1445. for (int i = 0; i < BUFFERUSAGE_MAX_ENUM; i++)
  1446. {
  1447. BufferUsage bufferusage = (BufferUsage) i;
  1448. const char *tname = nullptr;
  1449. if (!getConstant(bufferusage, tname))
  1450. continue;
  1451. if (luax_boolflag(L, 3, tname, false))
  1452. settings.usageFlags = (BufferUsageFlags)(settings.usageFlags | (1u << i));
  1453. }
  1454. luax_optbuffersettings(L, 3, settings);
  1455. std::vector<Buffer::DataDeclaration> format;
  1456. luax_checkbufferformat(L, 1, format);
  1457. Buffer *b = luax_newbuffer(L, 2, settings, format);
  1458. luax_pushtype(L, b);
  1459. b->release();
  1460. return 1;
  1461. }
  1462. static PrimitiveType luax_checkmeshdrawmode(lua_State *L, int idx)
  1463. {
  1464. const char *modestr = luaL_checkstring(L, idx);
  1465. PrimitiveType mode = PRIMITIVE_TRIANGLES;
  1466. if (!getConstant(modestr, mode))
  1467. luax_enumerror(L, "mesh draw mode", getConstants(mode), modestr);
  1468. return mode;
  1469. }
  1470. static Mesh *newStandardMesh(lua_State *L)
  1471. {
  1472. Mesh *t = nullptr;
  1473. PrimitiveType drawmode = luax_checkmeshdrawmode(L, 2);
  1474. BufferDataUsage usage = luax_optdatausage(L, 3, BUFFERDATAUSAGE_DYNAMIC);
  1475. std::vector<Buffer::DataDeclaration> format = Mesh::getDefaultVertexFormat();
  1476. // First argument is a table of standard vertices, or the number of
  1477. // standard vertices.
  1478. if (lua_istable(L, 1))
  1479. {
  1480. size_t vertexcount = luax_objlen(L, 1);
  1481. std::vector<Vertex> vertices;
  1482. vertices.reserve(vertexcount);
  1483. // Get the vertices from the table.
  1484. for (size_t i = 1; i <= vertexcount; i++)
  1485. {
  1486. lua_rawgeti(L, 1, (int) i);
  1487. if (lua_type(L, -1) != LUA_TTABLE)
  1488. {
  1489. luax_typerror(L, 1, "table of tables");
  1490. return nullptr;
  1491. }
  1492. for (int j = 1; j <= 8; j++)
  1493. lua_rawgeti(L, -j, j);
  1494. Vertex v;
  1495. v.x = (float) luaL_checknumber(L, -8);
  1496. v.y = (float) luaL_checknumber(L, -7);
  1497. v.s = (float) luaL_optnumber(L, -6, 0.0);
  1498. v.t = (float) luaL_optnumber(L, -5, 0.0);
  1499. v.color.r = (unsigned char) (luax_optnumberclamped01(L, -4, 1.0) * 255.0);
  1500. v.color.g = (unsigned char) (luax_optnumberclamped01(L, -3, 1.0) * 255.0);
  1501. v.color.b = (unsigned char) (luax_optnumberclamped01(L, -2, 1.0) * 255.0);
  1502. v.color.a = (unsigned char) (luax_optnumberclamped01(L, -1, 1.0) * 255.0);
  1503. lua_pop(L, 9);
  1504. vertices.push_back(v);
  1505. }
  1506. luax_catchexcept(L, [&](){ t = instance()->newMesh(format, vertices.data(), vertices.size() * sizeof(Vertex), drawmode, usage); });
  1507. }
  1508. else
  1509. {
  1510. int count = (int) luaL_checkinteger(L, 1);
  1511. luax_catchexcept(L, [&](){ t = instance()->newMesh(format, count, drawmode, usage); });
  1512. }
  1513. return t;
  1514. }
  1515. static Mesh *newCustomMesh(lua_State *L)
  1516. {
  1517. Mesh *t = nullptr;
  1518. // First argument is the vertex format, second is a table of vertices or
  1519. // the number of vertices.
  1520. std::vector<Buffer::DataDeclaration> vertexformat;
  1521. PrimitiveType drawmode = luax_checkmeshdrawmode(L, 3);
  1522. BufferDataUsage usage = luax_optdatausage(L, 4, BUFFERDATAUSAGE_DYNAMIC);
  1523. lua_rawgeti(L, 1, 1);
  1524. if (!lua_istable(L, -1))
  1525. {
  1526. luaL_argerror(L, 1, "table of tables expected");
  1527. return nullptr;
  1528. }
  1529. lua_pop(L, 1);
  1530. // Per-vertex attribute data formats.
  1531. for (int i = 1; i <= (int) luax_objlen(L, 1); i++)
  1532. {
  1533. lua_rawgeti(L, 1, i);
  1534. Buffer::DataDeclaration decl("", DATAFORMAT_MAX_ENUM);
  1535. lua_getfield(L, -1, "format");
  1536. bool hasformatfield = !lua_isnoneornil(L, -1);
  1537. lua_pop(L, 1);
  1538. if (hasformatfield || luax_objlen(L, -1) == 0)
  1539. decl = luax_checkdatadeclaration(L, 1, i, -1, true);
  1540. else
  1541. {
  1542. // Legacy format arguments: {name, datatype, components}
  1543. for (int j = 1; j <= 3; j++)
  1544. lua_rawgeti(L, -j, j);
  1545. decl.name = luaL_checkstring(L, -3);
  1546. const char* tname = luaL_checkstring(L, -2);
  1547. int components = (int)luaL_checkinteger(L, -1);
  1548. // Check deprecated format names.
  1549. if (strcmp(tname, "byte") == 0 || strcmp(tname, "unorm8") == 0)
  1550. {
  1551. if (components == 4)
  1552. decl.format = DATAFORMAT_UNORM8_VEC4;
  1553. else
  1554. luaL_error(L, "Invalid component count (%d) for vertex data type %s", components, tname);
  1555. }
  1556. else if (strcmp(tname, "unorm16") == 0)
  1557. {
  1558. if (components == 2)
  1559. decl.format = DATAFORMAT_UNORM16_VEC2;
  1560. else if (components == 4)
  1561. decl.format = DATAFORMAT_UNORM16_VEC4;
  1562. else
  1563. luaL_error(L, "Invalid component count (%d) for vertex data type %s", components, tname);
  1564. }
  1565. else if (strcmp(tname, "float") == 0)
  1566. {
  1567. if (components == 1)
  1568. decl.format = DATAFORMAT_FLOAT;
  1569. else if (components == 2)
  1570. decl.format = DATAFORMAT_FLOAT_VEC2;
  1571. else if (components == 3)
  1572. decl.format = DATAFORMAT_FLOAT_VEC3;
  1573. else if (components == 4)
  1574. decl.format = DATAFORMAT_FLOAT_VEC4;
  1575. else
  1576. luaL_error(L, "Invalid component count (%d) for vertex data type %s", components, tname);
  1577. }
  1578. if (decl.format == DATAFORMAT_MAX_ENUM)
  1579. luax_enumerror(L, "vertex data type", {"float", "byte", "unorm8", "unorm16"}, tname);
  1580. lua_pop(L, 3);
  1581. luax_markdeprecated(L, 1, "vertex format array values in love.graphics.newMesh", API_CUSTOM, DEPRECATED_REPLACED, "named table fields 'format' and 'name'");
  1582. }
  1583. lua_pop(L, 1);
  1584. vertexformat.push_back(decl);
  1585. }
  1586. if (lua_isnumber(L, 2))
  1587. {
  1588. int vertexcount = (int) luaL_checkinteger(L, 2);
  1589. luax_catchexcept(L, [&](){ t = instance()->newMesh(vertexformat, vertexcount, drawmode, usage); });
  1590. }
  1591. else if (luax_istype(L, 2, Data::type))
  1592. {
  1593. // Vertex data comes directly from a Data object.
  1594. Data *data = luax_checktype<Data>(L, 2);
  1595. luax_catchexcept(L, [&](){ t = instance()->newMesh(vertexformat, data->getData(), data->getSize(), drawmode, usage); });
  1596. }
  1597. else
  1598. {
  1599. // Table of vertices.
  1600. lua_rawgeti(L, 2, 1);
  1601. if (!lua_istable(L, -1))
  1602. {
  1603. luaL_argerror(L, 2, "expected table of tables");
  1604. return nullptr;
  1605. }
  1606. lua_pop(L, 1);
  1607. size_t numvertices = luax_objlen(L, 2);
  1608. luax_catchexcept(L, [&](){ t = instance()->newMesh(vertexformat, numvertices, drawmode, usage); });
  1609. char *data = (char *) t->getVertexData();
  1610. size_t stride = t->getVertexStride();
  1611. const auto &members = t->getVertexFormat();
  1612. for (size_t vertindex = 0; vertindex < numvertices; vertindex++)
  1613. {
  1614. // get vertices[vertindex]
  1615. lua_rawgeti(L, 2, vertindex + 1);
  1616. luaL_checktype(L, -1, LUA_TTABLE);
  1617. int n = 0;
  1618. for (size_t i = 0; i < vertexformat.size(); i++)
  1619. {
  1620. const auto &member = members[i];
  1621. const auto &info = getDataFormatInfo(member.decl.format);
  1622. // get vertices[vertindex][n]
  1623. for (int c = 0; c < info.components; c++)
  1624. {
  1625. n++;
  1626. lua_rawgeti(L, -(c + 1), n);
  1627. }
  1628. size_t offset = vertindex * stride + member.offset;
  1629. // Fetch the values from Lua and store them in data buffer.
  1630. luax_writebufferdata(L, -info.components, member.decl.format, data + offset);
  1631. lua_pop(L, info.components);
  1632. }
  1633. lua_pop(L, 1); // pop vertices[vertindex]
  1634. }
  1635. t->setVertexDataModified(0, stride * numvertices);
  1636. t->flush();
  1637. }
  1638. return t;
  1639. }
  1640. static bool luax_isbufferattributetable(lua_State* L, int idx)
  1641. {
  1642. if (lua_type(L, idx) != LUA_TTABLE)
  1643. return false;
  1644. lua_rawgeti(L, idx, 1);
  1645. if (lua_type(L, -1) != LUA_TTABLE)
  1646. {
  1647. lua_pop(L, 1);
  1648. return false;
  1649. }
  1650. lua_getfield(L, -1, "buffer");
  1651. bool isbuffer = luax_istype(L, -1, Buffer::type);
  1652. lua_pop(L, 2);
  1653. return isbuffer;
  1654. }
  1655. static Mesh::BufferAttribute luax_checkbufferattributetable(lua_State *L, int idx)
  1656. {
  1657. Mesh::BufferAttribute attrib = {};
  1658. attrib.step = STEP_PER_VERTEX;
  1659. attrib.enabled = true;
  1660. lua_getfield(L, idx, "buffer");
  1661. attrib.buffer = luax_checkbuffer(L, -1);
  1662. lua_pop(L, 1);
  1663. lua_getfield(L, idx, "name");
  1664. attrib.name = luax_checkstring(L, -1);
  1665. lua_pop(L, 1);
  1666. lua_getfield(L, idx, "step");
  1667. if (!lua_isnoneornil(L, -1))
  1668. {
  1669. const char *stepstr = luaL_checkstring(L, -1);
  1670. if (!getConstant(stepstr, attrib.step))
  1671. luax_enumerror(L, "vertex attribute step", getConstants(attrib.step), stepstr);
  1672. }
  1673. lua_pop(L, 1);
  1674. lua_getfield(L, idx, "nameinbuffer");
  1675. if (!lua_isnoneornil(L, -1))
  1676. attrib.nameInBuffer = luax_checkstring(L, -1);
  1677. else
  1678. attrib.nameInBuffer = attrib.name;
  1679. lua_pop(L, 1);
  1680. lua_getfield(L, idx, "startindex");
  1681. attrib.startArrayIndex = (int) luaL_optinteger(L, -1, 1) - 1;
  1682. lua_pop(L, 1);
  1683. return attrib;
  1684. }
  1685. static Mesh* newMeshFromBuffers(lua_State *L)
  1686. {
  1687. std::vector<Mesh::BufferAttribute> attributes;
  1688. for (size_t i = 1; i <= luax_objlen(L, 1); i++)
  1689. {
  1690. lua_rawgeti(L, 1, i);
  1691. attributes.push_back(luax_checkbufferattributetable(L, -1));
  1692. lua_pop(L, 1);
  1693. }
  1694. PrimitiveType drawmode = luax_checkmeshdrawmode(L, 2);
  1695. Mesh *t = nullptr;
  1696. luax_catchexcept(L, [&]() { t = instance()->newMesh(attributes, drawmode); });
  1697. return t;
  1698. }
  1699. int w_newMesh(lua_State *L)
  1700. {
  1701. luax_checkgraphicscreated(L);
  1702. // Check first argument: table or number of vertices.
  1703. int arg1type = lua_type(L, 1);
  1704. if (arg1type != LUA_TTABLE && arg1type != LUA_TNUMBER)
  1705. luaL_argerror(L, 1, "table or number expected");
  1706. Mesh *t = nullptr;
  1707. int arg2type = lua_type(L, 2);
  1708. if (luax_isbufferattributetable(L, 1))
  1709. t = newMeshFromBuffers(L);
  1710. else if (arg1type == LUA_TTABLE && (arg2type == LUA_TTABLE || arg2type == LUA_TNUMBER || arg2type == LUA_TUSERDATA))
  1711. t = newCustomMesh(L);
  1712. else
  1713. t = newStandardMesh(L);
  1714. luax_pushtype(L, t);
  1715. t->release();
  1716. return 1;
  1717. }
  1718. int w_newTextBatch(lua_State *L)
  1719. {
  1720. luax_checkgraphicscreated(L);
  1721. graphics::Font *font = luax_checkfont(L, 1);
  1722. TextBatch *t = nullptr;
  1723. if (lua_isnoneornil(L, 2))
  1724. luax_catchexcept(L, [&](){ t = instance()->newTextBatch(font); });
  1725. else
  1726. {
  1727. std::vector<love::font::ColoredString> text;
  1728. luax_checkcoloredstring(L, 2, text);
  1729. luax_catchexcept(L, [&](){ t = instance()->newTextBatch(font, text); });
  1730. }
  1731. luax_pushtype(L, t);
  1732. t->release();
  1733. return 1;
  1734. }
  1735. int w_newText(lua_State *L)
  1736. {
  1737. luax_markdeprecated(L, 1, "love.graphics.newText", API_FUNCTION, DEPRECATED_RENAMED, "love.graphics.newTextBatch");
  1738. return w_newTextBatch(L);
  1739. }
  1740. int w_newVideo(lua_State *L)
  1741. {
  1742. luax_checkgraphicscreated(L);
  1743. if (!luax_istype(L, 1, love::video::VideoStream::type))
  1744. luax_convobj(L, 1, "video", "newVideoStream");
  1745. auto stream = luax_checktype<love::video::VideoStream>(L, 1);
  1746. float dpiscale = (float) luaL_optnumber(L, 2, 1.0);
  1747. Video *video = nullptr;
  1748. luax_catchexcept(L, [&]() { video = instance()->newVideo(stream, dpiscale); });
  1749. luax_pushtype(L, video);
  1750. video->release();
  1751. return 1;
  1752. }
  1753. int w_readbackBuffer(lua_State *L)
  1754. {
  1755. Buffer *b = luax_checkbuffer(L, 1);
  1756. lua_Integer offset = luaL_optinteger(L, 2, 0);
  1757. lua_Integer size = luaL_optinteger(L, 3, b->getSize() - offset);
  1758. data::ByteData *dest = nullptr;
  1759. size_t destoffset = 0;
  1760. if (!lua_isnoneornil(L, 4))
  1761. {
  1762. dest = luax_checktype<data::ByteData>(L, 4);
  1763. destoffset = (size_t) luaL_optinteger(L, 5, 0);
  1764. }
  1765. love::data::ByteData *data = nullptr;
  1766. luax_catchexcept(L, [&]() { data = instance()->readbackBuffer(b, offset, size, dest, destoffset); });
  1767. luax_pushtype(L, data);
  1768. data->release();
  1769. return 1;
  1770. }
  1771. int w_readbackBufferAsync(lua_State *L)
  1772. {
  1773. Buffer *b = luax_checkbuffer(L, 1);
  1774. lua_Integer offset = luaL_optinteger(L, 2, 0);
  1775. lua_Integer size = luaL_optinteger(L, 3, b->getSize() - offset);
  1776. data::ByteData *dest = nullptr;
  1777. size_t destoffset = 0;
  1778. if (!lua_isnoneornil(L, 4))
  1779. {
  1780. dest = luax_checktype<data::ByteData>(L, 4);
  1781. destoffset = (size_t) luaL_optinteger(L, 5, 0);
  1782. }
  1783. GraphicsReadback *r = nullptr;
  1784. luax_catchexcept(L, [&]() { r = instance()->readbackBufferAsync(b, offset, size, dest, destoffset); });
  1785. luax_pushtype(L, r);
  1786. r->release();
  1787. return 1;
  1788. }
  1789. int w_readbackTexture(lua_State *L)
  1790. {
  1791. Texture *t = luax_checktexture(L, 1);
  1792. int slice = 0;
  1793. if (t->getTextureType() != TEXTURE_2D)
  1794. slice = (int) luaL_checkinteger(L, 2) - 1;
  1795. int mipmap = (int) luaL_optinteger(L, 3, 1) - 1;
  1796. Rect rect = {0, 0, t->getPixelWidth(mipmap), t->getPixelHeight(mipmap)};
  1797. if (!lua_isnoneornil(L, 4))
  1798. {
  1799. rect.x = (int) luaL_checkinteger(L, 4);
  1800. rect.y = (int) luaL_checkinteger(L, 5);
  1801. rect.w = (int) luaL_checkinteger(L, 6);
  1802. rect.h = (int) luaL_checkinteger(L, 7);
  1803. }
  1804. image::ImageData *dest = nullptr;
  1805. int destx = 0;
  1806. int desty = 0;
  1807. if (!lua_isnoneornil(L, 8))
  1808. {
  1809. dest = luax_checktype<image::ImageData>(L, 8);
  1810. destx = (int) luaL_optinteger(L, 9, 0);
  1811. desty = (int) luaL_optinteger(L, 10, 0);
  1812. }
  1813. image::ImageData *imagedata = nullptr;
  1814. luax_catchexcept(L, [&]() { imagedata = instance()->readbackTexture(t, slice, mipmap, rect, dest, destx, desty); });
  1815. luax_pushtype(L, imagedata);
  1816. imagedata->release();
  1817. return 1;
  1818. }
  1819. int w_readbackTextureAsync(lua_State *L)
  1820. {
  1821. Texture *t = luax_checktexture(L, 1);
  1822. int slice = 0;
  1823. if (t->getTextureType() != TEXTURE_2D)
  1824. slice = (int) luaL_checkinteger(L, 2) - 1;
  1825. int mipmap = (int) luaL_optinteger(L, 3, 1) - 1;
  1826. Rect rect = {0, 0, t->getPixelWidth(mipmap), t->getPixelHeight(mipmap)};
  1827. if (!lua_isnoneornil(L, 4))
  1828. {
  1829. rect.x = (int) luaL_checkinteger(L, 4);
  1830. rect.y = (int) luaL_checkinteger(L, 5);
  1831. rect.w = (int) luaL_checkinteger(L, 6);
  1832. rect.h = (int) luaL_checkinteger(L, 7);
  1833. }
  1834. image::ImageData *dest = nullptr;
  1835. int destx = 0;
  1836. int desty = 0;
  1837. if (!lua_isnoneornil(L, 8))
  1838. {
  1839. dest = luax_checktype<image::ImageData>(L, 8);
  1840. destx = (int) luaL_optinteger(L, 9, 0);
  1841. desty = (int) luaL_optinteger(L, 10, 0);
  1842. }
  1843. GraphicsReadback *r = nullptr;
  1844. luax_catchexcept(L, [&]() { r = instance()->readbackTextureAsync(t, slice, mipmap, rect, dest, destx, desty); });
  1845. luax_pushtype(L, r);
  1846. r->release();
  1847. return 1;
  1848. }
  1849. int w_setColor(lua_State *L)
  1850. {
  1851. Colorf c;
  1852. if (lua_istable(L, 1))
  1853. {
  1854. for (int i = 1; i <= 4; i++)
  1855. lua_rawgeti(L, 1, i);
  1856. c.r = (float) luaL_checknumber(L, -4);
  1857. c.g = (float) luaL_checknumber(L, -3);
  1858. c.b = (float) luaL_checknumber(L, -2);
  1859. c.a = (float) luaL_optnumber(L, -1, 1.0);
  1860. lua_pop(L, 4);
  1861. }
  1862. else
  1863. {
  1864. c.r = (float) luaL_checknumber(L, 1);
  1865. c.g = (float) luaL_checknumber(L, 2);
  1866. c.b = (float) luaL_checknumber(L, 3);
  1867. c.a = (float) luaL_optnumber(L, 4, 1.0);
  1868. }
  1869. instance()->setColor(c);
  1870. return 0;
  1871. }
  1872. int w_getColor(lua_State *L)
  1873. {
  1874. Colorf c = instance()->getColor();
  1875. lua_pushnumber(L, c.r);
  1876. lua_pushnumber(L, c.g);
  1877. lua_pushnumber(L, c.b);
  1878. lua_pushnumber(L, c.a);
  1879. return 4;
  1880. }
  1881. int w_setBackgroundColor(lua_State *L)
  1882. {
  1883. Colorf c;
  1884. if (lua_istable(L, 1))
  1885. {
  1886. for (int i = 1; i <= 4; i++)
  1887. lua_rawgeti(L, 1, i);
  1888. c.r = (float) luaL_checknumber(L, -4);
  1889. c.g = (float) luaL_checknumber(L, -3);
  1890. c.b = (float) luaL_checknumber(L, -2);
  1891. c.a = (float) luaL_optnumber(L, -1, 1.0);
  1892. lua_pop(L, 4);
  1893. }
  1894. else
  1895. {
  1896. c.r = (float) luaL_checknumber(L, 1);
  1897. c.g = (float) luaL_checknumber(L, 2);
  1898. c.b = (float) luaL_checknumber(L, 3);
  1899. c.a = (float) luaL_optnumber(L, 4, 1.0);
  1900. }
  1901. instance()->setBackgroundColor(c);
  1902. return 0;
  1903. }
  1904. int w_getBackgroundColor(lua_State *L)
  1905. {
  1906. Colorf c = instance()->getBackgroundColor();
  1907. lua_pushnumber(L, c.r);
  1908. lua_pushnumber(L, c.g);
  1909. lua_pushnumber(L, c.b);
  1910. lua_pushnumber(L, c.a);
  1911. return 4;
  1912. }
  1913. int w_setNewFont(lua_State *L)
  1914. {
  1915. luax_markdeprecated(L, 1, "love.graphics.setNewFont", API_FUNCTION, DEPRECATED_NO_REPLACEMENT, nullptr);
  1916. int ret = w_newFont(L);
  1917. Font *font = luax_checktype<Font>(L, -1);
  1918. instance()->setFont(font);
  1919. return ret;
  1920. }
  1921. int w_setFont(lua_State *L)
  1922. {
  1923. Font *font = luax_checktype<Font>(L, 1);
  1924. instance()->setFont(font);
  1925. return 0;
  1926. }
  1927. int w_getFont(lua_State *L)
  1928. {
  1929. Font *f = nullptr;
  1930. luax_catchexcept(L, [&](){ f = instance()->getFont(); });
  1931. luax_pushtype(L, f);
  1932. return 1;
  1933. }
  1934. int w_setColorMask(lua_State *L)
  1935. {
  1936. ColorChannelMask mask;
  1937. if (lua_isnoneornil(L, 1))
  1938. {
  1939. mask.r = mask.g = mask.b = mask.a = true;
  1940. }
  1941. else if (lua_gettop(L) <= 1)
  1942. {
  1943. // Set all color components if a single argument is given.
  1944. mask.r = mask.g = mask.b = mask.a = luax_checkboolean(L, 1);
  1945. }
  1946. else
  1947. {
  1948. mask.r = luax_checkboolean(L, 1);
  1949. mask.g = luax_checkboolean(L, 2);
  1950. mask.b = luax_checkboolean(L, 3);
  1951. mask.a = luax_checkboolean(L, 4);
  1952. }
  1953. instance()->setColorMask(mask);
  1954. return 0;
  1955. }
  1956. int w_getColorMask(lua_State *L)
  1957. {
  1958. ColorChannelMask mask = instance()->getColorMask();
  1959. luax_pushboolean(L, mask.r);
  1960. luax_pushboolean(L, mask.g);
  1961. luax_pushboolean(L, mask.b);
  1962. luax_pushboolean(L, mask.a);
  1963. return 4;
  1964. }
  1965. int w_setBlendMode(lua_State *L)
  1966. {
  1967. BlendMode mode;
  1968. const char *str = luaL_checkstring(L, 1);
  1969. if (!getConstant(str, mode))
  1970. return luax_enumerror(L, "blend mode", getConstants(mode), str);
  1971. BlendAlpha alphamode = BLENDALPHA_MULTIPLY;
  1972. if (!lua_isnoneornil(L, 2))
  1973. {
  1974. const char *alphastr = luaL_checkstring(L, 2);
  1975. if (!getConstant(alphastr, alphamode))
  1976. return luax_enumerror(L, "blend alpha mode", getConstants(alphamode), alphastr);
  1977. }
  1978. luax_catchexcept(L, [&](){ instance()->setBlendMode(mode, alphamode); });
  1979. return 0;
  1980. }
  1981. int w_getBlendMode(lua_State *L)
  1982. {
  1983. const char *str;
  1984. const char *alphastr;
  1985. BlendAlpha alphamode;
  1986. BlendMode mode = instance()->getBlendMode(alphamode);
  1987. if (!getConstant(mode, str))
  1988. return luaL_error(L, "Unknown blend mode");
  1989. if (!getConstant(alphamode, alphastr))
  1990. return luaL_error(L, "Unknown blend alpha mode");
  1991. lua_pushstring(L, str);
  1992. lua_pushstring(L, alphastr);
  1993. return 2;
  1994. }
  1995. static BlendOperation luax_checkblendop(lua_State *L, int idx)
  1996. {
  1997. BlendOperation op = BLENDOP_ADD;
  1998. const char *str = luaL_checkstring(L, idx);
  1999. if (!getConstant(str, op))
  2000. luax_enumerror(L, "blend operation", getConstants(op), str);
  2001. return op;
  2002. }
  2003. static BlendFactor luax_checkblendfactor(lua_State *L, int idx)
  2004. {
  2005. BlendFactor factor = BLENDFACTOR_ZERO;
  2006. const char *str = luaL_checkstring(L, idx);
  2007. if (!getConstant(str, factor))
  2008. luax_enumerror(L, "blend factor", getConstants(factor), str);
  2009. return factor;
  2010. }
  2011. static void luax_pushblendop(lua_State *L, BlendOperation op)
  2012. {
  2013. const char *str;
  2014. if (!getConstant(op, str))
  2015. luaL_error(L, "unknown blend operation");
  2016. lua_pushstring(L, str);
  2017. }
  2018. static void luax_pushblendfactor(lua_State *L, BlendFactor factor)
  2019. {
  2020. const char *str;
  2021. if (!getConstant(factor, str))
  2022. luaL_error(L, "unknown blend factor");
  2023. lua_pushstring(L, str);
  2024. }
  2025. int w_setBlendState(lua_State *L)
  2026. {
  2027. BlendState state;
  2028. if (!lua_isnoneornil(L, 1))
  2029. {
  2030. state.enable = true;
  2031. if (lua_gettop(L) >= 4)
  2032. {
  2033. state.operationRGB = luax_checkblendop(L, 1);
  2034. state.operationA = luax_checkblendop(L, 2);
  2035. state.srcFactorRGB = luax_checkblendfactor(L, 3);
  2036. state.srcFactorA = luax_checkblendfactor(L, 4);
  2037. state.dstFactorRGB = luax_checkblendfactor(L, 5);
  2038. state.dstFactorA = luax_checkblendfactor(L, 6);
  2039. }
  2040. else
  2041. {
  2042. state.operationRGB = state.operationA = luax_checkblendop(L, 1);
  2043. state.srcFactorRGB = state.srcFactorA = luax_checkblendfactor(L, 2);
  2044. state.dstFactorRGB = state.dstFactorA = luax_checkblendfactor(L, 3);
  2045. }
  2046. }
  2047. luax_catchexcept(L, [&](){ instance()->setBlendState(state); });
  2048. return 0;
  2049. }
  2050. int w_getBlendState(lua_State *L)
  2051. {
  2052. const BlendState &state = instance()->getBlendState();
  2053. if (state.enable)
  2054. {
  2055. luax_pushblendop(L, state.operationRGB);
  2056. luax_pushblendop(L, state.operationA);
  2057. luax_pushblendfactor(L, state.srcFactorRGB);
  2058. luax_pushblendfactor(L, state.srcFactorA);
  2059. luax_pushblendfactor(L, state.dstFactorRGB);
  2060. luax_pushblendfactor(L, state.dstFactorA);
  2061. }
  2062. else
  2063. {
  2064. for (int i = 0; i < 6; i++)
  2065. lua_pushnil(L);
  2066. }
  2067. return 6;
  2068. }
  2069. int w_setDefaultFilter(lua_State *L)
  2070. {
  2071. SamplerState s = instance()->getDefaultSamplerState();
  2072. const char *minstr = luaL_checkstring(L, 1);
  2073. const char *magstr = luaL_optstring(L, 2, minstr);
  2074. if (!SamplerState::getConstant(minstr, s.minFilter))
  2075. return luax_enumerror(L, "filter mode", SamplerState::getConstants(s.minFilter), minstr);
  2076. if (!SamplerState::getConstant(magstr, s.magFilter))
  2077. return luax_enumerror(L, "filter mode", SamplerState::getConstants(s.magFilter), magstr);
  2078. s.maxAnisotropy = std::min(std::max(1, (int) luaL_optnumber(L, 3, 1.0)), LOVE_UINT8_MAX);
  2079. instance()->setDefaultSamplerState(s);
  2080. return 0;
  2081. }
  2082. int w_getDefaultFilter(lua_State *L)
  2083. {
  2084. const SamplerState &s = instance()->getDefaultSamplerState();
  2085. const char *minstr;
  2086. const char *magstr;
  2087. if (!SamplerState::getConstant(s.minFilter, minstr))
  2088. return luaL_error(L, "Unknown minification filter mode");
  2089. if (!SamplerState::getConstant(s.magFilter, magstr))
  2090. return luaL_error(L, "Unknown magnification filter mode");
  2091. lua_pushstring(L, minstr);
  2092. lua_pushstring(L, magstr);
  2093. lua_pushnumber(L, s.maxAnisotropy);
  2094. return 3;
  2095. }
  2096. int w_setDefaultMipmapFilter(lua_State *L)
  2097. {
  2098. SamplerState s = instance()->getDefaultSamplerState();
  2099. s.mipmapFilter = SamplerState::MIPMAP_FILTER_NONE;
  2100. if (!lua_isnoneornil(L, 1))
  2101. {
  2102. const char *str = luaL_checkstring(L, 1);
  2103. if (!SamplerState::getConstant(str, s.mipmapFilter))
  2104. return luax_enumerror(L, "filter mode", SamplerState::getConstants(s.mipmapFilter), str);
  2105. }
  2106. s.lodBias = -((float) luaL_optnumber(L, 2, 0.0));
  2107. instance()->setDefaultSamplerState(s);
  2108. return 0;
  2109. }
  2110. int w_getDefaultMipmapFilter(lua_State *L)
  2111. {
  2112. const SamplerState &s = instance()->getDefaultSamplerState();
  2113. const char *str;
  2114. if (SamplerState::getConstant(s.mipmapFilter, str))
  2115. lua_pushstring(L, str);
  2116. else
  2117. lua_pushnil(L);
  2118. lua_pushnumber(L, -s.lodBias);
  2119. return 2;
  2120. }
  2121. int w_setLineWidth(lua_State *L)
  2122. {
  2123. float width = (float)luaL_checknumber(L, 1);
  2124. instance()->setLineWidth(width);
  2125. return 0;
  2126. }
  2127. int w_setLineStyle(lua_State *L)
  2128. {
  2129. Graphics::LineStyle style;
  2130. const char *str = luaL_checkstring(L, 1);
  2131. if (!Graphics::getConstant(str, style))
  2132. return luax_enumerror(L, "line style", Graphics::getConstants(style), str);
  2133. instance()->setLineStyle(style);
  2134. return 0;
  2135. }
  2136. int w_setLineJoin(lua_State *L)
  2137. {
  2138. Graphics::LineJoin join;
  2139. const char *str = luaL_checkstring(L, 1);
  2140. if (!Graphics::getConstant(str, join))
  2141. return luax_enumerror(L, "line join", Graphics::getConstants(join), str);
  2142. instance()->setLineJoin(join);
  2143. return 0;
  2144. }
  2145. int w_getLineWidth(lua_State *L)
  2146. {
  2147. lua_pushnumber(L, instance()->getLineWidth());
  2148. return 1;
  2149. }
  2150. int w_getLineStyle(lua_State *L)
  2151. {
  2152. Graphics::LineStyle style = instance()->getLineStyle();
  2153. const char *str;
  2154. if (!Graphics::getConstant(style, str))
  2155. return luaL_error(L, "Unknown line style");
  2156. lua_pushstring(L, str);
  2157. return 1;
  2158. }
  2159. int w_getLineJoin(lua_State *L)
  2160. {
  2161. Graphics::LineJoin join = instance()->getLineJoin();
  2162. const char *str;
  2163. if (!Graphics::getConstant(join, str))
  2164. return luaL_error(L, "Unknown line join");
  2165. lua_pushstring(L, str);
  2166. return 1;
  2167. }
  2168. int w_setPointSize(lua_State *L)
  2169. {
  2170. float size = (float)luaL_checknumber(L, 1);
  2171. instance()->setPointSize(size);
  2172. return 0;
  2173. }
  2174. int w_getPointSize(lua_State *L)
  2175. {
  2176. lua_pushnumber(L, instance()->getPointSize());
  2177. return 1;
  2178. }
  2179. int w_setDepthMode(lua_State *L)
  2180. {
  2181. if (lua_isnoneornil(L, 1) && lua_isnoneornil(L, 2))
  2182. luax_catchexcept(L, [&]() { instance()->setDepthMode(); });
  2183. else
  2184. {
  2185. CompareMode compare = COMPARE_ALWAYS;
  2186. const char *str = luaL_checkstring(L, 1);
  2187. bool write = luax_checkboolean(L, 2);
  2188. if (!getConstant(str, compare))
  2189. return luax_enumerror(L, "compare mode", getConstants(compare), str);
  2190. luax_catchexcept(L, [&]() { instance()->setDepthMode(compare, write); });
  2191. }
  2192. return 0;
  2193. }
  2194. int w_getDepthMode(lua_State *L)
  2195. {
  2196. CompareMode compare = COMPARE_ALWAYS;
  2197. bool write = false;
  2198. instance()->getDepthMode(compare, write);
  2199. const char *str;
  2200. if (!getConstant(compare, str))
  2201. return luaL_error(L, "Unknown compare mode");
  2202. lua_pushstring(L, str);
  2203. luax_pushboolean(L, write);
  2204. return 2;
  2205. }
  2206. int w_setMeshCullMode(lua_State *L)
  2207. {
  2208. const char *str = luaL_checkstring(L, 1);
  2209. CullMode mode;
  2210. if (!getConstant(str, mode))
  2211. return luax_enumerror(L, "cull mode", getConstants(mode), str);
  2212. luax_catchexcept(L, [&]() { instance()->setMeshCullMode(mode); });
  2213. return 0;
  2214. }
  2215. int w_getMeshCullMode(lua_State *L)
  2216. {
  2217. CullMode mode = instance()->getMeshCullMode();
  2218. const char *str;
  2219. if (!getConstant(mode, str))
  2220. return luaL_error(L, "Unknown cull mode");
  2221. lua_pushstring(L, str);
  2222. return 1;
  2223. }
  2224. int w_setFrontFaceWinding(lua_State *L)
  2225. {
  2226. const char *str = luaL_checkstring(L, 1);
  2227. Winding winding;
  2228. if (!getConstant(str, winding))
  2229. return luax_enumerror(L, "vertex winding", getConstants(winding), str);
  2230. luax_catchexcept(L, [&]() { instance()->setFrontFaceWinding(winding); });
  2231. return 0;
  2232. }
  2233. int w_getFrontFaceWinding(lua_State *L)
  2234. {
  2235. Winding winding = instance()->getFrontFaceWinding();
  2236. const char *str;
  2237. if (!getConstant(winding, str))
  2238. return luaL_error(L, "Unknown vertex winding");
  2239. lua_pushstring(L, str);
  2240. return 1;
  2241. }
  2242. int w_setWireframe(lua_State *L)
  2243. {
  2244. instance()->setWireframe(luax_checkboolean(L, 1));
  2245. return 0;
  2246. }
  2247. int w_isWireframe(lua_State *L)
  2248. {
  2249. luax_pushboolean(L, instance()->isWireframe());
  2250. return 1;
  2251. }
  2252. int w_setShader(lua_State *L)
  2253. {
  2254. if (lua_isnoneornil(L,1))
  2255. {
  2256. instance()->setShader();
  2257. return 0;
  2258. }
  2259. Shader *shader = luax_checkshader(L, 1);
  2260. instance()->setShader(shader);
  2261. return 0;
  2262. }
  2263. int w_getShader(lua_State *L)
  2264. {
  2265. Shader *shader = instance()->getShader();
  2266. if (shader)
  2267. luax_pushtype(L, shader);
  2268. else
  2269. lua_pushnil(L);
  2270. return 1;
  2271. }
  2272. int w_getSupported(lua_State *L)
  2273. {
  2274. const Graphics::Capabilities &caps = instance()->getCapabilities();
  2275. if (lua_istable(L, 1))
  2276. lua_pushvalue(L, 1);
  2277. else
  2278. lua_createtable(L, 0, (int) Graphics::FEATURE_MAX_ENUM);
  2279. for (int i = 0; i < (int) Graphics::FEATURE_MAX_ENUM; i++)
  2280. {
  2281. auto feature = (Graphics::Feature) i;
  2282. const char *name = nullptr;
  2283. if (!Graphics::getConstant(feature, name))
  2284. continue;
  2285. luax_pushboolean(L, caps.features[i]);
  2286. lua_setfield(L, -2, name);
  2287. }
  2288. return 1;
  2289. }
  2290. int w_getTextureFormats(lua_State *L)
  2291. {
  2292. luaL_checktype(L, 1, LUA_TTABLE);
  2293. bool rt = luax_checkboolflag(L, 1, Texture::getConstant(Texture::SETTING_RENDER_TARGET));
  2294. bool computewrite = luax_boolflag(L, 1, Texture::getConstant(Texture::SETTING_COMPUTE_WRITE), false);
  2295. OptionalBool readable;
  2296. lua_getfield(L, 1, Texture::getConstant(Texture::SETTING_READABLE));
  2297. if (!lua_isnoneornil(L, -1))
  2298. readable.set(luax_checkboolean(L, -1));
  2299. lua_pop(L, 1);
  2300. if (lua_istable(L, 2))
  2301. lua_pushvalue(L, 2);
  2302. else
  2303. lua_createtable(L, 0, (int) PIXELFORMAT_MAX_ENUM);
  2304. for (int i = 0; i < (int) PIXELFORMAT_MAX_ENUM; i++)
  2305. {
  2306. PixelFormat format = (PixelFormat) i;
  2307. const char *name = nullptr;
  2308. if (format == PIXELFORMAT_UNKNOWN || !love::getConstant(format, name))
  2309. continue;
  2310. if (rt && isPixelFormatDepth(format))
  2311. continue;
  2312. uint32 usage = PIXELFORMATUSAGEFLAGS_NONE;
  2313. if (rt)
  2314. usage |= PIXELFORMATUSAGEFLAGS_RENDERTARGET;
  2315. if (readable.get(!isPixelFormatDepthStencil(format)))
  2316. usage |= PIXELFORMATUSAGEFLAGS_SAMPLE;
  2317. if (computewrite)
  2318. usage |= PIXELFORMATUSAGEFLAGS_COMPUTEWRITE;
  2319. luax_pushboolean(L, instance()->isPixelFormatSupported(format, (PixelFormatUsageFlags) usage));
  2320. lua_setfield(L, -2, name);
  2321. }
  2322. return 1;
  2323. }
  2324. static int w__getFormats(lua_State *L, int idx, bool (*isFormatSupported)(PixelFormat), bool (*ignore)(PixelFormat))
  2325. {
  2326. if (lua_istable(L, idx))
  2327. lua_pushvalue(L, idx);
  2328. else
  2329. lua_createtable(L, 0, (int) PIXELFORMAT_MAX_ENUM);
  2330. for (int i = 0; i < (int) PIXELFORMAT_MAX_ENUM; i++)
  2331. {
  2332. PixelFormat format = (PixelFormat) i;
  2333. const char *name = nullptr;
  2334. if (format == PIXELFORMAT_UNKNOWN || !love::getConstant(format, name) || ignore(format))
  2335. continue;
  2336. luax_pushboolean(L, isFormatSupported(format));
  2337. lua_setfield(L, -2, name);
  2338. }
  2339. return 1;
  2340. }
  2341. int w_getCanvasFormats(lua_State *L)
  2342. {
  2343. luax_markdeprecated(L, 1, "love.graphics.getCanvasFormats", API_FUNCTION, DEPRECATED_REPLACED, "love.graphics.getTextureFormats");
  2344. bool (*supported)(PixelFormat);
  2345. int idx = 1;
  2346. if (lua_type(L, 1) == LUA_TBOOLEAN)
  2347. {
  2348. idx = 2;
  2349. if (luax_checkboolean(L, 1))
  2350. {
  2351. supported = [](PixelFormat format) -> bool
  2352. {
  2353. const uint32 usage = PIXELFORMATUSAGEFLAGS_SAMPLE | PIXELFORMATUSAGEFLAGS_RENDERTARGET;
  2354. return instance()->isPixelFormatSupported(format, (PixelFormatUsageFlags) usage);
  2355. };
  2356. }
  2357. else
  2358. {
  2359. supported = [](PixelFormat format) -> bool
  2360. {
  2361. return instance()->isPixelFormatSupported(format, PIXELFORMATUSAGEFLAGS_RENDERTARGET);
  2362. };
  2363. }
  2364. }
  2365. else
  2366. {
  2367. supported = [](PixelFormat format) -> bool
  2368. {
  2369. bool readable = !isPixelFormatDepthStencil(format);
  2370. uint32 usage = PIXELFORMATUSAGEFLAGS_RENDERTARGET;
  2371. if (readable)
  2372. usage |= PIXELFORMATUSAGEFLAGS_SAMPLE;
  2373. return instance()->isPixelFormatSupported(format, (PixelFormatUsageFlags) usage);
  2374. };
  2375. }
  2376. return w__getFormats(L, idx, supported, isPixelFormatCompressed);
  2377. }
  2378. int w_getImageFormats(lua_State *L)
  2379. {
  2380. luax_markdeprecated(L, 1, "love.graphics.getImageFormats", API_FUNCTION, DEPRECATED_REPLACED, "love.graphics.getTextureFormats");
  2381. const auto supported = [](PixelFormat format) -> bool
  2382. {
  2383. return instance()->isPixelFormatSupported(format, PIXELFORMATUSAGEFLAGS_SAMPLE);
  2384. };
  2385. const auto ignore = [](PixelFormat format) -> bool
  2386. {
  2387. return !(image::ImageData::validPixelFormat(format) || isPixelFormatCompressed(format));
  2388. };
  2389. return w__getFormats(L, 1, supported, ignore);
  2390. }
  2391. int w_getTextureTypes(lua_State *L)
  2392. {
  2393. const Graphics::Capabilities &caps = instance()->getCapabilities();
  2394. if (lua_istable(L, 1))
  2395. lua_pushvalue(L, 1);
  2396. else
  2397. lua_createtable(L, 0, (int) TEXTURE_MAX_ENUM);
  2398. for (int i = 0; i < (int) TEXTURE_MAX_ENUM; i++)
  2399. {
  2400. TextureType textype = (TextureType) i;
  2401. const char *name = nullptr;
  2402. if (!Texture::getConstant(textype, name))
  2403. continue;
  2404. luax_pushboolean(L, caps.textureTypes[i]);
  2405. lua_setfield(L, -2, name);
  2406. }
  2407. return 1;
  2408. }
  2409. int w_getRendererInfo(lua_State *L)
  2410. {
  2411. Graphics::RendererInfo info;
  2412. luax_catchexcept(L, [&](){ info = instance()->getRendererInfo(); });
  2413. luax_pushstring(L, info.name);
  2414. luax_pushstring(L, info.version);
  2415. luax_pushstring(L, info.vendor);
  2416. luax_pushstring(L, info.device);
  2417. return 4;
  2418. }
  2419. int w_getSystemLimits(lua_State *L)
  2420. {
  2421. const Graphics::Capabilities &caps = instance()->getCapabilities();
  2422. if (lua_istable(L, 1))
  2423. lua_pushvalue(L, 1);
  2424. else
  2425. lua_createtable(L, 0, (int) Graphics::LIMIT_MAX_ENUM);
  2426. for (int i = 0; i < (int) Graphics::LIMIT_MAX_ENUM; i++)
  2427. {
  2428. Graphics::SystemLimit limittype = (Graphics::SystemLimit) i;
  2429. const char *name = nullptr;
  2430. if (!Graphics::getConstant(limittype, name))
  2431. continue;
  2432. lua_pushnumber(L, caps.limits[i]);
  2433. lua_setfield(L, -2, name);
  2434. }
  2435. return 1;
  2436. }
  2437. int w_getStats(lua_State *L)
  2438. {
  2439. Graphics::Stats stats = instance()->getStats();
  2440. if (lua_istable(L, 1))
  2441. lua_pushvalue(L, 1);
  2442. else
  2443. lua_createtable(L, 0, 7);
  2444. lua_pushinteger(L, stats.drawCalls);
  2445. lua_setfield(L, -2, "drawcalls");
  2446. lua_pushinteger(L, stats.drawCallsBatched);
  2447. lua_setfield(L, -2, "drawcallsbatched");
  2448. lua_pushinteger(L, stats.renderTargetSwitches);
  2449. lua_setfield(L, -2, "canvasswitches");
  2450. lua_pushinteger(L, stats.shaderSwitches);
  2451. lua_setfield(L, -2, "shaderswitches");
  2452. lua_pushinteger(L, stats.textures);
  2453. lua_setfield(L, -2, "textures");
  2454. lua_pushinteger(L, stats.fonts);
  2455. lua_setfield(L, -2, "fonts");
  2456. lua_pushinteger(L, stats.textureMemory);
  2457. lua_setfield(L, -2, "texturememory");
  2458. return 1;
  2459. }
  2460. int w_draw(lua_State *L)
  2461. {
  2462. Drawable *drawable = nullptr;
  2463. Texture *texture = nullptr;
  2464. Quad *quad = nullptr;
  2465. int startidx = 2;
  2466. if (luax_istype(L, 2, Quad::type))
  2467. {
  2468. texture = luax_checktexture(L, 1);
  2469. quad = luax_totype<Quad>(L, 2);
  2470. startidx = 3;
  2471. }
  2472. else if (lua_isnil(L, 2) && !lua_isnoneornil(L, 3))
  2473. {
  2474. return luax_typerror(L, 2, "Quad");
  2475. }
  2476. else
  2477. {
  2478. drawable = luax_checktype<Drawable>(L, 1);
  2479. startidx = 2;
  2480. }
  2481. luax_checkstandardtransform(L, startidx, [&](const Matrix4 &m)
  2482. {
  2483. luax_catchexcept(L, [&]()
  2484. {
  2485. if (texture && quad)
  2486. instance()->draw(texture, quad, m);
  2487. else
  2488. instance()->draw(drawable, m);
  2489. });
  2490. });
  2491. return 0;
  2492. }
  2493. int w_drawLayer(lua_State *L)
  2494. {
  2495. Texture *texture = luax_checktexture(L, 1);
  2496. Quad *quad = nullptr;
  2497. int layer = (int) luaL_checkinteger(L, 2) - 1;
  2498. int startidx = 3;
  2499. if (luax_istype(L, startidx, Quad::type))
  2500. {
  2501. texture = luax_checktexture(L, 1);
  2502. quad = luax_totype<Quad>(L, startidx);
  2503. startidx++;
  2504. }
  2505. else if (lua_isnil(L, startidx) && !lua_isnoneornil(L, startidx + 1))
  2506. {
  2507. return luax_typerror(L, startidx, "Quad");
  2508. }
  2509. luax_checkstandardtransform(L, startidx, [&](const Matrix4 &m)
  2510. {
  2511. luax_catchexcept(L, [&]()
  2512. {
  2513. if (quad)
  2514. instance()->drawLayer(texture, layer, quad, m);
  2515. else
  2516. instance()->drawLayer(texture, layer, m);
  2517. });
  2518. });
  2519. return 0;
  2520. }
  2521. int w_drawInstanced(lua_State *L)
  2522. {
  2523. Mesh *t = luax_checkmesh(L, 1);
  2524. int instancecount = (int) luaL_checkinteger(L, 2);
  2525. luax_checkstandardtransform(L, 3, [&](const Matrix4 &m)
  2526. {
  2527. luax_catchexcept(L, [&]() { instance()->drawInstanced(t, m, instancecount); });
  2528. });
  2529. return 0;
  2530. }
  2531. int w_drawIndirect(lua_State *L)
  2532. {
  2533. Mesh *t = luax_checkmesh(L, 1);
  2534. Buffer *argsbuffer = luax_checkbuffer(L, 2);
  2535. int argsindex = (int) luaL_checkinteger(L, 3) - 1;
  2536. luax_checkstandardtransform(L, 4, [&](const Matrix4 &m)
  2537. {
  2538. luax_catchexcept(L, [&]() { instance()->drawIndirect(t, m, argsbuffer, argsindex); });
  2539. });
  2540. return 0;
  2541. }
  2542. int w_drawFromShader(lua_State *L)
  2543. {
  2544. if (luax_istype(L, 1, Buffer::type))
  2545. {
  2546. // Indexed drawing.
  2547. Buffer *t = luax_checkbuffer(L, 1);
  2548. int indexcount = (int) luaL_checkinteger(L, 2);
  2549. int instancecount = (int) luaL_optinteger(L, 3, 1);
  2550. int indexstart = (int) luaL_optinteger(L, 4, 1) - 1;
  2551. Texture *tex = nullptr;
  2552. if (!lua_isnoneornil(L, 5))
  2553. tex = luax_checktexture(L, 5);
  2554. luax_catchexcept(L, [&]() { instance()->drawFromShader(t, indexcount, instancecount, indexstart, tex); });
  2555. }
  2556. else
  2557. {
  2558. const char *primstr = luaL_checkstring(L, 1);
  2559. PrimitiveType primtype = PRIMITIVE_TRIANGLES;
  2560. if (!getConstant(primstr, primtype))
  2561. return luax_enumerror(L, "primitive type", getConstants(primtype), primstr);
  2562. int vertexcount = (int) luaL_checkinteger(L, 2);
  2563. int instancecount = (int) luaL_optinteger(L, 3, 1);
  2564. Texture *tex = nullptr;
  2565. if (!lua_isnoneornil(L, 4))
  2566. tex = luax_checktexture(L, 4);
  2567. luax_catchexcept(L, [&]() { instance()->drawFromShader(primtype, vertexcount, instancecount, tex); });
  2568. }
  2569. return 0;
  2570. }
  2571. int w_drawFromShaderIndirect(lua_State *L)
  2572. {
  2573. if (luax_istype(L, 1, Buffer::type))
  2574. {
  2575. // Indexed drawing.
  2576. Buffer *t = luax_checkbuffer(L, 1);
  2577. Buffer *argsbuffer = luax_checkbuffer(L, 2);
  2578. int argsindex = (int) luaL_optinteger(L, 3, 1) - 1;
  2579. Texture *tex = nullptr;
  2580. if (!lua_isnoneornil(L, 4))
  2581. tex = luax_checktexture(L, 4);
  2582. luax_catchexcept(L, [&]() { instance()->drawFromShaderIndirect(t, argsbuffer, argsindex, tex); });
  2583. }
  2584. else
  2585. {
  2586. const char *primstr = luaL_checkstring(L, 1);
  2587. PrimitiveType primtype = PRIMITIVE_TRIANGLES;
  2588. if (!getConstant(primstr, primtype))
  2589. return luax_enumerror(L, "primitive type", getConstants(primtype), primstr);
  2590. Buffer *argsbuffer = luax_checkbuffer(L, 2);
  2591. int argsindex = (int) luaL_optinteger(L, 3, 1) - 1;
  2592. Texture *tex = nullptr;
  2593. if (!lua_isnoneornil(L, 4))
  2594. tex = luax_checktexture(L, 4);
  2595. luax_catchexcept(L, [&]() { instance()->drawFromShaderIndirect(primtype, argsbuffer, argsindex, tex); });
  2596. }
  2597. return 0;
  2598. }
  2599. int w_print(lua_State *L)
  2600. {
  2601. std::vector<love::font::ColoredString> str;
  2602. luax_checkcoloredstring(L, 1, str);
  2603. if (luax_istype(L, 2, Font::type))
  2604. {
  2605. Font *font = luax_checkfont(L, 2);
  2606. luax_checkstandardtransform(L, 3, [&](const Matrix4 &m)
  2607. {
  2608. luax_catchexcept(L, [&](){ instance()->print(str, font, m); });
  2609. });
  2610. }
  2611. else
  2612. {
  2613. luax_checkstandardtransform(L, 2, [&](const Matrix4 &m)
  2614. {
  2615. luax_catchexcept(L, [&](){ instance()->print(str, m); });
  2616. });
  2617. }
  2618. return 0;
  2619. }
  2620. int w_printf(lua_State *L)
  2621. {
  2622. std::vector<love::font::ColoredString> str;
  2623. luax_checkcoloredstring(L, 1, str);
  2624. Font *font = nullptr;
  2625. int startidx = 2;
  2626. if (luax_istype(L, startidx, Font::type))
  2627. {
  2628. font = luax_checkfont(L, startidx);
  2629. startidx++;
  2630. }
  2631. Font::AlignMode align = Font::ALIGN_LEFT;
  2632. Matrix4 m;
  2633. int formatidx = startidx + 2;
  2634. if (luax_istype(L, startidx, math::Transform::type))
  2635. {
  2636. math::Transform *tf = luax_totype<math::Transform>(L, startidx);
  2637. m = tf->getMatrix();
  2638. formatidx = startidx + 1;
  2639. }
  2640. else
  2641. {
  2642. float x = (float)luaL_checknumber(L, startidx + 0);
  2643. float y = (float)luaL_checknumber(L, startidx + 1);
  2644. float angle = (float) luaL_optnumber(L, startidx + 4, 0.0f);
  2645. float sx = (float) luaL_optnumber(L, startidx + 5, 1.0f);
  2646. float sy = (float) luaL_optnumber(L, startidx + 6, sx);
  2647. float ox = (float) luaL_optnumber(L, startidx + 7, 0.0f);
  2648. float oy = (float) luaL_optnumber(L, startidx + 8, 0.0f);
  2649. float kx = (float) luaL_optnumber(L, startidx + 9, 0.0f);
  2650. float ky = (float) luaL_optnumber(L, startidx + 10, 0.0f);
  2651. m = Matrix4(x, y, angle, sx, sy, ox, oy, kx, ky);
  2652. }
  2653. float wrap = (float)luaL_checknumber(L, formatidx);
  2654. const char *astr = lua_isnoneornil(L, formatidx + 1) ? nullptr : luaL_checkstring(L, formatidx + 1);
  2655. if (astr != nullptr && !Font::getConstant(astr, align))
  2656. return luax_enumerror(L, "alignment", Font::getConstants(align), astr);
  2657. if (font != nullptr)
  2658. luax_catchexcept(L, [&](){ instance()->printf(str, font, wrap, align, m); });
  2659. else
  2660. luax_catchexcept(L, [&](){ instance()->printf(str, wrap, align, m); });
  2661. return 0;
  2662. }
  2663. int w_points(lua_State *L)
  2664. {
  2665. // love.graphics.points has 3 variants:
  2666. // - points(x1, y1, x2, y2, ...)
  2667. // - points({x1, y1, x2, y2, ...})
  2668. // - points({{x1, y1 [, r, g, b, a]}, {x2, y2 [, r, g, b, a]}, ...})
  2669. int args = lua_gettop(L);
  2670. bool is_table = false;
  2671. bool is_table_of_tables = false;
  2672. if (args == 1 && lua_istable(L, 1))
  2673. {
  2674. is_table = true;
  2675. args = (int) luax_objlen(L, 1);
  2676. lua_rawgeti(L, 1, 1);
  2677. is_table_of_tables = lua_istable(L, -1);
  2678. lua_pop(L, 1);
  2679. }
  2680. if (args % 2 != 0 && !is_table_of_tables)
  2681. return luaL_error(L, "Number of vertex components must be a multiple of two");
  2682. int numpositions = args / 2;
  2683. if (is_table_of_tables)
  2684. numpositions = args;
  2685. Vector2 *positions = nullptr;
  2686. Colorf *colors = nullptr;
  2687. if (is_table_of_tables)
  2688. {
  2689. size_t datasize = (sizeof(Vector2) + sizeof(Colorf)) * numpositions;
  2690. uint8 *data = instance()->getScratchBuffer<uint8>(datasize);
  2691. positions = (Vector2 *) data;
  2692. colors = (Colorf *) (data + sizeof(Vector2) * numpositions);
  2693. }
  2694. else
  2695. positions = instance()->getScratchBuffer<Vector2>(numpositions);
  2696. if (is_table)
  2697. {
  2698. if (is_table_of_tables)
  2699. {
  2700. // points({{x1, y1 [, r, g, b, a]}, {x2, y2 [, r, g, b, a]}, ...})
  2701. for (int i = 0; i < args; i++)
  2702. {
  2703. lua_rawgeti(L, 1, i + 1);
  2704. for (int j = 1; j <= 6; j++)
  2705. lua_rawgeti(L, -j, j);
  2706. positions[i].x = luax_checkfloat(L, -6);
  2707. positions[i].y = luax_checkfloat(L, -5);
  2708. colors[i].r = (float) luax_optnumberclamped01(L, -4, 1.0);
  2709. colors[i].g = (float) luax_optnumberclamped01(L, -3, 1.0);
  2710. colors[i].b = (float) luax_optnumberclamped01(L, -2, 1.0);
  2711. colors[i].a = (float) luax_optnumberclamped01(L, -1, 1.0);
  2712. lua_pop(L, 7);
  2713. }
  2714. }
  2715. else
  2716. {
  2717. // points({x1, y1, x2, y2, ...})
  2718. for (int i = 0; i < numpositions; i++)
  2719. {
  2720. lua_rawgeti(L, 1, i * 2 + 1);
  2721. lua_rawgeti(L, 1, i * 2 + 2);
  2722. positions[i].x = luax_checkfloat(L, -2);
  2723. positions[i].y = luax_checkfloat(L, -1);
  2724. lua_pop(L, 2);
  2725. }
  2726. }
  2727. }
  2728. else
  2729. {
  2730. for (int i = 0; i < numpositions; i++)
  2731. {
  2732. positions[i].x = luax_checkfloat(L, i * 2 + 1);
  2733. positions[i].y = luax_checkfloat(L, i * 2 + 2);
  2734. }
  2735. }
  2736. luax_catchexcept(L, [&](){ instance()->points(positions, colors, numpositions); });
  2737. return 0;
  2738. }
  2739. int w_line(lua_State *L)
  2740. {
  2741. int args = lua_gettop(L);
  2742. int arg1type = lua_type(L, 1);
  2743. bool is_table = false;
  2744. if (args == 1 && arg1type == LUA_TTABLE)
  2745. {
  2746. args = (int) luax_objlen(L, 1);
  2747. is_table = true;
  2748. }
  2749. if (arg1type != LUA_TTABLE && arg1type != LUA_TNUMBER)
  2750. return luax_typerror(L, 1, "table or number");
  2751. else if (args % 2 != 0)
  2752. return luaL_error(L, "Number of vertex components must be a multiple of two.");
  2753. else if (args < 4)
  2754. return luaL_error(L, "Need at least two vertices to draw a line.");
  2755. int numvertices = args / 2;
  2756. Vector2 *coords = instance()->getScratchBuffer<Vector2>(numvertices);
  2757. if (is_table)
  2758. {
  2759. for (int i = 0; i < numvertices; ++i)
  2760. {
  2761. lua_rawgeti(L, 1, (i * 2) + 1);
  2762. lua_rawgeti(L, 1, (i * 2) + 2);
  2763. coords[i].x = luax_checkfloat(L, -2);
  2764. coords[i].y = luax_checkfloat(L, -1);
  2765. lua_pop(L, 2);
  2766. }
  2767. }
  2768. else
  2769. {
  2770. for (int i = 0; i < numvertices; ++i)
  2771. {
  2772. coords[i].x = luax_checkfloat(L, (i * 2) + 1);
  2773. coords[i].y = luax_checkfloat(L, (i * 2) + 2);
  2774. }
  2775. }
  2776. luax_catchexcept(L,
  2777. [&](){ instance()->polyline(coords, numvertices); }
  2778. );
  2779. return 0;
  2780. }
  2781. int w_rectangle(lua_State *L)
  2782. {
  2783. Graphics::DrawMode mode;
  2784. const char *str = luaL_checkstring(L, 1);
  2785. if (!Graphics::getConstant(str, mode))
  2786. return luax_enumerror(L, "draw mode", Graphics::getConstants(mode), str);
  2787. float x = (float)luaL_checknumber(L, 2);
  2788. float y = (float)luaL_checknumber(L, 3);
  2789. float w = (float)luaL_checknumber(L, 4);
  2790. float h = (float)luaL_checknumber(L, 5);
  2791. if (lua_isnoneornil(L, 6))
  2792. {
  2793. luax_catchexcept(L, [&](){ instance()->rectangle(mode, x, y, w, h); });
  2794. return 0;
  2795. }
  2796. float rx = (float)luaL_optnumber(L, 6, 0.0);
  2797. float ry = (float)luaL_optnumber(L, 7, rx);
  2798. if (lua_isnoneornil(L, 8))
  2799. luax_catchexcept(L, [&](){ instance()->rectangle(mode, x, y, w, h, rx, ry); });
  2800. else
  2801. {
  2802. int points = (int) luaL_checkinteger(L, 8);
  2803. luax_catchexcept(L, [&](){ instance()->rectangle(mode, x, y, w, h, rx, ry, points); });
  2804. }
  2805. return 0;
  2806. }
  2807. int w_circle(lua_State *L)
  2808. {
  2809. Graphics::DrawMode mode;
  2810. const char *str = luaL_checkstring(L, 1);
  2811. if (!Graphics::getConstant(str, mode))
  2812. return luax_enumerror(L, "draw mode", Graphics::getConstants(mode), str);
  2813. float x = (float)luaL_checknumber(L, 2);
  2814. float y = (float)luaL_checknumber(L, 3);
  2815. float radius = (float)luaL_checknumber(L, 4);
  2816. if (lua_isnoneornil(L, 5))
  2817. luax_catchexcept(L, [&](){ instance()->circle(mode, x, y, radius); });
  2818. else
  2819. {
  2820. int points = (int) luaL_checkinteger(L, 5);
  2821. luax_catchexcept(L, [&](){ instance()->circle(mode, x, y, radius, points); });
  2822. }
  2823. return 0;
  2824. }
  2825. int w_ellipse(lua_State *L)
  2826. {
  2827. Graphics::DrawMode mode;
  2828. const char *str = luaL_checkstring(L, 1);
  2829. if (!Graphics::getConstant(str, mode))
  2830. return luax_enumerror(L, "draw mode", Graphics::getConstants(mode), str);
  2831. float x = (float)luaL_checknumber(L, 2);
  2832. float y = (float)luaL_checknumber(L, 3);
  2833. float a = (float)luaL_checknumber(L, 4);
  2834. float b = (float)luaL_optnumber(L, 5, a);
  2835. if (lua_isnoneornil(L, 6))
  2836. luax_catchexcept(L, [&](){ instance()->ellipse(mode, x, y, a, b); });
  2837. else
  2838. {
  2839. int points = (int) luaL_checkinteger(L, 6);
  2840. luax_catchexcept(L, [&](){ instance()->ellipse(mode, x, y, a, b, points); });
  2841. }
  2842. return 0;
  2843. }
  2844. int w_arc(lua_State *L)
  2845. {
  2846. Graphics::DrawMode drawmode;
  2847. const char *drawstr = luaL_checkstring(L, 1);
  2848. if (!Graphics::getConstant(drawstr, drawmode))
  2849. return luax_enumerror(L, "draw mode", Graphics::getConstants(drawmode), drawstr);
  2850. int startidx = 2;
  2851. Graphics::ArcMode arcmode = Graphics::ARC_PIE;
  2852. if (lua_type(L, 2) == LUA_TSTRING)
  2853. {
  2854. const char *arcstr = luaL_checkstring(L, 2);
  2855. if (!Graphics::getConstant(arcstr, arcmode))
  2856. return luax_enumerror(L, "arc mode", Graphics::getConstants(arcmode), arcstr);
  2857. startidx = 3;
  2858. }
  2859. float x = (float) luaL_checknumber(L, startidx + 0);
  2860. float y = (float) luaL_checknumber(L, startidx + 1);
  2861. float radius = (float) luaL_checknumber(L, startidx + 2);
  2862. float angle1 = (float) luaL_checknumber(L, startidx + 3);
  2863. float angle2 = (float) luaL_checknumber(L, startidx + 4);
  2864. if (lua_isnoneornil(L, startidx + 5))
  2865. luax_catchexcept(L, [&](){ instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2); });
  2866. else
  2867. {
  2868. int points = (int) luaL_checkinteger(L, startidx + 5);
  2869. luax_catchexcept(L, [&](){ instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2, points); });
  2870. }
  2871. return 0;
  2872. }
  2873. int w_polygon(lua_State *L)
  2874. {
  2875. int args = lua_gettop(L) - 1;
  2876. Graphics::DrawMode mode;
  2877. const char *str = luaL_checkstring(L, 1);
  2878. if (!Graphics::getConstant(str, mode))
  2879. return luax_enumerror(L, "draw mode", Graphics::getConstants(mode), str);
  2880. bool is_table = false;
  2881. if (args == 1 && lua_istable(L, 2))
  2882. {
  2883. args = (int) luax_objlen(L, 2);
  2884. is_table = true;
  2885. }
  2886. if (args % 2 != 0)
  2887. return luaL_error(L, "Number of vertex components must be a multiple of two");
  2888. else if (args < 6)
  2889. return luaL_error(L, "Need at least three vertices to draw a polygon");
  2890. int numvertices = args / 2;
  2891. // fetch coords
  2892. Vector2 *coords = instance()->getScratchBuffer<Vector2>(numvertices + 1);
  2893. if (is_table)
  2894. {
  2895. for (int i = 0; i < numvertices; ++i)
  2896. {
  2897. lua_rawgeti(L, 2, (i * 2) + 1);
  2898. lua_rawgeti(L, 2, (i * 2) + 2);
  2899. coords[i].x = luax_checkfloat(L, -2);
  2900. coords[i].y = luax_checkfloat(L, -1);
  2901. lua_pop(L, 2);
  2902. }
  2903. }
  2904. else
  2905. {
  2906. for (int i = 0; i < numvertices; ++i)
  2907. {
  2908. coords[i].x = luax_checkfloat(L, (i * 2) + 2);
  2909. coords[i].y = luax_checkfloat(L, (i * 2) + 3);
  2910. }
  2911. }
  2912. // make a closed loop
  2913. coords[numvertices] = coords[0];
  2914. luax_catchexcept(L, [&](){ instance()->polygon(mode, coords, numvertices+1); });
  2915. return 0;
  2916. }
  2917. int w_dispatchThreadgroups(lua_State* L)
  2918. {
  2919. Shader *shader = luax_checkshader(L, 1);
  2920. int x = (int) luaL_checkinteger(L, 2);
  2921. int y = (int) luaL_optinteger(L, 3, 1);
  2922. int z = (int) luaL_optinteger(L, 4, 1);
  2923. luax_catchexcept(L, [&](){ instance()->dispatchThreadgroups(shader, x, y, z); });
  2924. return 0;
  2925. }
  2926. int w_dispatchIndirect(lua_State *L)
  2927. {
  2928. Shader *shader = luax_checkshader(L, 1);
  2929. Buffer *argsbuffer = luax_checkbuffer(L, 2);
  2930. int argsindex = (int) luaL_optinteger(L, 3, 1) - 1;
  2931. luax_catchexcept(L, [&]() { instance()->dispatchIndirect(shader, argsbuffer, argsindex); });
  2932. return 0;
  2933. }
  2934. int w_copyBuffer(lua_State *L)
  2935. {
  2936. Buffer *source = luax_checkbuffer(L, 1);
  2937. Buffer *dest = luax_checkbuffer(L, 2);
  2938. ptrdiff_t sourceoffset = luaL_optinteger(L, 3, 0);
  2939. ptrdiff_t destoffset = luaL_optinteger(L, 4, 0);
  2940. ptrdiff_t size = std::min(source->getSize() - sourceoffset, dest->getSize() - destoffset);
  2941. if (!lua_isnoneornil(L, 5))
  2942. size = luaL_checkinteger(L, 5);
  2943. if (sourceoffset < 0 || destoffset < 0)
  2944. return luaL_error(L, "copyBuffer offsets cannot be negative.");
  2945. if (size <= 0)
  2946. return luaL_error(L, "copyBuffer size must be greater than 0.");
  2947. luax_catchexcept(L, [&](){ instance()->copyBuffer(source, dest, sourceoffset, destoffset, size); });
  2948. return 0;
  2949. }
  2950. int w_copyBufferToTexture(lua_State *L)
  2951. {
  2952. Buffer *source = luax_checkbuffer(L, 1);
  2953. Texture *dest = luax_checktexture(L, 2);
  2954. ptrdiff_t sourceoffset = luaL_optinteger(L, 3, 0);
  2955. if (sourceoffset < 0)
  2956. return luaL_error(L, "copyBufferToTexture source offset cannot be negative.");
  2957. int sourcewidth = (int) luaL_optinteger(L, 4, 0);
  2958. int slice = 0;
  2959. int mipmap = 0;
  2960. if (dest->getTextureType() != TEXTURE_2D)
  2961. slice = (int) luaL_checkinteger(L, 5) - 1;
  2962. mipmap = (int) luaL_optinteger(L, 6, 1) - 1;
  2963. Rect rect = {0, 0, dest->getPixelWidth(mipmap), dest->getPixelHeight(mipmap)};
  2964. if (!lua_isnoneornil(L, 7))
  2965. {
  2966. rect.x = (int) luaL_checkinteger(L, 7);
  2967. rect.y = (int) luaL_checkinteger(L, 8);
  2968. rect.w = (int) luaL_checkinteger(L, 9);
  2969. rect.h = (int) luaL_checkinteger(L, 10);
  2970. }
  2971. luax_catchexcept(L, [&](){ instance()->copyBufferToTexture(source, dest, sourceoffset, sourcewidth, slice, mipmap, rect); });
  2972. return 0;
  2973. }
  2974. int w_copyTextureToBuffer(lua_State *L)
  2975. {
  2976. Texture *source = luax_checktexture(L, 1);
  2977. Buffer *dest = luax_checkbuffer(L, 2);
  2978. int slice = 0;
  2979. int mipmap = 0;
  2980. if (source->getTextureType() != TEXTURE_2D)
  2981. slice = (int) luaL_checkinteger(L, 3) - 1;
  2982. mipmap = (int) luaL_optinteger(L, 4, 1) - 1;
  2983. Rect rect = {0, 0, source->getPixelWidth(mipmap), source->getPixelHeight(mipmap)};
  2984. if (!lua_isnoneornil(L, 5))
  2985. {
  2986. rect.x = (int) luaL_checkinteger(L, 5);
  2987. rect.y = (int) luaL_checkinteger(L, 6);
  2988. rect.w = (int) luaL_checkinteger(L, 7);
  2989. rect.h = (int) luaL_checkinteger(L, 8);
  2990. }
  2991. ptrdiff_t destoffset = luaL_optinteger(L, 9, 0);
  2992. if (destoffset < 0)
  2993. return luaL_error(L, "copyTextureToBuffer dest offset cannot be negative.");
  2994. int destwidth = (int) luaL_optinteger(L, 10, 0);
  2995. luax_catchexcept(L, [&](){ instance()->copyTextureToBuffer(source, dest, slice, mipmap, rect, destoffset, destwidth); });
  2996. return 0;
  2997. }
  2998. int w_flushBatch(lua_State *)
  2999. {
  3000. instance()->flushBatchedDraws();
  3001. return 0;
  3002. }
  3003. int w_getStackDepth(lua_State *L)
  3004. {
  3005. lua_pushnumber(L, instance()->getStackDepth());
  3006. return 1;
  3007. }
  3008. int w_push(lua_State *L)
  3009. {
  3010. Graphics::StackType stype = Graphics::STACK_TRANSFORM;
  3011. const char *sname = lua_isnoneornil(L, 1) ? nullptr : luaL_checkstring(L, 1);
  3012. if (sname && !Graphics::getConstant(sname, stype))
  3013. return luax_enumerror(L, "graphics stack type", Graphics::getConstants(stype), sname);
  3014. luax_catchexcept(L, [&](){ instance()->push(stype); });
  3015. if (luax_istype(L, 2, math::Transform::type))
  3016. {
  3017. math::Transform *t = luax_totype<math::Transform>(L, 2);
  3018. luax_catchexcept(L, [&]() { instance()->applyTransform(t->getMatrix()); });
  3019. }
  3020. return 0;
  3021. }
  3022. int w_pop(lua_State *L)
  3023. {
  3024. luax_catchexcept(L, [&](){ instance()->pop(); });
  3025. return 0;
  3026. }
  3027. int w_rotate(lua_State *L)
  3028. {
  3029. float angle = (float)luaL_checknumber(L, 1);
  3030. instance()->rotate(angle);
  3031. return 0;
  3032. }
  3033. int w_scale(lua_State *L)
  3034. {
  3035. float sx = (float)luaL_optnumber(L, 1, 1.0f);
  3036. float sy = (float)luaL_optnumber(L, 2, sx);
  3037. instance()->scale(sx, sy);
  3038. return 0;
  3039. }
  3040. int w_translate(lua_State *L)
  3041. {
  3042. float x = (float)luaL_checknumber(L, 1);
  3043. float y = (float)luaL_checknumber(L, 2);
  3044. instance()->translate(x, y);
  3045. return 0;
  3046. }
  3047. int w_shear(lua_State *L)
  3048. {
  3049. float kx = (float)luaL_checknumber(L, 1);
  3050. float ky = (float)luaL_checknumber(L, 2);
  3051. instance()->shear(kx, ky);
  3052. return 0;
  3053. }
  3054. int w_origin(lua_State * /*L*/)
  3055. {
  3056. instance()->origin();
  3057. return 0;
  3058. }
  3059. int w_applyTransform(lua_State *L)
  3060. {
  3061. luax_checkstandardtransform(L, 1, [&](const Matrix4 &m)
  3062. {
  3063. luax_catchexcept(L, [&]() { instance()->applyTransform(m); });
  3064. });
  3065. return 0;
  3066. }
  3067. int w_replaceTransform(lua_State *L)
  3068. {
  3069. luax_checkstandardtransform(L, 1, [&](const Matrix4 &m)
  3070. {
  3071. luax_catchexcept(L, [&]() { instance()->replaceTransform(m); });
  3072. });
  3073. return 0;
  3074. }
  3075. int w_transformPoint(lua_State *L)
  3076. {
  3077. Vector2 p;
  3078. p.x = (float) luaL_checknumber(L, 1);
  3079. p.y = (float) luaL_checknumber(L, 2);
  3080. p = instance()->transformPoint(p);
  3081. lua_pushnumber(L, p.x);
  3082. lua_pushnumber(L, p.y);
  3083. return 2;
  3084. }
  3085. int w_inverseTransformPoint(lua_State *L)
  3086. {
  3087. Vector2 p;
  3088. p.x = (float) luaL_checknumber(L, 1);
  3089. p.y = (float) luaL_checknumber(L, 2);
  3090. p = instance()->inverseTransformPoint(p);
  3091. lua_pushnumber(L, p.x);
  3092. lua_pushnumber(L, p.y);
  3093. return 2;
  3094. }
  3095. int w_setOrthoProjection(lua_State *L)
  3096. {
  3097. float w = (float) luaL_checknumber(L, 1);
  3098. float h = (float) luaL_checknumber(L, 2);
  3099. float near = (float) luaL_optnumber(L, 3, -10.0);
  3100. float far = (float) luaL_optnumber(L, 4, 10.0);
  3101. luax_catchexcept(L, [&]() { instance()->setOrthoProjection(w, h, near, far); });
  3102. return 0;
  3103. }
  3104. int w_setPerspectiveProjection(lua_State *L)
  3105. {
  3106. float verticalfov = (float) luaL_checknumber(L, 1);
  3107. float aspect = (float) luaL_checknumber(L, 2);
  3108. float near = (float) luaL_checknumber(L, 3);
  3109. float far = (float) luaL_checknumber(L, 4);
  3110. luax_catchexcept(L, [&]() { instance()->setPerspectiveProjection(verticalfov, aspect, near, far); });
  3111. return 0;
  3112. }
  3113. int w_resetProjection(lua_State */*L*/)
  3114. {
  3115. instance()->resetProjection();
  3116. return 0;
  3117. }
  3118. // List of functions to wrap.
  3119. static const luaL_Reg functions[] =
  3120. {
  3121. { "reset", w_reset },
  3122. { "clear", w_clear },
  3123. { "discard", w_discard },
  3124. { "present", w_present },
  3125. { "newCanvas", w_newCanvas },
  3126. { "newTexture", w_newTexture },
  3127. { "newCubeTexture", w_newCubeTexture },
  3128. { "newArrayTexture", w_newArrayTexture },
  3129. { "newVolumeTexture", w_newVolumeTexture },
  3130. { "newQuad", w_newQuad },
  3131. { "newFont", w_newFont },
  3132. { "newImageFont", w_newImageFont },
  3133. { "newSpriteBatch", w_newSpriteBatch },
  3134. { "newParticleSystem", w_newParticleSystem },
  3135. { "newShader", w_newShader },
  3136. { "newComputeShader", w_newComputeShader },
  3137. { "newBuffer", w_newBuffer },
  3138. { "newMesh", w_newMesh },
  3139. { "newTextBatch", w_newTextBatch },
  3140. { "_newVideo", w_newVideo },
  3141. { "readbackBuffer", w_readbackBuffer },
  3142. { "readbackBufferAsync", w_readbackBufferAsync },
  3143. { "readbackTexture", w_readbackTexture },
  3144. { "readbackTextureAsync", w_readbackTextureAsync },
  3145. { "validateShader", w_validateShader },
  3146. { "setCanvas", w_setCanvas },
  3147. { "getCanvas", w_getCanvas },
  3148. { "setColor", w_setColor },
  3149. { "getColor", w_getColor },
  3150. { "setBackgroundColor", w_setBackgroundColor },
  3151. { "getBackgroundColor", w_getBackgroundColor },
  3152. { "setNewFont", w_setNewFont },
  3153. { "setFont", w_setFont },
  3154. { "getFont", w_getFont },
  3155. { "setColorMask", w_setColorMask },
  3156. { "getColorMask", w_getColorMask },
  3157. { "setBlendMode", w_setBlendMode },
  3158. { "getBlendMode", w_getBlendMode },
  3159. { "setBlendState", w_setBlendState },
  3160. { "getBlendState", w_getBlendState },
  3161. { "setDefaultFilter", w_setDefaultFilter },
  3162. { "getDefaultFilter", w_getDefaultFilter },
  3163. { "setDefaultMipmapFilter", w_setDefaultMipmapFilter },
  3164. { "getDefaultMipmapFilter", w_getDefaultMipmapFilter },
  3165. { "setLineWidth", w_setLineWidth },
  3166. { "setLineStyle", w_setLineStyle },
  3167. { "setLineJoin", w_setLineJoin },
  3168. { "getLineWidth", w_getLineWidth },
  3169. { "getLineStyle", w_getLineStyle },
  3170. { "getLineJoin", w_getLineJoin },
  3171. { "setPointSize", w_setPointSize },
  3172. { "getPointSize", w_getPointSize },
  3173. { "setDepthMode", w_setDepthMode },
  3174. { "getDepthMode", w_getDepthMode },
  3175. { "setMeshCullMode", w_setMeshCullMode },
  3176. { "getMeshCullMode", w_getMeshCullMode },
  3177. { "setFrontFaceWinding", w_setFrontFaceWinding },
  3178. { "getFrontFaceWinding", w_getFrontFaceWinding },
  3179. { "setWireframe", w_setWireframe },
  3180. { "isWireframe", w_isWireframe },
  3181. { "setShader", w_setShader },
  3182. { "getShader", w_getShader },
  3183. { "getSupported", w_getSupported },
  3184. { "getTextureFormats", w_getTextureFormats },
  3185. { "getRendererInfo", w_getRendererInfo },
  3186. { "getSystemLimits", w_getSystemLimits },
  3187. { "getTextureTypes", w_getTextureTypes },
  3188. { "getStats", w_getStats },
  3189. { "captureScreenshot", w_captureScreenshot },
  3190. { "draw", w_draw },
  3191. { "drawLayer", w_drawLayer },
  3192. { "drawInstanced", w_drawInstanced },
  3193. { "drawIndirect", w_drawIndirect },
  3194. { "drawFromShader", w_drawFromShader },
  3195. { "drawFromShaderIndirect", w_drawFromShaderIndirect },
  3196. { "print", w_print },
  3197. { "printf", w_printf },
  3198. { "dispatchThreadgroups", w_dispatchThreadgroups },
  3199. { "dispatchIndirect", w_dispatchIndirect },
  3200. { "copyBuffer", w_copyBuffer },
  3201. { "copyBufferToTexture", w_copyBufferToTexture },
  3202. { "copyTextureToBuffer", w_copyTextureToBuffer },
  3203. { "isCreated", w_isCreated },
  3204. { "isActive", w_isActive },
  3205. { "isGammaCorrect", w_isGammaCorrect },
  3206. { "getWidth", w_getWidth },
  3207. { "getHeight", w_getHeight },
  3208. { "getDimensions", w_getDimensions },
  3209. { "getPixelWidth", w_getPixelWidth },
  3210. { "getPixelHeight", w_getPixelHeight },
  3211. { "getPixelDimensions", w_getPixelDimensions },
  3212. { "getDPIScale", w_getDPIScale },
  3213. { "getQuadIndexBuffer", w_getQuadIndexBuffer },
  3214. { "setScissor", w_setScissor },
  3215. { "intersectScissor", w_intersectScissor },
  3216. { "getScissor", w_getScissor },
  3217. { "setStencilMode", w_setStencilMode },
  3218. { "getStencilMode", w_getStencilMode },
  3219. { "points", w_points },
  3220. { "line", w_line },
  3221. { "rectangle", w_rectangle },
  3222. { "circle", w_circle },
  3223. { "ellipse", w_ellipse },
  3224. { "arc", w_arc },
  3225. { "polygon", w_polygon },
  3226. { "flushBatch", w_flushBatch },
  3227. { "getStackDepth", w_getStackDepth },
  3228. { "push", w_push },
  3229. { "pop", w_pop },
  3230. { "rotate", w_rotate },
  3231. { "scale", w_scale },
  3232. { "translate", w_translate },
  3233. { "shear", w_shear },
  3234. { "origin", w_origin },
  3235. { "applyTransform", w_applyTransform },
  3236. { "replaceTransform", w_replaceTransform },
  3237. { "transformPoint", w_transformPoint },
  3238. { "inverseTransformPoint", w_inverseTransformPoint },
  3239. { "setOrthoProjection", w_setOrthoProjection },
  3240. { "setPerspectiveProjection", w_setPerspectiveProjection },
  3241. { "resetProjection", w_resetProjection },
  3242. // Deprecated
  3243. { "newImage", w_newImage },
  3244. { "newArrayImage", w_newArrayImage },
  3245. { "newVolumeImage", w_newVolumeImage },
  3246. { "newCubeImage", w_newCubeImage },
  3247. { "newText", w_newText },
  3248. { "getCanvasFormats", w_getCanvasFormats },
  3249. { "getImageFormats", w_getImageFormats },
  3250. { 0, 0 }
  3251. };
  3252. static int luaopen_drawable(lua_State *L)
  3253. {
  3254. return luax_register_type(L, &Drawable::type, nullptr);
  3255. }
  3256. // Types for this module.
  3257. static const lua_CFunction types[] =
  3258. {
  3259. luaopen_drawable,
  3260. luaopen_texture,
  3261. luaopen_font,
  3262. luaopen_quad,
  3263. luaopen_graphicsbuffer,
  3264. luaopen_graphicsreadback,
  3265. luaopen_spritebatch,
  3266. luaopen_particlesystem,
  3267. luaopen_shader,
  3268. luaopen_mesh,
  3269. luaopen_textbatch,
  3270. luaopen_video,
  3271. 0
  3272. };
  3273. extern "C" int luaopen_love_graphics(lua_State *L)
  3274. {
  3275. Graphics *instance = Graphics::createInstance();
  3276. if (instance == nullptr)
  3277. {
  3278. printf("Cannot create graphics: no supported renderer on this system.\n");
  3279. return luaL_error(L, "Cannot create graphics: no supported renderer on this system.");
  3280. }
  3281. WrappedModule w;
  3282. w.module = instance;
  3283. w.name = "graphics";
  3284. w.type = &Graphics::type;
  3285. w.functions = functions;
  3286. w.types = types;
  3287. int n = luax_register_module(L, w);
  3288. if (luaL_loadbuffer(L, (const char *)graphics_lua, sizeof(graphics_lua), "=[love \"wrap_Graphics.lua\"]") == 0)
  3289. lua_call(L, 0, 0);
  3290. else
  3291. lua_error(L);
  3292. return n;
  3293. }
  3294. } // graphics
  3295. } // love