mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-12 22:17:26 +01:00
graphics: Unify dumping logic
This commit is contained in:
parent
d78db1738d
commit
0bf12d5519
@ -20,11 +20,9 @@ package ebiten
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/png"
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/shareable"
|
"github.com/hajimehoshi/ebiten/internal/shareable"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -49,30 +47,12 @@ func availableFilename(prefix, postfix string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func takeScreenshot(screen *Image) 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")
|
newname, err := availableFilename("screenshot_", ".png")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.Rename(name, newname); err != nil {
|
if err := screen.mipmap.orig.Dump(newname); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"strings"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/affine"
|
"github.com/hajimehoshi/ebiten/internal/affine"
|
||||||
"github.com/hajimehoshi/ebiten/internal/driver"
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
||||||
@ -177,12 +177,13 @@ func (i *Image) IsInvalidated() bool {
|
|||||||
return i.image.IsInvalidated()
|
return i.image.IsInvalidated()
|
||||||
}
|
}
|
||||||
|
|
||||||
// DumpAt dumps the image to the specified directory.
|
// Dump dumps the image to the specified path.
|
||||||
// The filename is determined by the image's ID.
|
// In the path, '*' is replaced with the image's ID.
|
||||||
//
|
//
|
||||||
// This is for testing usage.
|
// This is for testing usage.
|
||||||
func (i *Image) DumpAt(dir string) error {
|
func (i *Image) Dump(path string) error {
|
||||||
f, err := os.Create(filepath.Join(dir, fmt.Sprintf("%d.png", i.id)))
|
path = strings.ReplaceAll(path, "*", fmt.Sprintf("%d", i.id))
|
||||||
|
f, err := os.Create(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -583,3 +583,7 @@ func (i *Image) isInvalidated() bool {
|
|||||||
graphicscommand.FlushCommands()
|
graphicscommand.FlushCommands()
|
||||||
return i.image.IsInvalidated()
|
return i.image.IsInvalidated()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Image) Dump(path string) error {
|
||||||
|
return i.image.Dump(path)
|
||||||
|
}
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
package restorable
|
package restorable
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphicscommand"
|
"github.com/hajimehoshi/ebiten/internal/graphicscommand"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -99,7 +101,7 @@ func DumpImages(dir string) error {
|
|||||||
if img.screen {
|
if img.screen {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := img.image.DumpAt(dir); err != nil {
|
if err := img.image.Dump(filepath.Join(dir, "*.png")); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -492,6 +492,13 @@ func (i *Image) MakeVolatile() {
|
|||||||
i.neverShared = true
|
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 {
|
func NewScreenFramebufferImage(width, height int) *Image {
|
||||||
backendsM.Lock()
|
backendsM.Lock()
|
||||||
defer backendsM.Unlock()
|
defer backendsM.Unlock()
|
||||||
|
Loading…
Reference in New Issue
Block a user