Getting number of lines of printf

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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Getting number of lines of printf

Post by bartbes »

There now is a getWrap function returning the maximum width and the number of lines, the code I used to test it:

Code: Select all

function dbg(...)
	print(...)
	return ...
end

function love.load()
	ma_string = "THIS SHOULD WRAP-A-LOT, BUT NOT THERE!"
	font = love.graphics.newFont(12)
	love.graphics.setFont(font)
	width, lines = dbg(font:getWrap(ma_string, 15))
	height = (lines+1)*(font:getHeight()+font:getLineHeight())
end

function love.draw()
	love.graphics.rectangle("line", 100, 100, width, height)
	love.graphics.printf(ma_string, 100, 100, 15)
end
Result:
Success.png
Success.png (2.37 KiB) Viewed 1722 times
User avatar
Lap
Party member
Posts: 256
Joined: Fri Apr 30, 2010 3:46 pm

Re: Getting number of lines of printf

Post by Lap »

Well that was the fastest feature request to implementation time I can remember. Thanks.

Side question: Does the latest dev version use the new way of printing. I remember someone saying the starting x,y for text meant something different in the next version? I was waiting for this kind of function to be added to love and for whatever the new print method to be implemented before I went and tweaked all the text based stuff in my game.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Getting number of lines of printf

Post by bartbes »

It's top-left now, as is all drawing.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Getting number of lines of printf

Post by Jasoco »

So this means I'll be able to make my dialog bubbles dynamically sized?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Getting number of lines of printf

Post by bartbes »

Yes.
luminosity
Prole
Posts: 34
Joined: Fri Sep 24, 2010 5:46 am

Re: Getting number of lines of printf

Post by luminosity »

I think I'm missing something in this function. Test code:

Code: Select all

function love.load()
    font = love.graphics.newFont(12)
    love.graphics.setFont(font)
    lines = 0
    text = ""
    timePassed = 0
end

function love.draw()
    love.graphics.setColor(255, 255, 255)
    love.graphics.printf(text, 0, 0, 1000, "left")
end	

function love.update(dt)
    timePassed = timePassed + dt
    if timePassed > 1 then
        timePassed = timePassed - 1
        lines = lines + 1
        if #text > 0 then
            text = text .. "\n"
        end
        text = text .. 'a'
        local _, wrapLines = font:getWrap(text, 1000)
        print('Lines', lines, 'Wrap lines', wrapLines, _)
    end
end
Using the 0.7 beta on Windows 7.

The text takes up more and more lines, but the lines variable stays 1, while width increases. Has the function changed since this post? I couldn't find it in the wiki.

Edit

Okay so I went and looked at the C source, like I probably should have originally. It appears getWrap is intended to only find the wrap for a piece of text without newlines in it, hence my weird results. I was thrown off by assuming it was intended to measure the height of any string you could hand to love.graphics.print. I've rewritten my font rendering to bear this in mind and it works perfectly now.

Should I edit the documentation to add this method, with a quick explanation of this to ensure other people don't trip over this problem in future?
Post Reply

Who is online

Users browsing this forum: No registered users and 141 guests