Get display width and heigth

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Palmar
Prole
Posts: 23
Joined: Thu Dec 13, 2012 3:54 pm

Get display width and heigth

Post 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.
User avatar
Saegor
Party member
Posts: 119
Joined: Thu Nov 08, 2012 9:26 am
Location: Charleroi

Re: Get display width and heigth

Post 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
Current work : Isömap
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Get display width and heigth

Post 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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Palmar
Prole
Posts: 23
Joined: Thu Dec 13, 2012 3:54 pm

Re: Get display width and heigth

Post 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).
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Get display width and heigth

Post 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.
Palmar
Prole
Posts: 23
Joined: Thu Dec 13, 2012 3:54 pm

Re: Get display width and heigth

Post 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?
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Get display width and heigth

Post by Lafolie »

Use a smaller number to increment the alpha value.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Get display width and heigth

Post 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
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Get display width and heigth

Post 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)
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Get display width and heigth

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

Who is online

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