Creating animated GIFs

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.
Post Reply
gianmichele
Citizen
Posts: 63
Joined: Tue Jan 14, 2014 11:03 pm

Creating animated GIFs

Post by gianmichele »

Any way to create animated GIFs straight out of love?

I have my replay system working now and would love to save a small GIF of that ;)
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Creating animated GIFs

Post by Davidobot »

PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Creating animated GIFs

Post by undef »

You can make a screenshot and save it as a png every single frame and then make a gif from that.
Make sure to set a fixed frame length (don't use dt, but use a fixed time step to update your game).

Code: Select all

local frame = 0
function love.update( dt )
    dt = 1/60 -- this way your gif will be at 60 fps
    frame = frame + 1
    local s = love.graphics.newScreenshot()
    s:encode( string.format( "recording/%012d.png", frame ) )
end
Then you can use ffmpeg or imagemagick to glue it together:

Code: Select all

-- before closing your game you can run this if you have ffmpeg installed (don't know if this works on windows)
os.execute( "ffmpeg -i " .. love.filesystem.getSaveDirectory() .. "/recording/%012d.png " .. name )
twitter | steam | indieDB

Check out quadrant on Steam!
gianmichele
Citizen
Posts: 63
Joined: Tue Jan 14, 2014 11:03 pm

Re: Creating animated GIFs

Post by gianmichele »

I'm looking at a pure lua (or FFI) encoder, since this needs to run without any external tool.

Can't really find anything on the web, but there's a lot of javascript encoders that I can use as a reference. Will keep you posted if I can do or find something useful ;)
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Creating animated GIFs

Post by Nixola »

You could try "porting" this to the FFI: http://sydney.edu.au/engineering/it/~gr ... bgif/gif.c
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Creating animated GIFs

Post by zorg »

I have a semi-workable solution:
Let's assume that you can store the replay data, and can "replay" it without the need for it to run "realtime".
  1. Your game records a replay.
  2. Replay is stored somewhere. (either in memory or dumped to the harddrive, or both)
  3. If the user wants to generate a gif of their run, have a function that simulates the game loop, but can take more time than when it would be played back in realtime.
  4. For each frame defined by the replay system, call love.graphics.newScreenshot (or if you're smart, render everything to a canvas in the above function, and get an imagedata from it.
  5. Use a 3rd party lib (either written in lua, or c and loaded in with the FFI) to create a GIF. The lib should be able to receive each frame separately though!
  6. Finally, export that final gif file.
If you do go with lg.newScreenshot instead of using a canvas and getting its imagedata, you should call that in a very specific spot; sadly, i don't remember whether it was before or after love.graphics.present was called (or some other function even)
I realize this isn't the clearest explanation, i'll try to re-word it later if i have time.
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
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Creating animated GIFs

Post by Nixola »

You do not have to worry about love.graphics.present, you can take a screenshot at the end of love.draw (so, before it). I've had similar concerns in the past, ended up calling it after love.graphics.present and everything was screwed up on pretty much any other PC, as the buffer was filled with garbage.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
rmcode
Party member
Posts: 454
Joined: Tue Jul 15, 2014 12:04 pm
Location: Germany
Contact:

Re: Creating animated GIFs

Post by rmcode »

This was posted on itch.io a few days ago:

http://itch.io/t/12612/creating-a-gif-for-your-game

EDIT: argh ... didn't read the "directly out of löve" part :P

EDIT 2: aaaaaaan a double post for good measure :(
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 76 guests