2022-08-27 05:51:30 +02:00
|
|
|
// Copyright 2022 The Ebitengine Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
//go:build !js
|
|
|
|
// +build !js
|
|
|
|
|
|
|
|
package graphicscommand
|
|
|
|
|
|
|
|
import (
|
2022-08-31 06:10:41 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-08-27 05:51:30 +02:00
|
|
|
"image"
|
|
|
|
"os"
|
2022-08-30 20:10:10 +02:00
|
|
|
"path/filepath"
|
2022-08-27 05:51:30 +02:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
|
|
|
)
|
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool, rect image.Rectangle) (string, error) {
|
|
|
|
p, err := availableFilename(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2022-08-27 05:51:30 +02:00
|
|
|
}
|
2022-08-31 06:10:41 +02:00
|
|
|
path = p
|
2022-08-27 05:51:30 +02:00
|
|
|
|
2022-08-30 20:10:10 +02:00
|
|
|
path = i.dumpName(path)
|
2022-08-27 05:51:30 +02:00
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
2022-08-31 06:10:41 +02:00
|
|
|
return "", err
|
2022-08-27 05:51:30 +02:00
|
|
|
}
|
2022-09-09 18:52:46 +02:00
|
|
|
defer func() {
|
|
|
|
_ = f.Close()
|
|
|
|
}()
|
2022-08-27 05:51:30 +02:00
|
|
|
|
|
|
|
if err := i.dumpTo(f, graphicsDriver, blackbg, rect); err != nil {
|
2022-08-31 06:10:41 +02:00
|
|
|
return "", err
|
2022-08-27 05:51:30 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
return path, nil
|
2022-08-27 05:51:30 +02:00
|
|
|
}
|
2022-08-30 20:10:10 +02:00
|
|
|
|
|
|
|
// DumpImages dumps all the current images to the specified directory.
|
|
|
|
//
|
|
|
|
// This is for testing usage.
|
2022-08-31 06:10:41 +02:00
|
|
|
func DumpImages(images []*Image, graphicsDriver graphicsdriver.Graphics, dir string) (string, error) {
|
|
|
|
d, err := availableFilename(dir)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
dir = d
|
|
|
|
|
2022-08-30 20:10:10 +02:00
|
|
|
if err := os.Mkdir(dir, 0755); err != nil {
|
2022-08-31 06:10:41 +02:00
|
|
|
return "", err
|
2022-08-30 20:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, img := range images {
|
2022-08-31 06:10:41 +02:00
|
|
|
// Screen image cannot be dumped.
|
|
|
|
if img.screen {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(filepath.Join(dir, img.dumpName("*.png")))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-09-09 18:52:46 +02:00
|
|
|
defer func() {
|
|
|
|
_ = f.Close()
|
|
|
|
}()
|
2022-08-31 06:10:41 +02:00
|
|
|
|
|
|
|
if err := img.dumpTo(f, graphicsDriver, false, image.Rect(0, 0, img.width, img.height)); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-08-30 20:10:10 +02:00
|
|
|
}
|
2022-08-31 06:10:41 +02:00
|
|
|
name = fmt.Sprintf("%s_%d%s", base, i, ext)
|
2022-08-30 20:10:10 +02:00
|
|
|
}
|
2022-08-31 06:10:41 +02:00
|
|
|
return name, nil
|
2022-08-30 20:10:10 +02:00
|
|
|
}
|