Issues

Issue #479 resolved

Odd love.graphics.getFont() behavior.

josefnpat
created an issue

So, originally when I had this issue, I just assumed it was me, but then someone (paritybit) had the same issue, and started asking questions on IRC.

When I run love.graphics.getFont(), it ought to return a font no matter what, even if it is the default font. Considering that the font has to be loaded into the system, it shouldn't be hard to change this behavior.

Consider the following;

I have written a module that backs up the current font, uses its own font, and then restores the old font;

{{{

!Lua

mod = {} mod.font = ...

function mod.draw() local old_font = love.graphics.getFont() love.graphics.setFont(mod.font) -- snip -- love.graphics.setFont(old_font) end }}}

In the current trunk, this will error out as old_font is nil, unless the user setFont() beforehand. But what if I want to set the font back to the default font?

Also, considering forward regression (e.g. 0.8.1) the "workaround" will not break;

{{{

!Lua

mod = {} mod.font = ...

function mod.draw() local old_font = love.graphics.getFont() love.graphics.setFont(mod.font) -- snip -- if old_font then love.graphics.setFont(old_font) end end }}}

Tell me what you think, as this was something that bothered me.

Comments (3)

  1. John Glover

    I (paritybit on IRC) agree. The behavior with other similar functions is to return a default value which allows me to use the pattern I first tried (getting the value, setting the value and then setting the value back to the original). For example, calling love.graphics.getBackgroundColor returns a value even when it hasn't been set explicitly.

  2. Boolsheet

    Like bartbes in IRC said, there is no font set at startup. If you thought there was, then you probably used love.graphics.print which creates a font for you in case there is none set yet.

    It doesn't automatically create a font at startup because once it is set, there will always be a 512x512 texture around. If the lover never uses fonts, this is wasted memory.

    Anyway, love.graphics.getFont was changed not too long ago to also create a Font if none is set.

    love.graphics.getBackgroundColor always returns values because it's OpenGL's clear color. This is already allocated and always around. The default love.run does call love.graphics.clear and kind of needs something. The default values itself come from OpenGL.

  3. Log in to comment