wrap_Graphics.cpp 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827
  1. /**
  2. * Copyright (c) 2006-2022 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_FORMAT));
  609. if (!lua_isnoneornil(L, -1))
  610. {
  611. const char *str = luaL_checkstring(L, -1);
  612. if (!getConstant(str, s.format))
  613. luax_enumerror(L, "pixel format", str);
  614. }
  615. lua_pop(L, 1);
  616. if (checkType)
  617. {
  618. lua_getfield(L, idx, Texture::getConstant(Texture::SETTING_TYPE));
  619. if (!lua_isnoneornil(L, -1))
  620. {
  621. const char *str = luaL_checkstring(L, -1);
  622. if (!Texture::getConstant(str, s.type))
  623. luax_enumerror(L, "texture type", Texture::getConstants(s.type), str);
  624. }
  625. lua_pop(L, 1);
  626. }
  627. if (checkDimensions)
  628. {
  629. s.width = luax_checkintflag(L, idx, Texture::getConstant(Texture::SETTING_WIDTH));
  630. s.height = luax_checkintflag(L, idx, Texture::getConstant(Texture::SETTING_HEIGHT));
  631. if (s.type == TEXTURE_2D_ARRAY || s.type == TEXTURE_VOLUME)
  632. s.layers = luax_checkintflag(L, idx, Texture::getConstant(Texture::SETTING_LAYERS));
  633. }
  634. lua_getfield(L, idx, Texture::getConstant(Texture::SETTING_MIPMAPS));
  635. if (!lua_isnoneornil(L, -1))
  636. {
  637. if (lua_type(L, -1) == LUA_TBOOLEAN)
  638. s.mipmaps = luax_toboolean(L, -1) ? Texture::MIPMAPS_MANUAL : Texture::MIPMAPS_NONE;
  639. else
  640. {
  641. const char *str = luaL_checkstring(L, -1);
  642. if (!Texture::getConstant(str, s.mipmaps))
  643. luax_enumerror(L, "Texture mipmap mode", Texture::getConstants(s.mipmaps), str);
  644. }
  645. }
  646. lua_pop(L, 1);
  647. s.linear = luax_boolflag(L, idx, Texture::getConstant(Texture::SETTING_LINEAR), s.linear);
  648. s.msaa = luax_intflag(L, idx, Texture::getConstant(Texture::SETTING_MSAA), s.msaa);
  649. s.computeWrite = luax_boolflag(L, idx, Texture::getConstant(Texture::SETTING_COMPUTE_WRITE), s.computeWrite);
  650. lua_getfield(L, idx, Texture::getConstant(Texture::SETTING_READABLE));
  651. if (!lua_isnoneornil(L, -1))
  652. s.readable.set(luax_checkboolean(L, -1));
  653. lua_pop(L, 1);
  654. lua_getfield(L, idx, Texture::getConstant(Texture::SETTING_DPI_SCALE));
  655. if (lua_isnumber(L, -1))
  656. {
  657. s.dpiScale = (float) lua_tonumber(L, -1);
  658. setdpiscale = true;
  659. }
  660. lua_pop(L, 1);
  661. }
  662. int w_newCanvas(lua_State *L)
  663. {
  664. luax_checkgraphicscreated(L);
  665. Texture::Settings s;
  666. OptionalBool forceRenderTarget(true);
  667. bool setDPIScale = false;
  668. if (lua_istable(L, 1))
  669. {
  670. luax_checktexturesettings(L, 1, false, true, true, forceRenderTarget, s, setDPIScale);
  671. }
  672. else
  673. {
  674. // check if width and height are given. else default to screen dimensions.
  675. s.width = (int) luaL_optinteger(L, 1, instance()->getWidth());
  676. s.height = (int) luaL_optinteger(L, 2, instance()->getHeight());
  677. int startidx = 3;
  678. if (lua_isnumber(L, 3))
  679. {
  680. s.layers = (int) luaL_checkinteger(L, 3);
  681. s.type = TEXTURE_2D_ARRAY;
  682. startidx = 4;
  683. }
  684. luax_checktexturesettings(L, startidx, true, true, false, forceRenderTarget, s, setDPIScale);
  685. }
  686. // Default to the screen's current pixel density scale.
  687. if (!setDPIScale)
  688. s.dpiScale = instance()->getScreenDPIScale();
  689. Texture *texture = nullptr;
  690. luax_catchexcept(L, [&](){ texture = instance()->newTexture(s); });
  691. luax_pushtype(L, texture);
  692. texture->release();
  693. return 1;
  694. }
  695. int w_newTexture(lua_State *L)
  696. {
  697. luax_checkgraphicscreated(L);
  698. Texture::Slices slices(TEXTURE_2D);
  699. Texture::Slices *slicesref = &slices;
  700. Texture::Settings settings;
  701. settings.type = TEXTURE_2D;
  702. bool dpiscaleset = false;
  703. if (lua_type(L, 1) == LUA_TNUMBER)
  704. {
  705. slicesref = nullptr;
  706. settings.width = (int) luaL_checkinteger(L, 1);
  707. settings.height = (int) luaL_checkinteger(L, 2);
  708. int startidx = 3;
  709. if (lua_type(L, 3) == LUA_TNUMBER)
  710. {
  711. settings.layers = (int) luaL_checkinteger(L, 3);
  712. settings.type = TEXTURE_2D_ARRAY;
  713. startidx = 4;
  714. }
  715. luax_checktexturesettings(L, startidx, true, true, false, OptionalBool(), settings, dpiscaleset);
  716. }
  717. else
  718. {
  719. luax_checktexturesettings(L, 2, true, false, false, OptionalBool(), settings, dpiscaleset);
  720. float *autodpiscale = dpiscaleset ? nullptr : &settings.dpiScale;
  721. if (lua_istable(L, 1))
  722. {
  723. int n = std::max(1, (int) luax_objlen(L, 1));
  724. for (int i = 0; i < n; i++)
  725. {
  726. lua_rawgeti(L, 1, i + 1);
  727. auto data = getImageData(L, -1, true, i == 0 ? autodpiscale : nullptr);
  728. if (data.first.get())
  729. slices.set(0, i, data.first);
  730. else
  731. slices.set(0, i, data.second->getSlice(0, 0));
  732. }
  733. lua_pop(L, n);
  734. }
  735. else
  736. {
  737. auto data = getImageData(L, 1, true, autodpiscale);
  738. if (data.first.get())
  739. slices.set(0, 0, data.first);
  740. else
  741. slices.add(data.second, 0, 0, false, settings.mipmaps != Texture::MIPMAPS_NONE);
  742. }
  743. }
  744. return w__pushNewTexture(L, slicesref, settings);
  745. }
  746. int w_newCubeTexture(lua_State *L)
  747. {
  748. luax_checkgraphicscreated(L);
  749. Texture::Slices slices(TEXTURE_CUBE);
  750. Texture::Slices *slicesref = &slices;
  751. Texture::Settings settings;
  752. settings.type = TEXTURE_CUBE;
  753. bool dpiscaleset = false;
  754. if (lua_type(L, 1) == LUA_TNUMBER)
  755. {
  756. slicesref = nullptr;
  757. settings.width = settings.height = (int) luaL_checkinteger(L, 1);
  758. luax_checktexturesettings(L, 2, true, false, false, OptionalBool(), settings, dpiscaleset);
  759. }
  760. else
  761. {
  762. luax_checktexturesettings(L, 2, true, false, false, OptionalBool(), settings, dpiscaleset);
  763. float *autodpiscale = dpiscaleset ? nullptr : &settings.dpiScale;
  764. auto imagemodule = Module::getInstance<love::image::Image>(Module::M_IMAGE);
  765. if (!lua_istable(L, 1))
  766. {
  767. auto data = getImageData(L, 1, true, autodpiscale);
  768. std::vector<StrongRef<love::image::ImageData>> faces;
  769. if (data.first.get())
  770. {
  771. luax_catchexcept(L, [&](){ faces = imagemodule->newCubeFaces(data.first); });
  772. for (int i = 0; i < (int) faces.size(); i++)
  773. slices.set(i, 0, faces[i]);
  774. }
  775. else
  776. slices.add(data.second, 0, 0, true, settings.mipmaps != Texture::MIPMAPS_NONE);
  777. }
  778. else
  779. {
  780. int tlen = (int) luax_objlen(L, 1);
  781. if (luax_isarrayoftables(L, 1))
  782. {
  783. if (tlen != 6)
  784. return luaL_error(L, "Cubemap images must have 6 faces.");
  785. for (int face = 0; face < tlen; face++)
  786. {
  787. lua_rawgeti(L, 1, face + 1);
  788. luaL_checktype(L, -1, LUA_TTABLE);
  789. int miplen = std::max(1, (int) luax_objlen(L, -1));
  790. for (int mip = 0; mip < miplen; mip++)
  791. {
  792. lua_rawgeti(L, -1, mip + 1);
  793. auto data = getImageData(L, -1, true, face == 0 && mip == 0 ? autodpiscale : nullptr);
  794. if (data.first.get())
  795. slices.set(face, mip, data.first);
  796. else
  797. slices.set(face, mip, data.second->getSlice(0, 0));
  798. lua_pop(L, 1);
  799. }
  800. }
  801. }
  802. else
  803. {
  804. bool usemipmaps = false;
  805. for (int i = 0; i < tlen; i++)
  806. {
  807. lua_rawgeti(L, 1, i + 1);
  808. auto data = getImageData(L, -1, true, i == 0 ? autodpiscale : nullptr);
  809. if (data.first.get())
  810. {
  811. if (usemipmaps || data.first->getWidth() != data.first->getHeight())
  812. {
  813. usemipmaps = true;
  814. std::vector<StrongRef<love::image::ImageData>> faces;
  815. luax_catchexcept(L, [&](){ faces = imagemodule->newCubeFaces(data.first); });
  816. for (int face = 0; face < (int) faces.size(); face++)
  817. slices.set(face, i, faces[i]);
  818. }
  819. else
  820. slices.set(i, 0, data.first);
  821. }
  822. else
  823. slices.add(data.second, i, 0, false, settings.mipmaps != Texture::MIPMAPS_NONE);
  824. }
  825. }
  826. lua_pop(L, tlen);
  827. }
  828. }
  829. return w__pushNewTexture(L, slicesref, settings);
  830. }
  831. int w_newArrayTexture(lua_State *L)
  832. {
  833. luax_checkgraphicscreated(L);
  834. Texture::Slices slices(TEXTURE_2D_ARRAY);
  835. Texture::Slices *slicesref = &slices;
  836. Texture::Settings settings;
  837. settings.type = TEXTURE_2D_ARRAY;
  838. bool dpiscaleset = false;
  839. if (lua_type(L, 1) == LUA_TNUMBER)
  840. {
  841. slicesref = nullptr;
  842. settings.width = (int) luaL_checkinteger(L, 1);
  843. settings.height = (int) luaL_checkinteger(L, 2);
  844. settings.layers = (int) luaL_checkinteger(L, 3);
  845. luax_checktexturesettings(L, 4, true, false, false, OptionalBool(), settings, dpiscaleset);
  846. }
  847. else
  848. {
  849. luax_checktexturesettings(L, 2, true, false, false, OptionalBool(), settings, dpiscaleset);
  850. float *autodpiscale = dpiscaleset ? nullptr : &settings.dpiScale;
  851. if (lua_istable(L, 1))
  852. {
  853. int tlen = std::max(1, (int) luax_objlen(L, 1));
  854. if (luax_isarrayoftables(L, 1))
  855. {
  856. for (int slice = 0; slice < tlen; slice++)
  857. {
  858. lua_rawgeti(L, 1, slice + 1);
  859. luaL_checktype(L, -1, LUA_TTABLE);
  860. int miplen = std::max(1, (int) luax_objlen(L, -1));
  861. for (int mip = 0; mip < miplen; mip++)
  862. {
  863. lua_rawgeti(L, -1, mip + 1);
  864. auto data = getImageData(L, -1, true, slice == 0 && mip == 0 ? autodpiscale : nullptr);
  865. if (data.first.get())
  866. slices.set(slice, mip, data.first);
  867. else
  868. slices.set(slice, mip, data.second->getSlice(0, 0));
  869. lua_pop(L, 1);
  870. }
  871. }
  872. }
  873. else
  874. {
  875. for (int slice = 0; slice < tlen; slice++)
  876. {
  877. lua_rawgeti(L, 1, slice + 1);
  878. auto data = getImageData(L, -1, true, slice == 0 ? autodpiscale : nullptr);
  879. if (data.first.get())
  880. slices.set(slice, 0, data.first);
  881. else
  882. slices.add(data.second, slice, 0, false, settings.mipmaps != Texture::MIPMAPS_NONE);
  883. }
  884. }
  885. lua_pop(L, tlen);
  886. }
  887. else
  888. {
  889. auto data = getImageData(L, 1, true, autodpiscale);
  890. if (data.first.get())
  891. slices.set(0, 0, data.first);
  892. else
  893. slices.add(data.second, 0, 0, true, settings.mipmaps != Texture::MIPMAPS_NONE);
  894. }
  895. }
  896. return w__pushNewTexture(L, slicesref, settings);
  897. }
  898. int w_newVolumeTexture(lua_State *L)
  899. {
  900. luax_checkgraphicscreated(L);
  901. auto imagemodule = Module::getInstance<love::image::Image>(Module::M_IMAGE);
  902. Texture::Slices slices(TEXTURE_VOLUME);
  903. Texture::Slices *slicesref = &slices;
  904. Texture::Settings settings;
  905. settings.type = TEXTURE_VOLUME;
  906. bool dpiscaleset = false;
  907. if (lua_type(L, 1) == LUA_TNUMBER)
  908. {
  909. slicesref = nullptr;
  910. settings.width = (int) luaL_checkinteger(L, 1);
  911. settings.height = (int) luaL_checkinteger(L, 2);
  912. settings.layers = (int) luaL_checkinteger(L, 3);
  913. luax_checktexturesettings(L, 4, true, false, false, OptionalBool(), settings, dpiscaleset);
  914. }
  915. else
  916. {
  917. luax_checktexturesettings(L, 2, true, false, false, OptionalBool(), settings, dpiscaleset);
  918. float *autodpiscale = dpiscaleset ? nullptr : &settings.dpiScale;
  919. if (lua_istable(L, 1))
  920. {
  921. int tlen = std::max(1, (int) luax_objlen(L, 1));
  922. if (luax_isarrayoftables(L, 1))
  923. {
  924. for (int mip = 0; mip < tlen; mip++)
  925. {
  926. lua_rawgeti(L, 1, mip + 1);
  927. luaL_checktype(L, -1, LUA_TTABLE);
  928. int slicelen = std::max(1, (int) luax_objlen(L, -1));
  929. for (int slice = 0; slice < slicelen; slice++)
  930. {
  931. lua_rawgeti(L, -1, slice + 1);
  932. auto data = getImageData(L, -1, true, slice == 0 && mip == 0 ? autodpiscale : nullptr);
  933. if (data.first.get())
  934. slices.set(slice, mip, data.first);
  935. else
  936. slices.set(slice, mip, data.second->getSlice(0, 0));
  937. lua_pop(L, 1);
  938. }
  939. }
  940. }
  941. else
  942. {
  943. for (int layer = 0; layer < tlen; layer++)
  944. {
  945. lua_rawgeti(L, 1, layer + 1);
  946. auto data = getImageData(L, -1, true, layer == 0 ? autodpiscale : nullptr);
  947. if (data.first.get())
  948. slices.set(layer, 0, data.first);
  949. else
  950. slices.add(data.second, layer, 0, false, settings.mipmaps != Texture::MIPMAPS_NONE);
  951. }
  952. }
  953. lua_pop(L, tlen);
  954. }
  955. else
  956. {
  957. auto data = getImageData(L, 1, true, autodpiscale);
  958. if (data.first.get())
  959. {
  960. std::vector<StrongRef<love::image::ImageData>> layers;
  961. luax_catchexcept(L, [&](){ layers = imagemodule->newVolumeLayers(data.first); });
  962. for (int i = 0; i < (int) layers.size(); i++)
  963. slices.set(i, 0, layers[i]);
  964. }
  965. else
  966. slices.add(data.second, 0, 0, true, settings.mipmaps != Texture::MIPMAPS_NONE);
  967. }
  968. }
  969. return w__pushNewTexture(L, slicesref, settings);
  970. }
  971. int w_newImage(lua_State *L)
  972. {
  973. //luax_markdeprecated(L, 1, "love.graphics.newImage", API_FUNCTION, DEPRECATED_RENAMED, "love.graphics.newTexture");
  974. return w_newTexture(L);
  975. }
  976. int w_newCubeImage(lua_State *L)
  977. {
  978. //luax_markdeprecated(L, 1, "love.graphics.newCubeImage", API_FUNCTION, DEPRECATED_RENAMED, "love.graphics.newCubeTexture");
  979. return w_newCubeTexture(L);
  980. }
  981. int w_newArrayImage(lua_State *L)
  982. {
  983. //luax_markdeprecated(L, 1, "love.graphics.newArrayImage", API_FUNCTION, DEPRECATED_RENAMED, "love.graphics.newArrayTexture");
  984. return w_newArrayTexture(L);
  985. }
  986. int w_newVolumeImage(lua_State *L)
  987. {
  988. //luax_markdeprecated(L, 1, "love.graphics.newVolumeImage", API_FUNCTION, DEPRECATED_RENAMED, "love.graphics.newVolumeTexture");
  989. return w_newVolumeTexture(L);
  990. }
  991. int w_newQuad(lua_State *L)
  992. {
  993. luax_checkgraphicscreated(L);
  994. Quad::Viewport v;
  995. v.x = luaL_checknumber(L, 1);
  996. v.y = luaL_checknumber(L, 2);
  997. v.w = luaL_checknumber(L, 3);
  998. v.h = luaL_checknumber(L, 4);
  999. double sw = 0.0f;
  1000. double sh = 0.0f;
  1001. int layer = 0;
  1002. if (luax_istype(L, 5, Texture::type))
  1003. {
  1004. Texture *texture = luax_checktexture(L, 5);
  1005. sw = texture->getWidth();
  1006. sh = texture->getHeight();
  1007. }
  1008. else if (luax_istype(L, 6, Texture::type))
  1009. {
  1010. layer = (int) luaL_checkinteger(L, 5) - 1;
  1011. Texture *texture = luax_checktexture(L, 6);
  1012. sw = texture->getWidth();
  1013. sh = texture->getHeight();
  1014. }
  1015. else if (!lua_isnoneornil(L, 7))
  1016. {
  1017. layer = (int) luaL_checkinteger(L, 5) - 1;
  1018. sw = luaL_checknumber(L, 6);
  1019. sh = luaL_checknumber(L, 7);
  1020. }
  1021. else
  1022. {
  1023. sw = luaL_checknumber(L, 5);
  1024. sh = luaL_checknumber(L, 6);
  1025. }
  1026. Quad *quad = instance()->newQuad(v, sw, sh);
  1027. quad->setLayer(layer);
  1028. luax_pushtype(L, quad);
  1029. quad->release();
  1030. return 1;
  1031. }
  1032. int w_newFont(lua_State *L)
  1033. {
  1034. luax_checkgraphicscreated(L);
  1035. graphics::Font *font = nullptr;
  1036. // Convert to Rasterizer, if necessary.
  1037. if (!luax_istype(L, 1, love::font::Rasterizer::type))
  1038. {
  1039. std::vector<int> idxs;
  1040. for (int i = 0; i < lua_gettop(L); i++)
  1041. idxs.push_back(i + 1);
  1042. luax_convobj(L, idxs, "font", "newRasterizer");
  1043. }
  1044. love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1);
  1045. luax_catchexcept(L, [&]() {
  1046. font = instance()->newFont(rasterizer); }
  1047. );
  1048. // Push the type.
  1049. luax_pushtype(L, font);
  1050. font->release();
  1051. return 1;
  1052. }
  1053. int w_newImageFont(lua_State *L)
  1054. {
  1055. luax_checkgraphicscreated(L);
  1056. // Convert to Rasterizer if necessary.
  1057. if (!luax_istype(L, 1, love::font::Rasterizer::type))
  1058. {
  1059. luaL_checktype(L, 2, LUA_TSTRING);
  1060. std::vector<int> idxs;
  1061. for (int i = 0; i < lua_gettop(L); i++)
  1062. idxs.push_back(i + 1);
  1063. luax_convobj(L, idxs, "font", "newImageRasterizer");
  1064. }
  1065. love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1);
  1066. // Create the font.
  1067. Font *font = instance()->newFont(rasterizer);
  1068. // Push the type.
  1069. luax_pushtype(L, font);
  1070. font->release();
  1071. return 1;
  1072. }
  1073. int w_newSpriteBatch(lua_State *L)
  1074. {
  1075. luax_checkgraphicscreated(L);
  1076. Texture *texture = luax_checktexture(L, 1);
  1077. int size = (int) luaL_optinteger(L, 2, 1000);
  1078. BufferDataUsage usage = BUFFERDATAUSAGE_DYNAMIC;
  1079. if (lua_gettop(L) > 2)
  1080. {
  1081. const char *usagestr = luaL_checkstring(L, 3);
  1082. if (!getConstant(usagestr, usage))
  1083. return luax_enumerror(L, "usage hint", getConstants(usage), usagestr);
  1084. }
  1085. SpriteBatch *t = nullptr;
  1086. luax_catchexcept(L,
  1087. [&](){ t = instance()->newSpriteBatch(texture, size, usage); }
  1088. );
  1089. luax_pushtype(L, t);
  1090. t->release();
  1091. return 1;
  1092. }
  1093. int w_newParticleSystem(lua_State *L)
  1094. {
  1095. luax_checkgraphicscreated(L);
  1096. Texture *texture = luax_checktexture(L, 1);
  1097. lua_Number size = luaL_optnumber(L, 2, 1000);
  1098. ParticleSystem *t = nullptr;
  1099. if (size < 1.0 || size > ParticleSystem::MAX_PARTICLES)
  1100. return luaL_error(L, "Invalid ParticleSystem size");
  1101. luax_catchexcept(L,
  1102. [&](){ t = instance()->newParticleSystem(texture, int(size)); }
  1103. );
  1104. luax_pushtype(L, t);
  1105. t->release();
  1106. return 1;
  1107. }
  1108. static int w_getShaderSource(lua_State *L, int startidx, std::vector<std::string> &stages, Shader::CompileOptions &options)
  1109. {
  1110. using namespace love::filesystem;
  1111. luax_checkgraphicscreated(L);
  1112. auto fs = Module::getInstance<Filesystem>(Module::M_FILESYSTEM);
  1113. // read any filepath arguments
  1114. for (int i = startidx; i < startidx + 2; i++)
  1115. {
  1116. if (!lua_isstring(L, i))
  1117. {
  1118. if (luax_cangetfiledata(L, i))
  1119. {
  1120. FileData *fd = luax_getfiledata(L, i);
  1121. lua_pushlstring(L, (const char *) fd->getData(), fd->getSize());
  1122. fd->release();
  1123. lua_replace(L, i);
  1124. }
  1125. continue;
  1126. }
  1127. size_t slen = 0;
  1128. const char *str = lua_tolstring(L, i, &slen);
  1129. Filesystem::Info info = {};
  1130. if (fs != nullptr && fs->getInfo(str, info))
  1131. {
  1132. FileData *fd = nullptr;
  1133. luax_catchexcept(L, [&](){ fd = fs->read(str); });
  1134. lua_pushlstring(L, (const char *) fd->getData(), fd->getSize());
  1135. fd->release();
  1136. lua_replace(L, i);
  1137. }
  1138. else
  1139. {
  1140. // Check if the argument looks like a filepath - we want a nicer
  1141. // error for misspelled filepath arguments.
  1142. if (slen > 0 && slen < 64 && !strchr(str, '\n'))
  1143. {
  1144. const char *ext = strchr(str, '.');
  1145. if (ext != nullptr && !strchr(ext, ';') && !strchr(ext, ' '))
  1146. return luaL_error(L, "Could not open file %s. Does not exist.", str);
  1147. }
  1148. }
  1149. }
  1150. bool has_arg1 = lua_isstring(L, startidx + 0) != 0;
  1151. bool has_arg2 = lua_isstring(L, startidx + 1) != 0;
  1152. // require at least one string argument
  1153. if (!(has_arg1 || has_arg2))
  1154. luaL_checkstring(L, startidx);
  1155. if (has_arg1)
  1156. stages.push_back(luax_checkstring(L, startidx + 0));
  1157. if (has_arg2)
  1158. stages.push_back(luax_checkstring(L, startidx + 1));
  1159. int optionsidx = has_arg2 ? startidx + 2 : startidx + 1;
  1160. if (!lua_isnoneornil(L, optionsidx))
  1161. {
  1162. luaL_checktype(L, optionsidx, LUA_TTABLE);
  1163. lua_getfield(L, optionsidx, "defines");
  1164. if (!lua_isnoneornil(L, -1))
  1165. {
  1166. if (!lua_istable(L, -1))
  1167. luaL_argerror(L, optionsidx, "expected 'defines' field to be a table");
  1168. lua_pushnil(L);
  1169. while (lua_next(L, -2))
  1170. {
  1171. std::string defname;
  1172. std::string defval;
  1173. if (lua_type(L, -2) == LUA_TNUMBER && lua_type(L, -1) == LUA_TSTRING)
  1174. defname = luaL_checkstring(L, -1);
  1175. else if (lua_type(L, -2) != LUA_TSTRING)
  1176. luaL_argerror(L, optionsidx, "all fields in the 'defines' table must use string keys.");
  1177. else
  1178. {
  1179. defname = luaL_checkstring(L, -2);
  1180. if (lua_type(L, -1) == LUA_TBOOLEAN)
  1181. defval = luax_toboolean(L, -1) ? "1" : "0";
  1182. else
  1183. {
  1184. const char *val = lua_tostring(L, -1);
  1185. if (val == nullptr)
  1186. luaL_argerror(L, optionsidx, "'defines' table values must be strings, numbers, or booleans.");
  1187. defval = val;
  1188. }
  1189. }
  1190. options.defines[defname] = defval;
  1191. lua_pop(L, 1);
  1192. }
  1193. }
  1194. lua_pop(L, 1);
  1195. }
  1196. return 0;
  1197. }
  1198. int w_newShader(lua_State *L)
  1199. {
  1200. std::vector<std::string> stages;
  1201. Shader::CompileOptions options;
  1202. w_getShaderSource(L, 1, stages, options);
  1203. bool should_error = false;
  1204. try
  1205. {
  1206. Shader *shader = instance()->newShader(stages, options);
  1207. luax_pushtype(L, shader);
  1208. shader->release();
  1209. }
  1210. catch (love::Exception &e)
  1211. {
  1212. luax_getfunction(L, "graphics", "_transformGLSLErrorMessages");
  1213. lua_pushstring(L, e.what());
  1214. // Function pushes the new error string onto the stack.
  1215. lua_pcall(L, 1, 1, 0);
  1216. should_error = true;
  1217. }
  1218. if (should_error)
  1219. return lua_error(L);
  1220. return 1;
  1221. }
  1222. int w_newComputeShader(lua_State* L)
  1223. {
  1224. std::vector<std::string> stages;
  1225. Shader::CompileOptions options;
  1226. w_getShaderSource(L, 1, stages, options);
  1227. bool should_error = false;
  1228. try
  1229. {
  1230. Shader *shader = instance()->newComputeShader(stages[0], options);
  1231. luax_pushtype(L, shader);
  1232. shader->release();
  1233. }
  1234. catch (love::Exception &e)
  1235. {
  1236. luax_getfunction(L, "graphics", "_transformGLSLErrorMessages");
  1237. lua_pushstring(L, e.what());
  1238. // Function pushes the new error string onto the stack.
  1239. lua_pcall(L, 1, 1, 0);
  1240. should_error = true;
  1241. }
  1242. if (should_error)
  1243. return lua_error(L);
  1244. return 1;
  1245. }
  1246. int w_validateShader(lua_State *L)
  1247. {
  1248. bool gles = luax_checkboolean(L, 1);
  1249. std::vector<std::string> stages;
  1250. Shader::CompileOptions options;
  1251. w_getShaderSource(L, 2, stages, options);
  1252. bool success = true;
  1253. std::string err;
  1254. try
  1255. {
  1256. success = instance()->validateShader(gles, stages, options, err);
  1257. }
  1258. catch (love::Exception &e)
  1259. {
  1260. success = false;
  1261. err = e.what();
  1262. }
  1263. luax_pushboolean(L, success);
  1264. if (!success)
  1265. {
  1266. luax_pushstring(L, err);
  1267. return 2;
  1268. }
  1269. return 1;
  1270. }
  1271. static BufferDataUsage luax_optdatausage(lua_State *L, int idx, BufferDataUsage def)
  1272. {
  1273. const char *usagestr = lua_isnoneornil(L, idx) ? nullptr : luaL_checkstring(L, idx);
  1274. if (usagestr && !getConstant(usagestr, def))
  1275. luax_enumerror(L, "usage hint", getConstants(def), usagestr);
  1276. return def;
  1277. }
  1278. static void luax_optbuffersettings(lua_State *L, int idx, Buffer::Settings &settings)
  1279. {
  1280. if (lua_isnoneornil(L, idx))
  1281. return;
  1282. luaL_checktype(L, idx, LUA_TTABLE);
  1283. lua_getfield(L, idx, "usage");
  1284. settings.dataUsage = luax_optdatausage(L, -1, settings.dataUsage);
  1285. lua_pop(L, 1);
  1286. }
  1287. static Buffer::DataDeclaration luax_checkdatadeclaration(lua_State* L, int formattableidx, int arrayindex, int declindex, bool requirename)
  1288. {
  1289. Buffer::DataDeclaration decl("", DATAFORMAT_MAX_ENUM);
  1290. lua_getfield(L, declindex, "name");
  1291. if (requirename && lua_type(L, -1) != LUA_TSTRING)
  1292. {
  1293. std::ostringstream ss;
  1294. ss << "'name' field expected in array element #";
  1295. ss << arrayindex;
  1296. ss << " of format table";
  1297. std::string str = ss.str();
  1298. luaL_argerror(L, formattableidx, str.c_str());
  1299. }
  1300. else if (!lua_isnoneornil(L, -1))
  1301. decl.name = luax_checkstring(L, -1);
  1302. lua_pop(L, 1);
  1303. lua_getfield(L, declindex, "format");
  1304. if (lua_type(L, -1) != LUA_TSTRING)
  1305. {
  1306. std::ostringstream ss;
  1307. ss << "'format' field expected in array element #";
  1308. ss << arrayindex;
  1309. ss << " of format table";
  1310. std::string str = ss.str();
  1311. luaL_argerror(L, formattableidx, str.c_str());
  1312. }
  1313. const char* formatstr = luaL_checkstring(L, -1);
  1314. if (!getConstant(formatstr, decl.format))
  1315. luax_enumerror(L, "data format", getConstants(decl.format), formatstr);
  1316. lua_pop(L, 1);
  1317. decl.arrayLength = luax_intflag(L, declindex, "arraylength", 0);
  1318. return decl;
  1319. }
  1320. static void luax_checkbufferformat(lua_State *L, int idx, std::vector<Buffer::DataDeclaration> &format)
  1321. {
  1322. if (lua_type(L, idx) == LUA_TSTRING)
  1323. {
  1324. Buffer::DataDeclaration decl("", DATAFORMAT_MAX_ENUM);
  1325. const char *formatstr = luaL_checkstring(L, idx);
  1326. if (!getConstant(formatstr, decl.format))
  1327. luax_enumerror(L, "data format", getConstants(decl.format), formatstr);
  1328. format.push_back(decl);
  1329. return;
  1330. }
  1331. luaL_checktype(L, idx, LUA_TTABLE);
  1332. int tablelen = luax_objlen(L, idx);
  1333. for (int i = 1; i <= tablelen; i++)
  1334. {
  1335. lua_rawgeti(L, idx, i);
  1336. luaL_checktype(L, -1, LUA_TTABLE);
  1337. Buffer::DataDeclaration decl = luax_checkdatadeclaration(L, idx, i, -1, false);
  1338. format.push_back(decl);
  1339. lua_pop(L, 1);
  1340. }
  1341. }
  1342. static Buffer *luax_newbuffer(lua_State *L, int idx, Buffer::Settings settings, const std::vector<Buffer::DataDeclaration> &format)
  1343. {
  1344. size_t arraylength = 0;
  1345. size_t bytesize = 0;
  1346. Data *data = nullptr;
  1347. const void *initialdata = nullptr;
  1348. int ncomponents = 0;
  1349. for (const Buffer::DataDeclaration &decl : format)
  1350. ncomponents += getDataFormatInfo(decl.format).components;
  1351. if (luax_istype(L, idx, Data::type))
  1352. {
  1353. data = luax_checktype<Data>(L, idx);
  1354. initialdata = data->getData();
  1355. bytesize = data->getSize();
  1356. }
  1357. bool tableoftables = false;
  1358. if (lua_istable(L, idx))
  1359. {
  1360. arraylength = luax_objlen(L, idx);
  1361. lua_rawgeti(L, idx, 1);
  1362. tableoftables = lua_istable(L, -1);
  1363. lua_pop(L, 1);
  1364. if (!tableoftables)
  1365. {
  1366. if (arraylength % ncomponents != 0)
  1367. luaL_error(L, "Array length in flat array variant of newBuffer must be a multiple of the total number of components (%d)", ncomponents);
  1368. arraylength /= ncomponents;
  1369. }
  1370. }
  1371. else if (data == nullptr)
  1372. {
  1373. lua_Integer len = luaL_checkinteger(L, idx);
  1374. if (len <= 0)
  1375. luaL_argerror(L, idx, "number of elements must be greater than 0");
  1376. arraylength = (size_t) len;
  1377. settings.zeroInitialize = true;
  1378. }
  1379. Buffer *b = nullptr;
  1380. luax_catchexcept(L, [&] { b = instance()->newBuffer(settings, format, initialdata, bytesize, arraylength); });
  1381. if (lua_istable(L, idx))
  1382. {
  1383. Buffer::Mapper mapper(*b);
  1384. char *data = (char *) mapper.data;
  1385. const auto &members = b->getDataMembers();
  1386. size_t stride = b->getArrayStride();
  1387. if (tableoftables)
  1388. {
  1389. for (size_t i = 0; i < arraylength; i++)
  1390. {
  1391. // get arraydata[index]
  1392. lua_rawgeti(L, 2, i + 1);
  1393. luaL_checktype(L, -1, LUA_TTABLE);
  1394. // get arraydata[index][j]
  1395. for (int j = 1; j <= ncomponents; j++)
  1396. lua_rawgeti(L, -j, j);
  1397. int idx = -ncomponents;
  1398. for (const Buffer::DataMember &member : members)
  1399. {
  1400. luax_writebufferdata(L, idx, member.decl.format, data + member.offset);
  1401. idx += member.info.components;
  1402. }
  1403. lua_pop(L, ncomponents + 1);
  1404. data += stride;
  1405. }
  1406. }
  1407. else // Flat array
  1408. {
  1409. for (size_t i = 0; i < arraylength; i++)
  1410. {
  1411. // get arraydata[arrayindex * ncomponents + componentindex]
  1412. for (int componentindex = 1; componentindex <= ncomponents; componentindex++)
  1413. lua_rawgeti(L, 2, i * ncomponents + componentindex);
  1414. int idx = -ncomponents;
  1415. for (const Buffer::DataMember &member : members)
  1416. {
  1417. luax_writebufferdata(L, idx, member.decl.format, data + member.offset);
  1418. idx += member.info.components;
  1419. }
  1420. lua_pop(L, ncomponents);
  1421. data += stride;
  1422. }
  1423. }
  1424. }
  1425. return b;
  1426. }
  1427. int w_newBuffer(lua_State *L)
  1428. {
  1429. Buffer::Settings settings(0, BUFFERDATAUSAGE_DYNAMIC);
  1430. luaL_checktype(L, 3, LUA_TTABLE);
  1431. for (int i = 0; i < BUFFERUSAGE_MAX_ENUM; i++)
  1432. {
  1433. BufferUsage bufferusage = (BufferUsage) i;
  1434. const char *tname = nullptr;
  1435. if (!getConstant(bufferusage, tname))
  1436. continue;
  1437. if (luax_boolflag(L, 3, tname, false))
  1438. settings.usageFlags = (BufferUsageFlags)(settings.usageFlags | (1u << i));
  1439. }
  1440. luax_optbuffersettings(L, 3, settings);
  1441. std::vector<Buffer::DataDeclaration> format;
  1442. luax_checkbufferformat(L, 1, format);
  1443. Buffer *b = luax_newbuffer(L, 2, settings, format);
  1444. luax_pushtype(L, b);
  1445. b->release();
  1446. return 1;
  1447. }
  1448. int w_newVertexBuffer(lua_State *L)
  1449. {
  1450. Buffer::Settings settings(BUFFERUSAGEFLAG_VERTEX, BUFFERDATAUSAGE_DYNAMIC);
  1451. luax_optbuffersettings(L, 3, settings);
  1452. std::vector<Buffer::DataDeclaration> format;
  1453. luax_checkbufferformat(L, 1, format);
  1454. Buffer *b = luax_newbuffer(L, 2, settings, format);
  1455. luax_pushtype(L, b);
  1456. b->release();
  1457. return 1;
  1458. }
  1459. int w_newIndexBuffer(lua_State *L)
  1460. {
  1461. Buffer::Settings settings(BUFFERUSAGEFLAG_INDEX, BUFFERDATAUSAGE_DYNAMIC);
  1462. luax_optbuffersettings(L, 3, settings);
  1463. size_t arraylength = 0;
  1464. size_t bytesize = 0;
  1465. DataFormat format = DATAFORMAT_UINT16;
  1466. Data *data = nullptr;
  1467. const void *initialdata = nullptr;
  1468. if (luax_istype(L, 1, Data::type))
  1469. {
  1470. data = luax_checktype<Data>(L, 1);
  1471. initialdata = data->getData();
  1472. bytesize = data->getSize();
  1473. }
  1474. if (lua_istable(L, 1))
  1475. {
  1476. arraylength = luax_objlen(L, 1);
  1477. // Scan array for invalid types and the max value.
  1478. lua_Integer maxvalue = 0;
  1479. for (size_t i = 0; i < arraylength; i++)
  1480. {
  1481. lua_rawgeti(L, 1, i + 1);
  1482. lua_Integer v = luaL_checkinteger(L, -1);
  1483. lua_pop(L, 1);
  1484. if (v < 0)
  1485. return luaL_argerror(L, 1, "expected positive integer values in array");
  1486. else
  1487. maxvalue = std::max(maxvalue, v);
  1488. }
  1489. format = getIndexDataFormat(getIndexDataTypeFromMax(maxvalue));
  1490. }
  1491. else if (data == nullptr)
  1492. {
  1493. lua_Integer len = luaL_checkinteger(L, 1);
  1494. if (len <= 0)
  1495. return luaL_argerror(L, 1, "number of elements must be greater than 0");
  1496. arraylength = (size_t) len;
  1497. settings.zeroInitialize = true;
  1498. }
  1499. if (data != nullptr || !lua_isnoneornil(L, 2))
  1500. {
  1501. const char *formatstr = luaL_checkstring(L, 2);
  1502. if (!getConstant(formatstr, format))
  1503. return luax_enumerror(L, "index data format", getConstants(format), formatstr);
  1504. }
  1505. Buffer *b = nullptr;
  1506. luax_catchexcept(L, [&] { b = instance()->newBuffer(settings, format, initialdata, bytesize, arraylength); });
  1507. if (lua_istable(L, 1))
  1508. {
  1509. Buffer::Mapper mapper(*b);
  1510. uint16 *u16data = (uint16 *) mapper.data;
  1511. uint32 *u32data = (uint32 *) mapper.data;
  1512. for (size_t i = 0; i < arraylength; i++)
  1513. {
  1514. lua_rawgeti(L, 1, i + 1);
  1515. lua_Integer v = luaL_checkinteger(L, -1);
  1516. lua_pop(L, 1);
  1517. if (format == DATAFORMAT_UINT16)
  1518. u16data[i] = (uint16) v;
  1519. else
  1520. u32data[i] = (uint32) v;
  1521. }
  1522. }
  1523. luax_pushtype(L, b);
  1524. b->release();
  1525. return 1;
  1526. }
  1527. static PrimitiveType luax_checkmeshdrawmode(lua_State *L, int idx)
  1528. {
  1529. const char *modestr = luaL_checkstring(L, idx);
  1530. PrimitiveType mode = PRIMITIVE_TRIANGLES;
  1531. if (!getConstant(modestr, mode))
  1532. luax_enumerror(L, "mesh draw mode", getConstants(mode), modestr);
  1533. return mode;
  1534. }
  1535. static Mesh *newStandardMesh(lua_State *L)
  1536. {
  1537. Mesh *t = nullptr;
  1538. PrimitiveType drawmode = luax_checkmeshdrawmode(L, 2);
  1539. BufferDataUsage usage = luax_optdatausage(L, 3, BUFFERDATAUSAGE_DYNAMIC);
  1540. std::vector<Buffer::DataDeclaration> format = Mesh::getDefaultVertexFormat();
  1541. // First argument is a table of standard vertices, or the number of
  1542. // standard vertices.
  1543. if (lua_istable(L, 1))
  1544. {
  1545. size_t vertexcount = luax_objlen(L, 1);
  1546. std::vector<Vertex> vertices;
  1547. vertices.reserve(vertexcount);
  1548. // Get the vertices from the table.
  1549. for (size_t i = 1; i <= vertexcount; i++)
  1550. {
  1551. lua_rawgeti(L, 1, (int) i);
  1552. if (lua_type(L, -1) != LUA_TTABLE)
  1553. {
  1554. luax_typerror(L, 1, "table of tables");
  1555. return nullptr;
  1556. }
  1557. for (int j = 1; j <= 8; j++)
  1558. lua_rawgeti(L, -j, j);
  1559. Vertex v;
  1560. v.x = (float) luaL_checknumber(L, -8);
  1561. v.y = (float) luaL_checknumber(L, -7);
  1562. v.s = (float) luaL_optnumber(L, -6, 0.0);
  1563. v.t = (float) luaL_optnumber(L, -5, 0.0);
  1564. v.color.r = (unsigned char) (luax_optnumberclamped01(L, -4, 1.0) * 255.0);
  1565. v.color.g = (unsigned char) (luax_optnumberclamped01(L, -3, 1.0) * 255.0);
  1566. v.color.b = (unsigned char) (luax_optnumberclamped01(L, -2, 1.0) * 255.0);
  1567. v.color.a = (unsigned char) (luax_optnumberclamped01(L, -1, 1.0) * 255.0);
  1568. lua_pop(L, 9);
  1569. vertices.push_back(v);
  1570. }
  1571. luax_catchexcept(L, [&](){ t = instance()->newMesh(format, vertices.data(), vertices.size() * sizeof(Vertex), drawmode, usage); });
  1572. }
  1573. else
  1574. {
  1575. int count = (int) luaL_checkinteger(L, 1);
  1576. luax_catchexcept(L, [&](){ t = instance()->newMesh(format, count, drawmode, usage); });
  1577. }
  1578. return t;
  1579. }
  1580. static Mesh *newCustomMesh(lua_State *L)
  1581. {
  1582. Mesh *t = nullptr;
  1583. // First argument is the vertex format, second is a table of vertices or
  1584. // the number of vertices.
  1585. std::vector<Buffer::DataDeclaration> vertexformat;
  1586. PrimitiveType drawmode = luax_checkmeshdrawmode(L, 3);
  1587. BufferDataUsage usage = luax_optdatausage(L, 4, BUFFERDATAUSAGE_DYNAMIC);
  1588. lua_rawgeti(L, 1, 1);
  1589. if (!lua_istable(L, -1))
  1590. {
  1591. luaL_argerror(L, 1, "table of tables expected");
  1592. return nullptr;
  1593. }
  1594. lua_pop(L, 1);
  1595. // Per-vertex attribute data formats.
  1596. for (int i = 1; i <= (int) luax_objlen(L, 1); i++)
  1597. {
  1598. lua_rawgeti(L, 1, i);
  1599. Buffer::DataDeclaration decl("", DATAFORMAT_MAX_ENUM);
  1600. lua_getfield(L, -1, "format");
  1601. bool hasformatfield = !lua_isnoneornil(L, -1);
  1602. lua_pop(L, 1);
  1603. if (hasformatfield || luax_objlen(L, -1) == 0)
  1604. decl = luax_checkdatadeclaration(L, 1, i, -1, true);
  1605. else
  1606. {
  1607. // Legacy format arguments: {name, datatype, components}
  1608. for (int j = 1; j <= 3; j++)
  1609. lua_rawgeti(L, -j, j);
  1610. decl.name = luaL_checkstring(L, -3);
  1611. const char* tname = luaL_checkstring(L, -2);
  1612. int components = (int)luaL_checkinteger(L, -1);
  1613. // Check deprecated format names.
  1614. if (strcmp(tname, "byte") == 0 || strcmp(tname, "unorm8") == 0)
  1615. {
  1616. if (components == 4)
  1617. decl.format = DATAFORMAT_UNORM8_VEC4;
  1618. else
  1619. luaL_error(L, "Invalid component count (%d) for vertex data type %s", components, tname);
  1620. }
  1621. else if (strcmp(tname, "unorm16") == 0)
  1622. {
  1623. if (components == 2)
  1624. decl.format = DATAFORMAT_UNORM16_VEC2;
  1625. else if (components == 4)
  1626. decl.format = DATAFORMAT_UNORM16_VEC4;
  1627. else
  1628. luaL_error(L, "Invalid component count (%d) for vertex data type %s", components, tname);
  1629. }
  1630. else if (strcmp(tname, "float") == 0)
  1631. {
  1632. if (components == 1)
  1633. decl.format = DATAFORMAT_FLOAT;
  1634. else if (components == 2)
  1635. decl.format = DATAFORMAT_FLOAT_VEC2;
  1636. else if (components == 3)
  1637. decl.format = DATAFORMAT_FLOAT_VEC3;
  1638. else if (components == 4)
  1639. decl.format = DATAFORMAT_FLOAT_VEC4;
  1640. else
  1641. luaL_error(L, "Invalid component count (%d) for vertex data type %s", components, tname);
  1642. }
  1643. if (decl.format == DATAFORMAT_MAX_ENUM)
  1644. luax_enumerror(L, "vertex data format", getConstants(decl.format), tname);
  1645. lua_pop(L, 3);
  1646. luax_markdeprecated(L, 1, "vertex format array values in love.graphics.newMesh", API_CUSTOM, DEPRECATED_REPLACED, "named table fields 'format' and 'name'");
  1647. }
  1648. lua_pop(L, 1);
  1649. vertexformat.push_back(decl);
  1650. }
  1651. if (lua_isnumber(L, 2))
  1652. {
  1653. int vertexcount = (int) luaL_checkinteger(L, 2);
  1654. luax_catchexcept(L, [&](){ t = instance()->newMesh(vertexformat, vertexcount, drawmode, usage); });
  1655. }
  1656. else if (luax_istype(L, 2, Data::type))
  1657. {
  1658. // Vertex data comes directly from a Data object.
  1659. Data *data = luax_checktype<Data>(L, 2);
  1660. luax_catchexcept(L, [&](){ t = instance()->newMesh(vertexformat, data->getData(), data->getSize(), drawmode, usage); });
  1661. }
  1662. else
  1663. {
  1664. // Table of vertices.
  1665. lua_rawgeti(L, 2, 1);
  1666. if (!lua_istable(L, -1))
  1667. {
  1668. luaL_argerror(L, 2, "expected table of tables");
  1669. return nullptr;
  1670. }
  1671. lua_pop(L, 1);
  1672. size_t numvertices = luax_objlen(L, 2);
  1673. luax_catchexcept(L, [&](){ t = instance()->newMesh(vertexformat, numvertices, drawmode, usage); });
  1674. char *data = (char *) t->getVertexData();
  1675. size_t stride = t->getVertexStride();
  1676. const auto &members = t->getVertexFormat();
  1677. for (size_t vertindex = 0; vertindex < numvertices; vertindex++)
  1678. {
  1679. // get vertices[vertindex]
  1680. lua_rawgeti(L, 2, vertindex + 1);
  1681. luaL_checktype(L, -1, LUA_TTABLE);
  1682. int n = 0;
  1683. for (size_t i = 0; i < vertexformat.size(); i++)
  1684. {
  1685. const auto &member = members[i];
  1686. const auto &info = getDataFormatInfo(member.decl.format);
  1687. // get vertices[vertindex][n]
  1688. for (int c = 0; c < info.components; c++)
  1689. {
  1690. n++;
  1691. lua_rawgeti(L, -(c + 1), n);
  1692. }
  1693. size_t offset = vertindex * stride + member.offset;
  1694. // Fetch the values from Lua and store them in data buffer.
  1695. luax_writebufferdata(L, -info.components, member.decl.format, data + offset);
  1696. lua_pop(L, info.components);
  1697. }
  1698. lua_pop(L, 1); // pop vertices[vertindex]
  1699. }
  1700. t->setVertexDataModified(0, stride * numvertices);
  1701. t->flush();
  1702. }
  1703. return t;
  1704. }
  1705. int w_newMesh(lua_State *L)
  1706. {
  1707. luax_checkgraphicscreated(L);
  1708. // Check first argument: table or number of vertices.
  1709. int arg1type = lua_type(L, 1);
  1710. if (arg1type != LUA_TTABLE && arg1type != LUA_TNUMBER)
  1711. luaL_argerror(L, 1, "table or number expected");
  1712. Mesh *t = nullptr;
  1713. int arg2type = lua_type(L, 2);
  1714. if (arg1type == LUA_TTABLE && (arg2type == LUA_TTABLE || arg2type == LUA_TNUMBER || arg2type == LUA_TUSERDATA))
  1715. t = newCustomMesh(L);
  1716. else
  1717. t = newStandardMesh(L);
  1718. luax_pushtype(L, t);
  1719. t->release();
  1720. return 1;
  1721. }
  1722. int w_newText(lua_State *L)
  1723. {
  1724. luax_checkgraphicscreated(L);
  1725. graphics::Font *font = luax_checkfont(L, 1);
  1726. Text *t = nullptr;
  1727. if (lua_isnoneornil(L, 2))
  1728. luax_catchexcept(L, [&](){ t = instance()->newText(font); });
  1729. else
  1730. {
  1731. std::vector<Font::ColoredString> text;
  1732. luax_checkcoloredstring(L, 2, text);
  1733. luax_catchexcept(L, [&](){ t = instance()->newText(font, text); });
  1734. }
  1735. luax_pushtype(L, t);
  1736. t->release();
  1737. return 1;
  1738. }
  1739. int w_newVideo(lua_State *L)
  1740. {
  1741. luax_checkgraphicscreated(L);
  1742. if (!luax_istype(L, 1, love::video::VideoStream::type))
  1743. luax_convobj(L, 1, "video", "newVideoStream");
  1744. auto stream = luax_checktype<love::video::VideoStream>(L, 1);
  1745. float dpiscale = (float) luaL_optnumber(L, 2, 1.0);
  1746. Video *video = nullptr;
  1747. luax_catchexcept(L, [&]() { video = instance()->newVideo(stream, dpiscale); });
  1748. luax_pushtype(L, video);
  1749. video->release();
  1750. return 1;
  1751. }
  1752. int w_setColor(lua_State *L)
  1753. {
  1754. Colorf c;
  1755. if (lua_istable(L, 1))
  1756. {
  1757. for (int i = 1; i <= 4; i++)
  1758. lua_rawgeti(L, 1, i);
  1759. c.r = (float) luaL_checknumber(L, -4);
  1760. c.g = (float) luaL_checknumber(L, -3);
  1761. c.b = (float) luaL_checknumber(L, -2);
  1762. c.a = (float) luaL_optnumber(L, -1, 1.0);
  1763. lua_pop(L, 4);
  1764. }
  1765. else
  1766. {
  1767. c.r = (float) luaL_checknumber(L, 1);
  1768. c.g = (float) luaL_checknumber(L, 2);
  1769. c.b = (float) luaL_checknumber(L, 3);
  1770. c.a = (float) luaL_optnumber(L, 4, 1.0);
  1771. }
  1772. instance()->setColor(c);
  1773. return 0;
  1774. }
  1775. int w_getColor(lua_State *L)
  1776. {
  1777. Colorf c = instance()->getColor();
  1778. lua_pushnumber(L, c.r);
  1779. lua_pushnumber(L, c.g);
  1780. lua_pushnumber(L, c.b);
  1781. lua_pushnumber(L, c.a);
  1782. return 4;
  1783. }
  1784. int w_setBackgroundColor(lua_State *L)
  1785. {
  1786. Colorf c;
  1787. if (lua_istable(L, 1))
  1788. {
  1789. for (int i = 1; i <= 4; i++)
  1790. lua_rawgeti(L, 1, i);
  1791. c.r = (float) luaL_checknumber(L, -4);
  1792. c.g = (float) luaL_checknumber(L, -3);
  1793. c.b = (float) luaL_checknumber(L, -2);
  1794. c.a = (float) luaL_optnumber(L, -1, 1.0);
  1795. lua_pop(L, 4);
  1796. }
  1797. else
  1798. {
  1799. c.r = (float) luaL_checknumber(L, 1);
  1800. c.g = (float) luaL_checknumber(L, 2);
  1801. c.b = (float) luaL_checknumber(L, 3);
  1802. c.a = (float) luaL_optnumber(L, 4, 1.0);
  1803. }
  1804. instance()->setBackgroundColor(c);
  1805. return 0;
  1806. }
  1807. int w_getBackgroundColor(lua_State *L)
  1808. {
  1809. Colorf c = instance()->getBackgroundColor();
  1810. lua_pushnumber(L, c.r);
  1811. lua_pushnumber(L, c.g);
  1812. lua_pushnumber(L, c.b);
  1813. lua_pushnumber(L, c.a);
  1814. return 4;
  1815. }
  1816. int w_setNewFont(lua_State *L)
  1817. {
  1818. int ret = w_newFont(L);
  1819. Font *font = luax_checktype<Font>(L, -1);
  1820. instance()->setFont(font);
  1821. return ret;
  1822. }
  1823. int w_setFont(lua_State *L)
  1824. {
  1825. Font *font = luax_checktype<Font>(L, 1);
  1826. instance()->setFont(font);
  1827. return 0;
  1828. }
  1829. int w_getFont(lua_State *L)
  1830. {
  1831. Font *f = nullptr;
  1832. luax_catchexcept(L, [&](){ f = instance()->getFont(); });
  1833. luax_pushtype(L, f);
  1834. return 1;
  1835. }
  1836. int w_setColorMask(lua_State *L)
  1837. {
  1838. ColorChannelMask mask;
  1839. if (lua_gettop(L) <= 1)
  1840. {
  1841. // Set all color components if a single argument is given.
  1842. mask.r = mask.g = mask.b = mask.a = luax_checkboolean(L, 1);
  1843. }
  1844. else
  1845. {
  1846. mask.r = luax_checkboolean(L, 1);
  1847. mask.g = luax_checkboolean(L, 2);
  1848. mask.b = luax_checkboolean(L, 3);
  1849. mask.a = luax_checkboolean(L, 4);
  1850. }
  1851. instance()->setColorMask(mask);
  1852. return 0;
  1853. }
  1854. int w_getColorMask(lua_State *L)
  1855. {
  1856. ColorChannelMask mask = instance()->getColorMask();
  1857. luax_pushboolean(L, mask.r);
  1858. luax_pushboolean(L, mask.g);
  1859. luax_pushboolean(L, mask.b);
  1860. luax_pushboolean(L, mask.a);
  1861. return 4;
  1862. }
  1863. int w_setBlendMode(lua_State *L)
  1864. {
  1865. BlendMode mode;
  1866. const char *str = luaL_checkstring(L, 1);
  1867. if (!getConstant(str, mode))
  1868. return luax_enumerror(L, "blend mode", getConstants(mode), str);
  1869. BlendAlpha alphamode = BLENDALPHA_MULTIPLY;
  1870. if (!lua_isnoneornil(L, 2))
  1871. {
  1872. const char *alphastr = luaL_checkstring(L, 2);
  1873. if (!getConstant(alphastr, alphamode))
  1874. return luax_enumerror(L, "blend alpha mode", getConstants(alphamode), alphastr);
  1875. }
  1876. luax_catchexcept(L, [&](){ instance()->setBlendMode(mode, alphamode); });
  1877. return 0;
  1878. }
  1879. int w_getBlendMode(lua_State *L)
  1880. {
  1881. const char *str;
  1882. const char *alphastr;
  1883. BlendAlpha alphamode;
  1884. BlendMode mode = instance()->getBlendMode(alphamode);
  1885. if (!getConstant(mode, str))
  1886. return luaL_error(L, "Unknown blend mode");
  1887. if (!getConstant(alphamode, alphastr))
  1888. return luaL_error(L, "Unknown blend alpha mode");
  1889. lua_pushstring(L, str);
  1890. lua_pushstring(L, alphastr);
  1891. return 2;
  1892. }
  1893. static BlendOperation luax_checkblendop(lua_State *L, int idx)
  1894. {
  1895. BlendOperation op = BLENDOP_ADD;
  1896. const char *str = luaL_checkstring(L, idx);
  1897. if (!getConstant(str, op))
  1898. luax_enumerror(L, "blend operation", getConstants(op), str);
  1899. return op;
  1900. }
  1901. static BlendFactor luax_checkblendfactor(lua_State *L, int idx)
  1902. {
  1903. BlendFactor factor = BLENDFACTOR_ZERO;
  1904. const char *str = luaL_checkstring(L, idx);
  1905. if (!getConstant(str, factor))
  1906. luax_enumerror(L, "blend factor", getConstants(factor), str);
  1907. return factor;
  1908. }
  1909. static void luax_pushblendop(lua_State *L, BlendOperation op)
  1910. {
  1911. const char *str;
  1912. if (!getConstant(op, str))
  1913. luaL_error(L, "unknown blend operation");
  1914. lua_pushstring(L, str);
  1915. }
  1916. static void luax_pushblendfactor(lua_State *L, BlendFactor factor)
  1917. {
  1918. const char *str;
  1919. if (!getConstant(factor, str))
  1920. luaL_error(L, "unknown blend factor");
  1921. lua_pushstring(L, str);
  1922. }
  1923. int w_setBlendState(lua_State *L)
  1924. {
  1925. BlendState state;
  1926. if (!lua_isnoneornil(L, 1))
  1927. {
  1928. state.enable = true;
  1929. if (lua_gettop(L) >= 4)
  1930. {
  1931. state.operationRGB = luax_checkblendop(L, 1);
  1932. state.operationA = luax_checkblendop(L, 2);
  1933. state.srcFactorRGB = luax_checkblendfactor(L, 3);
  1934. state.srcFactorA = luax_checkblendfactor(L, 4);
  1935. state.dstFactorRGB = luax_checkblendfactor(L, 5);
  1936. state.dstFactorA = luax_checkblendfactor(L, 6);
  1937. }
  1938. else
  1939. {
  1940. state.operationRGB = state.operationA = luax_checkblendop(L, 1);
  1941. state.srcFactorRGB = state.srcFactorA = luax_checkblendfactor(L, 2);
  1942. state.dstFactorRGB = state.dstFactorA = luax_checkblendfactor(L, 3);
  1943. }
  1944. }
  1945. luax_catchexcept(L, [&](){ instance()->setBlendState(state); });
  1946. return 0;
  1947. }
  1948. int w_getBlendState(lua_State *L)
  1949. {
  1950. const BlendState &state = instance()->getBlendState();
  1951. if (state.enable)
  1952. {
  1953. luax_pushblendop(L, state.operationRGB);
  1954. luax_pushblendop(L, state.operationA);
  1955. luax_pushblendfactor(L, state.srcFactorRGB);
  1956. luax_pushblendfactor(L, state.srcFactorA);
  1957. luax_pushblendfactor(L, state.dstFactorRGB);
  1958. luax_pushblendfactor(L, state.dstFactorA);
  1959. }
  1960. else
  1961. {
  1962. for (int i = 0; i < 6; i++)
  1963. lua_pushnil(L);
  1964. }
  1965. return 6;
  1966. }
  1967. int w_setDefaultFilter(lua_State *L)
  1968. {
  1969. SamplerState s = instance()->getDefaultSamplerState();
  1970. const char *minstr = luaL_checkstring(L, 1);
  1971. const char *magstr = luaL_optstring(L, 2, minstr);
  1972. if (!SamplerState::getConstant(minstr, s.minFilter))
  1973. return luax_enumerror(L, "filter mode", SamplerState::getConstants(s.minFilter), minstr);
  1974. if (!SamplerState::getConstant(magstr, s.magFilter))
  1975. return luax_enumerror(L, "filter mode", SamplerState::getConstants(s.magFilter), magstr);
  1976. s.maxAnisotropy = std::min(std::max(1, (int) luaL_optnumber(L, 3, 1.0)), LOVE_UINT8_MAX);
  1977. instance()->setDefaultSamplerState(s);
  1978. return 0;
  1979. }
  1980. int w_getDefaultFilter(lua_State *L)
  1981. {
  1982. const SamplerState &s = instance()->getDefaultSamplerState();
  1983. const char *minstr;
  1984. const char *magstr;
  1985. if (!SamplerState::getConstant(s.minFilter, minstr))
  1986. return luaL_error(L, "Unknown minification filter mode");
  1987. if (!SamplerState::getConstant(s.magFilter, magstr))
  1988. return luaL_error(L, "Unknown magnification filter mode");
  1989. lua_pushstring(L, minstr);
  1990. lua_pushstring(L, magstr);
  1991. lua_pushnumber(L, s.maxAnisotropy);
  1992. return 3;
  1993. }
  1994. int w_setDefaultMipmapFilter(lua_State *L)
  1995. {
  1996. SamplerState s = instance()->getDefaultSamplerState();
  1997. s.mipmapFilter = SamplerState::MIPMAP_FILTER_NONE;
  1998. if (!lua_isnoneornil(L, 1))
  1999. {
  2000. const char *str = luaL_checkstring(L, 1);
  2001. if (!SamplerState::getConstant(str, s.mipmapFilter))
  2002. return luax_enumerror(L, "filter mode", SamplerState::getConstants(s.mipmapFilter), str);
  2003. }
  2004. s.lodBias = -((float) luaL_optnumber(L, 2, 0.0));
  2005. instance()->setDefaultSamplerState(s);
  2006. return 0;
  2007. }
  2008. int w_getDefaultMipmapFilter(lua_State *L)
  2009. {
  2010. const SamplerState &s = instance()->getDefaultSamplerState();
  2011. const char *str;
  2012. if (SamplerState::getConstant(s.mipmapFilter, str))
  2013. lua_pushstring(L, str);
  2014. else
  2015. lua_pushnil(L);
  2016. lua_pushnumber(L, -s.lodBias);
  2017. return 2;
  2018. }
  2019. int w_setLineWidth(lua_State *L)
  2020. {
  2021. float width = (float)luaL_checknumber(L, 1);
  2022. instance()->setLineWidth(width);
  2023. return 0;
  2024. }
  2025. int w_setLineStyle(lua_State *L)
  2026. {
  2027. Graphics::LineStyle style;
  2028. const char *str = luaL_checkstring(L, 1);
  2029. if (!Graphics::getConstant(str, style))
  2030. return luax_enumerror(L, "line style", Graphics::getConstants(style), str);
  2031. instance()->setLineStyle(style);
  2032. return 0;
  2033. }
  2034. int w_setLineJoin(lua_State *L)
  2035. {
  2036. Graphics::LineJoin join;
  2037. const char *str = luaL_checkstring(L, 1);
  2038. if (!Graphics::getConstant(str, join))
  2039. return luax_enumerror(L, "line join", Graphics::getConstants(join), str);
  2040. instance()->setLineJoin(join);
  2041. return 0;
  2042. }
  2043. int w_getLineWidth(lua_State *L)
  2044. {
  2045. lua_pushnumber(L, instance()->getLineWidth());
  2046. return 1;
  2047. }
  2048. int w_getLineStyle(lua_State *L)
  2049. {
  2050. Graphics::LineStyle style = instance()->getLineStyle();
  2051. const char *str;
  2052. if (!Graphics::getConstant(style, str))
  2053. return luaL_error(L, "Unknown line style");
  2054. lua_pushstring(L, str);
  2055. return 1;
  2056. }
  2057. int w_getLineJoin(lua_State *L)
  2058. {
  2059. Graphics::LineJoin join = instance()->getLineJoin();
  2060. const char *str;
  2061. if (!Graphics::getConstant(join, str))
  2062. return luaL_error(L, "Unknown line join");
  2063. lua_pushstring(L, str);
  2064. return 1;
  2065. }
  2066. int w_setPointSize(lua_State *L)
  2067. {
  2068. float size = (float)luaL_checknumber(L, 1);
  2069. instance()->setPointSize(size);
  2070. return 0;
  2071. }
  2072. int w_getPointSize(lua_State *L)
  2073. {
  2074. lua_pushnumber(L, instance()->getPointSize());
  2075. return 1;
  2076. }
  2077. int w_setDepthMode(lua_State *L)
  2078. {
  2079. if (lua_isnoneornil(L, 1) && lua_isnoneornil(L, 2))
  2080. luax_catchexcept(L, [&]() { instance()->setDepthMode(); });
  2081. else
  2082. {
  2083. CompareMode compare = COMPARE_ALWAYS;
  2084. const char *str = luaL_checkstring(L, 1);
  2085. bool write = luax_checkboolean(L, 2);
  2086. if (!getConstant(str, compare))
  2087. return luax_enumerror(L, "compare mode", getConstants(compare), str);
  2088. luax_catchexcept(L, [&]() { instance()->setDepthMode(compare, write); });
  2089. }
  2090. return 0;
  2091. }
  2092. int w_getDepthMode(lua_State *L)
  2093. {
  2094. CompareMode compare = COMPARE_ALWAYS;
  2095. bool write = false;
  2096. instance()->getDepthMode(compare, write);
  2097. const char *str;
  2098. if (!getConstant(compare, str))
  2099. return luaL_error(L, "Unknown compare mode");
  2100. lua_pushstring(L, str);
  2101. luax_pushboolean(L, write);
  2102. return 2;
  2103. }
  2104. int w_setMeshCullMode(lua_State *L)
  2105. {
  2106. const char *str = luaL_checkstring(L, 1);
  2107. CullMode mode;
  2108. if (!getConstant(str, mode))
  2109. return luax_enumerror(L, "cull mode", getConstants(mode), str);
  2110. luax_catchexcept(L, [&]() { instance()->setMeshCullMode(mode); });
  2111. return 0;
  2112. }
  2113. int w_getMeshCullMode(lua_State *L)
  2114. {
  2115. CullMode mode = instance()->getMeshCullMode();
  2116. const char *str;
  2117. if (!getConstant(mode, str))
  2118. return luaL_error(L, "Unknown cull mode");
  2119. lua_pushstring(L, str);
  2120. return 1;
  2121. }
  2122. int w_setFrontFaceWinding(lua_State *L)
  2123. {
  2124. const char *str = luaL_checkstring(L, 1);
  2125. Winding winding;
  2126. if (!getConstant(str, winding))
  2127. return luax_enumerror(L, "vertex winding", getConstants(winding), str);
  2128. luax_catchexcept(L, [&]() { instance()->setFrontFaceWinding(winding); });
  2129. return 0;
  2130. }
  2131. int w_getFrontFaceWinding(lua_State *L)
  2132. {
  2133. Winding winding = instance()->getFrontFaceWinding();
  2134. const char *str;
  2135. if (!getConstant(winding, str))
  2136. return luaL_error(L, "Unknown vertex winding");
  2137. lua_pushstring(L, str);
  2138. return 1;
  2139. }
  2140. int w_setWireframe(lua_State *L)
  2141. {
  2142. instance()->setWireframe(luax_checkboolean(L, 1));
  2143. return 0;
  2144. }
  2145. int w_isWireframe(lua_State *L)
  2146. {
  2147. luax_pushboolean(L, instance()->isWireframe());
  2148. return 1;
  2149. }
  2150. int w_setShader(lua_State *L)
  2151. {
  2152. if (lua_isnoneornil(L,1))
  2153. {
  2154. instance()->setShader();
  2155. return 0;
  2156. }
  2157. Shader *shader = luax_checkshader(L, 1);
  2158. instance()->setShader(shader);
  2159. return 0;
  2160. }
  2161. int w_getShader(lua_State *L)
  2162. {
  2163. Shader *shader = instance()->getShader();
  2164. if (shader)
  2165. luax_pushtype(L, shader);
  2166. else
  2167. lua_pushnil(L);
  2168. return 1;
  2169. }
  2170. int w_getSupported(lua_State *L)
  2171. {
  2172. const Graphics::Capabilities &caps = instance()->getCapabilities();
  2173. if (lua_istable(L, 1))
  2174. lua_pushvalue(L, 1);
  2175. else
  2176. lua_createtable(L, 0, (int) Graphics::FEATURE_MAX_ENUM);
  2177. for (int i = 0; i < (int) Graphics::FEATURE_MAX_ENUM; i++)
  2178. {
  2179. auto feature = (Graphics::Feature) i;
  2180. const char *name = nullptr;
  2181. if (!Graphics::getConstant(feature, name))
  2182. continue;
  2183. luax_pushboolean(L, caps.features[i]);
  2184. lua_setfield(L, -2, name);
  2185. }
  2186. return 1;
  2187. }
  2188. int w_getTextureFormats(lua_State *L)
  2189. {
  2190. luaL_checktype(L, 1, LUA_TTABLE);
  2191. bool rt = luax_checkboolflag(L, 1, Texture::getConstant(Texture::SETTING_RENDER_TARGET));
  2192. bool linear = luax_boolflag(L, 1, Texture::getConstant(Texture::SETTING_LINEAR), false);
  2193. bool computewrite = luax_boolflag(L, 1, Texture::getConstant(Texture::SETTING_COMPUTE_WRITE), false);
  2194. OptionalBool readable;
  2195. lua_getfield(L, 1, Texture::getConstant(Texture::SETTING_READABLE));
  2196. if (!lua_isnoneornil(L, -1))
  2197. readable.set(luax_checkboolean(L, -1));
  2198. lua_pop(L, 1);
  2199. if (lua_istable(L, 2))
  2200. lua_pushvalue(L, 2);
  2201. else
  2202. lua_createtable(L, 0, (int) PIXELFORMAT_MAX_ENUM);
  2203. for (int i = 0; i < (int) PIXELFORMAT_MAX_ENUM; i++)
  2204. {
  2205. PixelFormat format = (PixelFormat) i;
  2206. const char *name = nullptr;
  2207. if (format == PIXELFORMAT_UNKNOWN || !love::getConstant(format, name))
  2208. continue;
  2209. if (rt && isPixelFormatDepth(format))
  2210. continue;
  2211. bool sRGB = isGammaCorrect() && !linear;
  2212. uint32 usage = PIXELFORMATUSAGEFLAGS_NONE;
  2213. if (rt)
  2214. usage |= PIXELFORMATUSAGEFLAGS_RENDERTARGET;
  2215. if (readable.get(!isPixelFormatDepthStencil(format)))
  2216. usage |= PIXELFORMATUSAGEFLAGS_SAMPLE;
  2217. if (computewrite)
  2218. usage |= PIXELFORMATUSAGEFLAGS_COMPUTEWRITE;
  2219. luax_pushboolean(L, instance()->isPixelFormatSupported(format, (PixelFormatUsageFlags) usage, sRGB));
  2220. lua_setfield(L, -2, name);
  2221. }
  2222. return 1;
  2223. }
  2224. static int w__getFormats(lua_State *L, int idx, bool (*isFormatSupported)(PixelFormat), bool (*ignore)(PixelFormat))
  2225. {
  2226. if (lua_istable(L, idx))
  2227. lua_pushvalue(L, idx);
  2228. else
  2229. lua_createtable(L, 0, (int) PIXELFORMAT_MAX_ENUM);
  2230. for (int i = 0; i < (int) PIXELFORMAT_MAX_ENUM; i++)
  2231. {
  2232. PixelFormat format = (PixelFormat) i;
  2233. const char *name = nullptr;
  2234. if (format == PIXELFORMAT_UNKNOWN || !love::getConstant(format, name) || ignore(format))
  2235. continue;
  2236. luax_pushboolean(L, isFormatSupported(format));
  2237. lua_setfield(L, -2, name);
  2238. }
  2239. return 1;
  2240. }
  2241. int w_getCanvasFormats(lua_State *L)
  2242. {
  2243. luax_markdeprecated(L, 1, "love.graphics.getCanvasFormats", API_FUNCTION, DEPRECATED_REPLACED, "love.graphics.getTextureFormats");
  2244. bool (*supported)(PixelFormat);
  2245. int idx = 1;
  2246. if (lua_type(L, 1) == LUA_TBOOLEAN)
  2247. {
  2248. idx = 2;
  2249. if (luax_checkboolean(L, 1))
  2250. {
  2251. supported = [](PixelFormat format) -> bool
  2252. {
  2253. const uint32 usage = PIXELFORMATUSAGEFLAGS_SAMPLE | PIXELFORMATUSAGEFLAGS_RENDERTARGET;
  2254. return instance()->isPixelFormatSupported(format, (PixelFormatUsageFlags) usage, false);
  2255. };
  2256. }
  2257. else
  2258. {
  2259. supported = [](PixelFormat format) -> bool
  2260. {
  2261. return instance()->isPixelFormatSupported(format, PIXELFORMATUSAGEFLAGS_RENDERTARGET, false);
  2262. };
  2263. }
  2264. }
  2265. else
  2266. {
  2267. supported = [](PixelFormat format) -> bool
  2268. {
  2269. bool readable = !isPixelFormatDepthStencil(format);
  2270. uint32 usage = PIXELFORMATUSAGEFLAGS_RENDERTARGET;
  2271. if (readable)
  2272. usage |= PIXELFORMATUSAGEFLAGS_SAMPLE;
  2273. return instance()->isPixelFormatSupported(format, (PixelFormatUsageFlags) usage, false);
  2274. };
  2275. }
  2276. return w__getFormats(L, idx, supported, isPixelFormatCompressed);
  2277. }
  2278. int w_getImageFormats(lua_State *L)
  2279. {
  2280. luax_markdeprecated(L, 1, "love.graphics.getImageFormats", API_FUNCTION, DEPRECATED_REPLACED, "love.graphics.getTextureFormats");
  2281. const auto supported = [](PixelFormat format) -> bool
  2282. {
  2283. return instance()->isPixelFormatSupported(format, PIXELFORMATUSAGEFLAGS_SAMPLE, false);
  2284. };
  2285. const auto ignore = [](PixelFormat format) -> bool
  2286. {
  2287. return !(image::ImageData::validPixelFormat(format) || isPixelFormatCompressed(format));
  2288. };
  2289. return w__getFormats(L, 1, supported, ignore);
  2290. }
  2291. int w_getTextureTypes(lua_State *L)
  2292. {
  2293. const Graphics::Capabilities &caps = instance()->getCapabilities();
  2294. if (lua_istable(L, 1))
  2295. lua_pushvalue(L, 1);
  2296. else
  2297. lua_createtable(L, 0, (int) TEXTURE_MAX_ENUM);
  2298. for (int i = 0; i < (int) TEXTURE_MAX_ENUM; i++)
  2299. {
  2300. TextureType textype = (TextureType) i;
  2301. const char *name = nullptr;
  2302. if (!Texture::getConstant(textype, name))
  2303. continue;
  2304. luax_pushboolean(L, caps.textureTypes[i]);
  2305. lua_setfield(L, -2, name);
  2306. }
  2307. return 1;
  2308. }
  2309. int w_getRendererInfo(lua_State *L)
  2310. {
  2311. Graphics::RendererInfo info;
  2312. luax_catchexcept(L, [&](){ info = instance()->getRendererInfo(); });
  2313. luax_pushstring(L, info.name);
  2314. luax_pushstring(L, info.version);
  2315. luax_pushstring(L, info.vendor);
  2316. luax_pushstring(L, info.device);
  2317. return 4;
  2318. }
  2319. int w_getSystemLimits(lua_State *L)
  2320. {
  2321. const Graphics::Capabilities &caps = instance()->getCapabilities();
  2322. if (lua_istable(L, 1))
  2323. lua_pushvalue(L, 1);
  2324. else
  2325. lua_createtable(L, 0, (int) Graphics::LIMIT_MAX_ENUM);
  2326. for (int i = 0; i < (int) Graphics::LIMIT_MAX_ENUM; i++)
  2327. {
  2328. Graphics::SystemLimit limittype = (Graphics::SystemLimit) i;
  2329. const char *name = nullptr;
  2330. if (!Graphics::getConstant(limittype, name))
  2331. continue;
  2332. lua_pushnumber(L, caps.limits[i]);
  2333. lua_setfield(L, -2, name);
  2334. }
  2335. return 1;
  2336. }
  2337. int w_getStats(lua_State *L)
  2338. {
  2339. Graphics::Stats stats = instance()->getStats();
  2340. if (lua_istable(L, 1))
  2341. lua_pushvalue(L, 1);
  2342. else
  2343. lua_createtable(L, 0, 7);
  2344. lua_pushinteger(L, stats.drawCalls);
  2345. lua_setfield(L, -2, "drawcalls");
  2346. lua_pushinteger(L, stats.drawCallsBatched);
  2347. lua_setfield(L, -2, "drawcallsbatched");
  2348. lua_pushinteger(L, stats.renderTargetSwitches);
  2349. lua_setfield(L, -2, "canvasswitches");
  2350. lua_pushinteger(L, stats.shaderSwitches);
  2351. lua_setfield(L, -2, "shaderswitches");
  2352. lua_pushinteger(L, stats.textures);
  2353. lua_setfield(L, -2, "textures");
  2354. lua_pushinteger(L, stats.fonts);
  2355. lua_setfield(L, -2, "fonts");
  2356. lua_pushinteger(L, stats.textureMemory);
  2357. lua_setfield(L, -2, "texturememory");
  2358. return 1;
  2359. }
  2360. int w_draw(lua_State *L)
  2361. {
  2362. Drawable *drawable = nullptr;
  2363. Texture *texture = nullptr;
  2364. Quad *quad = nullptr;
  2365. int startidx = 2;
  2366. if (luax_istype(L, 2, Quad::type))
  2367. {
  2368. texture = luax_checktexture(L, 1);
  2369. quad = luax_totype<Quad>(L, 2);
  2370. startidx = 3;
  2371. }
  2372. else if (lua_isnil(L, 2) && !lua_isnoneornil(L, 3))
  2373. {
  2374. return luax_typerror(L, 2, "Quad");
  2375. }
  2376. else
  2377. {
  2378. drawable = luax_checktype<Drawable>(L, 1);
  2379. startidx = 2;
  2380. }
  2381. luax_checkstandardtransform(L, startidx, [&](const Matrix4 &m)
  2382. {
  2383. luax_catchexcept(L, [&]()
  2384. {
  2385. if (texture && quad)
  2386. instance()->draw(texture, quad, m);
  2387. else
  2388. instance()->draw(drawable, m);
  2389. });
  2390. });
  2391. return 0;
  2392. }
  2393. int w_drawLayer(lua_State *L)
  2394. {
  2395. Texture *texture = luax_checktexture(L, 1);
  2396. Quad *quad = nullptr;
  2397. int layer = (int) luaL_checkinteger(L, 2) - 1;
  2398. int startidx = 3;
  2399. if (luax_istype(L, startidx, Quad::type))
  2400. {
  2401. texture = luax_checktexture(L, 1);
  2402. quad = luax_totype<Quad>(L, startidx);
  2403. startidx++;
  2404. }
  2405. else if (lua_isnil(L, startidx) && !lua_isnoneornil(L, startidx + 1))
  2406. {
  2407. return luax_typerror(L, startidx, "Quad");
  2408. }
  2409. luax_checkstandardtransform(L, startidx, [&](const Matrix4 &m)
  2410. {
  2411. luax_catchexcept(L, [&]()
  2412. {
  2413. if (quad)
  2414. instance()->drawLayer(texture, layer, quad, m);
  2415. else
  2416. instance()->drawLayer(texture, layer, m);
  2417. });
  2418. });
  2419. return 0;
  2420. }
  2421. int w_drawInstanced(lua_State *L)
  2422. {
  2423. Mesh *t = luax_checkmesh(L, 1);
  2424. int instancecount = (int) luaL_checkinteger(L, 2);
  2425. luax_checkstandardtransform(L, 3, [&](const Matrix4 &m)
  2426. {
  2427. luax_catchexcept(L, [&]() { instance()->drawInstanced(t, m, instancecount); });
  2428. });
  2429. return 0;
  2430. }
  2431. int w_drawShaderVertices(lua_State *L)
  2432. {
  2433. if (luax_istype(L, 1, Buffer::type))
  2434. {
  2435. // Indexed drawing.
  2436. Buffer *t = luax_checkbuffer(L, 1);
  2437. int indexcount = (int) luaL_checkinteger(L, 2);
  2438. int instancecount = (int) luaL_optinteger(L, 3, 1);
  2439. int indexstart = (int) luaL_optinteger(L, 4, 1) - 1;
  2440. Texture *tex = nullptr;
  2441. if (!lua_isnoneornil(L, 5))
  2442. tex = luax_checktexture(L, 5);
  2443. luax_catchexcept(L, [&]() { instance()->drawShaderVertices(t, indexcount, instancecount, indexstart, tex); });
  2444. }
  2445. else
  2446. {
  2447. const char *primstr = luaL_checkstring(L, 1);
  2448. PrimitiveType primtype = PRIMITIVE_TRIANGLES;
  2449. if (!getConstant(primstr, primtype))
  2450. return luax_enumerror(L, "primitive type", getConstants(primtype), primstr);
  2451. int vertexcount = (int) luaL_checkinteger(L, 2);
  2452. int instancecount = (int) luaL_optinteger(L, 3, 1);
  2453. Texture *tex = nullptr;
  2454. if (!lua_isnoneornil(L, 4))
  2455. tex = luax_checktexture(L, 4);
  2456. luax_catchexcept(L, [&]() { instance()->drawShaderVertices(primtype, vertexcount, instancecount, tex); });
  2457. }
  2458. return 0;
  2459. }
  2460. int w_print(lua_State *L)
  2461. {
  2462. std::vector<Font::ColoredString> str;
  2463. luax_checkcoloredstring(L, 1, str);
  2464. if (luax_istype(L, 2, Font::type))
  2465. {
  2466. Font *font = luax_checkfont(L, 2);
  2467. luax_checkstandardtransform(L, 3, [&](const Matrix4 &m)
  2468. {
  2469. luax_catchexcept(L, [&](){ instance()->print(str, font, m); });
  2470. });
  2471. }
  2472. else
  2473. {
  2474. luax_checkstandardtransform(L, 2, [&](const Matrix4 &m)
  2475. {
  2476. luax_catchexcept(L, [&](){ instance()->print(str, m); });
  2477. });
  2478. }
  2479. return 0;
  2480. }
  2481. int w_printf(lua_State *L)
  2482. {
  2483. std::vector<Font::ColoredString> str;
  2484. luax_checkcoloredstring(L, 1, str);
  2485. Font *font = nullptr;
  2486. int startidx = 2;
  2487. if (luax_istype(L, startidx, Font::type))
  2488. {
  2489. font = luax_checkfont(L, startidx);
  2490. startidx++;
  2491. }
  2492. Font::AlignMode align = Font::ALIGN_LEFT;
  2493. Matrix4 m;
  2494. int formatidx = startidx + 2;
  2495. if (luax_istype(L, startidx, math::Transform::type))
  2496. {
  2497. math::Transform *tf = luax_totype<math::Transform>(L, startidx);
  2498. m = tf->getMatrix();
  2499. formatidx = startidx + 1;
  2500. }
  2501. else
  2502. {
  2503. float x = (float)luaL_checknumber(L, startidx + 0);
  2504. float y = (float)luaL_checknumber(L, startidx + 1);
  2505. float angle = (float) luaL_optnumber(L, startidx + 4, 0.0f);
  2506. float sx = (float) luaL_optnumber(L, startidx + 5, 1.0f);
  2507. float sy = (float) luaL_optnumber(L, startidx + 6, sx);
  2508. float ox = (float) luaL_optnumber(L, startidx + 7, 0.0f);
  2509. float oy = (float) luaL_optnumber(L, startidx + 8, 0.0f);
  2510. float kx = (float) luaL_optnumber(L, startidx + 9, 0.0f);
  2511. float ky = (float) luaL_optnumber(L, startidx + 10, 0.0f);
  2512. m = Matrix4(x, y, angle, sx, sy, ox, oy, kx, ky);
  2513. }
  2514. float wrap = (float)luaL_checknumber(L, formatidx);
  2515. const char *astr = lua_isnoneornil(L, formatidx + 1) ? nullptr : luaL_checkstring(L, formatidx + 1);
  2516. if (astr != nullptr && !Font::getConstant(astr, align))
  2517. return luax_enumerror(L, "alignment", Font::getConstants(align), astr);
  2518. if (font != nullptr)
  2519. luax_catchexcept(L, [&](){ instance()->printf(str, font, wrap, align, m); });
  2520. else
  2521. luax_catchexcept(L, [&](){ instance()->printf(str, wrap, align, m); });
  2522. return 0;
  2523. }
  2524. int w_points(lua_State *L)
  2525. {
  2526. // love.graphics.points has 3 variants:
  2527. // - points(x1, y1, x2, y2, ...)
  2528. // - points({x1, y1, x2, y2, ...})
  2529. // - points({{x1, y1 [, r, g, b, a]}, {x2, y2 [, r, g, b, a]}, ...})
  2530. int args = lua_gettop(L);
  2531. bool is_table = false;
  2532. bool is_table_of_tables = false;
  2533. if (args == 1 && lua_istable(L, 1))
  2534. {
  2535. is_table = true;
  2536. args = (int) luax_objlen(L, 1);
  2537. lua_rawgeti(L, 1, 1);
  2538. is_table_of_tables = lua_istable(L, -1);
  2539. lua_pop(L, 1);
  2540. }
  2541. if (args % 2 != 0 && !is_table_of_tables)
  2542. return luaL_error(L, "Number of vertex components must be a multiple of two");
  2543. int numpositions = args / 2;
  2544. if (is_table_of_tables)
  2545. numpositions = args;
  2546. Vector2 *positions = nullptr;
  2547. Colorf *colors = nullptr;
  2548. if (is_table_of_tables)
  2549. {
  2550. size_t datasize = (sizeof(Vector2) + sizeof(Colorf)) * numpositions;
  2551. uint8 *data = instance()->getScratchBuffer<uint8>(datasize);
  2552. positions = (Vector2 *) data;
  2553. colors = (Colorf *) (data + sizeof(Vector2) * numpositions);
  2554. }
  2555. else
  2556. positions = instance()->getScratchBuffer<Vector2>(numpositions);
  2557. if (is_table)
  2558. {
  2559. if (is_table_of_tables)
  2560. {
  2561. // points({{x1, y1 [, r, g, b, a]}, {x2, y2 [, r, g, b, a]}, ...})
  2562. for (int i = 0; i < args; i++)
  2563. {
  2564. lua_rawgeti(L, 1, i + 1);
  2565. for (int j = 1; j <= 6; j++)
  2566. lua_rawgeti(L, -j, j);
  2567. positions[i].x = luax_checkfloat(L, -6);
  2568. positions[i].y = luax_checkfloat(L, -5);
  2569. colors[i].r = (float) luax_optnumberclamped01(L, -4, 1.0);
  2570. colors[i].g = (float) luax_optnumberclamped01(L, -3, 1.0);
  2571. colors[i].b = (float) luax_optnumberclamped01(L, -2, 1.0);
  2572. colors[i].a = (float) luax_optnumberclamped01(L, -1, 1.0);
  2573. lua_pop(L, 7);
  2574. }
  2575. }
  2576. else
  2577. {
  2578. // points({x1, y1, x2, y2, ...})
  2579. for (int i = 0; i < numpositions; i++)
  2580. {
  2581. lua_rawgeti(L, 1, i * 2 + 1);
  2582. lua_rawgeti(L, 1, i * 2 + 2);
  2583. positions[i].x = luax_checkfloat(L, -2);
  2584. positions[i].y = luax_checkfloat(L, -1);
  2585. lua_pop(L, 2);
  2586. }
  2587. }
  2588. }
  2589. else
  2590. {
  2591. for (int i = 0; i < numpositions; i++)
  2592. {
  2593. positions[i].x = luax_checkfloat(L, i * 2 + 1);
  2594. positions[i].y = luax_checkfloat(L, i * 2 + 2);
  2595. }
  2596. }
  2597. luax_catchexcept(L, [&](){ instance()->points(positions, colors, numpositions); });
  2598. return 0;
  2599. }
  2600. int w_line(lua_State *L)
  2601. {
  2602. int args = lua_gettop(L);
  2603. int arg1type = lua_type(L, 1);
  2604. bool is_table = false;
  2605. if (args == 1 && arg1type == LUA_TTABLE)
  2606. {
  2607. args = (int) luax_objlen(L, 1);
  2608. is_table = true;
  2609. }
  2610. if (arg1type != LUA_TTABLE && arg1type != LUA_TNUMBER)
  2611. return luax_typerror(L, 1, "table or number");
  2612. else if (args % 2 != 0)
  2613. return luaL_error(L, "Number of vertex components must be a multiple of two.");
  2614. else if (args < 4)
  2615. return luaL_error(L, "Need at least two vertices to draw a line.");
  2616. int numvertices = args / 2;
  2617. Vector2 *coords = instance()->getScratchBuffer<Vector2>(numvertices);
  2618. if (is_table)
  2619. {
  2620. for (int i = 0; i < numvertices; ++i)
  2621. {
  2622. lua_rawgeti(L, 1, (i * 2) + 1);
  2623. lua_rawgeti(L, 1, (i * 2) + 2);
  2624. coords[i].x = luax_checkfloat(L, -2);
  2625. coords[i].y = luax_checkfloat(L, -1);
  2626. lua_pop(L, 2);
  2627. }
  2628. }
  2629. else
  2630. {
  2631. for (int i = 0; i < numvertices; ++i)
  2632. {
  2633. coords[i].x = luax_checkfloat(L, (i * 2) + 1);
  2634. coords[i].y = luax_checkfloat(L, (i * 2) + 2);
  2635. }
  2636. }
  2637. luax_catchexcept(L,
  2638. [&](){ instance()->polyline(coords, numvertices); }
  2639. );
  2640. return 0;
  2641. }
  2642. int w_rectangle(lua_State *L)
  2643. {
  2644. Graphics::DrawMode mode;
  2645. const char *str = luaL_checkstring(L, 1);
  2646. if (!Graphics::getConstant(str, mode))
  2647. return luax_enumerror(L, "draw mode", Graphics::getConstants(mode), str);
  2648. float x = (float)luaL_checknumber(L, 2);
  2649. float y = (float)luaL_checknumber(L, 3);
  2650. float w = (float)luaL_checknumber(L, 4);
  2651. float h = (float)luaL_checknumber(L, 5);
  2652. if (lua_isnoneornil(L, 6))
  2653. {
  2654. luax_catchexcept(L, [&](){ instance()->rectangle(mode, x, y, w, h); });
  2655. return 0;
  2656. }
  2657. float rx = (float)luaL_optnumber(L, 6, 0.0);
  2658. float ry = (float)luaL_optnumber(L, 7, rx);
  2659. if (lua_isnoneornil(L, 8))
  2660. luax_catchexcept(L, [&](){ instance()->rectangle(mode, x, y, w, h, rx, ry); });
  2661. else
  2662. {
  2663. int points = (int) luaL_checkinteger(L, 8);
  2664. luax_catchexcept(L, [&](){ instance()->rectangle(mode, x, y, w, h, rx, ry, points); });
  2665. }
  2666. return 0;
  2667. }
  2668. int w_circle(lua_State *L)
  2669. {
  2670. Graphics::DrawMode mode;
  2671. const char *str = luaL_checkstring(L, 1);
  2672. if (!Graphics::getConstant(str, mode))
  2673. return luax_enumerror(L, "draw mode", Graphics::getConstants(mode), str);
  2674. float x = (float)luaL_checknumber(L, 2);
  2675. float y = (float)luaL_checknumber(L, 3);
  2676. float radius = (float)luaL_checknumber(L, 4);
  2677. if (lua_isnoneornil(L, 5))
  2678. luax_catchexcept(L, [&](){ instance()->circle(mode, x, y, radius); });
  2679. else
  2680. {
  2681. int points = (int) luaL_checkinteger(L, 5);
  2682. luax_catchexcept(L, [&](){ instance()->circle(mode, x, y, radius, points); });
  2683. }
  2684. return 0;
  2685. }
  2686. int w_ellipse(lua_State *L)
  2687. {
  2688. Graphics::DrawMode mode;
  2689. const char *str = luaL_checkstring(L, 1);
  2690. if (!Graphics::getConstant(str, mode))
  2691. return luax_enumerror(L, "draw mode", Graphics::getConstants(mode), str);
  2692. float x = (float)luaL_checknumber(L, 2);
  2693. float y = (float)luaL_checknumber(L, 3);
  2694. float a = (float)luaL_checknumber(L, 4);
  2695. float b = (float)luaL_optnumber(L, 5, a);
  2696. if (lua_isnoneornil(L, 6))
  2697. luax_catchexcept(L, [&](){ instance()->ellipse(mode, x, y, a, b); });
  2698. else
  2699. {
  2700. int points = (int) luaL_checkinteger(L, 6);
  2701. luax_catchexcept(L, [&](){ instance()->ellipse(mode, x, y, a, b, points); });
  2702. }
  2703. return 0;
  2704. }
  2705. int w_arc(lua_State *L)
  2706. {
  2707. Graphics::DrawMode drawmode;
  2708. const char *drawstr = luaL_checkstring(L, 1);
  2709. if (!Graphics::getConstant(drawstr, drawmode))
  2710. return luax_enumerror(L, "draw mode", Graphics::getConstants(drawmode), drawstr);
  2711. int startidx = 2;
  2712. Graphics::ArcMode arcmode = Graphics::ARC_PIE;
  2713. if (lua_type(L, 2) == LUA_TSTRING)
  2714. {
  2715. const char *arcstr = luaL_checkstring(L, 2);
  2716. if (!Graphics::getConstant(arcstr, arcmode))
  2717. return luax_enumerror(L, "arc mode", Graphics::getConstants(arcmode), arcstr);
  2718. startidx = 3;
  2719. }
  2720. float x = (float) luaL_checknumber(L, startidx + 0);
  2721. float y = (float) luaL_checknumber(L, startidx + 1);
  2722. float radius = (float) luaL_checknumber(L, startidx + 2);
  2723. float angle1 = (float) luaL_checknumber(L, startidx + 3);
  2724. float angle2 = (float) luaL_checknumber(L, startidx + 4);
  2725. if (lua_isnoneornil(L, startidx + 5))
  2726. luax_catchexcept(L, [&](){ instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2); });
  2727. else
  2728. {
  2729. int points = (int) luaL_checkinteger(L, startidx + 5);
  2730. luax_catchexcept(L, [&](){ instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2, points); });
  2731. }
  2732. return 0;
  2733. }
  2734. int w_polygon(lua_State *L)
  2735. {
  2736. int args = lua_gettop(L) - 1;
  2737. Graphics::DrawMode mode;
  2738. const char *str = luaL_checkstring(L, 1);
  2739. if (!Graphics::getConstant(str, mode))
  2740. return luax_enumerror(L, "draw mode", Graphics::getConstants(mode), str);
  2741. bool is_table = false;
  2742. if (args == 1 && lua_istable(L, 2))
  2743. {
  2744. args = (int) luax_objlen(L, 2);
  2745. is_table = true;
  2746. }
  2747. if (args % 2 != 0)
  2748. return luaL_error(L, "Number of vertex components must be a multiple of two");
  2749. else if (args < 6)
  2750. return luaL_error(L, "Need at least three vertices to draw a polygon");
  2751. int numvertices = args / 2;
  2752. // fetch coords
  2753. Vector2 *coords = instance()->getScratchBuffer<Vector2>(numvertices + 1);
  2754. if (is_table)
  2755. {
  2756. for (int i = 0; i < numvertices; ++i)
  2757. {
  2758. lua_rawgeti(L, 2, (i * 2) + 1);
  2759. lua_rawgeti(L, 2, (i * 2) + 2);
  2760. coords[i].x = luax_checkfloat(L, -2);
  2761. coords[i].y = luax_checkfloat(L, -1);
  2762. lua_pop(L, 2);
  2763. }
  2764. }
  2765. else
  2766. {
  2767. for (int i = 0; i < numvertices; ++i)
  2768. {
  2769. coords[i].x = luax_checkfloat(L, (i * 2) + 2);
  2770. coords[i].y = luax_checkfloat(L, (i * 2) + 3);
  2771. }
  2772. }
  2773. // make a closed loop
  2774. coords[numvertices] = coords[0];
  2775. luax_catchexcept(L, [&](){ instance()->polygon(mode, coords, numvertices+1); });
  2776. return 0;
  2777. }
  2778. int w_dispatchThreadgroups(lua_State* L)
  2779. {
  2780. Shader *shader = luax_checkshader(L, 1);
  2781. int x = (int) luaL_checkinteger(L, 2);
  2782. int y = (int) luaL_optinteger(L, 3, 1);
  2783. int z = (int) luaL_optinteger(L, 4, 1);
  2784. luax_catchexcept(L, [&](){ instance()->dispatchThreadgroups(shader, x, y, z); });
  2785. return 0;
  2786. }
  2787. int w_copyBuffer(lua_State *L)
  2788. {
  2789. Buffer *source = luax_checkbuffer(L, 1);
  2790. Buffer *dest = luax_checkbuffer(L, 2);
  2791. ptrdiff_t sourceoffset = luaL_optinteger(L, 3, 0);
  2792. ptrdiff_t destoffset = luaL_optinteger(L, 4, 0);
  2793. ptrdiff_t size = std::min(source->getSize() - sourceoffset, dest->getSize() - destoffset);
  2794. if (!lua_isnoneornil(L, 5))
  2795. size = luaL_checkinteger(L, 5);
  2796. if (sourceoffset < 0 || destoffset < 0)
  2797. return luaL_error(L, "copyBuffer offsets cannot be negative.");
  2798. if (size <= 0)
  2799. return luaL_error(L, "copyBuffer size must be greater than 0.");
  2800. luax_catchexcept(L, [&](){ instance()->copyBuffer(source, dest, sourceoffset, destoffset, size); });
  2801. return 0;
  2802. }
  2803. int w_copyBufferToTexture(lua_State *L)
  2804. {
  2805. Buffer *source = luax_checkbuffer(L, 1);
  2806. Texture *dest = luax_checktexture(L, 2);
  2807. ptrdiff_t sourceoffset = luaL_optinteger(L, 3, 0);
  2808. if (sourceoffset < 0)
  2809. return luaL_error(L, "copyBufferToTexture source offset cannot be negative.");
  2810. int sourcewidth = (int) luaL_optinteger(L, 4, 0);
  2811. int slice = 0;
  2812. int mipmap = 0;
  2813. if (dest->getTextureType() != TEXTURE_2D)
  2814. slice = (int) luaL_checkinteger(L, 5) - 1;
  2815. mipmap = (int) luaL_optinteger(L, 6, 1) - 1;
  2816. Rect rect = {0, 0, dest->getPixelWidth(mipmap), dest->getPixelHeight(mipmap)};
  2817. if (!lua_isnoneornil(L, 7))
  2818. {
  2819. rect.x = (int) luaL_checkinteger(L, 7);
  2820. rect.y = (int) luaL_checkinteger(L, 8);
  2821. rect.w = (int) luaL_checkinteger(L, 9);
  2822. rect.h = (int) luaL_checkinteger(L, 10);
  2823. }
  2824. luax_catchexcept(L, [&](){ instance()->copyBufferToTexture(source, dest, sourceoffset, sourcewidth, slice, mipmap, rect); });
  2825. return 0;
  2826. }
  2827. int w_copyTextureToBuffer(lua_State *L)
  2828. {
  2829. Texture *source = luax_checktexture(L, 1);
  2830. Buffer *dest = luax_checkbuffer(L, 2);
  2831. int slice = 0;
  2832. int mipmap = 0;
  2833. if (source->getTextureType() != TEXTURE_2D)
  2834. slice = (int) luaL_checkinteger(L, 3) - 1;
  2835. mipmap = (int) luaL_optinteger(L, 4, 1) - 1;
  2836. Rect rect = {0, 0, source->getPixelWidth(mipmap), source->getPixelHeight(mipmap)};
  2837. if (!lua_isnoneornil(L, 5))
  2838. {
  2839. rect.x = (int) luaL_checkinteger(L, 5);
  2840. rect.y = (int) luaL_checkinteger(L, 6);
  2841. rect.w = (int) luaL_checkinteger(L, 7);
  2842. rect.h = (int) luaL_checkinteger(L, 8);
  2843. }
  2844. ptrdiff_t destoffset = luaL_optinteger(L, 9, 0);
  2845. if (destoffset < 0)
  2846. return luaL_error(L, "copyTextureToBuffer dest offset cannot be negative.");
  2847. int destwidth = (int) luaL_optinteger(L, 10, 0);
  2848. luax_catchexcept(L, [&](){ instance()->copyTextureToBuffer(source, dest, slice, mipmap, rect, destoffset, destwidth); });
  2849. return 0;
  2850. }
  2851. int w_flushBatch(lua_State *)
  2852. {
  2853. instance()->flushBatchedDraws();
  2854. return 0;
  2855. }
  2856. int w_getStackDepth(lua_State *L)
  2857. {
  2858. lua_pushnumber(L, instance()->getStackDepth());
  2859. return 1;
  2860. }
  2861. int w_push(lua_State *L)
  2862. {
  2863. Graphics::StackType stype = Graphics::STACK_TRANSFORM;
  2864. const char *sname = lua_isnoneornil(L, 1) ? nullptr : luaL_checkstring(L, 1);
  2865. if (sname && !Graphics::getConstant(sname, stype))
  2866. return luax_enumerror(L, "graphics stack type", Graphics::getConstants(stype), sname);
  2867. luax_catchexcept(L, [&](){ instance()->push(stype); });
  2868. if (luax_istype(L, 2, math::Transform::type))
  2869. {
  2870. math::Transform *t = luax_totype<math::Transform>(L, 2);
  2871. luax_catchexcept(L, [&]() { instance()->applyTransform(t->getMatrix()); });
  2872. }
  2873. return 0;
  2874. }
  2875. int w_pop(lua_State *L)
  2876. {
  2877. luax_catchexcept(L, [&](){ instance()->pop(); });
  2878. return 0;
  2879. }
  2880. int w_rotate(lua_State *L)
  2881. {
  2882. float angle = (float)luaL_checknumber(L, 1);
  2883. instance()->rotate(angle);
  2884. return 0;
  2885. }
  2886. int w_scale(lua_State *L)
  2887. {
  2888. float sx = (float)luaL_optnumber(L, 1, 1.0f);
  2889. float sy = (float)luaL_optnumber(L, 2, sx);
  2890. instance()->scale(sx, sy);
  2891. return 0;
  2892. }
  2893. int w_translate(lua_State *L)
  2894. {
  2895. float x = (float)luaL_checknumber(L, 1);
  2896. float y = (float)luaL_checknumber(L, 2);
  2897. instance()->translate(x, y);
  2898. return 0;
  2899. }
  2900. int w_shear(lua_State *L)
  2901. {
  2902. float kx = (float)luaL_checknumber(L, 1);
  2903. float ky = (float)luaL_checknumber(L, 2);
  2904. instance()->shear(kx, ky);
  2905. return 0;
  2906. }
  2907. int w_origin(lua_State * /*L*/)
  2908. {
  2909. instance()->origin();
  2910. return 0;
  2911. }
  2912. int w_applyTransform(lua_State *L)
  2913. {
  2914. luax_checkstandardtransform(L, 1, [&](const Matrix4 &m)
  2915. {
  2916. luax_catchexcept(L, [&]() { instance()->applyTransform(m); });
  2917. });
  2918. return 0;
  2919. }
  2920. int w_replaceTransform(lua_State *L)
  2921. {
  2922. luax_checkstandardtransform(L, 1, [&](const Matrix4 &m)
  2923. {
  2924. luax_catchexcept(L, [&]() { instance()->replaceTransform(m); });
  2925. });
  2926. return 0;
  2927. }
  2928. int w_transformPoint(lua_State *L)
  2929. {
  2930. Vector2 p;
  2931. p.x = (float) luaL_checknumber(L, 1);
  2932. p.y = (float) luaL_checknumber(L, 2);
  2933. p = instance()->transformPoint(p);
  2934. lua_pushnumber(L, p.x);
  2935. lua_pushnumber(L, p.y);
  2936. return 2;
  2937. }
  2938. int w_inverseTransformPoint(lua_State *L)
  2939. {
  2940. Vector2 p;
  2941. p.x = (float) luaL_checknumber(L, 1);
  2942. p.y = (float) luaL_checknumber(L, 2);
  2943. p = instance()->inverseTransformPoint(p);
  2944. lua_pushnumber(L, p.x);
  2945. lua_pushnumber(L, p.y);
  2946. return 2;
  2947. }
  2948. int w_setOrthoProjection(lua_State *L)
  2949. {
  2950. float w = (float) luaL_checknumber(L, 1);
  2951. float h = (float) luaL_checknumber(L, 2);
  2952. float near = (float) luaL_optnumber(L, 3, -10.0);
  2953. float far = (float) luaL_optnumber(L, 4, 10.0);
  2954. luax_catchexcept(L, [&]() { instance()->setOrthoProjection(w, h, near, far); });
  2955. return 0;
  2956. }
  2957. int w_setPerspectiveProjection(lua_State *L)
  2958. {
  2959. float verticalfov = (float) luaL_checknumber(L, 1);
  2960. float aspect = (float) luaL_checknumber(L, 2);
  2961. float near = (float) luaL_checknumber(L, 3);
  2962. float far = (float) luaL_checknumber(L, 4);
  2963. luax_catchexcept(L, [&]() { instance()->setPerspectiveProjection(verticalfov, aspect, near, far); });
  2964. return 0;
  2965. }
  2966. int w_resetProjection(lua_State */*L*/)
  2967. {
  2968. instance()->resetProjection();
  2969. return 0;
  2970. }
  2971. // List of functions to wrap.
  2972. static const luaL_Reg functions[] =
  2973. {
  2974. { "reset", w_reset },
  2975. { "clear", w_clear },
  2976. { "discard", w_discard },
  2977. { "present", w_present },
  2978. { "newCanvas", w_newCanvas },
  2979. { "newTexture", w_newTexture },
  2980. { "newCubeTexture", w_newCubeTexture },
  2981. { "newArrayTexture", w_newArrayTexture },
  2982. { "newVolumeTexture", w_newVolumeTexture },
  2983. { "newQuad", w_newQuad },
  2984. { "newFont", w_newFont },
  2985. { "newImageFont", w_newImageFont },
  2986. { "newSpriteBatch", w_newSpriteBatch },
  2987. { "newParticleSystem", w_newParticleSystem },
  2988. { "newShader", w_newShader },
  2989. { "newComputeShader", w_newComputeShader },
  2990. { "newBuffer", w_newBuffer },
  2991. { "newVertexBuffer", w_newVertexBuffer },
  2992. { "newIndexBuffer", w_newIndexBuffer },
  2993. { "newMesh", w_newMesh },
  2994. { "newText", w_newText },
  2995. { "_newVideo", w_newVideo },
  2996. { "validateShader", w_validateShader },
  2997. { "setCanvas", w_setCanvas },
  2998. { "getCanvas", w_getCanvas },
  2999. { "setColor", w_setColor },
  3000. { "getColor", w_getColor },
  3001. { "setBackgroundColor", w_setBackgroundColor },
  3002. { "getBackgroundColor", w_getBackgroundColor },
  3003. { "setNewFont", w_setNewFont },
  3004. { "setFont", w_setFont },
  3005. { "getFont", w_getFont },
  3006. { "setColorMask", w_setColorMask },
  3007. { "getColorMask", w_getColorMask },
  3008. { "setBlendMode", w_setBlendMode },
  3009. { "getBlendMode", w_getBlendMode },
  3010. { "setBlendState", w_setBlendState },
  3011. { "getBlendState", w_getBlendState },
  3012. { "setDefaultFilter", w_setDefaultFilter },
  3013. { "getDefaultFilter", w_getDefaultFilter },
  3014. { "setDefaultMipmapFilter", w_setDefaultMipmapFilter },
  3015. { "getDefaultMipmapFilter", w_getDefaultMipmapFilter },
  3016. { "setLineWidth", w_setLineWidth },
  3017. { "setLineStyle", w_setLineStyle },
  3018. { "setLineJoin", w_setLineJoin },
  3019. { "getLineWidth", w_getLineWidth },
  3020. { "getLineStyle", w_getLineStyle },
  3021. { "getLineJoin", w_getLineJoin },
  3022. { "setPointSize", w_setPointSize },
  3023. { "getPointSize", w_getPointSize },
  3024. { "setDepthMode", w_setDepthMode },
  3025. { "getDepthMode", w_getDepthMode },
  3026. { "setMeshCullMode", w_setMeshCullMode },
  3027. { "getMeshCullMode", w_getMeshCullMode },
  3028. { "setFrontFaceWinding", w_setFrontFaceWinding },
  3029. { "getFrontFaceWinding", w_getFrontFaceWinding },
  3030. { "setWireframe", w_setWireframe },
  3031. { "isWireframe", w_isWireframe },
  3032. { "setShader", w_setShader },
  3033. { "getShader", w_getShader },
  3034. { "getSupported", w_getSupported },
  3035. { "getTextureFormats", w_getTextureFormats },
  3036. { "getRendererInfo", w_getRendererInfo },
  3037. { "getSystemLimits", w_getSystemLimits },
  3038. { "getTextureTypes", w_getTextureTypes },
  3039. { "getStats", w_getStats },
  3040. { "captureScreenshot", w_captureScreenshot },
  3041. { "draw", w_draw },
  3042. { "drawLayer", w_drawLayer },
  3043. { "drawInstanced", w_drawInstanced },
  3044. { "drawShaderVertices", w_drawShaderVertices },
  3045. { "print", w_print },
  3046. { "printf", w_printf },
  3047. { "dispatchThreadgroups", w_dispatchThreadgroups },
  3048. { "copyBuffer", w_copyBuffer },
  3049. { "copyBufferToTexture", w_copyBufferToTexture },
  3050. { "copyTextureToBuffer", w_copyTextureToBuffer },
  3051. { "isCreated", w_isCreated },
  3052. { "isActive", w_isActive },
  3053. { "isGammaCorrect", w_isGammaCorrect },
  3054. { "getWidth", w_getWidth },
  3055. { "getHeight", w_getHeight },
  3056. { "getDimensions", w_getDimensions },
  3057. { "getPixelWidth", w_getPixelWidth },
  3058. { "getPixelHeight", w_getPixelHeight },
  3059. { "getPixelDimensions", w_getPixelDimensions },
  3060. { "getDPIScale", w_getDPIScale },
  3061. { "getQuadIndexBuffer", w_getQuadIndexBuffer },
  3062. { "setScissor", w_setScissor },
  3063. { "intersectScissor", w_intersectScissor },
  3064. { "getScissor", w_getScissor },
  3065. { "setStencilMode", w_setStencilMode },
  3066. { "getStencilMode", w_getStencilMode },
  3067. { "points", w_points },
  3068. { "line", w_line },
  3069. { "rectangle", w_rectangle },
  3070. { "circle", w_circle },
  3071. { "ellipse", w_ellipse },
  3072. { "arc", w_arc },
  3073. { "polygon", w_polygon },
  3074. { "flushBatch", w_flushBatch },
  3075. { "getStackDepth", w_getStackDepth },
  3076. { "push", w_push },
  3077. { "pop", w_pop },
  3078. { "rotate", w_rotate },
  3079. { "scale", w_scale },
  3080. { "translate", w_translate },
  3081. { "shear", w_shear },
  3082. { "origin", w_origin },
  3083. { "applyTransform", w_applyTransform },
  3084. { "replaceTransform", w_replaceTransform },
  3085. { "transformPoint", w_transformPoint },
  3086. { "inverseTransformPoint", w_inverseTransformPoint },
  3087. { "setOrthoProjection", w_setOrthoProjection },
  3088. { "setPerspectiveProjection", w_setPerspectiveProjection },
  3089. { "resetProjection", w_resetProjection },
  3090. // Deprecated
  3091. { "newImage", w_newImage },
  3092. { "newArrayImage", w_newArrayImage },
  3093. { "newVolumeImage", w_newVolumeImage },
  3094. { "newCubeImage", w_newCubeImage },
  3095. { "getCanvasFormats", w_getCanvasFormats },
  3096. { "getImageFormats", w_getImageFormats },
  3097. { 0, 0 }
  3098. };
  3099. static int luaopen_drawable(lua_State *L)
  3100. {
  3101. return luax_register_type(L, &Drawable::type, nullptr);
  3102. }
  3103. // Types for this module.
  3104. static const lua_CFunction types[] =
  3105. {
  3106. luaopen_drawable,
  3107. luaopen_texture,
  3108. luaopen_font,
  3109. luaopen_quad,
  3110. luaopen_graphicsbuffer,
  3111. luaopen_spritebatch,
  3112. luaopen_particlesystem,
  3113. luaopen_shader,
  3114. luaopen_mesh,
  3115. luaopen_text,
  3116. luaopen_video,
  3117. 0
  3118. };
  3119. extern "C" int luaopen_love_graphics(lua_State *L)
  3120. {
  3121. Graphics *instance = Graphics::createInstance();
  3122. if (instance == nullptr)
  3123. {
  3124. printf("Cannot create graphics: no supported renderer on this system.\n");
  3125. return luaL_error(L, "Cannot create graphics: no supported renderer on this system.");
  3126. }
  3127. WrappedModule w;
  3128. w.module = instance;
  3129. w.name = "graphics";
  3130. w.type = &Graphics::type;
  3131. w.functions = functions;
  3132. w.types = types;
  3133. int n = luax_register_module(L, w);
  3134. if (luaL_loadbuffer(L, (const char *)graphics_lua, sizeof(graphics_lua), "=[love \"wrap_Graphics.lua\"]") == 0)
  3135. lua_call(L, 0, 0);
  3136. else
  3137. lua_error(L);
  3138. return n;
  3139. }
  3140. } // graphics
  3141. } // love