Page 1 of 1

[SNIPPET] Pixels to scale factor

Posted: Thu Jul 24, 2014 2:58 pm
by Eamonn
I wrote a little snippet for converting from LÖVE's scale factor to a pixel size of your choice. Of course, you don't have to keep any particular aspect ratio, but I would recommend you do usually. Although in some cases you might need to set an enforced pixel size. Here is the snippet:

Code: Select all

function pixToScale( destSize, currSize )
    return destSize / currSize
end
You put the size you want as destSize, and the image/whatever's current size as currSize.

I hope you find this useful! I couldn't see this on the snippets part of the wiki. This is something I didn't find obvious, and had to search for a couple of days to find the answer. Just thought I'd share and help anyone else in need of this sort of thing!

Have a great day!

Re: [SNIPPET] Pixels to scale factor

Posted: Thu Jul 24, 2014 4:38 pm
by Ranguna259
That's completly right, I uses this method to resize images. And I'm probably gonna use it to resize the screen to make my games support multiple screen resolutions.

Re: [SNIPPET] Pixels to scale factor

Posted: Thu Jul 24, 2014 4:43 pm
by MadByte
Therefor I also got some snippets.

Code: Select all

function setResolution(w, h)
  WIDTH, HEIGHT = w, h
  SCREENWIDTH, SCREENHEIGHT = love.window.getDimensions()
  SCALEX, SCALEY = SCREENWIDTH / WIDTH, SCREENHEIGHT / HEIGHT
end


function setScale(w, h)
  SCREENWIDTH, SCREENHEIGHT = w, h
  SCALEX, SCALEY = SCREENWIDTH / WIDTH, SCREENHEIGHT / HEIGHT
  love.window.setMode(SCREENWIDTH, SCREENHEIGHT)
end
And in love.draw:

Code: Select all

love.graphics.scale(SCALEX, SCALEY)
-- draw stuff
Works great for my projects, but might need to be edited to suit your needs.

Re: [SNIPPET] Pixels to scale factor

Posted: Thu Jul 24, 2014 5:35 pm
by Eamonn
MadByte wrote:Therefor I also got some snippets.

Code: Select all

function setResolution(w, h)
  WIDTH, HEIGHT = w, h
  SCREENWIDTH, SCREENHEIGHT = love.window.getDimensions()
  SCALEX, SCALEY = SCREENWIDTH / WIDTH, SCREENHEIGHT / HEIGHT
end


function setScale(w, h)
  SCREENWIDTH, SCREENHEIGHT = w, h
  SCALEX, SCALEY = SCREENWIDTH / WIDTH, SCREENHEIGHT / HEIGHT
  love.window.setMode(SCREENWIDTH, SCREENHEIGHT)
end
And in love.draw:

Code: Select all

love.graphics.scale(SCALEX, SCALEY)
-- draw stuff
Works great for my projects, but might need to be edited to suit your needs.
Excellent snippets! Thanks for sharing :D

I'm interested in one-liner things now, ever since I found out about Python's Lambda functionality, so here's how you'd put my method on 1 line:

Code: Select all

pixToMet = function(destSize, currSize) return destSize / currSize end

-- Just call and pass arguments to the function as normal
pixToMet(dS, cS)
I believe they are called "local functions", are do you still refer to them as lambda's in Lua?

Re: [SNIPPET] Pixels to scale factor

Posted: Thu Jul 24, 2014 6:12 pm
by Tanner
Eamonn wrote:I believe they are called "local functions", are do you still refer to them as lambda's in Lua?
'Lambda' just denotes an anonymous function or closure. You can totally call any Lua function a lambda.