[SNIPPET] Pixels to scale factor

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Eamonn
Party member
Posts: 550
Joined: Sat May 04, 2013 1:29 pm
Location: Ireland

[SNIPPET] Pixels to scale factor

Post 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!
"In those quiet moments, you come into my mind" - Liam Reilly
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: [SNIPPET] Pixels to scale factor

Post 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.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: [SNIPPET] Pixels to scale factor

Post 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.
User avatar
Eamonn
Party member
Posts: 550
Joined: Sat May 04, 2013 1:29 pm
Location: Ireland

Re: [SNIPPET] Pixels to scale factor

Post 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?
"In those quiet moments, you come into my mind" - Liam Reilly
User avatar
Tanner
Party member
Posts: 166
Joined: Tue Apr 10, 2012 1:51 am

Re: [SNIPPET] Pixels to scale factor

Post 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.
Post Reply

Who is online

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