## AspectRatio - A scaling tool

nyenye
Citizen
Posts: 62
Joined: Fri Dec 02, 2016 1:44 pm

### AspectRatio - A scaling tool

Hi guys,

I've been doing some things with Love2d, and personally had some trouble finding a tool/lib that took care of aspect ratio, resizing and such, and worked for me, so I did my own thing. Just today I packed all the code related to resizing (or all that makes sense) in a Lua file.

Right now it cover the basic needs that I wanted to cover, so if you find anything missing let me know.

Edit: I've made a complete rewrite of all the code and changed the way to use it, to be more simple, yet more specific. Leave a demo.

Update: New update on the lib. Added a function to convert from window coordinates to game coordinates. Useful when using mouse or touch events.

Code: Select all

local x, y = love.mouse.getPosition()
x, y = aspect_ratio:window_to_game_position( x, y )

Also added a GitLab repo: https://gitlab.com/nyenye/love2d-aspect-ratio/
Attachments
example.love
Last edited by nyenye on Sat Jan 28, 2017 10:09 am, edited 7 times in total.

alberto_lara
Party member
Posts: 347
Joined: Wed Oct 30, 2013 8:59 pm

### Re: Rationer - A resizing tool

The zip you're providing has only a folder with an "init.lua", looking at the code, this file should be "main.lua" (check this, a .love file would nice ). About the license, MIT is a good choice.

nyenye
Citizen
Posts: 62
Joined: Fri Dec 02, 2016 1:44 pm

### Re: Rationer - A resizing tool

What I had in mind is to require the file. Something like this:

Code: Select all

local rationer = require('lib/rationer')
rationer:init(windowWidth, windowHeight, digitalWidth, digitalHeight) -- digitalWidth/digitalHeight is resolution expected for the game
end
love.draw()
-- scale stuff with rationer.scale
end

Isn't it a good way to do things? Require keeps singleton references to loaded files, so you could make a Rationer a global value, and you could access it from another file like a gamestate from HUMP.

Edit: Just realized that you are the creator of Süsse, jeje. Good job really. Want to use when I am at a point where it makes sense to make sprites
Last edited by nyenye on Thu Jan 19, 2017 7:45 am, edited 1 time in total.

alberto_lara
Party member
Posts: 347
Joined: Wed Oct 30, 2013 8:59 pm

### Re: Rationer - A resizing tool

What I has in mind is to require the file.
Sure, what I mean is, it's nice to put a "whole" LÖVE app (a .love file is the usual), it doesn't need to be big, it would be just main.lua with a "require" of your lib and a small example (2 .lua files in total, zip them and change the .zip extension for .love, instructions in the link I gave you).

P.S. I'm glad you like Süsse

D0NM
Party member
Posts: 244
Joined: Mon Feb 08, 2016 10:35 am
Contact:

### Re: Rationer - A resizing tool

2nyenye: hey!!
It is a nice and tidy lib!

I use a similar unfinished "push" lib from the love2d forum. It might change the window res, too.
-- push.lua v0.1 Copyright (c) 2016 Ulysse Ramage

I personally need some lib (i'll have to do it myself, i think)
to have better control on the gfx.

e.g. a Pixel perfect full screen mode with some filling by the sides.
LÖVE & Lua Video Lessons in Russian / Видео уроки по LÖVE и Lua

Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

### Re: Rationer - A resizing tool

D0NM wrote: I use a similar unfinished "push" lib from the love2d forum. It might change the window res, too.

e.g. a Pixel perfect full screen mode with some filling by the sides.
Hey, maker of push here. Please open an issue on github if you feel like some features are missing. Also I believe what you mentioned is already implemented, please clarify so I can help further.

nyenye
Citizen
Posts: 62
Joined: Fri Dec 02, 2016 1:44 pm

### Re: Rationer - A resizing tool

To alberto_lara:
Ooh, of couse, today I'll make a love with the examples. Thanks for the tips, was a first time making a tool/lib. Also I'll make a gitlab repo and I'll share it on the main post too.

2 D0NM:
I knew about push, and I did try it, but dunno what did wrong because I couldn't get it to work as I expected. Probably my fault.

About the pixel perfect, you mean without scaling right? It should be easy to implement, but if you have a window that is smaller than you intended graphics size, would you scale it down? Or would you cut "visual field"? (Only concerns you if you make it available with mobiles.)

As you you have seen right now it's meant to preserve aspect ratio, leaving blank bars on the sides, or top and bottom, centering the screen.

nyenye
Citizen
Posts: 62
Joined: Fri Dec 02, 2016 1:44 pm

### Re: Rationer - A resizing tool

Updated the fist post

nyenye
Citizen
Posts: 62
Joined: Fri Dec 02, 2016 1:44 pm

### Re: AspectRatio - A resizing tool

I've added a method to the library which given a position on the window (x, y), it return the real position on game coordinates. Can be useful to make user interfaces which could make use of mouse or touch events.

D0NM
Party member
Posts: 244
Joined: Mon Feb 08, 2016 10:35 am
Contact:

### Re: Rationer - A resizing tool

Ulydev wrote:Hey, maker of push here. Please open an issue on github if you feel like some features are missing. Also I believe what you mentioned is already implemented, please clarify so I can help further.
Hey, hey, mr. maker!
The only change/fix in the PUSH I found today is

Code: Select all

  self._borderColor = g and {color, g, b} or color

Code: Select all

-- push.lua v0.1
It is the same version 0.1 I use but with 1 fix.

So I use it with a workaround and it does its work.

Code: Select all

push:switchFullscreen(GLOBAL_SETTING.WINDOW_WIDTH,GLOBAL_SETTING.WINDOW_HEIGHT)
I pass the windows dimensions on every switching to the full screen.
It makes it work fine. If I omit the dimensions then I start getting some cut canvas/screen on the 3rd call of

Code: Select all

push:switchFullscreen()
PS Going to open the issues right now. Thank you in advance.

2nyenye: sorry for offtopic
Nope, by pixel perfect I mean those modes / dimensions where all your pixels are squares: 1x1 2x2 3x3 4x4 etc