Page 1 of 1

I have a question!

Posted: Thu Jul 04, 2013 6:01 pm
by XxFullMetalxX
i am new to love2d, and I was just wondering if there is a way to find the center of a screen.

If there is any way to find out please tell me!

Re: I have a question!

Posted: Thu Jul 04, 2013 7:25 pm
by Plu
You can use:
http://www.love2d.org/wiki/love.graphics.getHeight
http://www.love2d.org/wiki/love.graphics.getWidth

to get the total height/width of the screen. If you divide them by 2, you get the middle of the screen.

Re: I have a question!

Posted: Thu Jul 04, 2013 7:36 pm
by Eamonn
Just to add to what Plu said: If you ever want to find the center of the screen with an image, you would do the following:

Code: Select all

img = love.graphics.newImage("img.png")
centerX = ( ( love.graphics.getWidth()/2 )-( img:getWidth()/2 ) )
centerY = ( ( love.graphics.getHeight()/2 )-( img:getHeight()/2 ) )
Just in case you ever want to get the center of an image, there you go! Hope this helped! :awesome: :awesome: :awesome:

Re: I have a question!

Posted: Mon Jul 08, 2013 2:35 am
by davisdude
Not to be nit-picky or anything but you should really give a less general name to your post than that. Instead of 'I have a question!' you could have called it something like 'Getting the center of the screen' or something like that.

Figured I would give you a heads up for future questions.

Also something else useful: getting the width of text and what not.
If you ever want to do this (and believe me, you most likely will), this is how you would go about doing it:

Code: Select all

font = love.graphics.newFont('timesNewRomans.ttf', 32) --name of font, size
love.graphics.setFont(font)
text = 'Brought to you by XxFullMetalxX'
height = font:getHeight(text) --note that you don't actually have to put the argument text there since the font height will be the same. It's more of an organizational thing
width = font:getWidth(text) --You do need it for this one, however
center = { width/2, height/2 }
And there you have it! This is more than you probably wanted, but you never know if it might come in handy! :awesome: