Page 1 of 1

Compile Love for ImageData getPointer shortcut

Posted: Sun May 11, 2014 2:44 am
by JuniperBunny
Hi, I tried all day yesterday to compile Love so I could add this one function, but I have bad luck with other people's code. I just need to bypass having to store ImageData in Lua and go straight to the pointer, and I was wondering if someone who already knows how to compile Love could do it for me real quick. This code might be incorrect, I don't know how the Lua interface works, but something like this. I'm hoping it's not a big deal, and I would be thankful to have it

Code: Select all

int w_Canvas_getImageDataPointer(lua_State *L)
{
	Canvas *canvas = luax_checkcanvas(L, 1);
	love::image::Image *image = luax_getmodule<love::image::Image>(L, "image", MODULE_IMAGE_T);
	love::image::ImageData *img = canvas->getImageData(image);
	
	//Data *t = luax_checkdata(L, 1);
	lua_pushlightuserdata(L, img->getData());
	return 1;
}

Re: Compile Love for ImageData getPointer shortcut

Posted: Sun May 11, 2014 2:52 am
by slime
Canvas:getImageData basically takes a screenshot of the canvas and stores the screenshot in a new ImageData - the new ImageData needs to be stored somewhere that will properly deallocate the data when it's no longer referenced anywhere (normally Lua will take care of that for you.)

If the new ImageData is created (via canvas->getImageData) without making sure it will eventually be cleaned up properly, then you'll have a memory leak.
Additionally, if the pointer to the ImageData's memory is accessed after the ImageData is deallocated, bad / unexpected things will happen.

Re: Compile Love for ImageData getPointer shortcut

Posted: Sun May 11, 2014 3:00 am
by JuniperBunny
I don't want to create the data in Lua at all, and I thought img would be destroyed in the C code when it leaves scope

I thought there would be a constant address where the data for the canvas is stored, not something that has to be created over and over