default to default font

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.
User avatar
subrime
Citizen
Posts: 76
Joined: Thu Nov 13, 2008 6:18 pm
Location: Australia

default to default font

Post by subrime »

Just been browsing through the getting started section, and thought that something in keeping with making it as simple as possible to get things going would be to have the current font set to the default font when starting the load function.

Existing code would continue to work without change, and the example could be cut down to:

Code: Select all

function load()
   message = "Find love on the internet!"
end

function draw()
   love.graphics.draw(message, 100, 100)
end
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: default to default font

Post by rude »

I've been slightly reluctant to do this in the past, because it will in almost all cases but "hello world" lead to a unnecessary Font load.

However, you just gave me an idea which will satisfy everyone:

Code: Select all

if load then load() end

-- If a Font is set by now, then we're good.
-- Otherwise, set the default one.
if not love.graphics.getFont() then
   love.graphcis.setFont(love.default_font, 12)
end
User avatar
subrime
Citizen
Posts: 76
Joined: Thu Nov 13, 2008 6:18 pm
Location: Australia

Re: default to default font

Post by subrime »

Ah... I had no idea you were so keen on optimisation. Maybe a way to offset this a little (at least for programs that load at least one font) is load the default font by default, and the to swap it out the first time an explicit font load is performed.

Down sides are a little extra startup time, and a useless appendage for completely font free code (is this situation at all likely?).

-- my sausage brain just kicked in and I see your code is pretty much that, only better. ^^
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: default to default font

Post by rude »

The little extra startup time is completely negligible for Font-free code. Besides, it will be possible to override this behavior anyway.
u9_
Citizen
Posts: 54
Joined: Thu Oct 23, 2008 7:12 am

Re: default to default font

Post by u9_ »

Being torn between both sides (simplicity for the user vs. optimized code) I figured i will share my thoughts on this.

At first, i thought "of course there should be a default font loaded," but as rude pointed out, this is an unnecessary font load which is wasteful for every application except tutorials and very primitive games/prototypes and such.

The idea of checking if a font is set or not was brilliant, but won't work in all (many?) cases. Let me elaborate. If a game uses more then one font (and i believe this is very likely) the author would probably/possibly have no reason to call setFont() in the load() function, because he will have to set the font in the draw function whenever he needs to draw with a particular font.

So (surprising myself here) i think will vote that no font is loaded by default.
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: default to default font

Post by rude »

u9_ wrote:The idea of checking if a font is set or not was brilliant, but won't work in all (many?) cases. Let me elaborate. If a game uses more then one font (and i believe this is very likely) the author would probably/possibly have no reason to call setFont() in the load() function, because he will have to set the font in the draw function whenever he needs to draw with a particular font.
I did think about this, but I didn't seen any (good) way around it. 8-)

We could, of course, count the number of created Fonts instead of checking whether there is a currently set one.

Code: Select all

if load then load() end
if love.graphics.countCreatedFonts() == 0 then
   love.graphcis.setFont(love.default_font, 12)
end
User avatar
subrime
Citizen
Posts: 76
Joined: Thu Nov 13, 2008 6:18 pm
Location: Australia

Re: default to default font

Post by subrime »

Or, rather than putting the test/load-the-default-font code into the load function, it could be embedded at the start of the love.graphics.draw function such that any attempt to draw a message before a font has been set will trigger the loading of the default font.

Code: Select all

function original_draw(...) -- just a new name
  -- the existing draw code in all its glory
end

function first_draw(...) -- load a default font if required & restore the original draw function
  if type(arg[1])=='string' and not love.graphics.getFont() then
    love.graphcis.setFont(love.default_font, 12) 
    love.graphics.draw=original_draw -- now switch back to the normal behaviour
  end
  love.graphics.draw(unpack(arg))      -- and do it
end

love.graphics.draw=first_draw
u9_
Citizen
Posts: 54
Joined: Thu Oct 23, 2008 7:12 am

Re: default to default font

Post by u9_ »

waw... looking at that code makes me think Lua is awesome :) I'm sure rude will make a good decision (if any) :)
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: default to default font

Post by rude »

That, sir, was an amazingly good idea. 8-) I'm pretty sure your fake code will recurse forever unless the first argument is a string, but I get what you're trying to say.

For those not fluent in Lua yet: subrime's solution means that a default font will be loaded if needed. Games with some font already set will get an ultra-miniscule overhead the first time love.graphics.draw is called, although I'm not sure it even qualifies as "overhead".
User avatar
subrime
Citizen
Posts: 76
Joined: Thu Nov 13, 2008 6:18 pm
Location: Australia

Re: default to default font

Post by subrime »

Ah... you're right, I see the glitch now you point it out. In my head the last line of first_draw was meant to call original_draw directly, not the currently-pointed-to-love.graphics.draw. The slightly refined version:

Code: Select all

function original_draw(...) -- just a new name
  -- the existing draw code in all its glory
end

function first_draw(...) -- load a default font if required & restore the original draw function
  if type(arg[1])=='string' then            -- load font if needed, switch to original_draw 
    if  not love.graphics.getFont() then
      love.graphcis.setFont(love.default_font, 12)
    end
    love.graphics.draw=original_draw
  end
  original_draw(unpack(arg))               -- and do it (yay for bug-free code!)
end

love.graphics.draw=first_draw
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 4 guests