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:
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
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.
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.
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.
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.
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
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".
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:
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