saving a canvas as an image

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.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: saving a canvas as an image

Post by grump »

dj--alex@ya.ru wrote: Thu May 09, 2019 5:35 pm i don't like ideology with "my documents" folder. good old games always do save in their folders.
Good old games did it this way because they're old, they were played on single-user systems, and filesystem permissions did not exist. Today, any user data is expected to be saved in the user's home directory, so that it works for multiple users without conflicts or permission issues. A properly configured system will not allow users to write in application directories, so your solution does not work everywhere and will break sooner or later due to permission conflicts, or it has to be started with administrator privileges all the time. Players may even lose data when they uninstall/reinstall the game. You're shooting yourself in the foot for no good reason.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: saving a canvas as an image

Post by zorg »

grump: Sure sounds like OS-es coddling the users to me, the single user on my own computer. What's next? Removing any way of allowing users to even access the filesystem via file managers, ls in terminals or dir in cmd/powershell? Just trust the OS managing your storage devices and install/save stuff wherever it feels like? :v but i digress. This is discord tier debate material anyway : P

Anyway,
dj--alex@ya.ru wrote: Thu May 09, 2019 5:35 pm game already save game in special folder inside GAME folder,
i just want to save image with savegame folder.
You're right, i didn't fully understand what you wanted to do, i still don't, completely... if you actually managed to make your game save into its own folder where main.lua is, and you want it to save images into there as well, just get the FileData that's returned by the :encode() method, do a :getString() on it, and finally use your method of saving into the game's folder to save your image like that; it should work.

Alternatively, you can try HDPLocust's solution, if that worked (it should, it's a lua function, but again, depending on the OS, the paths may use different separators, lack drive letters, or even not support special... let's just say non-ASCII characters.
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.
User avatar
pgimeno
Party member
Posts: 3655
Joined: Sun Oct 18, 2015 2:58 pm

Re: saving a canvas as an image

Post by pgimeno »

Beware that the os.rename() method may not work if the destination is in a different filesystem. I'm not sure it will work for example with an SD card on Android.
dj--alex@ya.ru
Prole
Posts: 23
Joined: Sat Jun 17, 2017 1:23 pm

Re: saving a canvas as an image

Post by dj--alex@ya.ru »

i have no idea...
it just don't work and get some trash useless messages

realfilename=sourcewrite(datatowrite,levelname);
--realfilename - is correct path from savegame operator.
ImageData = GAMEWINDOWCANVAS:newImageData();
filedata = ImageData:encode( "png" );
realfilename=realfilename..".png";
if (ossys~="Android") then
local filepng = io.open(realfilename, 'wb');
if (filepng~=nil) then filepng:write(filedata); --STRING EXPECTED GOT USERDATA!!!!!!!WTF????!!!!!!!!!!!!!
filepng:close();
end;
end
if (ossys=="Android") then
love.filesystem.write(realfilename, filedata) -- for android
end;
smsg1="realfilenam="..realfilename.."";


It creates Png file with 0x0 and size 0 bytes.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: saving a canvas as an image

Post by zorg »

dj--alex@ya.ru wrote: Thu May 09, 2019 9:35 pm it just don't work and get some trash useless messages
That message was perfectly useful; Thank you for providing code, now i can tell you that you were missing a step i did mention previously:

Code: Select all

realfilename = sourcewrite(datatowrite, levelname)
imagedata    = GAMEWINDOWCANVAS:newImageData()
filedata     = imagedata:encode( "png" )
realfilename = realfilename .. ".png"
if (ossys ~= "Android") then
	local filepng = io.open(realfilename, 'wb')
	if (filepng ~= nil) then
		filepng:write(filedata:getString()) -- <- this right here; now it's a string.
		filepng:close()
	end
else
	love.filesystem.write(realfilename, filedata) -- i have a feeling this won't work though, chief. :|
end

smsg1 = "realfilename = " .. realfilename
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.
dj--alex@ya.ru
Prole
Posts: 23
Joined: Sat Jun 17, 2017 1:23 pm

Re: saving a canvas as an image

Post by dj--alex@ya.ru »

wow! thanks i test it! it's a really long problem)
on android i use standart function.
it's really works on pc!
if it not works on android i think i can still use old command with canvas
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests