is there a way to draw a part of a image inside a function?

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.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: is there a way to draw a part of a image inside a function?

Post by zorg »

Okay, so assuming this is what we're talking about: https://msdn.microsoft.com/en-us/library/ff433986.aspx (i.e. XNA)
And that links to this Rectangle page: https://msdn.microsoft.com/en-us/librar ... angle.aspx
With these internals: https://msdn.microsoft.com/en-us/librar ... mbers.aspx
Looks quite a bit like a Quad here (although i somewhat fail to see how the x,y and left,top fields aren't the same), although the constructor page doesn't detail it being wasteful to call it inside the loop... https://msdn.microsoft.com/en-us/librar ... angle.aspx
... that said, reading more into it, it looks like it's just an axis-aligned bounding box implementation (and a horrible one at that, only allowing integer coordinates, according to stackoverflow anyway).

Sadly, i can't tell whether creation would be amortized behind the scenes by XNA itself, having object pools, or something else; point is, if you're using the same objects over more than just one frame, do only create one of them, and re-use them. Memory should still be a concern in this age, otherwise, with some exaggeration, you'll create the next google chrome that may eat up 10s of gigabytes of RAM, if it can. (And löve has a garbage collector, so garbage can and will pile up fast if you're generating it at such a pace) :3



(not exaggerating, its RAM usage can go up to 24GB on my PC)
Me and my stuff :3True 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.
User avatar
pgimeno
Party member
Posts: 3639
Joined: Sun Oct 18, 2015 2:58 pm

Re: is there a way to draw a part of a image inside a function?

Post by pgimeno »

After taking a look at the source of Löve, I can say that if there's a difference, it will be very small.

A Löve quad is an object that stores some numbers. A rectangle is an object that stores some numbers. Both need to be allocated, and freed later. The quad makes a bit of math on creation.

The Löve SpriteBatch:add call takes a quad as an optional parameter; the XNA Draw call for a spritebatch takes a rectangle as an optional parameter. That call must do the math that the Löve quad normally makes, because that math needs to be made.

I made a mistake because I thought the quad also created an object in GPU memory, but that doesn't seem to be the case.
HaplessJake
Prole
Posts: 1
Joined: Tue Feb 08, 2022 12:34 am

Re: is there a way to draw a part of a image inside a function?

Post by HaplessJake »

I understand this is an old post, and I'm probably performing necromancy here.

However, I thought incase anyone else like me came through here looking for a way to do this I wanted to provide my method.

Like most have said you don't want to be making new quads each step. Instead, you will need to make and store a quad.

Use Quad:setViewport(x,y,width,height) to adjust the size of the quad how you like in an update function. You will have to do the math in your own head to figure out the change in numbers.

Ex. Apologies for the "_"'s I use them a lot in code helps me sort.

Code: Select all

local _healthPercent = 0.68 --chose a random number here
local _width, _height = 8, 16 --again random numbers
local _healthQuad = love.graphics.newQuad(0, 0, _width, _height, _width, _height)

function updateHealthQuad()
	_healthQuad:setViewport(0,0,_width*_healthPercent,_height) --reduces drawn width of images using this quad
end

function drawHealthBar()
	love.graphics.draw(_whateverimage, _healthQuad, 0,0)
end
I hope this is cohesive enough for anyone wondering through!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests