Page 1 of 1

Center image and resize to actual size

Posted: Tue Apr 21, 2020 4:22 am
by Lynnstrum
Hello, I am trying to place a image in my game and have no idea what I'm doing lol.
I want it in the center of the screen, and the size of the actual image.
Instead, I get a really, really big image and not at all in the center.
This is my code:

Code: Select all


gReward = love.graphics.newImage('goldReward.png')
quad = love.graphics.newQuad(0, 0, 128, 64, gReward:getWidth(), gReward:getHeight())

love.graphics.draw(gReward, 100, 100, 0, 1, 1, VIRTUAL_WIDTH / 2, VIRTUAL_HEIGHT / 2)

Thank you! Please explain how it works too please.
I don't just want the correct code, I want to understand why it's correct!

Re: Center image and resize to actual size

Posted: Tue Apr 21, 2020 4:15 pm
by MynameisBI
Hello there.

So firstly, you're actually mistaken the last 2 offsets arguments of the love.graphics.draw function. These 2 arguments basically say substract the VIRTUAL_WIDTH / 2 from the 100.
Assuming the scale is normal 1:1, LOVE will then draw the image at coordinate 100 - VIRTUAL_WIDTH/2, which is why the image isn't at the center of the screen.
What you can use instead, assuming VIRTUAL_WIDTH is the screen width, is:

Code: Select all

love.graphics.draw(gReward, VIRTUAL_WIDTH / 2, VIRTUAL_HEIGHT / 2, 0, 1, 1, gReward:getWidth() / 2, gReward:getHeight() / 2)
Which tell LOVE to first draw the top left of the image at the center, after that offsets the image a value equal to half its size


And about the size of the image, I don't see anything wrong with your code. Could you attach a .love file so I can take a look?

Re: Center image and resize to actual size

Posted: Thu Apr 23, 2020 2:30 pm
by zorg
i see the udemy or whatever course makes people think the push library is inherently needed to make a game...

Re: Center image and resize to actual size

Posted: Fri Apr 24, 2020 1:13 am
by Varkas
I wonder too, how many are using virtual resolutions and struggle with the intricate details that come with it. Retina displays?