mirror of
https://github.com/love2d/love.git
synced 2026-08-16 16:20:42 +02:00
Added the getString method to all Data objects (issue #608)
This commit is contained in:
@@ -50,8 +50,8 @@ int w_FileData_getExtension(lua_State *L)
|
||||
|
||||
static const luaL_Reg w_FileData_functions[] =
|
||||
{
|
||||
|
||||
// Data
|
||||
{ "getString", w_Data_getString },
|
||||
{ "getSize", w_Data_getSize },
|
||||
|
||||
{ "getFilename", w_FileData_getFilename },
|
||||
|
||||
@@ -31,6 +31,62 @@ CompressedData *luax_checkcompresseddata(lua_State *L, int idx)
|
||||
return luax_checktype<CompressedData>(L, idx, "CompressedData", IMAGE_COMPRESSED_DATA_T);
|
||||
}
|
||||
|
||||
int w_CompressedData_getString(lua_State *L)
|
||||
{
|
||||
CompressedData *t = luax_checkcompresseddata(L, 1);
|
||||
|
||||
// CompressedData's data isn't contiguous in memory, so we need to copy it
|
||||
// all to a single block before sending the string.
|
||||
|
||||
size_t totalsize = 0;
|
||||
|
||||
for (int i = 0; i < t->getNumMipmaps(); i++)
|
||||
totalsize += t->getSize(i);
|
||||
|
||||
if (totalsize == 0)
|
||||
{
|
||||
lua_pushstring(L, "");
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *datastr = 0;
|
||||
try
|
||||
{
|
||||
datastr = new char[totalsize];
|
||||
}
|
||||
catch (std::exception &)
|
||||
{
|
||||
return luaL_error(L, "Out of memory.");
|
||||
}
|
||||
|
||||
size_t curpos = 0;
|
||||
for (int i = 0; i < t->getNumMipmaps(); i++)
|
||||
{
|
||||
memcpy(&datastr[curpos], t->getData(i), t->getSize(i));
|
||||
curpos += t->getSize(i);
|
||||
}
|
||||
|
||||
lua_pushlstring(L, datastr, totalsize);
|
||||
|
||||
delete[] datastr;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_CompressedData_getSize(lua_State *L)
|
||||
{
|
||||
CompressedData *t = luax_checkcompresseddata(L, 1);
|
||||
|
||||
size_t totalsize = 0;
|
||||
|
||||
for (int i = 0; i < t->getNumMipmaps(); i++)
|
||||
totalsize += t->getSize(i);
|
||||
|
||||
lua_pushnumber(L, (lua_Number) totalsize);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_CompressedData_getWidth(lua_State *L)
|
||||
{
|
||||
CompressedData *t = luax_checkcompresseddata(L, 1);
|
||||
@@ -109,7 +165,9 @@ int w_CompressedData_getType(lua_State *L)
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
// Data
|
||||
{ "getSize", w_Data_getSize },
|
||||
// CompressedData's data is not in contiguous memory, so it needs custom functions.
|
||||
{ "getString", w_CompressedData_getString },
|
||||
{ "getSize", w_CompressedData_getSize },
|
||||
|
||||
{ "getWidth", w_CompressedData_getWidth },
|
||||
{ "getHeight", w_CompressedData_getHeight },
|
||||
|
||||
@@ -31,6 +31,8 @@ namespace image
|
||||
{
|
||||
|
||||
CompressedData *luax_checkcompresseddata(lua_State *L, int idx);
|
||||
int w_CompressedData_getString(lua_State *L);
|
||||
int w_CompressedData_getSize(lua_State *L);
|
||||
int w_CompressedData_getWidth(lua_State *L);
|
||||
int w_CompressedData_getHeight(lua_State *L);
|
||||
int w_CompressedData_getDimensions(lua_State *L);
|
||||
|
||||
@@ -131,13 +131,6 @@ int w_ImageData_mapPixel(lua_State *L)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_ImageData_getString(lua_State *L)
|
||||
{
|
||||
ImageData *t = luax_checkimagedata(L, 1);
|
||||
lua_pushlstring(L, (const char *)t->getData(), t->getSize());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_ImageData_paste(lua_State *L)
|
||||
{
|
||||
ImageData *t = luax_checkimagedata(L, 1);
|
||||
@@ -191,6 +184,7 @@ int w_ImageData_encode(lua_State *L)
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
// Data
|
||||
{ "getString", w_Data_getString },
|
||||
{ "getSize", w_Data_getSize },
|
||||
|
||||
{ "getWidth", w_ImageData_getWidth },
|
||||
@@ -199,7 +193,6 @@ static const luaL_Reg functions[] =
|
||||
{ "getPixel", w_ImageData_getPixel },
|
||||
{ "setPixel", w_ImageData_setPixel },
|
||||
{ "mapPixel", w_ImageData_mapPixel },
|
||||
{ "getString", w_ImageData_getString },
|
||||
{ "paste", w_ImageData_paste },
|
||||
{ "encode", w_ImageData_encode },
|
||||
{ 0, 0 }
|
||||
|
||||
@@ -37,7 +37,6 @@ int w_ImageData_getDimensions(lua_State *L);
|
||||
int w_ImageData_getPixel(lua_State *L);
|
||||
int w_ImageData_setPixel(lua_State *L);
|
||||
int w_ImageData_mapPixel(lua_State *L);
|
||||
int w_ImageData_getString(lua_State *L);
|
||||
int w_ImageData_paste(lua_State *L);
|
||||
int w_ImageData_encode(lua_State *L);
|
||||
extern "C" int luaopen_imagedata(lua_State *L);
|
||||
|
||||
@@ -86,8 +86,8 @@ int w_SoundData_getSample(lua_State *L)
|
||||
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
|
||||
// Data
|
||||
{ "getString", w_Data_getString },
|
||||
{ "getSize", w_Data_getSize },
|
||||
|
||||
{ "getChannels", w_SoundData_getChannels },
|
||||
|
||||
Reference in New Issue
Block a user