internal/graphicscommand: enable EBITENGINE_INTERNAL_IMAGES_KEY on browsers (#2283)

Closes #2270

Co-authored-by: Hajime Hoshi <hajimehoshi@gmail.com>
This commit is contained in:
Terra Brown 2022-08-30 14:10:10 -04:00 committed by GitHub
parent 216ca95960
commit 26a58d20b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 32 deletions

2
doc.go
View File

@ -63,7 +63,7 @@
//
// `EBITENGINE_INTERNAL_IMAGES_KEY` environment variable specifies the key
// to dump all the internal images. This is valid only when the build tag
// 'ebitenginedebug' is specified. This works only on desktops.
// 'ebitenginedebug' is specified. This works only on desktops and browsers.
//
// `EBITENGINE_GRAPHICS_LIBRARY` environment variable specifies the graphics library.
// If the specified graphics library is not available, RunGame returns an error.

View File

@ -71,10 +71,6 @@ func dumpInternalImages() error {
return err
}
if err := os.Mkdir(dir, 0755); err != nil {
return err
}
if err := ui.DumpImages(dir); err != nil {
return err
}

View File

@ -18,6 +18,8 @@ import (
"image"
"io"
"sort"
"strconv"
"strings"
"github.com/hajimehoshi/ebiten/v2/internal/affine"
"github.com/hajimehoshi/ebiten/v2/internal/debug"
@ -192,6 +194,10 @@ func (i *Image) IsInvalidated() bool {
return i.image.IsInvalidated()
}
func (i *Image) dumpName(path string) string {
return strings.ReplaceAll(path, "*", strconv.Itoa(i.id))
}
// dumpTo dumps the image to the specified writer.
//
// If blackbg is true, any alpha values in the dumped image will be 255.

View File

@ -15,43 +15,67 @@
package graphicscommand
import (
"archive/zip"
"bytes"
"image"
"path/filepath"
"strconv"
"strings"
"syscall/js"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
)
func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool, rect image.Rectangle) error {
// Screen image cannot be dumped.
if i.screen {
return nil
}
path = strings.ReplaceAll(path, "*", strconv.Itoa(i.id))
path = filepath.Base(path)
func download(buf *bytes.Buffer, mime string, path string) {
global := js.Global()
buf := &bytes.Buffer{}
if err := i.dumpTo(buf, graphicsDriver, blackbg, rect); err != nil {
return err
}
jsData := global.Get("Uint8Array").New(buf.Len())
js.CopyBytesToJS(jsData, buf.Bytes())
a := global.Get("document").Call("createElement", "a")
blob := global.Get("Blob").New(
[]interface{}{jsData},
map[string]interface{}{"type": "image/png"},
map[string]interface{}{"type": mime},
)
a.Set("href", global.Get("URL").Call("createObjectURL", blob))
a.Set("download", path)
a.Call("click")
}
func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool, rect image.Rectangle) error {
// Screen image cannot be dumped.
if i.screen {
return nil
}
buf := &bytes.Buffer{}
if err := i.dumpTo(buf, graphicsDriver, blackbg, rect); err != nil {
return err
}
download(buf, "image/png", i.dumpName(path))
return nil
}
// DumpImages dumps all the specified images to the specified directory.
//
// This is for testing usage.
func DumpImages(images []*Image, graphicsDriver graphicsdriver.Graphics, dir string) error {
buf := &bytes.Buffer{}
zw := zip.NewWriter(buf)
for _, img := range images {
f, err := zw.Create(img.dumpName("*.png"))
if err != nil {
return err
}
if err := img.dumpTo(f, graphicsDriver, false, image.Rect(0, 0, img.width, img.height)); err != nil {
return err
}
}
zw.Close()
download(buf, "archive/zip", dir+".zip")
return nil
}

View File

@ -20,8 +20,7 @@ package graphicscommand
import (
"image"
"os"
"strconv"
"strings"
"path/filepath"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
)
@ -32,7 +31,7 @@ func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackb
return nil
}
path = strings.ReplaceAll(path, "*", strconv.Itoa(i.id))
path = i.dumpName(path)
f, err := os.Create(path)
if err != nil {
return err
@ -45,3 +44,19 @@ func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackb
return nil
}
// DumpImages dumps all the current images to the specified directory.
//
// This is for testing usage.
func DumpImages(images []*Image, graphicsDriver graphicsdriver.Graphics, dir string) error {
if err := os.Mkdir(dir, 0755); err != nil {
return err
}
for _, img := range images {
if err := img.Dump(graphicsDriver, filepath.Join(dir, "*.png"), false, image.Rect(0, 0, img.width, img.height)); err != nil {
return err
}
}
return nil
}

View File

@ -15,8 +15,6 @@
package restorable
import (
"image"
"path/filepath"
"runtime"
"github.com/hajimehoshi/ebiten/v2/internal/debug"
@ -125,12 +123,12 @@ func RestoreIfNeeded(graphicsDriver graphicsdriver.Graphics) error {
//
// This is for testing usage.
func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) error {
images := make([]*graphicscommand.Image, 0, len(theImages.images))
for img := range theImages.images {
if err := img.Dump(graphicsDriver, filepath.Join(dir, "*.png"), false, image.Rect(0, 0, img.width, img.height)); err != nil {
return err
}
images = append(images, img.image)
}
return nil
return graphicscommand.DumpImages(images, graphicsDriver, dir)
}
// add adds img to the images.