ebiten up, imageref own file
This commit is contained in:
parent
8d38619396
commit
39598f0124
4 changed files with 66 additions and 56 deletions
|
@ -3,24 +3,10 @@ package engine
|
|||
import (
|
||||
"compress/gzip"
|
||||
"encoding/gob"
|
||||
"image"
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
imageCache = make(map[assetKey]*ebiten.Image)
|
||||
|
||||
// Ensure types satisfy interfaces.
|
||||
_ Loader = &ImageRef{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
gob.Register(ImageRef{})
|
||||
}
|
||||
|
||||
type assetKey struct {
|
||||
assets fs.FS
|
||||
path string
|
||||
|
@ -61,44 +47,3 @@ func SaveGobz(src interface{}, name string) error {
|
|||
}
|
||||
return os.Rename(f.Name(), name)
|
||||
}
|
||||
|
||||
// ImageRef loads images from the AssetFS into *ebiten.Image form.
|
||||
// It is your responsibility to import _ "image/..." for whatever
|
||||
// format the files are in, and to load it (either return it as a
|
||||
// subcomponent from Scan so that Game will Load it, or call Load
|
||||
// yourself).
|
||||
type ImageRef struct {
|
||||
Path string
|
||||
|
||||
image *ebiten.Image
|
||||
}
|
||||
|
||||
// Image returns the image, or nil if not loaded.
|
||||
// Multiple distinct ImageRefs can use the same path.
|
||||
func (r *ImageRef) Image() *ebiten.Image {
|
||||
return r.image
|
||||
}
|
||||
|
||||
// Load loads the image. Load is required before Image returns.
|
||||
// Loading the same path multiple times uses a cache to return
|
||||
// the same image.
|
||||
func (r *ImageRef) Load(assets fs.FS) error {
|
||||
// Fast path load from cache
|
||||
r.image = imageCache[assetKey{assets, r.Path}]
|
||||
if r.image != nil {
|
||||
return nil
|
||||
}
|
||||
// Slow path
|
||||
f, err := assets.Open(r.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
i, _, err := image.Decode(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.image = ebiten.NewImageFromImage(i)
|
||||
imageCache[assetKey{assets, r.Path}] = r.image
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
// Ensure Image satisfies interfaces.
|
||||
// Ensure Billboard satisfies interfaces.
|
||||
var (
|
||||
_ Identifier = &Billboard{}
|
||||
_ Drawer = &Billboard{}
|
||||
|
|
61
engine/imageref.go
Normal file
61
engine/imageref.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
package engine
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"image"
|
||||
"io/fs"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
imageCache = make(map[assetKey]*ebiten.Image)
|
||||
|
||||
// Ensure types satisfy interfaces.
|
||||
_ Loader = &ImageRef{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
gob.Register(ImageRef{})
|
||||
}
|
||||
|
||||
// ImageRef loads images from the AssetFS into *ebiten.Image form.
|
||||
// It is your responsibility to import _ "image/..." for whatever
|
||||
// format the files are in, and to load it (either return it as a
|
||||
// subcomponent from Scan so that Game will Load it, or call Load
|
||||
// yourself).
|
||||
type ImageRef struct {
|
||||
Path string
|
||||
|
||||
image *ebiten.Image
|
||||
}
|
||||
|
||||
// Image returns the image, or nil if not loaded.
|
||||
// Multiple distinct ImageRefs can use the same path.
|
||||
func (r *ImageRef) Image() *ebiten.Image {
|
||||
return r.image
|
||||
}
|
||||
|
||||
// Load loads the image. Load is required before Image returns.
|
||||
// Loading the same path multiple times uses a cache to return
|
||||
// the same image.
|
||||
func (r *ImageRef) Load(assets fs.FS) error {
|
||||
// Fast path load from cache
|
||||
r.image = imageCache[assetKey{assets, r.Path}]
|
||||
if r.image != nil {
|
||||
return nil
|
||||
}
|
||||
// Slow path
|
||||
f, err := assets.Open(r.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
i, _, err := image.Decode(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.image = ebiten.NewImageFromImage(i)
|
||||
imageCache[assetKey{assets, r.Path}] = r.image
|
||||
return nil
|
||||
}
|
4
go.sum
4
go.sum
|
@ -1,11 +1,15 @@
|
|||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210410170116-ea3d685f79fb h1:T6gaWBvRzJjuOrdCtg8fXXjKai2xSDqWTcKFUPuw8Tw=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210410170116-ea3d685f79fb/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210727001814-0db043d8d5be h1:vEIVIuBApEBQTEJt19GfhoU+zFSV+sNTa9E9FdnRYfk=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210727001814-0db043d8d5be/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||
github.com/gofrs/flock v0.8.0 h1:MSdYClljsF3PbENUUEx85nkWfJSGfzYI9yEBZOJz6CY=
|
||||
github.com/gofrs/flock v0.8.0/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
|
||||
github.com/hajimehoshi/bitmapfont/v2 v2.1.3/go.mod h1:2BnYrkTQGThpr/CY6LorYtt/zEPNzvE/ND69CRTaHMs=
|
||||
github.com/hajimehoshi/ebiten/v2 v2.1.4 h1:ok1sjnDUm1VHVRvI4HEHzk7CjAsnNj/dqIo2/ObGcy4=
|
||||
github.com/hajimehoshi/ebiten/v2 v2.1.4/go.mod h1:mpAvpmTRbMdhQDZplZ4rfEogRhdsfAGTC0zLhxawKHY=
|
||||
github.com/hajimehoshi/ebiten/v2 v2.1.5 h1:yx8g5YQy7xnVbT4lCZCAQHx454j50emlRs6Aa78vdPc=
|
||||
github.com/hajimehoshi/ebiten/v2 v2.1.5/go.mod h1:jySpxHAruK+OxqSiU5+ga2OGvlQCIRNlKhDZTIyn9po=
|
||||
github.com/hajimehoshi/file2byteslice v0.0.0-20200812174855-0e5e8a80490e/go.mod h1:CqqAHp7Dk/AqQiwuhV1yT2334qbA/tFWQW0MD2dGqUE=
|
||||
github.com/hajimehoshi/go-mp3 v0.3.2/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
|
||||
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
|
||||
|
|
Loading…
Reference in a new issue