ebiten/ebitenutil/gif.go

136 lines
3.3 KiB
Go
Raw Normal View History

// Copyright 2014 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2014-12-23 16:08:22 +01:00
package ebitenutil
import (
"github.com/hajimehoshi/ebiten"
"image"
2014-12-27 19:54:07 +01:00
"image/color"
"image/color/palette"
2014-12-23 16:08:22 +01:00
"image/draw"
"image/gif"
"io"
2014-12-27 19:54:07 +01:00
"sync"
2014-12-23 16:08:22 +01:00
)
type recorder struct {
inner func(screen *ebiten.Image) error
writer io.Writer
2014-12-27 19:54:07 +01:00
frameNum int
skips int
2014-12-23 16:08:22 +01:00
gif *gif.GIF
currentFrame int
2014-12-27 19:54:07 +01:00
wg sync.WaitGroup
}
var cheapPalette = color.Palette{
2014-12-27 19:54:07 +01:00
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
2014-12-23 16:08:22 +01:00
}
func (r *recorder) palette() color.Palette {
if 1 < (r.frameNum-1)/r.skips+1 {
return cheapPalette
}
return palette.Plan9
}
2014-12-23 16:08:22 +01:00
func (r *recorder) update(screen *ebiten.Image) error {
if err := r.inner(screen); err != nil {
return err
}
2014-12-27 19:54:07 +01:00
if r.currentFrame == r.frameNum {
2014-12-23 16:08:22 +01:00
return nil
}
2014-12-27 19:54:07 +01:00
if r.currentFrame%r.skips == 0 {
if r.gif == nil {
num := (r.frameNum-1)/r.skips + 1
r.gif = &gif.GIF{
Image: make([]*image.Paletted, num),
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(), r.palette())
2014-12-27 19:54:07 +01:00
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()
}()
}
2014-12-23 16:08:22 +01:00
r.currentFrame++
2014-12-27 19:54:07 +01:00
if r.currentFrame == r.frameNum {
r.wg.Wait()
2014-12-23 16:08:22 +01:00
if err := gif.EncodeAll(r.writer, r.gif); err != nil {
return err
}
}
return nil
}
// RecordScreenAsGIF returns updating function with recording the screen as an animation GIF image.
//
// This encodes each screen at each frame and may slows the application.
2014-12-27 16:52:24 +01:00
//
// Here is the example to record initial 120 frames of your game:
//
// func update(screen *ebiten.Image) error {
// // ...
// }
//
// func main() {
// out, err := os.Create("output.gif")
// if err != nil {
// log.Fatal(err)
// }
// defer out.Close()
//
// update := RecordScreenAsGIF(update, out, 120)
// if err := ebiten.Run(update, 320, 240, 2, "Your game's title"); err != nil {
// log.Fatal(err)
// }
// }
2014-12-23 16:08:22 +01:00
func RecordScreenAsGIF(update func(*ebiten.Image) error, out io.Writer, frameNum int) func(*ebiten.Image) error {
r := &recorder{
2014-12-27 19:54:07 +01:00
inner: update,
writer: out,
frameNum: frameNum,
2014-12-28 07:26:12 +01:00
skips: 10,
2014-12-23 16:08:22 +01:00
}
return r.update
}