[Solved] love.graphics.printf return number of lines?

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
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

[Solved] love.graphics.printf return number of lines?

Post by Germanunkol »

I don't understand why love.graphics.printf doesn't return anything? I think it would be very useful if it returned the number of lines printed...?

Any thoughts on this? Developers, would this be hard to implement?
Last edited by Germanunkol on Wed Jul 10, 2013 9:39 am, edited 1 time in total.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
XOdysseusX
Prole
Posts: 11
Joined: Fri Apr 26, 2013 3:32 pm

Re: love.graphics.printf return number of lines?

Post by XOdysseusX »

The number of lines printed? You mean in the console?

If so, it wouldn't be in love.graphics.
It seems like an interesting idea, though I don't know why you would need it, the value would increase by one every time you use the print() command, which can be implemented yourself easily.

Ofcourse, shortcuts are always nice. :nyu:
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: love.graphics.printf return number of lines?

Post by Santos »

Is Font:getWrap what you're looking for?
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: love.graphics.printf return number of lines?

Post by Germanunkol »

Sorry for the necro post, but yes, that was what I was looking for - thanks! :)

However, I don't understand why this isn't also returned by love.graphics.printf. This will mean the processing has to be done twice... and string manipulation isn't the quickest.

@ XOdysseus:
I need it for a lot of reasons. For example: When you use love.graphics.printf to output a chat box, then one line of text a user inputs might be more than one line of text in the chat box. If the next player also sends something, and that new text should be displayed underneath the old one, then I need to know how much space the old text used up, vertically, so that I can call the next printf function to place text below that.
Now I can use Font:getWrap and Font:getHeight() to know the number of pixels, so that works.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: love.graphics.printf return number of lines?

Post by davisdude »

You could also do

Code: Select all

font = love.graphics.newFont('font.ttf')
love.graphics.setFont(font)

string = 'lol'

width = font:getWidth( string )
And then apply math. Of course the other also works (and is probably easier, too...)
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
slime
Solid Snayke
Posts: 3170
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: love.graphics.printf return number of lines?

Post by slime »

Germanunkol wrote:This will mean the processing has to be done twice... and string manipulation isn't the quickest.
Most strings won't change as often as they're drawn. You only need to determine the number of lines a string will take up when the string changes or the textbox size changes.
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: love.graphics.printf return number of lines?

Post by Germanunkol »

slime wrote:
Germanunkol wrote:This will mean the processing has to be done twice... and string manipulation isn't the quickest.
Most strings won't change as often as they're drawn. You only need to determine the number of lines a string will take up when the string changes or the textbox size changes.
You're right, of course, and I'm not really complaining. All I'm saying is that love.graphics.printf _could_ return the number of lines which would make sense to me and would be a nice design.
But with the finding of font:getWrap the problem is solved. I also understand why they implemented this function, this way you can check how much space a text would take up before actually printing it.

Thanks for all the replies & help!
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], plexity and 5 guests