Page 1 of 1

Scaling text in printf

Posted: Sat Mar 06, 2021 5:57 pm
by jefolo
Hello,
i'm triyng to write a text centered in the screen that fade out and increase in size.
I wrote the following code, but the text, when increase, move from the center of the screen to the right (i wold like to mantain it centerd).

Code: Select all

ww, wh = love.graphics.getDimensions()
msgFont = love.graphics.newFont(24)
msgFontHeight = msgFont:getHeight()
msgTextOpacity = 300
msgTextSize = 1

function love.update(dt)
  msgTextOpacity = msgTextOpacity - 300*dt
  msgTextSize = msgTextSize + 1*dt
end

function love.draw()
  love.graphics.setColor(1, 1, 1, msgTextOpacity/255)
  love.graphics.setFont(msgFont)
  love.graphics.printf( "Hello World", 0, wh * 0.5 - (msgFontHeight*0.5), ww, "center", r, msgTextSize, msgTextSize, ox, oy, kx, ky )
end
I played with ox and oy, but without success.
Can somebody help me?

Thank you

Re: Scaling text in printf

Posted: Sat Mar 06, 2021 9:22 pm
by eliddell
You can calculate the x-coordinate yourself (from Text:getWidth(), msgTextSize, and ww) rather than relying on printf's AlignMode to place the text. Or possibly use a Transformation object. Or there's probably a third solution that requires understanding the fine print about how printf behaves.

Re: Scaling text in printf

Posted: Sat Mar 06, 2021 10:25 pm
by GVovkiv

Code: Select all

string = "Hello, World!"
ww, wh = 0, 0
msgFont = love.graphics.newFont(24)
msgFontHeight = msgFont:getHeight()
msgTextOpacity = 300
msgTextSize = 1

function love.update(dt)
  msgTextOpacity = msgTextOpacity - 300*dt
  msgTextSize = msgTextSize + dt
  ww, wh = love.graphics.getWidth() / msgTextSize, love.graphics.getHeight() / msgTextSize
end

function love.draw()
  love.graphics.setColor(1, 1, 1, msgTextOpacity/255)
  love.graphics.setFont(msgFont)
  love.graphics.scale(msgTextSize, msgTextSize)
  love.graphics.print(string, (ww / 2) - msgFont:getWidth(string)/2, (wh / 2) - msgFont:getHeight(string)/2)
end
(string, (ww / 2) - msgFont:getWidth(string)/2, (wh / 2) - msgFont:getHeight(string)/2) -- center text on screen
ww and wh - scaled window size

Re: Scaling text in printf

Posted: Sat Mar 06, 2021 11:00 pm
by jefolo
GVovkiv wrote: Sat Mar 06, 2021 10:25 pm

Code: Select all

string = "Hello, World!"
ww, wh = 0, 0
msgFont = love.graphics.newFont(24)
msgFontHeight = msgFont:getHeight()
msgTextOpacity = 300
msgTextSize = 1

function love.update(dt)
  msgTextOpacity = msgTextOpacity - 300*dt
  msgTextSize = msgTextSize + dt
  ww, wh = love.graphics.getWidth() / msgTextSize, love.graphics.getHeight() / msgTextSize
end

function love.draw()
  love.graphics.setColor(1, 1, 1, msgTextOpacity/255)
  love.graphics.setFont(msgFont)
  love.graphics.scale(msgTextSize, msgTextSize)
  love.graphics.print(string, (ww / 2) - msgFont:getWidth(string)/2, (wh / 2) - msgFont:getHeight(string)/2)
end
(string, (ww / 2) - msgFont:getWidth(string)/2, (wh / 2) - msgFont:getHeight(string)/2) -- center text on screen
ww and wh - scaled window size
Thank you for your solution.
At the beginning I was using print and a similar approach, but in the context of the game the 'sting' is longer tha just "hello world" and with printf it could be splitted in two lines.
I couldn't figure out how to mix the two things....

Re: Scaling text in printf

Posted: Sun Mar 07, 2021 9:49 am
by GVovkiv
jefolo wrote: Sat Mar 06, 2021 11:00 pm Thank you for your solution.
At the beginning I was using print and a similar approach, but in the context of the game the 'sting' is longer tha just "hello world" and with printf it could be splitted in two lines.
I couldn't figure out how to mix the two things....
Well, you can use "\n" in string to make something like "Hello, World!\nNext Line" -- >
"Hello, World!
Next Line"

Re: Scaling text in printf

Posted: Sun Mar 07, 2021 12:34 pm
by pgimeno
The problem is that the width parameter is scaled with the scale parameter. To compensate for that, use ww/msgTextSize instead of just ww as the width parameter for printf.

Re: Scaling text in printf

Posted: Sun Mar 07, 2021 5:44 pm
by jefolo
Thank you GVovkiv, thank you pgimeno, both of your solution works!
At the end I will go for GVovkiv's solution, because using the printf solution, when the text scale, the number of words change during the movement.

Thanks again!