Maintain Aspect Ratio When Resizing Window?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
immakinggames
Prole
Posts: 22
Joined: Sat Jun 01, 2013 4:40 am

Maintain Aspect Ratio When Resizing Window?

Post by immakinggames »

Image

I want to be able to keep the original aspect ratio of my project with a window that is resizeable.

TLFres seems really outdated and I can't get it to work. Is there any quick (or built-in) solution to this?
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Maintain Aspect Ratio When Resizing Window?

Post by Nixola »

Draw everything to a canvas, check the aspect ratio of the window (local ratio = love.graphics.getWidth()/love.graphics.getHeight(); if ratio < 16/9 then) and calculate the offsett you need to draw the canvas to (offsetX = (love.graphics.getWidth() - love.graphics.getHeight()/9*16)/2; offsetY = 0) and do the same thing for Y (elseif ratio > 16/9 then offsetY = (love.graphics.getHeight() - love.graphics.getWidth()/16*9)/2; offsetX = 0 end). Then calculate the scaling for your canvas (local scale = ratio < 16/9 and love.graphics.getHeight()/(canvas_height) or love.graphics.getWidth()/(canvas_width); love.graphics.draw(canvas, offsetX, offsetY, 0, scale, scale)). This code has not been thoroughly tested.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

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