Added a variant of love.graphics.captureScreenshot which takes a single filename parameter. Resolves issue #1293.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2017-07-29 12:54:53 -03:00
parent 2c80cce00b
commit 5ef6730497
6 changed files with 76 additions and 16 deletions
+46
View File
@@ -439,8 +439,54 @@ static void screenshotCallback(love::image::ImageData *i, Reference *ref, void *
delete ref;
}
static int screenshotSaveToFile(lua_State *L)
{
image::ImageData *id = image::luax_checkimagedata(L, 1);
const char *filename = luaL_checkstring(L, lua_upvalueindex(1));
const char *ext = luaL_checkstring(L, lua_upvalueindex(2));
image::ImageData::EncodedFormat format;
if (!image::ImageData::getConstant(ext, format))
return 0;
try
{
id->encode(format, filename, true);
}
catch (love::Exception &e)
{
printf("Screenshot encoding or saving failed: %s", e.what());
// Do nothing...
}
return 0;
}
int w_captureScreenshot(lua_State *L)
{
if (lua_isstring(L, 1))
{
std::string filename = luax_checkstring(L, 1);
std::string ext;
size_t dotpos = filename.rfind('.');
if (dotpos != std::string::npos)
ext = filename.substr(dotpos + 1);
std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
image::ImageData::EncodedFormat format;
if (!image::ImageData::getConstant(ext.c_str(), format))
return luaL_error(L, "Invalid encoded image format: %s", ext.c_str());
lua_pushvalue(L, 1);
luax_pushstring(L, ext);
lua_pushcclosure(L, screenshotSaveToFile, 2);
lua_replace(L, 1);
}
luaL_checktype(L, 1, LUA_TFUNCTION);
Graphics::ScreenshotInfo info;