graphics: Use ID for dumped internal images

Updates #900
This commit is contained in:
Hajime Hoshi 2019-07-19 23:29:19 +09:00
parent 0793d35c40
commit 94852b07b2
4 changed files with 35 additions and 45 deletions

View File

@ -20,10 +20,8 @@ package ebiten
import (
"fmt"
"image"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/hajimehoshi/ebiten/internal/png"
@ -94,24 +92,8 @@ func dumpInternalImages() error {
return err
}
dump := func(img image.Image, index int) error {
filename := filepath.Join(dir, fmt.Sprintf("%d.png", index))
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
if err := png.Encode(f, img); err != nil {
return err
}
return nil
}
for i, img := range shareable.Images() {
if err := dump(img, i); err != nil {
return err
}
if err := shareable.DumpImages(dir); err != nil {
return err
}
if _, err := fmt.Fprintf(os.Stderr, "Dumped the internal images at: %s\n", dir); err != nil {

View File

@ -15,9 +15,15 @@
package graphicscommand
import (
"fmt"
"image"
"os"
"path/filepath"
"github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/png"
)
type lastCommand int
@ -170,3 +176,23 @@ func (i *Image) IsInvalidated() bool {
}
return i.image.IsInvalidated()
}
// DumpImages dumps the image to the specified directory.
//
// 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)))
if err != nil {
return err
}
defer f.Close()
if err := png.Encode(f, &image.RGBA{
Pix: i.Pixels(),
Stride: 4 * i.width,
Rect: image.Rect(0, 0, i.width, i.height),
}); err != nil {
return err
}
return nil
}

View File

@ -15,8 +15,6 @@
package restorable
import (
"image"
"github.com/hajimehoshi/ebiten/internal/graphicscommand"
)
@ -90,11 +88,10 @@ func RestoreIfNeeded() error {
return theImages.restore()
}
// Images returns all the current images.
// DumpImages dumps all the current images to the specified directory.
//
// This is for testing usage.
func Images() []image.Image {
var imgs []image.Image
func DumpImages(dir string) error {
for img := range theImages.images {
if img.volatile {
continue
@ -102,25 +99,11 @@ func Images() []image.Image {
if img.screen {
continue
}
w, h := img.Size()
pix := make([]byte, 4*w*h)
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
r, g, b, a := img.At(i, j)
pix[4*(i+j*w)] = r
pix[4*(i+j*w)+1] = g
pix[4*(i+j*w)+2] = b
pix[4*(i+j*w)+3] = a
}
if err := img.image.DumpAt(dir); err != nil {
return err
}
imgs = append(imgs, &image.RGBA{
Pix: pix,
Stride: 4 * w,
Rect: image.Rect(0, 0, w, h),
})
}
return imgs
return nil
}
// add adds img to the images.

View File

@ -16,7 +16,6 @@ package shareable
import (
"fmt"
"image"
"runtime"
"sync"
@ -528,10 +527,10 @@ func RestoreIfNeeded() error {
return restorable.RestoreIfNeeded()
}
func Images() []image.Image {
func DumpImages(dir string) error {
backendsM.Lock()
defer backendsM.Unlock()
return restorable.Images()
return restorable.DumpImages(dir)
}
func Error() error {