is there a way to crop a part of a image and get a texture/image out of it?
I don't want to just draw it but actually get a object out of it so I can store it in a array.
crop a image not draw part of it?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Citizen
- Posts: 73
- Joined: Wed Oct 28, 2015 11:18 pm
Re: crop a image not draw part of it?
I strongly recommend against that, as there are usually alternatives, but if you only have an Image object and you really really need to crop it, the only way I can think of is to draw the image to a canvas of the final size and then use Canvas:newImageData. However, if you load the image first to an ImageData object, then you can use the Data:getString method and slice the string with string.sub to form the cropped image, which you would then load into a new ImageData object. Or even better, you can use Data:getPointer and do the data manipulation with FFI, which I expect would be faster. Maybe someone can think of something simpler; I have that terrible feeling that I'm missing something obvious and it must be simpler.
So, let's go with the alternatives. Rather than storing a cropped image in the array, why don't you store both the original image and the quad that crops it, and use both when you want to draw the cropped image?
So, let's go with the alternatives. Rather than storing a cropped image in the array, why don't you store both the original image and the quad that crops it, and use both when you want to draw the cropped image?
- Sir_Silver
- Party member
- Posts: 286
- Joined: Mon Aug 22, 2016 2:25 pm
- Contact:
Re: crop a image not draw part of it?
Uh, wouldn't a quad be perfect for this?
https://love2d.org/wiki/love.graphics.newQuad
Edit: didn't see above post already mentioned quads, nonetheless, I think that's what you want to use.
https://love2d.org/wiki/love.graphics.newQuad
Edit: didn't see above post already mentioned quads, nonetheless, I think that's what you want to use.
-
- Citizen
- Posts: 73
- Joined: Wed Oct 28, 2015 11:18 pm
Re: crop a image not draw part of it?
@pgimeno so love doesn't have a simple image.crop function? thats dumb.
@Sir_Silver like I said I don't want to draw it I need to crop it and store it into a tile array
I guess I could post a suggestion, but I am starting to feel that every game engine/framework that is easy to use ends up being sandboxed or made by devs that think that people should download libraries or code the function themselves.
its kinda of annoying
@Sir_Silver like I said I don't want to draw it I need to crop it and store it into a tile array
I guess I could post a suggestion, but I am starting to feel that every game engine/framework that is easy to use ends up being sandboxed or made by devs that think that people should download libraries or code the function themselves.
its kinda of annoying
Re: crop a image not draw part of it?
It's not efficient to duplicate an image in memory, considering that you can draw cropped images using quads.Shadowblitz16 wrote: ↑Wed Apr 25, 2018 3:11 am @pgimeno so love doesn't have a simple image.crop function? thats dumb.
It's smart that such a function doesn't exist since it encourages people to use better options (like quads).
Ideally, you should crop your images ahead of time using an image editor.Shadowblitz16 wrote: ↑Wed Apr 25, 2018 3:11 am @Sir_Silver like I said I don't want to draw it I need to crop it and store it into a tile array
Love2d provides the most common functionality that you need.Shadowblitz16 wrote: ↑Wed Apr 25, 2018 3:11 am I guess I could post a suggestion, but I am starting to feel that every game engine/framework that is easy to use ends up being sandboxed or made by devs that think that people should download libraries or code the function themselves.
Of course you still have to write your own functions, what did you expect.
Re: crop a image not draw part of it?
Code: Select all
local img = love.image.newImageData('image.jpg')
local cropped = love.image.newImageData(cropWidth, cropHeight)
cropped:paste(img, 0, 0, cropX, cropY, cropWidth, cropHeight)
Last edited by grump on Wed Apr 25, 2018 7:16 am, edited 1 time in total.
- zorg
- Party member
- Posts: 3444
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: crop a image not draw part of it?
You know, you could just go the easy route and learn to develop for consoles that worked in the way you want, like the NES.Shadowblitz16 wrote: ↑Wed Apr 25, 2018 3:11 am
I guess I could post a suggestion, but I am starting to feel that every game engine/framework that is easy to use ends up being sandboxed or made by devs that think that people should download libraries or code the function themselves.
its kinda of annoying
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
- Sir_Silver
- Party member
- Posts: 286
- Joined: Mon Aug 22, 2016 2:25 pm
- Contact:
Re: crop a image not draw part of it?
What grump posted looks like it's exactly what you want!
Worth mentioning that, after you do the code grump posted which was
If you decide you want to draw it, you'll need to create a drawable from that imageData so you'll need to do something like
Now you can just store this image in your array!
Worth mentioning that, after you do the code grump posted which was
Code: Select all
local img = love.image.newImageData('image.jpg')
local cropped = love.image.newImageData(cropWidth, cropHeight)
cropped:paste(img, 0, 0, cropX, cropY, cropWidth, cropHeight)
Code: Select all
cropped = love.graphics.newImage(cropped)
Re: crop a image not draw part of it?
If you want to have at least a chance for good performance (as long as you use version >= 11 with automatic batching) you could use something like this
Create some tiles in whatever way fits your project/assets
and later on
Code: Select all
local function crop( image, srcX, srcY, width, height )
return {
quad = love.graphics.newQuad( srcX, srcY, width, height, image:getDimensions() ),
image = image,
}
end
love.graphics.drawTile = function( tile, x, y )
love.graphics.draw( tile.quad, tile.image, x, y )
end
Code: Select all
local myTileArray = {}
myTileArray[1] = crop( myPreviouslyLoadedImage, 0, 0, 32, 32 )
Code: Select all
love.graphics.drawTile( myTileArray[myTileIdx], myDrawXPos, myDrawYPos)
-
- Citizen
- Posts: 73
- Joined: Wed Oct 28, 2015 11:18 pm
Re: crop a image not draw part of it?
@grump thankyou this is very helpful
@zorg I want to make a GB Zelda editor thats why I don't want to learn console programming
@Nelvin ya I would like it to be as fast as possible but I want to store tiles individually so idk
@zorg I want to make a GB Zelda editor thats why I don't want to learn console programming
@Nelvin ya I would like it to be as fast as possible but I want to store tiles individually so idk
Who is online
Users browsing this forum: No registered users and 200 guests