mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
internal/graphicscommand: move availableFilename to graphicscommand
This commit is contained in:
parent
d66c552912
commit
ae41530f1c
@ -18,64 +18,39 @@
|
|||||||
package ebiten
|
package ebiten
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/debug"
|
"github.com/hajimehoshi/ebiten/v2/internal/debug"
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/ui"
|
"github.com/hajimehoshi/ebiten/v2/internal/ui"
|
||||||
)
|
)
|
||||||
|
|
||||||
// availableFilename returns a filename that is valid as a new file or directory.
|
func datetimeForFilename() string {
|
||||||
func availableFilename(prefix, postfix string) (string, error) {
|
|
||||||
const datetimeFormat = "20060102030405"
|
const datetimeFormat = "20060102030405"
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
name := fmt.Sprintf("%s%s%s", prefix, now.Format(datetimeFormat), postfix)
|
return now.Format(datetimeFormat)
|
||||||
for i := 1; ; i++ {
|
|
||||||
if _, err := os.Stat(name); err != nil {
|
|
||||||
if os.IsNotExist(err) || errors.Is(err, syscall.ENOSYS) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if !os.IsNotExist(err) {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
name = fmt.Sprintf("%s%s_%d%s", prefix, now.Format(datetimeFormat), i, postfix)
|
|
||||||
}
|
|
||||||
return name, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func takeScreenshot(screen *Image) error {
|
func takeScreenshot(screen *Image) error {
|
||||||
newname, err := availableFilename("screenshot_", ".png")
|
name := "screenshot_" + datetimeForFilename() + ".png"
|
||||||
|
blackbg := !IsScreenTransparent()
|
||||||
|
dumpedName, err := screen.image.DumpScreenshot(name, blackbg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if _, err := fmt.Fprintf(os.Stderr, "Saved screenshot: %s\n", dumpedName); err != nil {
|
||||||
blackbg := !IsScreenTransparent()
|
|
||||||
if err := screen.image.DumpScreenshot(newname, blackbg); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(os.Stderr, "Saved screenshot: %s\n", newname); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func dumpInternalImages() error {
|
func dumpInternalImages() error {
|
||||||
dir, err := availableFilename("internalimages_", "")
|
dumpedDir, err := ui.DumpImages("internalimages_" + datetimeForFilename())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if _, err := fmt.Fprintf(os.Stderr, "Dumped the internal images at: %s\n", dumpedDir); err != nil {
|
||||||
if err := ui.DumpImages(dir); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(os.Stderr, "Dumped the internal images at: %s\n", dir); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -733,7 +733,7 @@ func (i *Image) allocate(putOnAtlas bool) {
|
|||||||
i.node = n
|
i.node = n
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool) error {
|
func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool) (string, error) {
|
||||||
backendsM.Lock()
|
backendsM.Lock()
|
||||||
defer backendsM.Unlock()
|
defer backendsM.Unlock()
|
||||||
|
|
||||||
@ -786,7 +786,7 @@ func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) error {
|
func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) (string, error) {
|
||||||
backendsM.Lock()
|
backendsM.Lock()
|
||||||
defer backendsM.Unlock()
|
defer backendsM.Unlock()
|
||||||
return restorable.DumpImages(graphicsDriver, dir)
|
return restorable.DumpImages(graphicsDriver, dir)
|
||||||
|
@ -113,7 +113,7 @@ func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name string, blackbg bool) error {
|
func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name string, blackbg bool) (string, error) {
|
||||||
checkDelayedCommandsFlushed("Dump")
|
checkDelayedCommandsFlushed("Dump")
|
||||||
return i.img.DumpScreenshot(graphicsDriver, name, blackbg)
|
return i.img.DumpScreenshot(graphicsDriver, name, blackbg)
|
||||||
}
|
}
|
||||||
|
@ -39,26 +39,26 @@ func download(buf *bytes.Buffer, mime string, path string) {
|
|||||||
a.Call("click")
|
a.Call("click")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool, rect image.Rectangle) error {
|
func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool, rect image.Rectangle) (string, error) {
|
||||||
// Screen image cannot be dumped.
|
// Screen image cannot be dumped.
|
||||||
if i.screen {
|
if i.screen {
|
||||||
return nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
if err := i.dumpTo(buf, graphicsDriver, blackbg, rect); err != nil {
|
if err := i.dumpTo(buf, graphicsDriver, blackbg, rect); err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
download(buf, "image/png", i.dumpName(path))
|
download(buf, "image/png", i.dumpName(path))
|
||||||
|
|
||||||
return nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DumpImages dumps all the specified images to the specified directory.
|
// DumpImages dumps all the specified images to the specified directory.
|
||||||
//
|
//
|
||||||
// This is for testing usage.
|
// This is for testing usage.
|
||||||
func DumpImages(images []*Image, graphicsDriver graphicsdriver.Graphics, dir string) error {
|
func DumpImages(images []*Image, graphicsDriver graphicsdriver.Graphics, dir string) (string, error) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
zw := zip.NewWriter(buf)
|
zw := zip.NewWriter(buf)
|
||||||
|
|
||||||
@ -70,17 +70,18 @@ func DumpImages(images []*Image, graphicsDriver graphicsdriver.Graphics, dir str
|
|||||||
|
|
||||||
f, err := zw.Create(img.dumpName("*.png"))
|
f, err := zw.Create(img.dumpName("*.png"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := img.dumpTo(f, graphicsDriver, false, image.Rect(0, 0, img.width, img.height)); err != nil {
|
if err := img.dumpTo(f, graphicsDriver, false, image.Rect(0, 0, img.width, img.height)); err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
zw.Close()
|
zw.Close()
|
||||||
|
|
||||||
download(buf, "archive/zip", dir+".zip")
|
zip := dir + ".zip"
|
||||||
|
download(buf, "archive/zip", zip)
|
||||||
|
|
||||||
return nil
|
return zip, nil
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
package graphicscommand
|
package graphicscommand
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -25,38 +27,74 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool, rect image.Rectangle) error {
|
func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool, rect image.Rectangle) (string, error) {
|
||||||
// Screen image cannot be dumped.
|
p, err := availableFilename(path)
|
||||||
if i.screen {
|
if err != nil {
|
||||||
return nil
|
return "", err
|
||||||
}
|
}
|
||||||
|
path = p
|
||||||
|
|
||||||
path = i.dumpName(path)
|
path = i.dumpName(path)
|
||||||
f, err := os.Create(path)
|
f, err := os.Create(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
if err := i.dumpTo(f, graphicsDriver, blackbg, rect); err != nil {
|
if err := i.dumpTo(f, graphicsDriver, blackbg, rect); err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DumpImages dumps all the current images to the specified directory.
|
// DumpImages dumps all the current images to the specified directory.
|
||||||
//
|
//
|
||||||
// This is for testing usage.
|
// This is for testing usage.
|
||||||
func DumpImages(images []*Image, graphicsDriver graphicsdriver.Graphics, dir string) error {
|
func DumpImages(images []*Image, graphicsDriver graphicsdriver.Graphics, dir string) (string, error) {
|
||||||
|
d, err := availableFilename(dir)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
dir = d
|
||||||
|
|
||||||
if err := os.Mkdir(dir, 0755); err != nil {
|
if err := os.Mkdir(dir, 0755); err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, img := range images {
|
for _, img := range images {
|
||||||
if err := img.Dump(graphicsDriver, filepath.Join(dir, "*.png"), false, image.Rect(0, 0, img.width, img.height)); err != nil {
|
// Screen image cannot be dumped.
|
||||||
return err
|
if img.screen {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Create(filepath.Join(dir, img.dumpName("*.png")))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
if err := img.dumpTo(f, graphicsDriver, false, image.Rect(0, 0, img.width, img.height)); err != nil {
|
||||||
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
return dir, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// availableFilename returns a filename that is valid as a new file or directory.
|
||||||
|
func availableFilename(name string) (string, error) {
|
||||||
|
ext := filepath.Ext(name)
|
||||||
|
base := name[:len(name)-len(ext)]
|
||||||
|
|
||||||
|
for i := 1; ; i++ {
|
||||||
|
if _, err := os.Stat(name); err != nil {
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
name = fmt.Sprintf("%s_%d%s", base, i, ext)
|
||||||
|
}
|
||||||
|
return name, nil
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ func New(width, height int, imageType atlas.ImageType) *Mipmap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mipmap) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name string, blackbg bool) error {
|
func (m *Mipmap) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name string, blackbg bool) (string, error) {
|
||||||
return m.orig.DumpScreenshot(graphicsDriver, name, blackbg)
|
return m.orig.DumpScreenshot(graphicsDriver, name, blackbg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -668,7 +668,7 @@ func (i *Image) isInvalidated(graphicsDriver graphicsdriver.Graphics) (bool, err
|
|||||||
return i.image.IsInvalidated(), nil
|
return i.image.IsInvalidated(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool, rect image.Rectangle) error {
|
func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool, rect image.Rectangle) (string, error) {
|
||||||
return i.image.Dump(graphicsDriver, path, blackbg, rect)
|
return i.image.Dump(graphicsDriver, path, blackbg, rect)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,7 +122,7 @@ func RestoreIfNeeded(graphicsDriver graphicsdriver.Graphics) error {
|
|||||||
// DumpImages dumps all the current images to the specified directory.
|
// DumpImages dumps all the current images to the specified directory.
|
||||||
//
|
//
|
||||||
// This is for testing usage.
|
// This is for testing usage.
|
||||||
func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) error {
|
func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) (string, error) {
|
||||||
images := make([]*graphicscommand.Image, 0, len(theImages.images))
|
images := make([]*graphicscommand.Image, 0, len(theImages.images))
|
||||||
for img := range theImages.images {
|
for img := range theImages.images {
|
||||||
images = append(images, img.image)
|
images = append(images, img.image)
|
||||||
|
@ -89,11 +89,11 @@ func (i *Image) ReadPixels(pixels []byte, x, y, width, height int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) DumpScreenshot(name string, blackbg bool) error {
|
func (i *Image) DumpScreenshot(name string, blackbg bool) (string, error) {
|
||||||
return theUI.dumpScreenshot(i.mipmap, name, blackbg)
|
return theUI.dumpScreenshot(i.mipmap, name, blackbg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DumpImages(dir string) error {
|
func DumpImages(dir string) (string, error) {
|
||||||
return theUI.dumpImages(dir)
|
return theUI.dumpImages(dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,10 +95,10 @@ func (u *UserInterface) readPixels(mipmap *mipmap.Mipmap, pixels []byte, x, y, w
|
|||||||
return mipmap.ReadPixels(u.graphicsDriver, pixels, x, y, width, height)
|
return mipmap.ReadPixels(u.graphicsDriver, pixels, x, y, width, height)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) dumpScreenshot(mipmap *mipmap.Mipmap, name string, blackbg bool) error {
|
func (u *UserInterface) dumpScreenshot(mipmap *mipmap.Mipmap, name string, blackbg bool) (string, error) {
|
||||||
return mipmap.DumpScreenshot(u.graphicsDriver, name, blackbg)
|
return mipmap.DumpScreenshot(u.graphicsDriver, name, blackbg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) dumpImages(dir string) error {
|
func (u *UserInterface) dumpImages(dir string) (string, error) {
|
||||||
return atlas.DumpImages(u.graphicsDriver, dir)
|
return atlas.DumpImages(u.graphicsDriver, dir)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user