wrap_Graphics.cpp 80 KB

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