Resolution problems(updated 6.Jan)

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
User avatar
Mermersk
Party member
Posts: 108
Joined: Tue Dec 20, 2011 3:27 am

Resolution problems(updated 6.Jan)

Post by Mermersk »

Hi,

So I have somewhat of a problem here. I have developed a game for android phones, and it's finished now, only thing left is for it to work on different resolutions. But the problem Is I hadn't thought about that when I began, so now I can find no way to make it work for different resolutions. I thought there would be some easy way but I am not finding it. :brows:

Here is all the code of the game: https://github.com/Mermersk/Duckentry

What I have done is that some/all graphics scale correctly but everything else doesn't. I don't know if it all scales because I cant progress. I cant progress because all the buttons are wonky and don't really work.

I have googled and asked but all the solutions I find are either to complicated for me or I just don't see how I can make it work with my game.

I've got this in load:

Code: Select all

g_width, g_height = love.window.getDimensions()
	game_width = 480
	game_height = 320
	
	scale_x = g_width/game_width
	scale_y = g_height/game_height
And then I use this in love.update and love.draw:

Code: Select all

love.graphics.scale(g_width/game_width, g_height/game_height)
Any help is appreciated and also pointer on how to properly code a game if you know it will have to scale for different resolutions! :3
Last edited by Mermersk on Wed Jan 07, 2015 3:29 am, edited 2 times in total.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Resolution problems

Post by Azhukar »

I've removed all your scaling functionality and added 2 scaling wrappers on the end of your main.lua file. This will cause your game to be stretched if displayed on a resolution ratio different than the one it was originally written for. Note that this is an awful way to handle resolution problems. Read the comments on the functions I included, they are at the end of your main.lua file.

Next time you code something, keep in mind that all on screen coordinates should always depend on screen resolution. They are most of the time not something you want to be hardcoded as a flat number value.

EDIT: I forgot to scale mx, my = love.mouse.getPosition() on line 57
Attachments
main.lua
(7.59 KiB) Downloaded 152 times
User avatar
Mermersk
Party member
Posts: 108
Joined: Tue Dec 20, 2011 3:27 am

Re: Resolution problems

Post by Mermersk »

Thanks for the help Azhukar!

So if I understand this correctly your'e basicly altering how the whole love.draw and love.mousepressed works in the program? making you own version of it?

Then I should be able to do the same with x, y = love.mouse.getPosition() function ?

Strange thing is that when I print out the "my" value without altering love.mouse.getposition() function I get a number from 0(top of the screen) to 1071(bottom), why that range? My screen resolution is 1280x1920.

But yeah "mx, my = love.mouse.getPostion" don't work without changing something, the mouse is far above the actual duck when your'e controlling it.

Also why is love.graphics.push and love.graphics.pop there? I tried removing them and it works pretty well, so why are they necessary?
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Resolution problems

Post by Azhukar »

Mermersk wrote:So if I understand this correctly your'e basicly altering how the whole love.draw and love.mousepressed works in the program? making you own version of it?
Yes, that's exactly what it does.
Mermersk wrote:Then I should be able to do the same with x, y = love.mouse.getPosition() function ?
Yes, you need to divide the coordinates by the scale ratios.
Mermersk wrote:Strange thing is that when I print out the "my" value without altering love.mouse.getposition() function I get a number from 0(top of the screen) to 1071(bottom), why that range? My screen resolution is 1280x1920.
My guess would be it has something to do with how your OS handles window mode. If it's fullscreen without borders then I have no idea.
Mermersk wrote:Also why is love.graphics.push and love.graphics.pop there? I tried removing them and it works pretty well, so why are they necessary?
In the way I used it it has no purpose at all. I'm assuming you know what it does by now.
User avatar
Mermersk
Party member
Posts: 108
Joined: Tue Dec 20, 2011 3:27 am

Re: Resolution problems

Post by Mermersk »

Hopefully someone can answer this. So in Azhukar response he changes the love.mousepressed function like this:

Code: Select all

local love_mousepressed = love.mousepressed --store original mousepressed callback
        love.mousepressed = function(x,y,button) 
	local width,height = love.window.getDimensions()
	local original_width = 480
	local original_height = 320
	local sx,sy = width/original_width,height/original_height
	love_mousepressed(x/sx,y/sy,button) 
end
But how would you do the same for love.mouse.getposition() ? since you always have to write

Code: Select all

x, y = love.mouse.getPosition()
I've gotten this far:

Code: Select all

local mouse_getPosition = love.mouse.getPostion
       love.mouse.getPostion = function()
       local width, height = love.window.getDimensions()
	local original_width = 480
	local original_height = 320
	local sx, sy = width/original_width, height/original_height
	mx, my = mouse_getPosition()
	return mouse_getPosition(mx/sx, my/sy)
end
But it isn't working, so how do you change the love.mouse.getPosition function? So that it works when the game scales.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Resolution problems(updated 30.des)

Post by Azhukar »

Code: Select all

local mouse_getPosition = love.mouse.getPosition
love.mouse.getPosition = function()
	local width, height = love.window.getDimensions()
	local original_width = 480
	local original_height = 320
	local sx,sy = width/original_width, height/original_height
	local mx,my = mouse_getPosition()
	return mouse_getPosition(mx/sx, my/sy)
end
You had a typo there. getPosition vs getPostion
User avatar
Mermersk
Party member
Posts: 108
Joined: Tue Dec 20, 2011 3:27 am

Re: Resolution problems(updated 30.des)

Post by Mermersk »

Thanks man, ended up working like this:

Code: Select all

local mouse_getPosition = love.mouse.getPosition
    love.mouse.getPosition = function()
    local width, height = love.window.getDimensions()
	local original_width = 480
	local original_height = 320
	local sx, sy = width/original_width, height/original_height
	x, y = mouse_getPosition()
	x = x/sx
	y = y/sy
	return x, y
end
But I have encountered another problem, late in the game I have a moving forest with trees drawn on a canvas, for some reason this scaling messes it up, it just doesn't appear. I have confirmed the scaling to be the problem since it does work when I disable the scaling. The canvas does appear(x, y coords of the canvas move) but there is nothing on it. The water also doesn't appear, but since it's linked to the trees it will likely fixed if the trees are fixed. The tree code is in "tre.lua" on the github link I posted in original post.

Strange thing is that the planet and nebulas are kinda the same thing, yet they work 100% with the scaling. Well difference is that those canvases are static, still, while the trees and water have things moving in them.

I cant figure out what is causing this, does anybody know?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 46 guests