graphics: Unify dumping logic

This commit is contained in:
Hajime Hoshi 2019-07-20 01:42:19 +09:00
parent d78db1738d
commit 0bf12d5519
5 changed files with 21 additions and 27 deletions

View File

@ -20,11 +20,9 @@ package ebiten
import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/hajimehoshi/ebiten/internal/png"
"github.com/hajimehoshi/ebiten/internal/shareable"
)
@ -49,30 +47,12 @@ func availableFilename(prefix, postfix string) (string, error) {
}
func takeScreenshot(screen *Image) error {
dump := func() (string, error) {
f, err := ioutil.TempFile("", "")
if err != nil {
return "", err
}
defer f.Close()
if err := png.Encode(f, screen); err != nil {
return "", err
}
return f.Name(), nil
}
name, err := dump()
if err != nil {
return err
}
newname, err := availableFilename("screenshot_", ".png")
if err != nil {
return err
}
if err := os.Rename(name, newname); err != nil {
if err := screen.mipmap.orig.Dump(newname); err != nil {
return err
}

View File

@ -18,7 +18,7 @@ import (
"fmt"
"image"
"os"
"path/filepath"
"strings"
"github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/driver"
@ -177,12 +177,13 @@ func (i *Image) IsInvalidated() bool {
return i.image.IsInvalidated()
}
// DumpAt dumps the image to the specified directory.
// The filename is determined by the image's ID.
// Dump dumps the image to the specified path.
// In the path, '*' is replaced with the image's ID.
//
// This is for testing usage.
func (i *Image) DumpAt(dir string) error {
f, err := os.Create(filepath.Join(dir, fmt.Sprintf("%d.png", i.id)))
func (i *Image) Dump(path string) error {
path = strings.ReplaceAll(path, "*", fmt.Sprintf("%d", i.id))
f, err := os.Create(path)
if err != nil {
return err
}

View File

@ -583,3 +583,7 @@ func (i *Image) isInvalidated() bool {
graphicscommand.FlushCommands()
return i.image.IsInvalidated()
}
func (i *Image) Dump(path string) error {
return i.image.Dump(path)
}

View File

@ -15,6 +15,8 @@
package restorable
import (
"path/filepath"
"github.com/hajimehoshi/ebiten/internal/graphicscommand"
)
@ -99,7 +101,7 @@ func DumpImages(dir string) error {
if img.screen {
continue
}
if err := img.image.DumpAt(dir); err != nil {
if err := img.image.Dump(filepath.Join(dir, "*.png")); err != nil {
return err
}
}

View File

@ -492,6 +492,13 @@ func (i *Image) MakeVolatile() {
i.neverShared = true
}
func (i *Image) Dump(path string) error {
backendsM.Lock()
defer backendsM.Unlock()
return i.backend.restorable.Dump(path)
}
func NewScreenFramebufferImage(width, height int) *Image {
backendsM.Lock()
defer backendsM.Unlock()