[SOLVED]Saving images opened via filedropped

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
jalamaya
Prole
Posts: 21
Joined: Wed Jan 13, 2016 9:14 pm

[SOLVED]Saving images opened via filedropped

Post 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")
Last edited by jalamaya on Tue Feb 09, 2016 4:43 pm, edited 2 times in total.
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Saving images opened via filedropped

Post by zorg »

Explain what you mean by the way you planned, please. What does it do? what did you expect it to do?
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
jalamaya
Prole
Posts: 21
Joined: Wed Jan 13, 2016 9:14 pm

Re: Saving images opened via filedropped

Post 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
jalamaya
Prole
Posts: 21
Joined: Wed Jan 13, 2016 9:14 pm

Re: Saving images opened via filedropped

Post 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.
jalamaya
Prole
Posts: 21
Joined: Wed Jan 13, 2016 9:14 pm

Re: Saving images opened via filedropped

Post 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'))))
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Saving images opened via filedropped

Post 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
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Saving images opened via filedropped

Post 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.)
jalamaya
Prole
Posts: 21
Joined: Wed Jan 13, 2016 9:14 pm

Re: Saving images opened via filedropped

Post 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
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Saving images opened via filedropped

Post by bartbes »

Oops, my bad! But in that case, don't do what you said. ;)
jalamaya
Prole
Posts: 21
Joined: Wed Jan 13, 2016 9:14 pm

Re: Saving images opened via filedropped

Post 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
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests