This commit is contained in:
Josh Deprez 2021-08-26 11:35:15 +10:00
parent 39598f0124
commit 0b66b118b8

View file

@ -19,26 +19,24 @@ func init() {
gob.Register(ImageRef{}) gob.Register(ImageRef{})
} }
// ImageRef loads images from the AssetFS into *ebiten.Image form. // ImageRef loads images from the AssetFS into *ebiten.Image form. It is your
// It is your responsibility to import _ "image/..." for whatever // responsibility to import _ "image/..." for whatever format the files are in,
// format the files are in, and to load it (either return it as a // and to load it (either return it as a subcomponent from Scan so that Game
// subcomponent from Scan so that Game will Load it, or call Load // will Load it, or call Load yourself).
// yourself).
type ImageRef struct { type ImageRef struct {
Path string Path string
image *ebiten.Image image *ebiten.Image
} }
// Image returns the image, or nil if not loaded. // Image returns the image, or nil if not loaded. Multiple distinct ImageRefs
// Multiple distinct ImageRefs can use the same path. // can use the same path efficiently.
func (r *ImageRef) Image() *ebiten.Image { func (r *ImageRef) Image() *ebiten.Image {
return r.image return r.image
} }
// Load loads the image. Load is required before Image returns. // Load loads the image. Load is required before Image returns. Loading the same
// Loading the same path multiple times uses a cache to return // path multiple times uses a cache to return the same image.
// the same image.
func (r *ImageRef) Load(assets fs.FS) error { func (r *ImageRef) Load(assets fs.FS) error {
// Fast path load from cache // Fast path load from cache
r.image = imageCache[assetKey{assets, r.Path}] r.image = imageCache[assetKey{assets, r.Path}]