Further optimization for RecordScreenAsGIF

This commit is contained in:
Hajime Hoshi 2014-12-28 04:10:47 +09:00
parent 4b78ae3d36
commit 89a3297e74
2 changed files with 13 additions and 5 deletions

View File

@ -18,7 +18,7 @@ import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"image" "image"
"image/color" "image/color"
//"image/color/palette" "image/color/palette"
"image/draw" "image/draw"
"image/gif" "image/gif"
"io" "io"
@ -35,7 +35,7 @@ type recorder struct {
wg sync.WaitGroup wg sync.WaitGroup
} }
var palette = color.Palette{ var cheapPalette = color.Palette{
color.RGBA{0x00, 0x00, 0x00, 0xff}, color.RGBA{0x00, 0x00, 0x00, 0xff},
color.RGBA{0xff, 0x00, 0x00, 0xff}, color.RGBA{0xff, 0x00, 0x00, 0xff},
color.RGBA{0x00, 0xff, 0x00, 0xff}, color.RGBA{0x00, 0xff, 0x00, 0xff},
@ -55,6 +55,13 @@ func (r *recorder) delay() int {
return delay return delay
} }
func (r *recorder) palette() color.Palette {
if 1 < (r.frameNum-1)/r.skips+1 {
return cheapPalette
}
return palette.Plan9
}
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
@ -74,7 +81,7 @@ func (r *recorder) update(screen *ebiten.Image) error {
s := image.NewNRGBA(screen.Bounds()) s := image.NewNRGBA(screen.Bounds())
draw.Draw(s, s.Bounds(), screen, screen.Bounds().Min, draw.Src) draw.Draw(s, s.Bounds(), screen, screen.Bounds().Min, draw.Src)
img := image.NewPaletted(s.Bounds(), palette) img := image.NewPaletted(s.Bounds(), r.palette())
f := r.currentFrame / r.skips f := r.currentFrame / r.skips
r.wg.Add(1) r.wg.Add(1)
go func() { go func() {
@ -122,7 +129,7 @@ func RecordScreenAsGIF(update func(*ebiten.Image) error, out io.Writer, frameNum
inner: update, inner: update,
writer: out, writer: out,
frameNum: frameNum, frameNum: frameNum,
skips: 4, skips: 5,
} }
return r.update return r.update
} }

View File

@ -42,7 +42,8 @@ func main() {
} }
game := blocks.NewGame() game := blocks.NewGame()
if err := ebiten.Run(game.Update, blocks.ScreenWidth, blocks.ScreenHeight, 2, "Blocks (Ebiten Demo)"); err != nil { update := game.Update
if err := ebiten.Run(update, blocks.ScreenWidth, blocks.ScreenHeight, 2, "Blocks (Ebiten Demo)"); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }