Page 1 of 1
Automatic image rescale
Posted: Mon Feb 15, 2016 11:09 am
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.
Re: Automatic image rescale
Posted: Mon Feb 15, 2016 11:51 am
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.
Re: Automatic image rescale
Posted: Mon Feb 15, 2016 11:54 am
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.
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
Re: Automatic image rescale
Posted: Mon Feb 15, 2016 12:20 pm
by mmanso
Thanks! Sorry the lame questions but I'm starting with all this.
Again, thanks a lot.
Re: Automatic image rescale
Posted: Sun Feb 21, 2016 3:40 am
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