123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328 |
- /**
- * Copyright (c) 2006-2016 LOVE Development Team
- *
- * This software is provided 'as-is', without any express or implied
- * warranty. In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- * claim that you wrote the original software. If you use this software
- * in a product, an acknowledgment in the product documentation would be
- * appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- * misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- **/
- #include "wrap_Graphics.h"
- #include "OpenGL.h"
- #include "graphics/Texture.h"
- #include "image/ImageData.h"
- #include "image/Image.h"
- #include "font/Rasterizer.h"
- #include "filesystem/wrap_Filesystem.h"
- #include "video/VideoStream.h"
- #include "image/wrap_Image.h"
- #include "common/Reference.h"
- #include "math/wrap_Transform.h"
- #include <cassert>
- #include <cstring>
- #include <algorithm>
- // Shove the wrap_Graphics.lua code directly into a raw string literal.
- static const char graphics_lua[] =
- #include "wrap_Graphics.lua"
- ;
- namespace love
- {
- namespace graphics
- {
- namespace opengl
- {
- #define instance() (Module::getInstance<Graphics>(Module::M_GRAPHICS))
- static int luax_checkgraphicscreated(lua_State *L)
- {
- if (!instance()->isCreated())
- return luaL_error(L, "love.graphics cannot function without a window!");
- return 0;
- }
- int w_reset(lua_State *)
- {
- instance()->reset();
- return 0;
- }
- int w_present(lua_State *L)
- {
- luax_catchexcept(L, [&]() { instance()->present(L); });
- return 0;
- }
- int w_isCreated(lua_State *L)
- {
- luax_pushboolean(L, instance()->isCreated());
- return 1;
- }
- int w_isActive(lua_State *L)
- {
- luax_pushboolean(L, instance()->isActive());
- return 1;
- }
- int w_isGammaCorrect(lua_State *L)
- {
- luax_pushboolean(L, instance()->isGammaCorrect());
- return 1;
- }
- int w_getWidth(lua_State *L)
- {
- lua_pushinteger(L, instance()->getWidth());
- return 1;
- }
- int w_getHeight(lua_State *L)
- {
- lua_pushinteger(L, instance()->getHeight());
- return 1;
- }
- int w_getDimensions(lua_State *L)
- {
- lua_pushinteger(L, instance()->getWidth());
- lua_pushinteger(L, instance()->getHeight());
- return 2;
- }
- int w_getPassWidth(lua_State *L)
- {
- lua_pushinteger(L, instance()->getPassWidth());
- return 1;
- }
- int w_getPassHeight(lua_State *L)
- {
- lua_pushinteger(L, instance()->getPassHeight());
- return 1;
- }
- int w_getPassDimensions(lua_State *L)
- {
- lua_pushinteger(L, instance()->getPassWidth());
- lua_pushinteger(L, instance()->getPassHeight());
- return 2;
- }
- static int w__beginPass(lua_State *L)
- {
- int nextstartidx = 1;
- if (lua_isnoneornil(L, 1))
- {
- luax_catchexcept(L, [&]() { instance()->beginPass(PassInfo::BEGIN_LOAD, Colorf()); });
- nextstartidx = 1;
- }
- else if (lua_isnumber(L, 1))
- {
- Colorf c;
- c.r = (float) luaL_checknumber(L, 1);
- c.g = (float) luaL_checknumber(L, 2);
- c.b = (float) luaL_checknumber(L, 3);
- if (lua_isnumber(L, 4))
- {
- c.a = (float) lua_tonumber(L, 4);
- nextstartidx = 5;
- }
- else
- {
- c.a = 1.0f;
- nextstartidx = 4;
- }
- luax_catchexcept(L, [&]() { instance()->beginPass(PassInfo::BEGIN_CLEAR, c); });
- }
- else if (luax_istype(L, 1, GRAPHICS_CANVAS_ID))
- {
- PassInfo::ColorAttachment attachment;
- attachment.canvas = luax_checkcanvas(L, 1);
- attachment.beginAction = PassInfo::BEGIN_LOAD;
- if (lua_isnumber(L, 2))
- {
- attachment.beginAction = PassInfo::BEGIN_CLEAR;
- attachment.clearColor.r = (float) luaL_checknumber(L, 2);
- attachment.clearColor.g = (float) luaL_checknumber(L, 3);
- attachment.clearColor.b = (float) luaL_checknumber(L, 4);
- if (lua_isnumber(L, 5))
- {
- attachment.clearColor.a = (float) lua_tonumber(L, 5);
- nextstartidx = 6;
- }
- else
- {
- attachment.clearColor.a = 1.0f;
- nextstartidx = 5;
- }
- }
- else
- nextstartidx = 2;
- PassInfo info;
- info.addColorAttachment(attachment);
- if (lua_isboolean(L, nextstartidx))
- {
- info.stencil = luax_toboolean(L, nextstartidx);
- nextstartidx++;
- }
- else
- info.stencil = false;
- luax_catchexcept(L, [&]() { instance()->beginPass(info); });
- }
- else
- {
- luaL_checktype(L, 1, LUA_TTABLE);
- int nattachments = std::max((int) luax_objlen(L, 1), 1);
- if (nattachments > MAX_COLOR_RENDER_TARGETS)
- return luaL_error(L, "Cannot render to %d Canvases at once!", nattachments);
- PassInfo info;
- for (int i = 1; i <= nattachments; i++)
- {
- lua_rawgeti(L, 1, i);
- luaL_checktype(L, -1, LUA_TTABLE);
- PassInfo::ColorAttachment attachment;
- attachment.beginAction = PassInfo::BEGIN_LOAD;
- lua_rawgeti(L, -1, 1);
- attachment.canvas = luax_checkcanvas(L, -1);
- lua_rawgeti(L, -2, 2);
- if (!lua_isnoneornil(L, -1))
- {
- attachment.beginAction = PassInfo::BEGIN_CLEAR;
- for (int j = 3; j < 6; j++)
- lua_rawgeti(L, -j, j);
- attachment.clearColor.r = (float) luaL_checknumber(L, -4);
- attachment.clearColor.g = (float) luaL_checknumber(L, -3);
- attachment.clearColor.b = (float) luaL_checknumber(L, -2);
- attachment.clearColor.a = (float) luaL_optnumber(L, -1, 1.0);
- }
- lua_pop(L, 2 + (attachment.beginAction == PassInfo::BEGIN_CLEAR ? 4 : 1));
- info.addColorAttachment(attachment);
- }
- info.stencil = luax_boolflag(L, 1, "stencil", false);
- luax_catchexcept(L, [&]() { instance()->beginPass(info); });
- nextstartidx = 2;
- }
- return nextstartidx;
- }
- int w_beginPass(lua_State *L)
- {
- w__beginPass(L);
- return 0;
- }
- static void screenshotCallback(love::image::ImageData *i, Reference *ref, void *gd)
- {
- if (i != nullptr)
- {
- lua_State *L = (lua_State *) gd;
- ref->push(L);
- delete ref;
- luax_pushtype(L, IMAGE_IMAGE_DATA_ID, i);
- lua_call(L, 1, 0);
- }
- else
- delete ref;
- }
- static int w__endPass(lua_State *L, int startidx)
- {
- if (lua_isnoneornil(L, startidx))
- {
- luax_catchexcept(L, []() { instance()->endPass(); });
- }
- else
- {
- int x, y, w, h;
- if (lua_isnumber(L, startidx))
- {
- x = (int) luaL_checknumber(L, 1);
- y = (int) luaL_checknumber(L, 2);
- w = (int) luaL_checknumber(L, 3);
- h = (int) luaL_checknumber(L, 4);
- startidx += 4;
- }
- else
- {
- x = 0;
- y = 0;
- w = instance()->getPassWidth();
- h = instance()->getPassHeight();
- }
- luaL_checktype(L, startidx, LUA_TFUNCTION);
- Graphics::ScreenshotInfo info;
- info.callback = screenshotCallback;
- lua_pushvalue(L, startidx);
- info.ref = luax_refif(L, LUA_TFUNCTION);
- lua_pop(L, 1);
- luax_catchexcept(L,
- [&]() { instance()->endPass(x, y, w, h, &info, L); },
- [&](bool except) { if (except) delete info.ref; }
- );
- }
- return 0;
- }
- int w_endPass(lua_State *L)
- {
- return w__endPass(L, 1);
- }
- int w_renderPass(lua_State *L)
- {
- int startidx = w__beginPass(L);
- if (lua_type(L, startidx) != LUA_TFUNCTION)
- {
- w__endPass(L, startidx + 1);
- luaL_checktype(L, startidx, LUA_TFUNCTION);
- return 0;
- }
- int nargs = lua_gettop(L) - startidx;
- int status = lua_pcall(L, nargs, 0, 0);
- w__endPass(L, startidx + 1);
- if (status != 0)
- return lua_error(L);
- return 0;
- }
- int w_isPassActive(lua_State *L)
- {
- luax_pushboolean(L, instance()->isPassActive());
- return 1;
- }
- int w_getPassCanvases(lua_State *L)
- {
- if (!instance()->isPassActive())
- return 0;
- const PassInfo &info = instance()->getActivePass();
- for (const auto &attachment : info.colorAttachments)
- luax_pushtype(L, GRAPHICS_CANVAS_ID, attachment.canvas);
- return info.colorAttachmentCount;
- }
- int w_captureScreenshot(lua_State *L)
- {
- luaL_checktype(L, 1, LUA_TFUNCTION);
- Graphics::ScreenshotInfo info;
- info.callback = screenshotCallback;
- lua_pushvalue(L, 1);
- info.ref = luax_refif(L, LUA_TFUNCTION);
- lua_pop(L, 1);
- luax_catchexcept(L,
- [&]() { instance()->captureScreenshot(info); },
- [&](bool except) { if (except) delete info.ref; }
- );
- return 0;
- }
- int w_setScissor(lua_State *L)
- {
- int nargs = lua_gettop(L);
- if (nargs == 0 || (nargs == 4 && lua_isnil(L, 1) && lua_isnil(L, 2)
- && lua_isnil(L, 3) && lua_isnil(L, 4)))
- {
- instance()->setScissor();
- return 0;
- }
- int x = (int) luaL_checknumber(L, 1);
- int y = (int) luaL_checknumber(L, 2);
- int w = (int) luaL_checknumber(L, 3);
- int h = (int) luaL_checknumber(L, 4);
- if (w < 0 || h < 0)
- return luaL_error(L, "Can't set scissor with negative width and/or height.");
- instance()->setScissor(x, y, w, h);
- return 0;
- }
- int w_intersectScissor(lua_State *L)
- {
- int x = (int) luaL_checknumber(L, 1);
- int y = (int) luaL_checknumber(L, 2);
- int w = (int) luaL_checknumber(L, 3);
- int h = (int) luaL_checknumber(L, 4);
- if (w < 0 || h < 0)
- return luaL_error(L, "Can't set scissor with negative width and/or height.");
- instance()->intersectScissor(x, y, w, h);
- return 0;
- }
- int w_getScissor(lua_State *L)
- {
- int x, y, w, h;
- if (!instance()->getScissor(x, y, w, h))
- return 0;
- lua_pushinteger(L, x);
- lua_pushinteger(L, y);
- lua_pushinteger(L, w);
- lua_pushinteger(L, h);
- return 4;
- }
- int w_stencil(lua_State *L)
- {
- luaL_checktype(L, 1, LUA_TFUNCTION);
- Graphics::StencilAction action = Graphics::STENCIL_REPLACE;
- if (!lua_isnoneornil(L, 2))
- {
- const char *actionstr = luaL_checkstring(L, 2);
- if (!Graphics::getConstant(actionstr, action))
- return luaL_error(L, "Invalid stencil draw action: %s", actionstr);
- }
- int stencilvalue = (int) luaL_optnumber(L, 3, 1);
- // Fourth argument: whether to keep the contents of the stencil buffer.
- if (lua_toboolean(L, 4) == 0)
- instance()->clearStencil();
- luax_catchexcept(L, [&](){ instance()->drawToStencilBuffer(action, stencilvalue); });
- // Call stencilfunc()
- lua_pushvalue(L, 1);
- lua_call(L, 0, 0);
- luax_catchexcept(L, [&](){ instance()->stopDrawToStencilBuffer(); });
- return 0;
- }
- int w_setStencilTest(lua_State *L)
- {
- // COMPARE_ALWAYS effectively disables stencil testing.
- Graphics::CompareMode compare = Graphics::COMPARE_ALWAYS;
- int comparevalue = 0;
- if (!lua_isnoneornil(L, 1))
- {
- const char *comparestr = luaL_checkstring(L, 1);
- if (!Graphics::getConstant(comparestr, compare))
- return luaL_error(L, "Invalid compare mode: %s", comparestr);
- comparevalue = (int) luaL_checknumber(L, 2);
- }
- luax_catchexcept(L, [&](){ instance()->setStencilTest(compare, comparevalue); });
- return 0;
- }
- int w_getStencilTest(lua_State *L)
- {
- Graphics::CompareMode compare = Graphics::COMPARE_ALWAYS;
- int comparevalue = 1;
- instance()->getStencilTest(compare, comparevalue);
- const char *comparestr;
- if (!Graphics::getConstant(compare, comparestr))
- return luaL_error(L, "Unknown compare mode.");
- lua_pushstring(L, comparestr);
- lua_pushnumber(L, comparevalue);
- return 2;
- }
- static const char *imageFlagName(Image::FlagType flagtype)
- {
- const char *name = nullptr;
- Image::getConstant(flagtype, name);
- return name;
- }
- int w_newImage(lua_State *L)
- {
- luax_checkgraphicscreated(L);
- std::vector<love::image::ImageData *> data;
- std::vector<love::image::CompressedImageData *> cdata;
- Image::Flags flags;
- if (!lua_isnoneornil(L, 2))
- {
- luaL_checktype(L, 2, LUA_TTABLE);
- flags.mipmaps = luax_boolflag(L, 2, imageFlagName(Image::FLAG_TYPE_MIPMAPS), flags.mipmaps);
- flags.linear = luax_boolflag(L, 2, imageFlagName(Image::FLAG_TYPE_LINEAR), flags.linear);
- }
- bool releasedata = false;
- // Convert to ImageData / CompressedImageData, if necessary.
- if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_ID) || luax_istype(L, 1, FILESYSTEM_FILE_DATA_ID))
- {
- auto imagemodule = Module::getInstance<love::image::Image>(Module::M_IMAGE);
- if (imagemodule == nullptr)
- return luaL_error(L, "Cannot load images without the love.image module.");
- love::filesystem::FileData *fdata = love::filesystem::luax_getfiledata(L, 1);
- if (imagemodule->isCompressed(fdata))
- {
- luax_catchexcept(L,
- [&]() { cdata.push_back(imagemodule->newCompressedData(fdata)); },
- [&](bool) { fdata->release(); }
- );
- }
- else
- {
- luax_catchexcept(L,
- [&]() { data.push_back(imagemodule->newImageData(fdata)); },
- [&](bool) { fdata->release(); }
- );
- }
- // Lua's GC won't release the image data, so we should do it ourselves.
- releasedata = true;
- }
- else if (luax_istype(L, 1, IMAGE_COMPRESSED_IMAGE_DATA_ID))
- cdata.push_back(love::image::luax_checkcompressedimagedata(L, 1));
- else
- data.push_back(love::image::luax_checkimagedata(L, 1));
- if (lua_istable(L, 2))
- {
- lua_getfield(L, 2, imageFlagName(Image::FLAG_TYPE_MIPMAPS));
- // Add all manually specified mipmap images to the array of imagedata.
- // i.e. flags = {mipmaps = {mip1, mip2, ...}}.
- if (lua_istable(L, -1))
- {
- for (size_t i = 1; i <= luax_objlen(L, -1); i++)
- {
- lua_rawgeti(L, -1, i);
- if (!data.empty())
- {
- if (!luax_istype(L, -1, IMAGE_IMAGE_DATA_ID))
- luax_convobj(L, -1, "image", "newImageData");
- data.push_back(love::image::luax_checkimagedata(L, -1));
- }
- else if (!cdata.empty())
- {
- if (!luax_istype(L, -1, IMAGE_COMPRESSED_IMAGE_DATA_ID))
- luax_convobj(L, -1, "image", "newCompressedData");
- cdata.push_back(love::image::luax_checkcompressedimagedata(L, -1));
- }
- lua_pop(L, 1);
- }
- }
- lua_pop(L, 1);
- }
- // Create the image.
- Image *image = nullptr;
- luax_catchexcept(L,
- [&]() {
- if (!cdata.empty())
- image = instance()->newImage(cdata, flags);
- else if (!data.empty())
- image = instance()->newImage(data, flags);
- },
- [&](bool) {
- if (releasedata)
- {
- for (auto d : data)
- d->release();
- for (auto d : cdata)
- d->release();
- }
- }
- );
- if (image == nullptr)
- return luaL_error(L, "Could not load image.");
- // Push the type.
- luax_pushtype(L, GRAPHICS_IMAGE_ID, image);
- image->release();
- return 1;
- }
- int w_newQuad(lua_State *L)
- {
- luax_checkgraphicscreated(L);
- Quad::Viewport v;
- v.x = luaL_checknumber(L, 1);
- v.y = luaL_checknumber(L, 2);
- v.w = luaL_checknumber(L, 3);
- v.h = luaL_checknumber(L, 4);
- double sw = luaL_checknumber(L, 5);
- double sh = luaL_checknumber(L, 6);
- Quad *quad = instance()->newQuad(v, sw, sh);
- luax_pushtype(L, GRAPHICS_QUAD_ID, quad);
- quad->release();
- return 1;
- }
- int w_newFont(lua_State *L)
- {
- luax_checkgraphicscreated(L);
- Font *font = nullptr;
- // Convert to Rasterizer, if necessary.
- if (!luax_istype(L, 1, FONT_RASTERIZER_ID))
- {
- std::vector<int> idxs;
- for (int i = 0; i < lua_gettop(L); i++)
- idxs.push_back(i + 1);
- luax_convobj(L, &idxs[0], (int) idxs.size(), "font", "newRasterizer");
- }
- love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1, FONT_RASTERIZER_ID);
- luax_catchexcept(L, [&]() {
- font = instance()->newFont(rasterizer, instance()->getDefaultFilter()); }
- );
- // Push the type.
- luax_pushtype(L, GRAPHICS_FONT_ID, font);
- font->release();
- return 1;
- }
- int w_newImageFont(lua_State *L)
- {
- luax_checkgraphicscreated(L);
- // filter for glyphs
- Texture::Filter filter = instance()->getDefaultFilter();
- // Convert to ImageData if necessary.
- if (luax_istype(L, 1, GRAPHICS_IMAGE_ID))
- {
- Image *i = luax_checktype<Image>(L, 1, GRAPHICS_IMAGE_ID);
- filter = i->getFilter();
- const auto &idlevels = i->getImageData();
- if (idlevels.empty())
- return luaL_argerror(L, 1, "Image must not be compressed.");
- luax_pushtype(L, IMAGE_IMAGE_DATA_ID, idlevels[0].get());
- lua_replace(L, 1);
- }
- // Convert to Rasterizer if necessary.
- if (!luax_istype(L, 1, FONT_RASTERIZER_ID))
- {
- luaL_checktype(L, 2, LUA_TSTRING);
- std::vector<int> idxs;
- for (int i = 0; i < lua_gettop(L); i++)
- idxs.push_back(i + 1);
- luax_convobj(L, &idxs[0], (int) idxs.size(), "font", "newImageRasterizer");
- }
- love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1, FONT_RASTERIZER_ID);
- // Create the font.
- Font *font = instance()->newFont(rasterizer, filter);
- // Push the type.
- luax_pushtype(L, GRAPHICS_FONT_ID, font);
- font->release();
- return 1;
- }
- int w_newSpriteBatch(lua_State *L)
- {
- luax_checkgraphicscreated(L);
- Texture *texture = luax_checktexture(L, 1);
- int size = (int) luaL_optnumber(L, 2, 1000);
- Mesh::Usage usage = Mesh::USAGE_DYNAMIC;
- if (lua_gettop(L) > 2)
- {
- const char *usagestr = luaL_checkstring(L, 3);
- if (!Mesh::getConstant(usagestr, usage))
- return luaL_error(L, "Invalid SpriteBatch usage hint: %s", usagestr);
- }
- SpriteBatch *t = nullptr;
- luax_catchexcept(L,
- [&](){ t = instance()->newSpriteBatch(texture, size, usage); }
- );
- luax_pushtype(L, GRAPHICS_SPRITE_BATCH_ID, t);
- t->release();
- return 1;
- }
- int w_newParticleSystem(lua_State *L)
- {
- luax_checkgraphicscreated(L);
- Texture *texture = luax_checktexture(L, 1);
- lua_Number size = luaL_optnumber(L, 2, 1000);
- ParticleSystem *t = 0;
- if (size < 1.0 || size > ParticleSystem::MAX_PARTICLES)
- return luaL_error(L, "Invalid ParticleSystem size");
- luax_catchexcept(L,
- [&](){ t = instance()->newParticleSystem(texture, int(size)); }
- );
- luax_pushtype(L, GRAPHICS_PARTICLE_SYSTEM_ID, t);
- t->release();
- return 1;
- }
- int w_newCanvas(lua_State *L)
- {
- luax_checkgraphicscreated(L);
- // check if width and height are given. else default to screen dimensions.
- int width = (int) luaL_optnumber(L, 1, instance()->getWidth());
- int height = (int) luaL_optnumber(L, 2, instance()->getHeight());
- const char *str = luaL_optstring(L, 3, "normal");
- int msaa = (int) luaL_optnumber(L, 4, 0);
- Canvas::Format format;
- if (!Canvas::getConstant(str, format))
- return luaL_error(L, "Invalid Canvas format: %s", str);
- Canvas *canvas = nullptr;
- luax_catchexcept(L,
- [&](){ canvas = instance()->newCanvas(width, height, format, msaa); }
- );
- if (canvas == nullptr)
- return luaL_error(L, "Canvas not created, but no error thrown. I don't even...");
- luax_pushtype(L, GRAPHICS_CANVAS_ID, canvas);
- canvas->release();
- return 1;
- }
- int w_newShader(lua_State *L)
- {
- luax_checkgraphicscreated(L);
- // clamp stack to 2 elements
- lua_settop(L, 2);
- // read any filepath arguments
- for (int i = 1; i <= 2; i++)
- {
- if (!lua_isstring(L, i))
- continue;
- // call love.filesystem.isFile(arg_i)
- luax_getfunction(L, "filesystem", "isFile");
- lua_pushvalue(L, i);
- lua_call(L, 1, 1);
- bool isFile = luax_toboolean(L, -1);
- lua_pop(L, 1);
- if (isFile)
- {
- luax_getfunction(L, "filesystem", "read");
- lua_pushvalue(L, i);
- lua_call(L, 1, 1);
- lua_replace(L, i);
- }
- else
- {
- // Check if the argument looks like a filepath - we want a nicer
- // error for misspelled filepath arguments.
- size_t slen = 0;
- const char *str = lua_tolstring(L, i, &slen);
- if (slen > 0 && slen < 256 && !strchr(str, '\n'))
- {
- const char *ext = strchr(str, '.');
- if (ext != nullptr && !strchr(ext, ';') && !strchr(ext, ' '))
- return luaL_error(L, "Could not open file %s. Does not exist.", str);
- }
- }
- }
- bool has_arg1 = lua_isstring(L, 1) != 0;
- bool has_arg2 = lua_isstring(L, 2) != 0;
- // require at least one string argument
- if (!(has_arg1 || has_arg2))
- luaL_checkstring(L, 1);
- luax_getfunction(L, "graphics", "_shaderCodeToGLSL");
- // push vertexcode and pixelcode strings to the top of the stack
- lua_pushvalue(L, 1);
- lua_pushvalue(L, 2);
- // call effectCodeToGLSL, returned values will be at the top of the stack
- if (lua_pcall(L, 2, 2, 0) != 0)
- return luaL_error(L, "%s", lua_tostring(L, -1));
- Shader::ShaderSource source;
- // vertex shader code
- if (lua_isstring(L, -2))
- source.vertex = luax_checkstring(L, -2);
- else if (has_arg1 && has_arg2)
- return luaL_error(L, "Could not parse vertex shader code (missing 'position' function?)");
- // pixel shader code
- if (lua_isstring(L, -1))
- source.pixel = luax_checkstring(L, -1);
- else if (has_arg1 && has_arg2)
- return luaL_error(L, "Could not parse pixel shader code (missing 'effect' function?)");
- if (source.vertex.empty() && source.pixel.empty())
- {
- // Original args had source code, but effectCodeToGLSL couldn't translate it
- for (int i = 1; i <= 2; i++)
- {
- if (lua_isstring(L, i))
- return luaL_argerror(L, i, "missing 'position' or 'effect' function?");
- }
- }
- bool should_error = false;
- try
- {
- Shader *shader = instance()->newShader(source);
- luax_pushtype(L, GRAPHICS_SHADER_ID, shader);
- shader->release();
- }
- catch (love::Exception &e)
- {
- luax_getfunction(L, "graphics", "_transformGLSLErrorMessages");
- lua_pushstring(L, e.what());
- // Function pushes the new error string onto the stack.
- lua_pcall(L, 1, 1, 0);
- should_error = true;
- }
- if (should_error)
- return lua_error(L);
- return 1;
- }
- static Mesh::Usage luax_optmeshusage(lua_State *L, int idx, Mesh::Usage def)
- {
- const char *usagestr = lua_isnoneornil(L, idx) ? nullptr : luaL_checkstring(L, idx);
- if (usagestr && !Mesh::getConstant(usagestr, def))
- luaL_error(L, "Invalid mesh usage hint: %s", usagestr);
- return def;
- }
- static Mesh::DrawMode luax_optmeshdrawmode(lua_State *L, int idx, Mesh::DrawMode def)
- {
- const char *modestr = lua_isnoneornil(L, idx) ? nullptr : luaL_checkstring(L, idx);
- if (modestr && !Mesh::getConstant(modestr, def))
- luaL_error(L, "Invalid mesh draw mode: %s", modestr);
- return def;
- }
- static Mesh *newStandardMesh(lua_State *L)
- {
- Mesh *t = nullptr;
- Mesh::DrawMode drawmode = luax_optmeshdrawmode(L, 2, Mesh::DRAWMODE_FAN);
- Mesh::Usage usage = luax_optmeshusage(L, 3, Mesh::USAGE_DYNAMIC);
- // First argument is a table of standard vertices, or the number of
- // standard vertices.
- if (lua_istable(L, 1))
- {
- size_t vertexcount = luax_objlen(L, 1);
- std::vector<Vertex> vertices;
- vertices.reserve(vertexcount);
- // Get the vertices from the table.
- for (size_t i = 1; i <= vertexcount; i++)
- {
- lua_rawgeti(L, 1, (int) i);
- if (lua_type(L, -1) != LUA_TTABLE)
- {
- luax_typerror(L, 1, "table of tables");
- return nullptr;
- }
- for (int j = 1; j <= 8; j++)
- lua_rawgeti(L, -j, j);
- Vertex v;
- v.x = (float) luaL_checknumber(L, -8);
- v.y = (float) luaL_checknumber(L, -7);
- v.s = (float) luaL_optnumber(L, -6, 0.0);
- v.t = (float) luaL_optnumber(L, -5, 0.0);
- v.r = (unsigned char) (luaL_optnumber(L, -4, 1.0) * 255.0);
- v.g = (unsigned char) (luaL_optnumber(L, -3, 1.0) * 255.0);
- v.b = (unsigned char) (luaL_optnumber(L, -2, 1.0) * 255.0);
- v.a = (unsigned char) (luaL_optnumber(L, -1, 1.0) * 255.0);
- lua_pop(L, 9);
- vertices.push_back(v);
- }
- luax_catchexcept(L, [&](){ t = instance()->newMesh(vertices, drawmode, usage); });
- }
- else
- {
- int count = (int) luaL_checknumber(L, 1);
- luax_catchexcept(L, [&](){ t = instance()->newMesh(count, drawmode, usage); });
- }
- return t;
- }
- static Mesh *newCustomMesh(lua_State *L)
- {
- Mesh *t = nullptr;
- // First argument is the vertex format, second is a table of vertices or
- // the number of vertices.
- std::vector<Mesh::AttribFormat> vertexformat;
- Mesh::DrawMode drawmode = luax_optmeshdrawmode(L, 3, Mesh::DRAWMODE_FAN);
- Mesh::Usage usage = luax_optmeshusage(L, 4, Mesh::USAGE_DYNAMIC);
- lua_rawgeti(L, 1, 1);
- if (!lua_istable(L, -1))
- {
- luaL_argerror(L, 1, "table of tables expected");
- return nullptr;
- }
- lua_pop(L, 1);
- // Per-vertex attribute formats.
- for (int i = 1; i <= (int) luax_objlen(L, 1); i++)
- {
- lua_rawgeti(L, 1, i);
- // {name, datatype, components}
- for (int j = 1; j <= 3; j++)
- lua_rawgeti(L, -j, j);
- Mesh::AttribFormat format;
- format.name = luaL_checkstring(L, -3);
- const char *tname = luaL_checkstring(L, -2);
- if (!Mesh::getConstant(tname, format.type))
- {
- luaL_error(L, "Invalid Mesh vertex data type name: %s", tname);
- return nullptr;
- }
- format.components = (int) luaL_checknumber(L, -1);
- if (format.components <= 0 || format.components > 4)
- {
- luaL_error(L, "Number of vertex attribute components must be between 1 and 4 (got %d)", format.components);
- return nullptr;
- }
- lua_pop(L, 4);
- vertexformat.push_back(format);
- }
- if (lua_isnumber(L, 2))
- {
- int vertexcount = (int) luaL_checknumber(L, 2);
- luax_catchexcept(L, [&](){ t = instance()->newMesh(vertexformat, vertexcount, drawmode, usage); });
- }
- else if (luax_istype(L, 2, DATA_ID))
- {
- // Vertex data comes directly from a Data object.
- Data *data = luax_checktype<Data>(L, 2, DATA_ID);
- luax_catchexcept(L, [&](){ t = instance()->newMesh(vertexformat, data->getData(), data->getSize(), drawmode, usage); });
- }
- else
- {
- // Table of vertices.
- lua_rawgeti(L, 2, 1);
- if (!lua_istable(L, -1))
- {
- luaL_argerror(L, 2, "expected table of tables");
- return nullptr;
- }
- lua_pop(L, 1);
- int vertexcomponents = 0;
- for (const Mesh::AttribFormat &format : vertexformat)
- vertexcomponents += format.components;
- size_t numvertices = luax_objlen(L, 2);
- luax_catchexcept(L, [&](){ t = instance()->newMesh(vertexformat, numvertices, drawmode, usage); });
- // Maximum possible data size for a single vertex attribute.
- char data[sizeof(float) * 4];
- for (size_t vertindex = 0; vertindex < numvertices; vertindex++)
- {
- // get vertices[vertindex]
- lua_rawgeti(L, 2, vertindex + 1);
- luaL_checktype(L, -1, LUA_TTABLE);
- int n = 0;
- for (size_t i = 0; i < vertexformat.size(); i++)
- {
- int components = vertexformat[i].components;
- // get vertices[vertindex][n]
- for (int c = 0; c < components; c++)
- {
- n++;
- lua_rawgeti(L, -(c + 1), n);
- }
- // Fetch the values from Lua and store them in data buffer.
- luax_writeAttributeData(L, -components, vertexformat[i].type, components, data);
- lua_pop(L, components);
- luax_catchexcept(L,
- [&](){ t->setVertexAttribute(vertindex, i, data, sizeof(float) * 4); },
- [&](bool diderror){ if (diderror) t->release(); }
- );
- }
- lua_pop(L, 1); // pop vertices[vertindex]
- }
- t->flush();
- }
- return t;
- }
- int w_newMesh(lua_State *L)
- {
- luax_checkgraphicscreated(L);
- // Check first argument: table or number of vertices.
- int arg1type = lua_type(L, 1);
- if (arg1type != LUA_TTABLE && arg1type != LUA_TNUMBER)
- luaL_argerror(L, 1, "table or number expected");
- Mesh *t = nullptr;
- int arg2type = lua_type(L, 2);
- if (arg1type == LUA_TTABLE && (arg2type == LUA_TTABLE || arg2type == LUA_TNUMBER || arg2type == LUA_TUSERDATA))
- t = newCustomMesh(L);
- else
- t = newStandardMesh(L);
- luax_pushtype(L, GRAPHICS_MESH_ID, t);
- t->release();
- return 1;
- }
- int w_newText(lua_State *L)
- {
- luax_checkgraphicscreated(L);
- Font *font = luax_checkfont(L, 1);
- Text *t = nullptr;
- if (lua_isnoneornil(L, 2))
- luax_catchexcept(L, [&](){ t = instance()->newText(font); });
- else
- {
- std::vector<Font::ColoredString> text;
- luax_checkcoloredstring(L, 2, text);
- luax_catchexcept(L, [&](){ t = instance()->newText(font, text); });
- }
- luax_pushtype(L, GRAPHICS_TEXT_ID, t);
- t->release();
- return 1;
- }
- int w_newVideo(lua_State *L)
- {
- luax_checkgraphicscreated(L);
- if (!luax_istype(L, 1, VIDEO_VIDEO_STREAM_ID))
- luax_convobj(L, 1, "video", "newVideoStream");
- auto stream = luax_checktype<love::video::VideoStream>(L, 1, VIDEO_VIDEO_STREAM_ID);
- Video *video = nullptr;
- luax_catchexcept(L, [&]() { video = instance()->newVideo(stream); });
- luax_pushtype(L, GRAPHICS_VIDEO_ID, video);
- video->release();
- return 1;
- }
- int w_setColor(lua_State *L)
- {
- Colorf c;
- if (lua_istable(L, 1))
- {
- for (int i = 1; i <= 4; i++)
- lua_rawgeti(L, 1, i);
- c.r = (float) luaL_checknumber(L, -4);
- c.g = (float) luaL_checknumber(L, -3);
- c.b = (float) luaL_checknumber(L, -2);
- c.a = (float) luaL_optnumber(L, -1, 1.0);
- lua_pop(L, 4);
- }
- else
- {
- c.r = (float) luaL_checknumber(L, 1);
- c.g = (float) luaL_checknumber(L, 2);
- c.b = (float) luaL_checknumber(L, 3);
- c.a = (float) luaL_optnumber(L, 4, 1.0);
- }
- instance()->setColor(c);
- return 0;
- }
- int w_getColor(lua_State *L)
- {
- Colorf c = instance()->getColor();
- lua_pushnumber(L, c.r);
- lua_pushnumber(L, c.g);
- lua_pushnumber(L, c.b);
- lua_pushnumber(L, c.a);
- return 4;
- }
- int w_setBackgroundColor(lua_State *L)
- {
- Colorf c;
- if (lua_istable(L, 1))
- {
- for (int i = 1; i <= 4; i++)
- lua_rawgeti(L, 1, i);
- c.r = (float) luaL_checknumber(L, -4);
- c.g = (float) luaL_checknumber(L, -3);
- c.b = (float) luaL_checknumber(L, -2);
- c.a = (float) luaL_optnumber(L, -1, 1.0);
- lua_pop(L, 4);
- }
- else
- {
- c.r = (float) luaL_checknumber(L, 1);
- c.g = (float) luaL_checknumber(L, 2);
- c.b = (float) luaL_checknumber(L, 3);
- c.a = (float) luaL_optnumber(L, 4, 1.0);
- }
- instance()->setBackgroundColor(c);
- return 0;
- }
- int w_getBackgroundColor(lua_State *L)
- {
- Colorf c = instance()->getBackgroundColor();
- lua_pushnumber(L, c.r);
- lua_pushnumber(L, c.g);
- lua_pushnumber(L, c.b);
- lua_pushnumber(L, c.a);
- return 4;
- }
- int w_setNewFont(lua_State *L)
- {
- int ret = w_newFont(L);
- Font *font = luax_checktype<Font>(L, -1, GRAPHICS_FONT_ID);
- instance()->setFont(font);
- return ret;
- }
- int w_setFont(lua_State *L)
- {
- Font *font = luax_checktype<Font>(L, 1, GRAPHICS_FONT_ID);
- instance()->setFont(font);
- return 0;
- }
- int w_getFont(lua_State *L)
- {
- Font *f = nullptr;
- luax_catchexcept(L, [&](){ f = instance()->getFont(); });
- luax_pushtype(L, GRAPHICS_FONT_ID, f);
- return 1;
- }
- int w_setColorMask(lua_State *L)
- {
- Graphics::ColorMask mask;
- if (lua_gettop(L) <= 1 && lua_isnoneornil(L, 1))
- {
- // Enable all color components if no argument is given.
- mask.r = mask.g = mask.b = mask.a = true;
- }
- else
- {
- mask.r = luax_toboolean(L, 1);
- mask.g = luax_toboolean(L, 2);
- mask.b = luax_toboolean(L, 3);
- mask.a = luax_toboolean(L, 4);
- }
- instance()->setColorMask(mask);
- return 0;
- }
- int w_getColorMask(lua_State *L)
- {
- Graphics::ColorMask mask = instance()->getColorMask();
- luax_pushboolean(L, mask.r);
- luax_pushboolean(L, mask.g);
- luax_pushboolean(L, mask.b);
- luax_pushboolean(L, mask.a);
- return 4;
- }
- int w_setBlendMode(lua_State *L)
- {
- Graphics::BlendMode mode;
- const char *str = luaL_checkstring(L, 1);
- if (!Graphics::getConstant(str, mode))
- return luaL_error(L, "Invalid blend mode: %s", str);
- Graphics::BlendAlpha alphamode = Graphics::BLENDALPHA_MULTIPLY;
- if (!lua_isnoneornil(L, 2))
- {
- const char *alphastr = luaL_checkstring(L, 2);
- if (!Graphics::getConstant(alphastr, alphamode))
- return luaL_error(L, "Invalid blend alpha mode: %s", alphastr);
- }
- luax_catchexcept(L, [&](){ instance()->setBlendMode(mode, alphamode); });
- return 0;
- }
- int w_getBlendMode(lua_State *L)
- {
- const char *str;
- const char *alphastr;
- Graphics::BlendAlpha alphamode;
- Graphics::BlendMode mode = instance()->getBlendMode(alphamode);
- if (!Graphics::getConstant(mode, str))
- return luaL_error(L, "Unknown blend mode");
- if (!Graphics::getConstant(alphamode, alphastr))
- return luaL_error(L, "Unknown blend alpha mode");
- lua_pushstring(L, str);
- lua_pushstring(L, alphastr);
- return 2;
- }
- int w_setDefaultFilter(lua_State *L)
- {
- Texture::Filter f;
- const char *minstr = luaL_checkstring(L, 1);
- const char *magstr = luaL_optstring(L, 2, minstr);
- if (!Texture::getConstant(minstr, f.min))
- return luaL_error(L, "Invalid filter mode: %s", minstr);
- if (!Texture::getConstant(magstr, f.mag))
- return luaL_error(L, "Invalid filter mode: %s", magstr);
- f.anisotropy = (float) luaL_optnumber(L, 3, 1.0);
- instance()->setDefaultFilter(f);
- return 0;
- }
- int w_getDefaultFilter(lua_State *L)
- {
- const Texture::Filter &f = instance()->getDefaultFilter();
- const char *minstr;
- const char *magstr;
- if (!Texture::getConstant(f.min, minstr))
- return luaL_error(L, "Unknown minification filter mode");
- if (!Texture::getConstant(f.mag, magstr))
- return luaL_error(L, "Unknown magnification filter mode");
- lua_pushstring(L, minstr);
- lua_pushstring(L, magstr);
- lua_pushnumber(L, f.anisotropy);
- return 3;
- }
- int w_setDefaultMipmapFilter(lua_State *L)
- {
- Texture::FilterMode filter = Texture::FILTER_NONE;
- if (!lua_isnoneornil(L, 1))
- {
- const char *str = luaL_checkstring(L, 1);
- if (!Texture::getConstant(str, filter))
- return luaL_error(L, "Invalid filter mode: %s", str);
- }
- float sharpness = (float) luaL_optnumber(L, 2, 0);
- instance()->setDefaultMipmapFilter(filter, sharpness);
- return 0;
- }
- int w_getDefaultMipmapFilter(lua_State *L)
- {
- Texture::FilterMode filter;
- float sharpness;
- instance()->getDefaultMipmapFilter(&filter, &sharpness);
- const char *str;
- if (Texture::getConstant(filter, str))
- lua_pushstring(L, str);
- else
- lua_pushnil(L);
- lua_pushnumber(L, sharpness);
- return 2;
- }
- int w_setLineWidth(lua_State *L)
- {
- float width = (float)luaL_checknumber(L, 1);
- instance()->setLineWidth(width);
- return 0;
- }
- int w_setLineStyle(lua_State *L)
- {
- Graphics::LineStyle style;
- const char *str = luaL_checkstring(L, 1);
- if (!Graphics::getConstant(str, style))
- return luaL_error(L, "Invalid line style: %s", str);
- instance()->setLineStyle(style);
- return 0;
- }
- int w_setLineJoin(lua_State *L)
- {
- Graphics::LineJoin join;
- const char *str = luaL_checkstring(L, 1);
- if (!Graphics::getConstant(str, join))
- return luaL_error(L, "Invalid line join mode: %s", str);
- instance()->setLineJoin(join);
- return 0;
- }
- int w_getLineWidth(lua_State *L)
- {
- lua_pushnumber(L, instance()->getLineWidth());
- return 1;
- }
- int w_getLineStyle(lua_State *L)
- {
- Graphics::LineStyle style = instance()->getLineStyle();
- const char *str;
- if (!Graphics::getConstant(style, str))
- return luaL_error(L, "Unknown line style");
- lua_pushstring(L, str);
- return 1;
- }
- int w_getLineJoin(lua_State *L)
- {
- Graphics::LineJoin join = instance()->getLineJoin();
- const char *str;
- if (!Graphics::getConstant(join, str))
- return luaL_error(L, "Unknown line join");
- lua_pushstring(L, str);
- return 1;
- }
- int w_setPointSize(lua_State *L)
- {
- float size = (float)luaL_checknumber(L, 1);
- instance()->setPointSize(size);
- return 0;
- }
- int w_getPointSize(lua_State *L)
- {
- lua_pushnumber(L, instance()->getPointSize());
- return 1;
- }
- int w_setWireframe(lua_State *L)
- {
- instance()->setWireframe(luax_toboolean(L, 1));
- return 0;
- }
- int w_isWireframe(lua_State *L)
- {
- luax_pushboolean(L, instance()->isWireframe());
- return 1;
- }
- int w_setShader(lua_State *L)
- {
- if (lua_isnoneornil(L,1))
- {
- instance()->setShader();
- return 0;
- }
- Shader *shader = luax_checkshader(L, 1);
- instance()->setShader(shader);
- return 0;
- }
- int w_getShader(lua_State *L)
- {
- Shader *shader = instance()->getShader();
- if (shader)
- luax_pushtype(L, GRAPHICS_SHADER_ID, shader);
- else
- lua_pushnil(L);
- return 1;
- }
- int w_setDefaultShaderCode(lua_State *L)
- {
- luaL_checktype(L, 1, LUA_TTABLE);
- luaL_checktype(L, 2, LUA_TTABLE);
- for (int i = 0; i < 2; i++)
- {
- for (int renderer = 0; renderer < Graphics::RENDERER_MAX_ENUM; renderer++)
- {
- const char *lang = renderer == Graphics::RENDERER_OPENGLES ? "glsles" : "glsl";
- lua_getfield(L, i + 1, lang);
- lua_getfield(L, -1, "vertex");
- lua_getfield(L, -2, "pixel");
- lua_getfield(L, -3, "videopixel");
- Shader::ShaderSource code;
- code.vertex = luax_checkstring(L, -3);
- code.pixel = luax_checkstring(L, -2);
- Shader::ShaderSource videocode;
- videocode.vertex = luax_checkstring(L, -3);
- videocode.pixel = luax_checkstring(L, -1);
- lua_pop(L, 4);
- Shader::defaultCode[renderer][i] = code;
- Shader::defaultVideoCode[renderer][i] = videocode;
- }
- }
- return 0;
- }
- int w_getSupported(lua_State *L)
- {
- lua_createtable(L, 0, (int) Graphics::FEATURE_MAX_ENUM);
- for (int i = 0; i < (int) Graphics::FEATURE_MAX_ENUM; i++)
- {
- auto feature = (Graphics::Feature) i;
- const char *name = nullptr;
- if (!Graphics::getConstant(feature, name))
- continue;
- luax_pushboolean(L, instance()->isSupported(feature));
- lua_setfield(L, -2, name);
- }
- return 1;
- }
- int w_getCanvasFormats(lua_State *L)
- {
- lua_createtable(L, 0, (int) Canvas::FORMAT_MAX_ENUM);
- for (int i = 0; i < (int) Canvas::FORMAT_MAX_ENUM; i++)
- {
- Canvas::Format format = (Canvas::Format) i;
- const char *name = nullptr;
- if (!Canvas::getConstant(format, name))
- continue;
- luax_pushboolean(L, Canvas::isFormatSupported(format));
- lua_setfield(L, -2, name);
- }
- return 1;
- }
- int w_getRawImageFormats(lua_State *L)
- {
- lua_createtable(L, 0, (int) image::ImageData::FORMAT_MAX_ENUM);
- for (int i = 0; i < (int) image::ImageData::FORMAT_MAX_ENUM; i++)
- {
- auto format = (image::ImageData::Format) i;
- const char *name = nullptr;
- if (!image::ImageData::getConstant(format, name))
- continue;
- luax_pushboolean(L, Image::hasTextureSupport(format));
- lua_setfield(L, -2, name);
- }
- return 1;
- }
- int w_getCompressedImageFormats(lua_State *L)
- {
- lua_createtable(L, 0, (int) image::CompressedImageData::FORMAT_MAX_ENUM);
- for (int i = 0; i < (int) image::CompressedImageData::FORMAT_MAX_ENUM; i++)
- {
- auto format = (image::CompressedImageData::Format) i;
- const char *name = nullptr;
- if (format == image::CompressedImageData::FORMAT_UNKNOWN)
- continue;
- if (!image::CompressedImageData::getConstant(format, name))
- continue;
- luax_pushboolean(L, Image::hasCompressedTextureSupport(format, false));
- lua_setfield(L, -2, name);
- }
- return 1;
- }
- int w_getRendererInfo(lua_State *L)
- {
- Graphics::RendererInfo info;
- luax_catchexcept(L, [&](){ info = instance()->getRendererInfo(); });
- luax_pushstring(L, info.name);
- luax_pushstring(L, info.version);
- luax_pushstring(L, info.vendor);
- luax_pushstring(L, info.device);
- return 4;
- }
- int w_getSystemLimits(lua_State *L)
- {
- lua_createtable(L, 0, (int) Graphics::LIMIT_MAX_ENUM);
- for (int i = 0; i < (int) Graphics::LIMIT_MAX_ENUM; i++)
- {
- Graphics::SystemLimit limittype = (Graphics::SystemLimit) i;
- const char *name = nullptr;
- if (!Graphics::getConstant(limittype, name))
- continue;
- lua_pushnumber(L, instance()->getSystemLimit(limittype));
- lua_setfield(L, -2, name);
- }
- return 1;
- }
- int w_getStats(lua_State *L)
- {
- Graphics::Stats stats = instance()->getStats();
- lua_createtable(L, 0, 7);
- lua_pushinteger(L, stats.drawCalls);
- lua_setfield(L, -2, "drawcalls");
- lua_pushinteger(L, stats.renderPasses);
- lua_setfield(L, -2, "renderpasses");
- lua_pushinteger(L, stats.shaderSwitches);
- lua_setfield(L, -2, "shaderswitches");
- lua_pushinteger(L, stats.canvases);
- lua_setfield(L, -2, "canvases");
- lua_pushinteger(L, stats.images);
- lua_setfield(L, -2, "images");
- lua_pushinteger(L, stats.fonts);
- lua_setfield(L, -2, "fonts");
- lua_pushinteger(L, stats.textureMemory);
- lua_setfield(L, -2, "texturememory");
- return 1;
- }
- int w_draw(lua_State *L)
- {
- Drawable *drawable = nullptr;
- Texture *texture = nullptr;
- Quad *quad = nullptr;
- int startidx = 2;
- if (luax_istype(L, 2, GRAPHICS_QUAD_ID))
- {
- texture = luax_checktexture(L, 1);
- quad = luax_totype<Quad>(L, 2, GRAPHICS_QUAD_ID);
- startidx = 3;
- }
- else if (lua_isnil(L, 2) && !lua_isnoneornil(L, 3))
- {
- return luax_typerror(L, 2, "Quad");
- }
- else
- {
- drawable = luax_checktype<Drawable>(L, 1, GRAPHICS_DRAWABLE_ID);
- startidx = 2;
- }
- if (luax_istype(L, startidx, MATH_TRANSFORM_ID))
- {
- math::Transform *tf = luax_totype<math::Transform>(L, startidx, MATH_TRANSFORM_ID);
- luax_catchexcept(L, [&]() {
- if (texture && quad)
- instance()->drawq(texture, quad, tf->getMatrix());
- else
- instance()->draw(drawable, tf->getMatrix());
- });
- }
- else
- {
- float x = (float) luaL_optnumber(L, startidx + 0, 0.0);
- float y = (float) luaL_optnumber(L, startidx + 1, 0.0);
- float a = (float) luaL_optnumber(L, startidx + 2, 0.0);
- float sx = (float) luaL_optnumber(L, startidx + 3, 1.0);
- float sy = (float) luaL_optnumber(L, startidx + 4, sx);
- float ox = (float) luaL_optnumber(L, startidx + 5, 0.0);
- float oy = (float) luaL_optnumber(L, startidx + 6, 0.0);
- float kx = (float) luaL_optnumber(L, startidx + 7, 0.0);
- float ky = (float) luaL_optnumber(L, startidx + 8, 0.0);
- Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
- luax_catchexcept(L, [&]() {
- if (texture && quad)
- instance()->drawq(texture, quad, m);
- else if (drawable)
- instance()->draw(drawable, m);
- });
- }
- return 0;
- }
- int w_print(lua_State *L)
- {
- std::vector<Font::ColoredString> str;
- luax_checkcoloredstring(L, 1, str);
- if (luax_istype(L, 2, MATH_TRANSFORM_ID))
- {
- math::Transform *tf = luax_totype<math::Transform>(L, 2, MATH_TRANSFORM_ID);
- luax_catchexcept(L, [&](){ instance()->print(str, tf->getMatrix()); });
- }
- else
- {
- float x = (float)luaL_optnumber(L, 2, 0.0);
- float y = (float)luaL_optnumber(L, 3, 0.0);
- float angle = (float)luaL_optnumber(L, 4, 0.0f);
- float sx = (float)luaL_optnumber(L, 5, 1.0f);
- float sy = (float)luaL_optnumber(L, 6, sx);
- float ox = (float)luaL_optnumber(L, 7, 0.0f);
- float oy = (float)luaL_optnumber(L, 8, 0.0f);
- float kx = (float)luaL_optnumber(L, 9, 0.0f);
- float ky = (float)luaL_optnumber(L, 10, 0.0f);
- Matrix4 m(x, y, angle, sx, sy, ox, oy, kx, ky);
- luax_catchexcept(L, [&](){ instance()->print(str, m); });
- }
- return 0;
- }
- int w_printf(lua_State *L)
- {
- std::vector<Font::ColoredString> str;
- luax_checkcoloredstring(L, 1, str);
- Font::AlignMode align = Font::ALIGN_LEFT;
- Matrix4 m;
- int formatidx = 4;
- if (luax_istype(L, 2, MATH_TRANSFORM_ID))
- {
- math::Transform *tf = luax_totype<math::Transform>(L, 2, MATH_TRANSFORM_ID);
- m = tf->getMatrix();
- formatidx = 3;
- }
- else
- {
- float x = (float)luaL_checknumber(L, 2);
- float y = (float)luaL_checknumber(L, 3);
- float angle = (float) luaL_optnumber(L, 6, 0.0f);
- float sx = (float) luaL_optnumber(L, 7, 1.0f);
- float sy = (float) luaL_optnumber(L, 8, sx);
- float ox = (float) luaL_optnumber(L, 9, 0.0f);
- float oy = (float) luaL_optnumber(L, 10, 0.0f);
- float kx = (float) luaL_optnumber(L, 11, 0.0f);
- float ky = (float) luaL_optnumber(L, 12, 0.0f);
- m = Matrix4(x, y, angle, sx, sy, ox, oy, kx, ky);
- }
- float wrap = (float)luaL_checknumber(L, formatidx);
- const char *astr = lua_isnoneornil(L, formatidx + 1) ? nullptr : luaL_checkstring(L, formatidx + 1);
- if (astr != nullptr && !Font::getConstant(astr, align))
- return luaL_error(L, "Incorrect alignment: %s", astr);
- luax_catchexcept(L, [&](){ instance()->printf(str, wrap, align, m); });
- return 0;
- }
- int w_points(lua_State *L)
- {
- // love.graphics.points has 3 variants:
- // - points(x1, y1, x2, y2, ...)
- // - points({x1, y1, x2, y2, ...})
- // - points({{x1, y1 [, r, g, b, a]}, {x2, y2 [, r, g, b, a]}, ...})
- int args = lua_gettop(L);
- bool is_table = false;
- bool is_table_of_tables = false;
- if (args == 1 && lua_istable(L, 1))
- {
- is_table = true;
- args = (int) luax_objlen(L, 1);
- lua_rawgeti(L, 1, 1);
- is_table_of_tables = lua_istable(L, -1);
- lua_pop(L, 1);
- }
- if (args % 2 != 0 && !is_table_of_tables)
- return luaL_error(L, "Number of vertex components must be a multiple of two");
- int numpoints = args / 2;
- if (is_table_of_tables)
- numpoints = args;
- float *coords = nullptr;
- uint8 *colors = nullptr;
- coords = new float[numpoints * 2];
- if (is_table_of_tables)
- colors = new uint8[numpoints * 4];
- if (is_table)
- {
- if (is_table_of_tables)
- {
- // points({{x1, y1 [, r, g, b, a]}, {x2, y2 [, r, g, b, a]}, ...})
- for (int i = 0; i < args; i++)
- {
- lua_rawgeti(L, 1, i + 1);
- for (int j = 1; j <= 6; j++)
- lua_rawgeti(L, -j, j);
- coords[i * 2 + 0] = luax_tofloat(L, -6);
- coords[i * 2 + 1] = luax_tofloat(L, -5);
- colors[i * 4 + 0] = (uint8) (luaL_optnumber(L, -4, 1.0) * 255.0);
- colors[i * 4 + 1] = (uint8) (luaL_optnumber(L, -3, 1.0) * 255.0);
- colors[i * 4 + 2] = (uint8) (luaL_optnumber(L, -2, 1.0) * 255.0);
- colors[i * 4 + 3] = (uint8) (luaL_optnumber(L, -1, 1.0) * 255.0);
- lua_pop(L, 7);
- }
- }
- else
- {
- // points({x1, y1, x2, y2, ...})
- for (int i = 0; i < args; i++)
- {
- lua_rawgeti(L, 1, i + 1);
- coords[i] = luax_tofloat(L, -1);
- lua_pop(L, 1);
- }
- }
- }
- else
- {
- for (int i = 0; i < args; i++)
- coords[i] = luax_tofloat(L, i + 1);
- }
- luax_catchexcept(L,
- [&](){ instance()->points(coords, colors, numpoints); },
- [&](bool) {
- delete[] coords;
- if (colors)
- delete[] colors;
- }
- );
- return 0;
- }
- int w_line(lua_State *L)
- {
- int args = lua_gettop(L);
- bool is_table = false;
- if (args == 1 && lua_istable(L, 1))
- {
- args = (int) luax_objlen(L, 1);
- is_table = true;
- }
- if (args % 2 != 0)
- return luaL_error(L, "Number of vertex components must be a multiple of two");
- else if (args < 4)
- return luaL_error(L, "Need at least two vertices to draw a line");
- float *coords = new float[args];
- if (is_table)
- {
- for (int i = 0; i < args; ++i)
- {
- lua_rawgeti(L, 1, i + 1);
- coords[i] = luax_tofloat(L, -1);
- lua_pop(L, 1);
- }
- }
- else
- {
- for (int i = 0; i < args; ++i)
- coords[i] = luax_tofloat(L, i + 1);
- }
- luax_catchexcept(L,
- [&](){ instance()->polyline(coords, args); },
- [&](bool) { delete[] coords; }
- );
- return 0;
- }
- int w_rectangle(lua_State *L)
- {
- Graphics::DrawMode mode;
- const char *str = luaL_checkstring(L, 1);
- if (!Graphics::getConstant(str, mode))
- return luaL_error(L, "Invalid draw mode: %s", str);
- float x = (float)luaL_checknumber(L, 2);
- float y = (float)luaL_checknumber(L, 3);
- float w = (float)luaL_checknumber(L, 4);
- float h = (float)luaL_checknumber(L, 5);
- if (lua_isnoneornil(L, 6))
- {
- instance()->rectangle(mode, x, y, w, h);
- return 0;
- }
- float rx = (float)luaL_optnumber(L, 6, 0.0);
- float ry = (float)luaL_optnumber(L, 7, rx);
- if (lua_isnoneornil(L, 8))
- luax_catchexcept(L, [&](){ instance()->rectangle(mode, x, y, w, h, rx, ry); });
- else
- {
- int points = (int) luaL_checknumber(L, 8);
- luax_catchexcept(L, [&](){ instance()->rectangle(mode, x, y, w, h, rx, ry, points); });
- }
- return 0;
- }
- int w_circle(lua_State *L)
- {
- Graphics::DrawMode mode;
- const char *str = luaL_checkstring(L, 1);
- if (!Graphics::getConstant(str, mode))
- return luaL_error(L, "Invalid draw mode: %s", str);
- float x = (float)luaL_checknumber(L, 2);
- float y = (float)luaL_checknumber(L, 3);
- float radius = (float)luaL_checknumber(L, 4);
- if (lua_isnoneornil(L, 5))
- luax_catchexcept(L, [&](){ instance()->circle(mode, x, y, radius); });
- else
- {
- int points = (int) luaL_checknumber(L, 5);
- luax_catchexcept(L, [&](){ instance()->circle(mode, x, y, radius, points); });
- }
- return 0;
- }
- int w_ellipse(lua_State *L)
- {
- Graphics::DrawMode mode;
- const char *str = luaL_checkstring(L, 1);
- if (!Graphics::getConstant(str, mode))
- return luaL_error(L, "Invalid draw mode: %s", str);
- float x = (float)luaL_checknumber(L, 2);
- float y = (float)luaL_checknumber(L, 3);
- float a = (float)luaL_checknumber(L, 4);
- float b = (float)luaL_optnumber(L, 5, a);
- if (lua_isnoneornil(L, 6))
- luax_catchexcept(L, [&](){ instance()->ellipse(mode, x, y, a, b); });
- else
- {
- int points = (int) luaL_checknumber(L, 6);
- luax_catchexcept(L, [&](){ instance()->ellipse(mode, x, y, a, b, points); });
- }
- return 0;
- }
- int w_arc(lua_State *L)
- {
- Graphics::DrawMode drawmode;
- const char *drawstr = luaL_checkstring(L, 1);
- if (!Graphics::getConstant(drawstr, drawmode))
- return luaL_error(L, "Invalid draw mode: %s", drawstr);
- int startidx = 2;
- Graphics::ArcMode arcmode = Graphics::ARC_PIE;
- if (lua_type(L, 2) == LUA_TSTRING)
- {
- const char *arcstr = luaL_checkstring(L, 2);
- if (!Graphics::getConstant(arcstr, arcmode))
- return luaL_error(L, "Invalid arc mode: %s", arcstr);
- startidx = 3;
- }
- float x = (float) luaL_checknumber(L, startidx + 0);
- float y = (float) luaL_checknumber(L, startidx + 1);
- float radius = (float) luaL_checknumber(L, startidx + 2);
- float angle1 = (float) luaL_checknumber(L, startidx + 3);
- float angle2 = (float) luaL_checknumber(L, startidx + 4);
- if (lua_isnoneornil(L, startidx + 5))
- luax_catchexcept(L, [&](){ instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2); });
- else
- {
- int points = (int) luaL_checknumber(L, startidx + 5);
- luax_catchexcept(L, [&](){ instance()->arc(drawmode, arcmode, x, y, radius, angle1, angle2, points); });
- }
- return 0;
- }
- int w_polygon(lua_State *L)
- {
- int args = lua_gettop(L) - 1;
- Graphics::DrawMode mode;
- const char *str = luaL_checkstring(L, 1);
- if (!Graphics::getConstant(str, mode))
- return luaL_error(L, "Invalid draw mode: %s", str);
- bool is_table = false;
- float *coords;
- if (args == 1 && lua_istable(L, 2))
- {
- args = (int) luax_objlen(L, 2);
- is_table = true;
- }
- if (args % 2 != 0)
- return luaL_error(L, "Number of vertex components must be a multiple of two");
- else if (args < 6)
- return luaL_error(L, "Need at least three vertices to draw a polygon");
- // fetch coords
- coords = new float[args + 2];
- if (is_table)
- {
- for (int i = 0; i < args; ++i)
- {
- lua_rawgeti(L, 2, i + 1);
- coords[i] = luax_tofloat(L, -1);
- lua_pop(L, 1);
- }
- }
- else
- {
- for (int i = 0; i < args; ++i)
- coords[i] = luax_tofloat(L, i + 2);
- }
- // make a closed loop
- coords[args] = coords[0];
- coords[args+1] = coords[1];
- luax_catchexcept(L,
- [&](){ instance()->polygon(mode, coords, args+2); },
- [&](bool) { delete[] coords; }
- );
- return 0;
- }
- int w_push(lua_State *L)
- {
- Graphics::StackType stype = Graphics::STACK_TRANSFORM;
- const char *sname = lua_isnoneornil(L, 1) ? nullptr : luaL_checkstring(L, 1);
- if (sname && !Graphics::getConstant(sname, stype))
- return luaL_error(L, "Invalid graphics stack type: %s", sname);
- luax_catchexcept(L, [&](){ instance()->push(stype); });
- if (luax_istype(L, 2, MATH_TRANSFORM_ID))
- {
- math::Transform *t = luax_totype<math::Transform>(L, 2, MATH_TRANSFORM_ID);
- instance()->applyTransform(t);
- }
- return 0;
- }
- int w_pop(lua_State *L)
- {
- luax_catchexcept(L, [&](){ instance()->pop(); });
- return 0;
- }
- int w_rotate(lua_State *L)
- {
- float angle = (float)luaL_checknumber(L, 1);
- instance()->rotate(angle);
- return 0;
- }
- int w_scale(lua_State *L)
- {
- float sx = (float)luaL_optnumber(L, 1, 1.0f);
- float sy = (float)luaL_optnumber(L, 2, sx);
- instance()->scale(sx, sy);
- return 0;
- }
- int w_translate(lua_State *L)
- {
- float x = (float)luaL_checknumber(L, 1);
- float y = (float)luaL_checknumber(L, 2);
- instance()->translate(x, y);
- return 0;
- }
- int w_shear(lua_State *L)
- {
- float kx = (float)luaL_checknumber(L, 1);
- float ky = (float)luaL_checknumber(L, 2);
- instance()->shear(kx, ky);
- return 0;
- }
- int w_origin(lua_State * /*L*/)
- {
- instance()->origin();
- return 0;
- }
- int w_applyTransform(lua_State *L)
- {
- math::Transform *t = math::luax_checktransform(L, 1);
- instance()->applyTransform(t);
- return 0;
- }
- int w_replaceTransform(lua_State *L)
- {
- math::Transform *t = math::luax_checktransform(L, 1);
- instance()->replaceTransform(t);
- return 0;
- }
- int w_transformPoint(lua_State *L)
- {
- Vector p;
- p.x = (float) luaL_checknumber(L, 1);
- p.y = (float) luaL_checknumber(L, 2);
- p = instance()->transformPoint(p);
- lua_pushnumber(L, p.x);
- lua_pushnumber(L, p.y);
- return 2;
- }
- int w_inverseTransformPoint(lua_State *L)
- {
- Vector p;
- p.x = (float) luaL_checknumber(L, 1);
- p.y = (float) luaL_checknumber(L, 2);
- p = instance()->inverseTransformPoint(p);
- lua_pushnumber(L, p.x);
- lua_pushnumber(L, p.y);
- return 2;
- }
- // List of functions to wrap.
- static const luaL_Reg functions[] =
- {
- { "reset", w_reset },
- { "present", w_present },
- { "newImage", w_newImage },
- { "newQuad", w_newQuad },
- { "newFont", w_newFont },
- { "newImageFont", w_newImageFont },
- { "newSpriteBatch", w_newSpriteBatch },
- { "newParticleSystem", w_newParticleSystem },
- { "newCanvas", w_newCanvas },
- { "newShader", w_newShader },
- { "newMesh", w_newMesh },
- { "newText", w_newText },
- { "_newVideo", w_newVideo },
- { "setColor", w_setColor },
- { "getColor", w_getColor },
- { "setBackgroundColor", w_setBackgroundColor },
- { "getBackgroundColor", w_getBackgroundColor },
- { "setNewFont", w_setNewFont },
- { "setFont", w_setFont },
- { "getFont", w_getFont },
- { "setColorMask", w_setColorMask },
- { "getColorMask", w_getColorMask },
- { "setBlendMode", w_setBlendMode },
- { "getBlendMode", w_getBlendMode },
- { "setDefaultFilter", w_setDefaultFilter },
- { "getDefaultFilter", w_getDefaultFilter },
- { "setDefaultMipmapFilter", w_setDefaultMipmapFilter },
- { "getDefaultMipmapFilter", w_getDefaultMipmapFilter },
- { "setLineWidth", w_setLineWidth },
- { "setLineStyle", w_setLineStyle },
- { "setLineJoin", w_setLineJoin },
- { "getLineWidth", w_getLineWidth },
- { "getLineStyle", w_getLineStyle },
- { "getLineJoin", w_getLineJoin },
- { "setPointSize", w_setPointSize },
- { "getPointSize", w_getPointSize },
- { "setWireframe", w_setWireframe },
- { "isWireframe", w_isWireframe },
- { "setShader", w_setShader },
- { "getShader", w_getShader },
- { "_setDefaultShaderCode", w_setDefaultShaderCode },
- { "getSupported", w_getSupported },
- { "getCanvasFormats", w_getCanvasFormats },
- { "getRawImageFormats", w_getRawImageFormats },
- { "getCompressedImageFormats", w_getCompressedImageFormats },
- { "getRendererInfo", w_getRendererInfo },
- { "getSystemLimits", w_getSystemLimits },
- { "getStats", w_getStats },
- { "beginPass", w_beginPass },
- { "endPass", w_endPass },
- { "renderPass", w_renderPass },
- { "isPassActive", w_isPassActive },
- { "getPassCanvases", w_getPassCanvases },
- { "captureScreenshot", w_captureScreenshot },
- { "draw", w_draw },
- { "print", w_print },
- { "printf", w_printf },
- { "isCreated", w_isCreated },
- { "isActive", w_isActive },
- { "isGammaCorrect", w_isGammaCorrect },
- { "getWidth", w_getWidth },
- { "getHeight", w_getHeight },
- { "getDimensions", w_getDimensions },
- { "getPassWidth", w_getPassWidth },
- { "getPassHeight", w_getPassHeight },
- { "getPassDimensions", w_getPassDimensions },
- { "setScissor", w_setScissor },
- { "intersectScissor", w_intersectScissor },
- { "getScissor", w_getScissor },
- { "stencil", w_stencil },
- { "setStencilTest", w_setStencilTest },
- { "getStencilTest", w_getStencilTest },
- { "points", w_points },
- { "line", w_line },
- { "rectangle", w_rectangle },
- { "circle", w_circle },
- { "ellipse", w_ellipse },
- { "arc", w_arc },
- { "polygon", w_polygon },
- { "push", w_push },
- { "pop", w_pop },
- { "rotate", w_rotate },
- { "scale", w_scale },
- { "translate", w_translate },
- { "shear", w_shear },
- { "origin", w_origin },
- { "applyTransform", w_applyTransform },
- { "replaceTransform", w_replaceTransform },
- { "transformPoint", w_transformPoint },
- { "inverseTransformPoint", w_inverseTransformPoint },
- { 0, 0 }
- };
- static int luaopen_drawable(lua_State *L)
- {
- return luax_register_type(L, GRAPHICS_DRAWABLE_ID, "Drawable", nullptr);
- }
- // Types for this module.
- static const lua_CFunction types[] =
- {
- luaopen_drawable,
- luaopen_texture,
- luaopen_font,
- luaopen_image,
- luaopen_quad,
- luaopen_spritebatch,
- luaopen_particlesystem,
- luaopen_canvas,
- luaopen_shader,
- luaopen_mesh,
- luaopen_text,
- luaopen_video,
- 0
- };
- extern "C" int luaopen_love_graphics(lua_State *L)
- {
- Graphics *instance = instance();
- if (instance == nullptr)
- {
- luax_catchexcept(L, [&](){ instance = new Graphics(); });
- }
- else
- instance->retain();
- WrappedModule w;
- w.module = instance;
- w.name = "graphics";
- w.type = MODULE_GRAPHICS_ID;
- w.functions = functions;
- w.types = types;
- int n = luax_register_module(L, w);
- if (luaL_loadbuffer(L, (const char *)graphics_lua, sizeof(graphics_lua), "wrap_Graphics.lua") == 0)
- lua_call(L, 0, 0);
- return n;
- }
- } // opengl
- } // graphics
- } // love
|