I'm kind of confused on what you are specifically asking...so I'll just run through using love.graphics.print()
There are two ways of dealing with fonts. Either just simply use the pre-loaded font (I think it's Arial - 12pt), in which case you don't need to do any loading with love.graphics.newFont(), or import your own:
Code: Select all
function love.load()
font = love.graphics.newFont("PATH\TO\FONT", size)
end
Unlike many other programming languages, all declared variables in Lua are implicitly set to global, meaning they can be accessed from anywhere within the code once they have been declared. So this:
Is pretty much the same as this:
With that in mind, if you go the route of declaring a new font, that variable is accessible everywhere. However, in order for love to start writing with that font, you must use love.graphics.setFont() (In LOVE 0.7.2, you could cut out the middle man and simply load a new font directly into setFont(), but 0.8.0 [the next release] gets rid of that feature).
So updating our font code:
Code: Select all
function love.load()
font = love.graphics.newFont("PATH\TO\FONT", size)
love.graphics.setFont(font)
end
This will now make all calls to love.graphics.print() or love.graphics.printf() use that font.
Now, to display the font, you are needing to draw it onto the screen. In LOVE, all drawing (repeat: ALL DRAWING) must original from the callback love.draw() in some form or fashion. So, when we have some string of text we want to be in the window, we do:
Code: Select all
function love.draw()
love.graphics.print("Hello, world!", x-coordinate, y-coordinate)
end
However, this would cause "Hello, world!" to be always drawn to the screen, and it seemed as if you wanted to toggle it on or off with a key press. love.keypressed(), like love.draw(), must handle all key pressed events. Additionally, like all the love callbacks, you should only declare it once.
What I do for toggling is this:
Code: Select all
function love.load()
textToggle = false
end
function love.draw()
if textToggle then
love.graphics.print("Hello, world!", 0, 0)
end
end
local function toggle(bool) -- A helper function for toggling
if bool then return false -- for if you do a lot of toggling
else return true end -- Returns false if true and true if false
end
function love.keypressed(k)
if k == "p" then
textToggle = toggle(textToggle)
end
end
If you want to only have the text appear as you are holding down a key, the easiest way would be:
Code: Select all
function love.draw()
if love.keyboard.isDown("p") then
love.graphics.print("Hello, world!", 0, 0)
end
end
Although for some reason I got yelled at by Robin for suggesting that to someone.