Discord
Login
Community
DARK THEME

drawText draws different size text after first draw

// The first time the text is large and thin. After that, it's smaller and very bold.

init = function() drawn = 0 end

update = function() if keyboard.release.SPACE then drawn = 0 end end

draw = function() if not drawn then screen.clear() screen.drawText("Hello", 0, 0, 40, 40 ) drawn = 1 end

end

On discord:

FeniX — 03/27/2021

To fix issue of font not being loaded I started adding drawText with empty text at the beginning of the game.

Gilles — 03/27/2021

this is the best workaround, until the engine improves...

Got it. Thanks!

update

I tried that workaround, I couldn't get it to work.

But putting screen.setFont() in init does seem to work, and it keeps the original larger thinner font.

BTW what is the default font when started?

I see this behavior on a new project, even with setFont() in init. That is, it draws in a different font over the expected drawText() - though, not when it live refreshes, only a full load of the game. I also don't seem to see it on existing games, which seems really odd to me.

Is it just me? Or is there a known workaround?

This problem is annoying, it is caused by the fact that font loading is asynchronous (the font starts loading when you first use it, thus on the very first draw call, it is not ready yet and a default font is used instead ; then on subsequent draw calls, the font may finally become loaded, thus your text will finally be painted with the desired font).

The good news is that I just pushed two new functions which will help a lot! The function screen.loadFont(font_name) allows to initiate loading of a particular font. The function screen.isFontReady(font_name) allows to check whether a given font is actually loaded and ready to be used. You can also use it without argument, in which case it will check whether the default font is ready.

Here is the documentation I have added for these two functions:

screen.loadFont( font_name )

Initiates the loading of a font. Useful in conjunction with screen.isFontReady

screen.loadFont("DigitalDisco")
screen.isFontReady( font_name )

Returns 1 (true) if the given font is loaded and ready to be used. Make sure to call screen.loadFont first or your font may never be loaded.

You can omit the function argument, in which case it checks whether the current active font is loaded and ready to be used.

if screen.isFontReady() then
  // we can use the current font
  screen.drawText("MY TEXT",0,0,50)
end

screen.loadFont("DigitalDisco") // make sure DigitalDisco will be loaded

if screen.isFontReady("DigitalDisco")
  screen.setFont("DigitalDisco")
  screen.drawText("SOME OTHER TEXT",0,50,20)
end

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community