Search code examples
lualove2d

Love2D NewImageFont won't render


I'm working on a game and have created a simple numeric "font" for displaying the number of days.

Love2D's newImageFont() function lets one use a specifically-formatted image to create a font for any number of glyphs.

I've created my font, loaded it in my love.load() function thus:

dayFont = gr.newImageFont("/fonts/dayFont.png", "1234567")

Here's the font image file:

enter image description here

And then in love.draw(), I am trying to use it to display one number:

love.graphics.reset()
love.graphics.setFont(dayFont)
love.graphics.printf("1", 260, 20, 30, "left")

But nothing appears.

While troubleshooting, I discovered that when I don't set the font, the number does appear in the location expected, so the problem seems to be the imageFont itself.


Solution

  • The problem was in the 4th parameter of love.graphics.printf(), which sets the max width of the text. Because it was set to a max width of 30 pixels, and the glyphs themselves are 36 pixels wide, it simply doesn't render any glyphs at all.

    Updating the line in love.draw() to:

    love.graphics.printf("1", 260, 20, 40, "left")

    fixes the issue.