Scaling text in 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.
Post Reply
jefolo
Prole
Posts: 12
Joined: Wed Dec 23, 2020 3:20 pm

Scaling text in printf

Post 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
eliddell
Prole
Posts: 20
Joined: Sat Dec 10, 2016 6:38 pm

Re: Scaling text in printf

Post 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.
User avatar
GVovkiv
Party member
Posts: 686
Joined: Fri Jan 15, 2021 7:29 am

Re: Scaling text in printf

Post 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
jefolo
Prole
Posts: 12
Joined: Wed Dec 23, 2020 3:20 pm

Re: Scaling text in printf

Post 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....
User avatar
GVovkiv
Party member
Posts: 686
Joined: Fri Jan 15, 2021 7:29 am

Re: Scaling text in printf

Post 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"
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: Scaling text in printf

Post 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.
jefolo
Prole
Posts: 12
Joined: Wed Dec 23, 2020 3:20 pm

Re: Scaling text in printf

Post 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!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests