Automatic image rescale

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
mmanso
Prole
Posts: 12
Joined: Tue Feb 09, 2016 12:11 pm

Automatic image rescale

Post by mmanso »

Hi all,

I'm loading an image (800x600) when my app starts with:

img = love.graphics.newImage("assets/images/states/main.png")

Then, on the love.draw() I do:

love.graphics.draw(img, img.x, img.y)

This works lovely.

Now, when I resize the window (for example, to 1024x768) is there any way to make the draw rescale it? I've seen the draw functions has a "scale" option but I'm not following very well the way it should work.

Thanks a lot.
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Automatic image rescale

Post by Davidobot »

The scale can be calculated using the following formula:

Code: Select all

scaleX = newWidth/oldWidth
scaleY = newHeight/oldHeight
There is a call back called love.resize that passes some variable, iirc.

You can then set the scale as follows:

Code: Select all

love.graphics.push()
love.graphics.scale(scaleX, scaleY)
-- DRAW STUFF --
love.graphics.pop()
Disclaimer: This code is untested.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Automatic image rescale

Post by zorg »

A scale parameter of 1.0 will give you the 1:1 (same) scale, this should be evident, as it is the default value, if you check the wiki. :3
Now then, if the window is smaller than the image, we need to draw the image scaled down, otherwise, if the window is larger than the image, we need to scale it up.

Code: Select all

local sx = love.window.getWidth() / img:getWidth()
local sy = love.window.getHeight() / img:getHeigth()
Now, this will scale the image -exactly- to the screen, which will stretch it if the aspect ratio is not the same. To combat this, we may do either of the following:

Code: Select all

love.graphics.draw(img, img.x, img.y, 0, math.min(sx,sy)) -- if we want the image to fit inside, or
love.graphics.draw(img, img.x, img.y, 0, math.max(sx,sy)) -- if we don't want black bars at all.
On how to center the image with the above solutions is left as an excercise to the reader :3
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.
mmanso
Prole
Posts: 12
Joined: Tue Feb 09, 2016 12:11 pm

Re: Automatic image rescale

Post by mmanso »

Thanks! Sorry the lame questions but I'm starting with all this.

Again, thanks a lot.
Zatherz
Prole
Posts: 6
Joined: Sun Feb 21, 2016 2:07 am

Re: Automatic image rescale

Post by Zatherz »

I wrote a small module for scaling in pixel art games, where perfect and square pixels are important. If you're writing one, check out ScaleInator :D
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 0 guests