Updated CompressedData to store its data in a single block of memory, like other Data objects

This commit is contained in:
Alex Szpakowski
2013-08-29 16:24:13 -03:00
parent 6b51c5aadc
commit 4c4a385ce6
8 changed files with 80 additions and 116 deletions
+2 -59
View File
@@ -31,62 +31,6 @@ 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->getMipmapCount(); 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->getMipmapCount(); 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->getMipmapCount(); 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);
@@ -165,9 +109,8 @@ int w_CompressedData_getFormat(lua_State *L)
static const luaL_Reg functions[] =
{
// Data
// CompressedData's data is not in contiguous memory, so it needs custom functions.
{ "getString", w_CompressedData_getString },
{ "getSize", w_CompressedData_getSize },
{ "getString", w_Data_getString },
{ "getSize", w_Data_getSize },
{ "getWidth", w_CompressedData_getWidth },
{ "getHeight", w_CompressedData_getHeight },