Page 1 of 2

Get display width and heigth

Posted: Fri Jan 04, 2013 12:48 pm
by Palmar
Hi everyone.
How can i get the display width and heigth of computer running my game? I want to use it for love.graphics.setMode, cause if I set width and heigth to 0, my game don't want to draw anything.

Re: Get display width and heigth

Posted: Fri Jan 04, 2013 12:52 pm
by Saegor
love.graphics.getWidth() and love.graphics.getHeight() return the width and the height of the game screen

some tips :

1) if you want to call these functions repeteadly, don't forget you can use something like this in love.load() to accelerate the acces

Code: Select all

getW, getH = love.graphics.getWidth, love.graphics.getHeight
now use getW() instead of love.graphics.getWidth() in love.update()

2) there is a function i use, inspired from here. it sort screen modes and apply the highest availaible mode and activate fullscreen

Code: Select all

function setModeMax()

	modes = love.graphics.getModes()

	table.sort(modes, function(a, b) return a.width * a.height > b.width * b.height end)
	
	love.graphics.setMode(modes[1].width, modes[1].height, true)
end

Re: Get display width and heigth

Posted: Fri Jan 04, 2013 1:11 pm
by Nixola
If you want to obtain the current width and height of the whole monitor (they could be not the highest possible) you can set width and height to 0 in conf.lua and then get the width and height of the window

Re: Get display width and heigth

Posted: Fri Jan 04, 2013 2:45 pm
by Palmar
On my display(1440 x 900) game isn't running. How can is solve this problem?
UPD
I just turned fullscreen off. Thanks you, guys. But can i ask another question here? How can i created image, that is fading in? I made some tries, using for , love.setColor(255, 255, 255, FadeAlpha) , but fade is too fast(Fades in one moment).

Re: Get display width and heigth

Posted: Fri Jan 04, 2013 3:18 pm
by bartbes
I believe 0.8.0 had a bug with setMode with a width and height of 0, where you had to then setMode again with the actual with and height (gotten from getWidth and getHeight).

As for the fade, just don't increase your alpha that fast.

Re: Get display width and heigth

Posted: Fri Jan 04, 2013 4:02 pm
by Palmar
bartbes wrote: As for the fade, just don't increase your alpha that fast.
I really don't understand, how to make delays. Who can help, posting, how to make normally fading white rectangle?

Re: Get display width and heigth

Posted: Fri Jan 04, 2013 4:23 pm
by Lafolie
Use a smaller number to increment the alpha value.

Re: Get display width and heigth

Posted: Fri Jan 04, 2013 4:58 pm
by Santos
A common thing to do is to add dt from love.update to a variable, multiplied by another variable which controls the speed.

When changing the alpha value, you'll also what to make sure it doesn't go about the maximum value of 255.

Code: Select all

function love.load()
	alpha = 0
	fade_speed = 100
end

function love.update(dt)
	if alpha < 255 then
		alpha = alpha + dt * fade_speed

		if alpha > 255 then
			alpha = 255
		end
	end
end

function love.draw()
	love.graphics.setColor(255, 255, 255, alpha)
	love.graphics.rectangle('fill', 200, 200, 200, 200)
end

Re: Get display width and heigth

Posted: Fri Jan 04, 2013 6:47 pm
by Lafolie
You can also do:

Code: Select all

a = a + b * dt < 255 and a + b * dt or 255

--OR

a = math.min(255, a + b * dt)

Re: Get display width and heigth

Posted: Sat Jan 05, 2013 3:30 am
by Jasoco
Palmar wrote:Hi everyone.
How can i get the display width and heigth of computer running my game? I want to use it for love.graphics.setMode, cause if I set width and heigth to 0, my game don't want to draw anything.
Here's the trick I use. Call this function at the beginning of your code. (This is basically what Nixola said in code form)

Code: Select all

function getScreenMaxRes()
	local oldw, oldh = love.graphics.getWidth(), love.graphics.getHeight()
	love.graphics.setMode(0,0,false,false)
	screen_width = love.graphics.getWidth()
	screen_height = love.graphics.getHeight()
	love.graphics.setMode(oldw,oldh,false,false)
end
What it does is first saves the user set width and height to return to later. It then setMode's to 0, 0 which makes the game area the width and height of the screen itself. It then records those values into variables for retrieval later. Then it sets the window size back to what it was before. You will see a flash, but it will work. For some reason Löve has a bug where it returns 0 instead of the actual width and height so the draw area ends up blank. This will bypass it in a small amount of code.

If you want you can omit the recording of the current size for use later and just use the three middle lines. That's the important part.