From 61a5f0c7e2da9df945bbd90c56497a9ba1cce866 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Tue, 4 Feb 2020 20:05:31 -0400 Subject: [PATCH] Move Canvas:renderTo to Texture:renderTo. --- src/modules/graphics/wrap_Canvas.cpp | 48 ---------------------- src/modules/graphics/wrap_Texture.cpp | 59 +++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 48 deletions(-) diff --git a/src/modules/graphics/wrap_Canvas.cpp b/src/modules/graphics/wrap_Canvas.cpp index 167db507a..5aad53644 100644 --- a/src/modules/graphics/wrap_Canvas.cpp +++ b/src/modules/graphics/wrap_Canvas.cpp @@ -31,56 +31,8 @@ Canvas *luax_checkcanvas(lua_State *L, int idx) return luax_checktype(L, idx); } -int w_Canvas_renderTo(lua_State *L) -{ - Graphics::RenderTarget rt(luax_checkcanvas(L, 1)); - - int startidx = 2; - - if (rt.texture->getTextureType() != TEXTURE_2D) - { - rt.slice = (int) luaL_checkinteger(L, 2) - 1; - startidx++; - } - - luaL_checktype(L, startidx, LUA_TFUNCTION); - - auto graphics = Module::getInstance(Module::M_GRAPHICS); - - if (graphics) - { - // Save the current render targets so we can restore them when we're done. - Graphics::RenderTargets oldtargets = graphics->getCanvas(); - - for (auto c : oldtargets.colors) - c.texture->retain(); - - if (oldtargets.depthStencil.texture != nullptr) - oldtargets.depthStencil.texture->retain(); - - luax_catchexcept(L, [&](){ graphics->setCanvas(rt, false); }); - - lua_settop(L, 2); // make sure the function is on top of the stack - int status = lua_pcall(L, 0, 0, 0); - - graphics->setCanvas(oldtargets); - - for (auto c : oldtargets.colors) - c.texture->release(); - - if (oldtargets.depthStencil.texture != nullptr) - oldtargets.depthStencil.texture->release(); - - if (status != 0) - return lua_error(L); - } - - return 0; -} - static const luaL_Reg w_Canvas_functions[] = { - { "renderTo", w_Canvas_renderTo }, { 0, 0 } }; diff --git a/src/modules/graphics/wrap_Texture.cpp b/src/modules/graphics/wrap_Texture.cpp index a216a1139..a27b55c54 100644 --- a/src/modules/graphics/wrap_Texture.cpp +++ b/src/modules/graphics/wrap_Texture.cpp @@ -19,6 +19,7 @@ **/ #include "wrap_Texture.h" +#include "Graphics.h" namespace love { @@ -398,6 +399,63 @@ int w_Texture_newImageData(lua_State *L) return 1; } +int w_Texture_renderTo(lua_State *L) +{ + Graphics::RenderTarget rt(luax_checktexture(L, 1)); + + int startidx = 2; + + if (rt.texture->getTextureType() != TEXTURE_2D) + { + rt.slice = (int) luaL_checkinteger(L, 2) - 1; + startidx++; + } + + luaL_checktype(L, startidx, LUA_TFUNCTION); + + auto graphics = Module::getInstance(Module::M_GRAPHICS); + + if (graphics) + { + // Save the current render targets so we can restore them when we're done. + Graphics::RenderTargets oldtargets = graphics->getCanvas(); + + for (auto c : oldtargets.colors) + c.texture->retain(); + + if (oldtargets.depthStencil.texture != nullptr) + oldtargets.depthStencil.texture->retain(); + + luax_catchexcept(L, + [&]() { graphics->setCanvas(rt, 0); }, + [&](bool err) + { + if (err) + { + for (auto c : oldtargets.colors) + c.texture->release(); + } + } + ); + + lua_settop(L, 2); // make sure the function is on top of the stack + int status = lua_pcall(L, 0, 0, 0); + + graphics->setCanvas(oldtargets); + + for (auto c : oldtargets.colors) + c.texture->release(); + + if (oldtargets.depthStencil.texture != nullptr) + oldtargets.depthStencil.texture->release(); + + if (status != 0) + return lua_error(L); + } + + return 0; +} + const luaL_Reg w_Texture_functions[] = { { "getTextureType", w_Texture_getTextureType }, @@ -428,6 +486,7 @@ const luaL_Reg w_Texture_functions[] = { "generateMipmaps", w_Texture_generateMipmaps }, { "replacePixels", w_Texture_replacePixels }, { "newImageData", w_Texture_newImageData }, + { "renderTo", w_Texture_renderTo }, { 0, 0 } };