Page 1 of 2

[SOLVED]Saving images opened via filedropped

Posted: Mon Feb 08, 2016 1:43 pm
by jalamaya
i have a simple saving mechanism where i write the data using file:write() in a lua file(settings.lua) upon a certain event. then if you start the program, the main.lua would load data from settings.lua.

for boolean, numbers and strings, it works quite fine.

now i have a love.filedropped function and i want also to save the image dropped. i have no idea how to do this, i tried writing the imagedata but it doesnt work the way i planned

[SOLUTION]
in function love.filedropped()

Code: Select all

love.filesystem.write("default.jpg",data) 
in main.lua:

Code: Select all

defaultImg = love.graphics.newImage("default.jpg")

Re: Saving images opened via filedropped

Posted: Mon Feb 08, 2016 3:25 pm
by zorg
Explain what you mean by the way you planned, please. What does it do? what did you expect it to do?

Re: Saving images opened via filedropped

Posted: Mon Feb 08, 2016 3:47 pm
by jalamaya
zorg wrote:Explain what you mean by the way you planned, please. What does it do? what did you expect it to do?
sorry if i didn't explained it clearly :D :P

settings like: sounds, screen resolution, game mode, i save(string, boolean, numbers) the values by writing it in settings.lua. then i require it in main.lua then the data in settings.lua will be loaded so after everytime the user runs the program, the settings will be the same just how he/she set it before.

for images, the user will drag and drop the desired image using function love.filedropped(file).

i want the image set by the user to be the default image when he/she reruns the program

Re: Saving images opened via filedropped

Posted: Mon Feb 08, 2016 7:20 pm
by jalamaya
zorg wrote:Explain what you mean by the way you planned, please. What does it do? what did you expect it to do?
sorry again, i didnt read your comment carefully.

what i planned is

Code: Select all

file = io.open("image.jpg","w+")
file:write(tostring(data)) --data is the filedata of the image dragged and dropped
then in main.lua

Code: Select all

function love.load()
	defaultImage = love.graphics.newImage("image.jpg")
end
but i get an error message: Could not decode image (stb_image assertion 'n>=0 && n< (int)(sizeof(stbi_bmask)/sizeof(*stbi_bmask))'failed

and when i open "image.jpg" in file viewer, the image formed is very broken.

Re: Saving images opened via filedropped

Posted: Tue Feb 09, 2016 2:19 am
by jalamaya
is there a possible way to use this string:

ImageData: 0x375fc192f0

i got it from:

Code: Select all

file:write(tostring(love.image.newImageData(love.filesystem.newFileData(data, 'img', 'file'))))

Re: Saving images opened via filedropped

Posted: Tue Feb 09, 2016 3:27 am
by davisdude
I don't think so- AFAIK it's unique to the instance, just like tables are. But, I think you could try using ImageData:encode

Re: Saving images opened via filedropped

Posted: Tue Feb 09, 2016 9:51 am
by bartbes
I'd advise against using the lua io library in any capacity, and sticking to love.filesystem wherever possible. One issue you may be having is that you haven't opened the file in binary mode, causing the io lib/windows to completely break the file you're saving out. Guess what, love.filesystem doesn't have that problem. The paths are also tricky, as they depend on where the game was run from, and whether it was fused or not, generally leading to very brittle code when using io.

In case you're trying to load the dropped file into an image, use love.filesystem.newFileData on the data and pass the result to love.graphics.newImage. In case you're trying to write the actual dropped data to a file, don't do what jalamaya said, and just write the data directly. (But either use love.filesystem, or open your file in binary mode.)

Re: Saving images opened via filedropped

Posted: Tue Feb 09, 2016 11:41 am
by jalamaya
bartbes wrote:I'd advise against using the lua io library in any capacity, and sticking to love.filesystem wherever possible. One issue you may be having is that you haven't opened the file in binary mode, causing the io lib/windows to completely break the file you're saving out. Guess what, love.filesystem doesn't have that problem. The paths are also tricky, as they depend on where the game was run from, and whether it was fused or not, generally leading to very brittle code when using io.

In case you're trying to load the dropped file into an image, use love.filesystem.newFileData on the data and pass the result to love.graphics.newImage. In case you're trying to write the actual dropped data to a file, don't do what jalamaya said, and just write the data directly. (But either use love.filesystem, or open your file in binary mode.)
i am the OP hahaha :D

I will try what you suggested. thanks :D

Re: Saving images opened via filedropped

Posted: Tue Feb 09, 2016 11:44 am
by bartbes
Oops, my bad! But in that case, don't do what you said. ;)

Re: Saving images opened via filedropped

Posted: Tue Feb 09, 2016 12:42 pm
by jalamaya
bartbes wrote:Oops, my bad! But in that case, don't do what you said. ;)

what shouldn't i do again? im kinda confused sorry