[SOLVED]Centering text to screen

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
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

[SOLVED]Centering text to screen

Post by veethree »

Hey guys. quick question. Is it possible to center text displayed with love.graphics.print? I know the "formula" to center stuff to the screen but for that i'd need something that would return the width of said thing. And i've looked through the wiki and didn't find any function or something that would do that. Perhaps there is one, But i'm just blind/stupid.
Thanks in advance.
Last edited by veethree on Fri Mar 09, 2012 3:36 pm, edited 1 time in total.
zd
Prole
Posts: 5
Joined: Thu Mar 08, 2012 2:27 am

Re: Centering text to screen

Post by zd »

You could use a fixed width font which would allow you to calculate the width easily
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Centering text to screen

Post by coffee »

veethree wrote:Hey guys. quick question. Is it possible to center text displayed with love.graphics.print? I know the "formula" to center stuff to the screen but for that i'd need something that would return the width of said thing. And i've looked through the wiki and didn't find any function or something that would do that. Perhaps there is one, But i'm just blind/stupid.
Thanks in advance.
Don't know if this is the formula you referring but can't you use printf instead of print? Only need to know the screen width (so usually a fixed var) and not always getting object/text size
w = love.graphics.getWidth()
love.graphics.printf(text,0,0,w)
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Centering text to screen

Post by veethree »

coffee wrote:
veethree wrote:Hey guys. quick question. Is it possible to center text displayed with love.graphics.print? I know the "formula" to center stuff to the screen but for that i'd need something that would return the width of said thing. And i've looked through the wiki and didn't find any function or something that would do that. Perhaps there is one, But i'm just blind/stupid.
Thanks in advance.
Don't know if this is the formula you referring but can't you use printf instead of print? Only need to know the screen width (so usually a fixed var) and not always getting object/text size
w = love.graphics.getWidth()
love.graphics.printf(text,0,0,w)
I completely forgot about printf, Haven't fully tested it, But it appears to work if i do what you did and set the alignment to "center". Thanks :D


Oh and the formula i was referring to is setting the X position of an object to the center then subtracting half of it's width. (not sure if that makes sense, just look at the code. lol)

something like this:

Code: Select all

w = love.graphics.getWidth()
	love.graphics.rectangle("fill", w/2 - 50, 10, 100, 100)
Post Reply

Who is online

Users browsing this forum: No registered users and 43 guests