Set origin for text rendering

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
Reygoch
Prole
Posts: 2
Joined: Fri Feb 20, 2015 9:40 pm

Set origin for text rendering

Post 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?
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Set origin for text rendering

Post 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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Set origin for text rendering

Post by kikito »

Use printf with x=0, full width of the screen and align right.
When I write def I mean function.
Reygoch
Prole
Posts: 2
Joined: Fri Feb 20, 2015 9:40 pm

Re: Set origin for text rendering

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Set origin for text rendering

Post 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.
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests