2021-07-30 17:26:23 +10:00
|
|
|
package engine
|
|
|
|
|
2021-07-31 16:38:54 +10:00
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"io/fs"
|
2021-07-31 20:06:49 +10:00
|
|
|
"log"
|
2021-07-31 16:38:54 +10:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-07-31 21:23:58 +10:00
|
|
|
// Assets (usually embed.FS)
|
2021-07-31 16:38:54 +10:00
|
|
|
AssetFS fs.FS
|
|
|
|
|
2021-07-31 21:23:58 +10:00
|
|
|
// AnimDefs are easier to write as Go expressions -
|
|
|
|
// so just set this.
|
2021-08-01 14:41:39 +10:00
|
|
|
AnimDefs map[string]*AnimDef
|
2021-07-31 21:23:58 +10:00
|
|
|
|
|
|
|
imageCache = make(map[string]*ebiten.Image)
|
2021-07-31 16:38:54 +10:00
|
|
|
)
|
2021-07-30 17:26:23 +10:00
|
|
|
|
2021-07-31 21:23:58 +10:00
|
|
|
// AnimRef manages an Anim using a premade AnimDef from the cache.
|
2021-07-31 17:15:32 +10:00
|
|
|
type AnimRef struct {
|
2021-07-31 21:23:58 +10:00
|
|
|
Key string
|
2021-07-31 17:15:32 +10:00
|
|
|
|
2021-07-31 19:49:24 +10:00
|
|
|
anim *Anim
|
2021-07-31 17:15:32 +10:00
|
|
|
}
|
|
|
|
|
2021-07-31 20:06:49 +10:00
|
|
|
func (r *AnimRef) Anim() *Anim {
|
2021-07-31 19:49:24 +10:00
|
|
|
if r.anim != nil {
|
2021-07-31 20:06:49 +10:00
|
|
|
return r.anim
|
2021-07-31 17:17:26 +10:00
|
|
|
}
|
2021-08-12 14:45:23 +10:00
|
|
|
ad := AnimDefs[r.Key]
|
|
|
|
if ad == nil {
|
|
|
|
log.Fatalf("Unknown AnimDef %q", r.Key)
|
|
|
|
return nil
|
2021-07-31 19:49:24 +10:00
|
|
|
}
|
2021-08-12 14:45:23 +10:00
|
|
|
r.anim = &Anim{Def: ad}
|
|
|
|
return r.anim
|
2021-07-31 17:17:26 +10:00
|
|
|
}
|
|
|
|
|
2021-07-31 17:15:32 +10:00
|
|
|
// ImageRef loads images from the AssetFS into *ebiten.Image form.
|
|
|
|
// It is your responsibility to import _ "image/..." for whatever
|
|
|
|
// format the files are in.
|
2021-07-30 17:26:23 +10:00
|
|
|
type ImageRef struct {
|
|
|
|
Path string
|
|
|
|
|
|
|
|
image *ebiten.Image
|
|
|
|
}
|
|
|
|
|
2021-07-31 17:15:32 +10:00
|
|
|
// Image returns the image. If it hasn't been loaded yet, it loads.
|
|
|
|
// Multiple distinct ImageRefs can use the same path.
|
2021-07-31 20:06:49 +10:00
|
|
|
func (r *ImageRef) Image() *ebiten.Image {
|
2021-07-31 16:38:54 +10:00
|
|
|
if r.image != nil {
|
2021-07-31 20:06:49 +10:00
|
|
|
return r.image
|
2021-07-31 16:38:54 +10:00
|
|
|
}
|
|
|
|
r.image = imageCache[r.Path]
|
|
|
|
if r.image != nil {
|
2021-07-31 20:06:49 +10:00
|
|
|
return r.image
|
2021-07-31 16:38:54 +10:00
|
|
|
}
|
|
|
|
f, err := AssetFS.Open(r.Path)
|
|
|
|
if err != nil {
|
2021-07-31 20:06:49 +10:00
|
|
|
log.Fatalf("Couldn't open asset: %v", err)
|
2021-07-31 16:38:54 +10:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
i, _, err := image.Decode(f)
|
|
|
|
if err != nil {
|
2021-07-31 20:06:49 +10:00
|
|
|
log.Fatalf("Couldn't decode asset: %v", err)
|
2021-07-30 17:26:23 +10:00
|
|
|
}
|
2021-07-31 16:38:54 +10:00
|
|
|
r.image = ebiten.NewImageFromImage(i)
|
|
|
|
imageCache[r.Path] = r.image
|
2021-07-31 20:06:49 +10:00
|
|
|
return r.image
|
2021-07-30 17:26:23 +10:00
|
|
|
}
|