Page 1 of 1
Load image from different directory
Posted: Sun Dec 16, 2012 5:04 pm
by PartyCow
If a image is in another folder than the script, how will I load it?
Thanks
Re: Load image from different directory
Posted: Sun Dec 16, 2012 6:57 pm
by Robin
If you're saying what I think you're saying then you can't.
Otherwise: suppose your game is at "(....)/mygame/". Then your main.lua would need to be at "(....)/mygame/main.lua". If you have an image at "(....)/mygame/images/background.png", you would load it as:
Code: Select all
background = love.graphics.newImage("images/background.png")
Re: Load image from different directory
Posted: Thu Feb 08, 2018 5:11 am
by MoseGames
this should work:
Code: Select all
local file = io.open(self:path(path) , "r")
local contents = file:read("*all")
local data = love.filesystem.newFileData( contents, "img.png", "file" )
local imgdata = love.image.newImageData( data )
file:close()
local img = love.graphics.rawNewImage( imgdata )
It's a bit long, but it worked for me.
Re: Load image from different directory
Posted: Thu Feb 08, 2018 11:31 am
by zorg
MoseGames wrote: ↑Thu Feb 08, 2018 5:11 am
this should work:
Code: Select all
local file = io.open(self:path(path) , "r")
local contents = file:read("*all")
local data = love.filesystem.newFileData( contents, "img.png", "file" )
local imgdata = love.image.newImageData( data )
file:close()
local img = love.graphics.rawNewImage( imgdata )
It's a bit long, but it worked for me.
This is neat and all, but what's self in that context?
Also, what's love.graphics.rawNewImage??? last i checked, only love.graphics.newImage exists.
Anyway, io.open(path, 'r') is fine, since it does take a path, no need to make it complicated.
That said, you should only use lua's io functions if you want to access files from directories
ABOVE/OUTSIDE the one where your main.lua is; if it's a sub-directory, then you can access those simply by doing love.image.newImage('subfolder/image.png').