Browse Source

Added the getString method to all Data objects (issue #608)

Alex Szpakowski 12 years ago
parent
commit
fb9f8d2962

+ 8 - 0
src/common/wrap_Data.cpp

@@ -28,6 +28,13 @@ Data *luax_checkdata(lua_State *L, int idx)
 	return luax_checktype<Data>(L, idx, "Data", DATA_T);
 	return luax_checktype<Data>(L, idx, "Data", DATA_T);
 }
 }
 
 
+int w_Data_getString(lua_State *L)
+{
+	Data *t = luax_checkdata(L, 1);
+	lua_pushlstring(L, (const char *) t->getData(), (size_t) t->getSize());
+	return 1;
+}
+
 int w_Data_getSize(lua_State *L)
 int w_Data_getSize(lua_State *L)
 {
 {
 	Data *t = luax_checkdata(L, 1);
 	Data *t = luax_checkdata(L, 1);
@@ -37,6 +44,7 @@ int w_Data_getSize(lua_State *L)
 
 
 const luaL_Reg w_Data_functions[] =
 const luaL_Reg w_Data_functions[] =
 {
 {
+	{ "getString", w_Data_getString },
 	{ "getSize", w_Data_getSize },
 	{ "getSize", w_Data_getSize },
 	{ 0, 0 }
 	{ 0, 0 }
 };
 };

+ 1 - 0
src/common/wrap_Data.h

@@ -29,6 +29,7 @@ namespace love
 {
 {
 
 
 Data *luax_checkdata(lua_State *L, int idx);
 Data *luax_checkdata(lua_State *L, int idx);
+int w_Data_getString(lua_State *L);
 int w_Data_getSize(lua_State *L);
 int w_Data_getSize(lua_State *L);
 int w_Data_open(lua_State *L);
 int w_Data_open(lua_State *L);
 
 

+ 1 - 1
src/modules/filesystem/physfs/wrap_FileData.cpp

@@ -50,8 +50,8 @@ int w_FileData_getExtension(lua_State *L)
 
 
 static const luaL_Reg w_FileData_functions[] =
 static const luaL_Reg w_FileData_functions[] =
 {
 {
-
 	// Data
 	// Data
+	{ "getString", w_Data_getString },
 	{ "getSize", w_Data_getSize },
 	{ "getSize", w_Data_getSize },
 
 
 	{ "getFilename", w_FileData_getFilename },
 	{ "getFilename", w_FileData_getFilename },

+ 59 - 1
src/modules/image/wrap_CompressedData.cpp

@@ -31,6 +31,62 @@ CompressedData *luax_checkcompresseddata(lua_State *L, int idx)
 	return luax_checktype<CompressedData>(L, idx, "CompressedData", IMAGE_COMPRESSED_DATA_T);
 	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)
 int w_CompressedData_getWidth(lua_State *L)
 {
 {
 	CompressedData *t = luax_checkcompresseddata(L, 1);
 	CompressedData *t = luax_checkcompresseddata(L, 1);
@@ -109,7 +165,9 @@ int w_CompressedData_getType(lua_State *L)
 static const luaL_Reg functions[] =
 static const luaL_Reg functions[] =
 {
 {
 	// Data
 	// 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 },
 	{ "getWidth", w_CompressedData_getWidth },
 	{ "getHeight", w_CompressedData_getHeight },
 	{ "getHeight", w_CompressedData_getHeight },

+ 2 - 0
src/modules/image/wrap_CompressedData.h

@@ -31,6 +31,8 @@ namespace image
 {
 {
 
 
 CompressedData *luax_checkcompresseddata(lua_State *L, int idx);
 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_getWidth(lua_State *L);
 int w_CompressedData_getHeight(lua_State *L);
 int w_CompressedData_getHeight(lua_State *L);
 int w_CompressedData_getDimensions(lua_State *L);
 int w_CompressedData_getDimensions(lua_State *L);

+ 1 - 8
src/modules/image/wrap_ImageData.cpp

@@ -131,13 +131,6 @@ int w_ImageData_mapPixel(lua_State *L)
 	return 0;
 	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)
 int w_ImageData_paste(lua_State *L)
 {
 {
 	ImageData *t = luax_checkimagedata(L, 1);
 	ImageData *t = luax_checkimagedata(L, 1);
@@ -191,6 +184,7 @@ int w_ImageData_encode(lua_State *L)
 static const luaL_Reg functions[] =
 static const luaL_Reg functions[] =
 {
 {
 	// Data
 	// Data
+	{ "getString", w_Data_getString },
 	{ "getSize", w_Data_getSize },
 	{ "getSize", w_Data_getSize },
 
 
 	{ "getWidth", w_ImageData_getWidth },
 	{ "getWidth", w_ImageData_getWidth },
@@ -199,7 +193,6 @@ static const luaL_Reg functions[] =
 	{ "getPixel", w_ImageData_getPixel },
 	{ "getPixel", w_ImageData_getPixel },
 	{ "setPixel", w_ImageData_setPixel },
 	{ "setPixel", w_ImageData_setPixel },
 	{ "mapPixel", w_ImageData_mapPixel },
 	{ "mapPixel", w_ImageData_mapPixel },
-	{ "getString", w_ImageData_getString },
 	{ "paste", w_ImageData_paste },
 	{ "paste", w_ImageData_paste },
 	{ "encode", w_ImageData_encode },
 	{ "encode", w_ImageData_encode },
 	{ 0, 0 }
 	{ 0, 0 }

+ 0 - 1
src/modules/image/wrap_ImageData.h

@@ -37,7 +37,6 @@ int w_ImageData_getDimensions(lua_State *L);
 int w_ImageData_getPixel(lua_State *L);
 int w_ImageData_getPixel(lua_State *L);
 int w_ImageData_setPixel(lua_State *L);
 int w_ImageData_setPixel(lua_State *L);
 int w_ImageData_mapPixel(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_paste(lua_State *L);
 int w_ImageData_encode(lua_State *L);
 int w_ImageData_encode(lua_State *L);
 extern "C" int luaopen_imagedata(lua_State *L);
 extern "C" int luaopen_imagedata(lua_State *L);

+ 1 - 1
src/modules/sound/wrap_SoundData.cpp

@@ -86,8 +86,8 @@ int w_SoundData_getSample(lua_State *L)
 
 
 static const luaL_Reg functions[] =
 static const luaL_Reg functions[] =
 {
 {
-
 	// Data
 	// Data
+	{ "getString", w_Data_getString },
 	{ "getSize", w_Data_getSize },
 	{ "getSize", w_Data_getSize },
 
 
 	{ "getChannels", w_SoundData_getChannels },
 	{ "getChannels", w_SoundData_getChannels },