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.
Automatic image rescale
Re: Automatic image rescale
The scale can be calculated using the following formula:
There is a call back called love.resize that passes some variable, iirc.
You can then set the scale as follows:
Disclaimer: This code is untested.
Code: Select all
scaleX = newWidth/oldWidth
scaleY = newHeight/oldHeight
You can then set the scale as follows:
Code: Select all
love.graphics.push()
love.graphics.scale(scaleX, scaleY)
-- DRAW STUFF --
love.graphics.pop()
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
personal page and a raycaster
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Automatic image rescale
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.
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:
On how to center the image with the above solutions is left as an excercise to the reader
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()
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.
Me and my stuff True 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.
Re: Automatic image rescale
Thanks! Sorry the lame questions but I'm starting with all this.
Again, thanks a lot.
Again, thanks a lot.
Re: Automatic image rescale
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
Who is online
Users browsing this forum: Google [Bot] and 0 guests