Hey, I'm trying to load an image into my level editor from an absolute file path. Right now i have it working with a relative spritepath but I need to have two identical "Graphics" folders, one for the game and one for the editor. (I don't want one of them under the folder of the other)
This is my code to load the file that I found online and tried to use for my project:
function LoadSprite (aSpritePath, aObjectName)
local file = assert(io.open(aSpritePath, "r"))
local filedata, err = love.filesystem.newFileData(file:read("*all"), aObjectName .. ".png")
file:close()
local image
if (err == nil) then
local imageData = love.image.newImageData(filedata)
image = love.graphics.newImage(imageData)
end
return image
end
I get this error: Could not decode file '<myImageName>.png' to ImageData: unsupported file format.
I've tried to search online, but I can't really find much about this.
Would be awesome if anyone could help me
[SOLVED] Loading image from absolute directory using IO.Open
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
[SOLVED] Loading image from absolute directory using IO.Open
Last edited by Forzi on Fri Oct 14, 2016 10:54 pm, edited 1 time in total.
- slime
- Solid Snayke
- Posts: 3163
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Loading image from absolute directory using IO.Open
Try opening the file in binary mode: io.open("rb") instead of io.open("r").
Re: Loading image from absolute directory using IO.Open
Omg that works! Thank you so much @slime! Funny that only one extra letter made it work #programminglife
Re: Loading image from absolute directory using IO.Open
Here is the working code if anyone else wanna load pictures outside of your project folder and with absolute paths!
function LoadSprite (aSpritePath, aObjectName)
local file = assert(io.open(aSpritePath, "rb"))
local filedata, err = love.filesystem.newFileData(file:read("*all"), aObjectName .. ".png")
local image = love.graphics.newImage("/Graphics/Objects/Pickupable/Floor2_Bedroom_MrTiny.png")
file:close()
if (err == nil) then
local imageData = love.image.newImageData(filedata)
image = love.graphics.newImage(imageData)
end
return image
end
--- > Use love.filesystem.getSource() to get the file path to your project!
function LoadSprite (aSpritePath, aObjectName)
local file = assert(io.open(aSpritePath, "rb"))
local filedata, err = love.filesystem.newFileData(file:read("*all"), aObjectName .. ".png")
local image = love.graphics.newImage("/Graphics/Objects/Pickupable/Floor2_Bedroom_MrTiny.png")
file:close()
if (err == nil) then
local imageData = love.image.newImageData(filedata)
image = love.graphics.newImage(imageData)
end
return image
end
--- > Use love.filesystem.getSource() to get the file path to your project!
Who is online
Users browsing this forum: whateverest and 5 guests