Page 1 of 1

Set origin for text rendering

Posted: Fri Feb 20, 2015 10:06 pm
by Reygoch
Hi,

I'm new here and I'm poking around love2d a little. I'm interested if there's a way to print text to the left of the origin point. I have tried using printf and aligned text to the right, but it's still required to define offset and box and it's annoying to recalculate bounding box if the text is dynamic.

To the point. I want to have text in the top right and top left corners of my game, and if I set position of print to (gameWidth, 0) text goes out of rendering area.

I didn't find anything in the documentation about it. Is there maybe some trick to it?

Re: Set origin for text rendering

Posted: Fri Feb 20, 2015 10:23 pm
by davisdude
There are two options, both of which involve getting the width:

Code: Select all

local font = love.graphics.getFont() -- No need to do this unless you're using default font
local width = font:getWidth( text )

-- Method 1 (not as clean, IMHO)
love.graphics.print( text, GameWidth - width, 0 ) -- Maybe have -2 or something for padding
-- Method 2
love.graphics.print( text, GameWidth, 0, 0, 1, 1, width, 0 ) -- Also could use padding
See here for more.

Re: Set origin for text rendering

Posted: Fri Feb 20, 2015 10:27 pm
by kikito
Use printf with x=0, full width of the screen and align right.

Re: Set origin for text rendering

Posted: Sat Feb 21, 2015 12:12 am
by Reygoch
So, you can't just switch origin point? Shame. I like @kikito 's solution. Much simpler than calculating font length. Although I might get into some trouble when rendering UI with a background. I guess long term solution would be calculating font length for more complicated layouts.

Re: Set origin for text rendering

Posted: Sat Feb 21, 2015 10:47 am
by kikito
On a ui, you can use the with and left corner of the "container of the text" (text box, window, etc). The effect should be the same.