Nicolas
Nicolas

Reputation: 7111

Love2d can't find font file in the same directory

I'm using love2d 0.10.1. Love2d returns error that font file cannot be loaded as it doesn't exist. However the file is in a child directory. I tried giving full path with filename and it won't work. The font file has to be in another directory because in will not be included in the final exe.

My current directories look like this

--- love 2d
------ game
---------- main.lua
---------- fonts
-------------- segoeui.ttf
------ love.exe

And will look like this when the final exe is made

--- love 2d
------ fonts
---------- segoeui.ttf
------ game.exe

My code (simplified)

love.window.setMode(500, 500)  -- Open window
font = love.graphics.newFont('segoeui.ttf', 20)  -- Load font
love.graphics.print(text, 100, 100)  -- Print text
love.graphics.present()  -- Display text
while true do end

Error message

enter image description here

I found someone saying this is because love can't access folders outside main one so I tried this

love.window.setMode(500, 500)  -- Open window
local file = io.open("fonts\\segoeui.ttf", "r")
local content = file:read "*a"
local data, err = love.filesystem.newFileData(content, 'segoeui.ttf')
font = love.graphics.newFont(data, 20)  -- Load font
love.graphics.print("hey", 100, 100)  -- Print text
love.graphics.present()  -- Display text
while true do end

This way io.open opens the file successfully, content is read then converted into a FileData and then fed to newFont. However this returns the error that font can't be loaded because data is incorrect. I guess it's not the way to load it.

Upvotes: 1

Views: 1225

Answers (1)

Cricket
Cricket

Reputation: 148

If I'm not mistaken, you want the font to be in the directory of the application that you're running with love2d (the .lua files), not in the directory with the love2d executable.

Upvotes: 1

Related Questions