How do I create a memory copy of an image and of an image-part? [RESOLVED]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Rastashmup
Prole
Posts: 31
Joined: Mon Jun 26, 2017 10:36 am

How do I create a memory copy of an image and of an image-part? [RESOLVED]

Post by Rastashmup »

Hello

I have been searching the documentation in the wiki for ways to create a copy of an image, but that has not been successful. I found a clone() function in the section ImageData, but it is referring to Data:clone() rather than ImageData:clone().How do you read that? Is Data the base class of ImageData? I tried the following, by working on the ImageData,but it doesn't work:

Code: Select all

backGroundGph    = love.graphics.newImage("gfx/Levels/Background.png")

dat = backGroundGph:getData()

imgData2 = dat:clone()
Note that I can still use getData() which was removed in löve version 11.0 because I'm still using löve 9.0. But I get the message "attempt to call field 'clone (a nil value)'". So that's not the way to do it. So how do I create a second image with a real copy of the first one?
I'm currently working on a game where I need to take it even one step further and create a copy of only a part of an image in another image.
What I'm trying to do is to create an explosion effect that looks like the enemy explosions in Robotron 2084, where enemies explode in strips.

See Robotron:
https://www.youtube.com/watch?v=aOVA2Axxfdk

I think what I would have to do is to have one image of the enemy graphic, and then copy the strip-parts of that image into separate images which I would then draw drifting apart to create the effect of the explosion. If there is simpler ways to do it, please let me know. I still need to be able to make memory copies of an image for another game where I'd like to recolor the same image over and over again to give each enemy a completely random color.
It would be best for me to have a solution that still works in löve 9.0. Thanks in advance for your suggestions.
Last edited by Rastashmup on Wed Feb 09, 2022 1:04 pm, edited 1 time in total.
User avatar
BrotSagtMist
Party member
Posts: 615
Joined: Fri Aug 06, 2021 10:30 pm

Re: How do I create a memory copy of an image and of an image-part?

Post by BrotSagtMist »

There probably is an easy way to do this, but its a bit of a mess to explain when you stick to an older version. see replace.pixels etc.

Anyways, none of your ideas actually needs that kind of manipulation.
To chop the image into pieces you can use quads, https://love2d.org/wiki/Quad
For coloring enemies note that setColor actually affects pictures.
And to compose more complicated views you should use a canvas. Canvases can be used create images too, but the benefit of that is questionable.
obey
Rastashmup
Prole
Posts: 31
Joined: Mon Jun 26, 2017 10:36 am

Re: How do I create a memory copy of an image and of an image-part?

Post by Rastashmup »

Thanks for your reply, I will see what I can do with the Quads, seen the example code in the wiki, so this should work for the explosions.

With the recoloring of the enemies, what I meant was not to repeatedly recolor the literal same image instance multiple times, but rather to have the enemies have memory copies of the original image so I can then recolor the copies. Having a solution in löve 9.0 would be nice, though I will update the version I'm working with if absolutely neccessary.
User avatar
BrotSagtMist
Party member
Posts: 615
Joined: Fri Aug 06, 2021 10:30 pm

Re: How do I create a memory copy of an image and of an image-part?

Post by BrotSagtMist »

Yes this is calling for a canvas...
obey
User avatar
pgimeno
Party member
Posts: 3551
Joined: Sun Oct 18, 2015 2:58 pm

Re: How do I create a memory copy of an image and of an image-part?

Post by pgimeno »

Take a look at ImageData:paste which if I understand properly, does what you originally intended.

Also, if you load the image as ImageData rather than as Image, with love.image.newImageData(filename), and you convert to image later instead of the opposite, that part will be ready for porting to 0.10 and 11.x.
Rastashmup
Prole
Posts: 31
Joined: Mon Jun 26, 2017 10:36 am

Re: How do I create a memory copy of an image and of an image-part?

Post by Rastashmup »

ImageData:paste looks like exactly what I was looking for, but I have a problem getting it to work. For test-purposes I added a piece of test code to my game that takes the first best enemy of a list and tries to paste the imagedata of its current frame image into a test-imagedata which is then used to try and create a new test-image. I added the following variables to my gamestate:

Code: Select all

self.blTestImageCreated = false
self.testImageData = nil
self.testImage = nil
Then in the update routine I added the following code snippet that fetches the first active enemy and tries to create a new imagedata and image from it, with the variable currentGph being the enemy's current frame-image and enmWidth,enmHeight being the size of the frame-image:

Code: Select all

for i = 1,globMaxEnemyT,1 do
	if globEnemyT[i] ~= nil and globEnemyT[i].active == true then 
			
		if self.blTestImageCreated == false then --only create the test-image once
			self.blTestImageCreated = true
					
			self.testImageData = love.image.newImageData( globEnemyT[i].enmWidth, globEnemyT[i].enmHeight)
					
			self.testImageData:paste( globEnemyT[i].currentGph:getData(), 0, 0, 0, 0, globEnemyT[i].enmWidth, globEnemyT[i].enmHeight )
					
			self.testImage = love.graphics.newImage(testImageData)
		end
	end
end
When I run my code I get the following error: bad argument #1 to newImage (ImageData expected, got nil)

Can you find anything obvious that I'm doing wrong here?
Rastashmup
Prole
Posts: 31
Joined: Mon Jun 26, 2017 10:36 am

Re: How do I create a memory copy of an image and of an image-part?

Post by Rastashmup »

Well, that was a dumb mistake, I just couldn't figure it out for quite a while. I need to pass "self.testImageData" to the newImage function instead of just "testImageData". Now I got it working and I can draw the self.testImage.
Rastashmup
Prole
Posts: 31
Joined: Mon Jun 26, 2017 10:36 am

Re: How do I create a memory copy of an image and of an image-part?

Post by Rastashmup »

Also tried out pasting only a part of the image and it worked. I can cut the image into pieces now and will use the paste function for it rather than the Quad. Also tried to recolor the testImage and it did not affect the original image, so what I got is a real copy just like intended. With this I can do what I was planning to do now. So I will mark this thread as [RESOLVED].
Post Reply

Who is online

Users browsing this forum: No registered users and 70 guests