mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 11:12:44 +01:00
Speed up: RecordScreenAsGIF
This commit is contained in:
parent
4bf4b0c264
commit
ed777b7e94
@ -17,35 +17,77 @@ package ebitenutil
|
|||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"image"
|
"image"
|
||||||
"image/color/palette"
|
"image/color"
|
||||||
|
//"image/color/palette"
|
||||||
"image/draw"
|
"image/draw"
|
||||||
"image/gif"
|
"image/gif"
|
||||||
"io"
|
"io"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type recorder struct {
|
type recorder struct {
|
||||||
inner func(screen *ebiten.Image) error
|
inner func(screen *ebiten.Image) error
|
||||||
writer io.Writer
|
writer io.Writer
|
||||||
|
frameNum int
|
||||||
|
skips int
|
||||||
gif *gif.GIF
|
gif *gif.GIF
|
||||||
currentFrame int
|
currentFrame int
|
||||||
|
wg sync.WaitGroup
|
||||||
|
}
|
||||||
|
|
||||||
|
var palette = color.Palette{
|
||||||
|
color.RGBA{0x00, 0x00, 0x00, 0xff},
|
||||||
|
color.RGBA{0xff, 0x00, 0x00, 0xff},
|
||||||
|
color.RGBA{0x00, 0xff, 0x00, 0xff},
|
||||||
|
color.RGBA{0x00, 0x00, 0xff, 0xff},
|
||||||
|
color.RGBA{0xff, 0xff, 0x00, 0xff},
|
||||||
|
color.RGBA{0xff, 0x00, 0xff, 0xff},
|
||||||
|
color.RGBA{0x00, 0xff, 0xff, 0xff},
|
||||||
|
color.RGBA{0xff, 0xff, 0xff, 0xff},
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *recorder) delay() int {
|
||||||
|
// Assume that the FPS is 60.
|
||||||
|
delay := 100 * r.skips / 60
|
||||||
|
if delay < 2 {
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
return delay
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *recorder) update(screen *ebiten.Image) error {
|
func (r *recorder) update(screen *ebiten.Image) error {
|
||||||
if err := r.inner(screen); err != nil {
|
if err := r.inner(screen); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if r.currentFrame == len(r.gif.Image) {
|
if r.currentFrame == r.frameNum {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
img := image.NewPaletted(screen.Bounds(), palette.Plan9)
|
if r.currentFrame%r.skips == 0 {
|
||||||
// TODO: This is too slow.
|
if r.gif == nil {
|
||||||
draw.Draw(img, img.Bounds(), screen, screen.Bounds().Min, draw.Src)
|
num := (r.frameNum-1)/r.skips + 1
|
||||||
r.gif.Image[r.currentFrame] = img
|
r.gif = &gif.GIF{
|
||||||
// The actual FPS is 60, but GIF can't have such FPS. Set 50 FPS instead.
|
Image: make([]*image.Paletted, num),
|
||||||
r.gif.Delay[r.currentFrame] = 2
|
Delay: make([]int, num),
|
||||||
|
LoopCount: -1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s := image.NewNRGBA(screen.Bounds())
|
||||||
|
draw.Draw(s, s.Bounds(), screen, screen.Bounds().Min, draw.Src)
|
||||||
|
|
||||||
|
img := image.NewPaletted(s.Bounds(), palette)
|
||||||
|
f := r.currentFrame / r.skips
|
||||||
|
r.wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer r.wg.Done()
|
||||||
|
draw.FloydSteinberg.Draw(img, img.Bounds(), s, s.Bounds().Min)
|
||||||
|
r.gif.Image[f] = img
|
||||||
|
r.gif.Delay[f] = r.delay()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
r.currentFrame++
|
r.currentFrame++
|
||||||
if r.currentFrame == len(r.gif.Image) {
|
if r.currentFrame == r.frameNum {
|
||||||
|
r.wg.Wait()
|
||||||
if err := gif.EncodeAll(r.writer, r.gif); err != nil {
|
if err := gif.EncodeAll(r.writer, r.gif); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -79,11 +121,8 @@ func RecordScreenAsGIF(update func(*ebiten.Image) error, out io.Writer, frameNum
|
|||||||
r := &recorder{
|
r := &recorder{
|
||||||
inner: update,
|
inner: update,
|
||||||
writer: out,
|
writer: out,
|
||||||
gif: &gif.GIF{
|
frameNum: frameNum,
|
||||||
Image: make([]*image.Paletted, frameNum),
|
skips: 4,
|
||||||
Delay: make([]int, frameNum),
|
|
||||||
LoopCount: -1,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
return r.update
|
return r.update
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user