Center image and resize to actual size

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
Lynnstrum
Prole
Posts: 1
Joined: Tue Apr 21, 2020 4:16 am

Center image and resize to actual size

Post 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!
User avatar
MynameisBI
Prole
Posts: 7
Joined: Sun Dec 15, 2019 1:16 pm

Re: Center image and resize to actual size

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

Re: Center image and resize to actual size

Post by zorg »

i see the udemy or whatever course makes people think the push library is inherently needed to make a game...
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
Varkas
Citizen
Posts: 83
Joined: Mon Mar 09, 2020 2:26 pm

Re: Center image and resize to actual size

Post by Varkas »

I wonder too, how many are using virtual resolutions and struggle with the intricate details that come with it. Retina displays?
In soviet russia, code debugs you.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 10 guests