pass assets FS into Load - cleaner?
This commit is contained in:
parent
eb2929f6cc
commit
7e76032ee2
4 changed files with 10 additions and 10 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"compress/gzip"
|
||||
"encoding/gob"
|
||||
"image"
|
||||
"io/fs"
|
||||
"log"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
|
@ -69,14 +70,14 @@ func (r *ImageRef) Image() *ebiten.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(g *Game) error {
|
||||
func (r *ImageRef) Load(assets fs.FS) error {
|
||||
// Fast path load from cache
|
||||
r.image = imageCache[r.Path]
|
||||
if r.image != nil {
|
||||
return nil
|
||||
}
|
||||
// Slow path
|
||||
f, err := g.AssetFS.Open(r.Path)
|
||||
f, err := assets.Open(r.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -106,8 +107,8 @@ type SceneRef struct {
|
|||
}
|
||||
|
||||
// Load loads the scene from the file.
|
||||
func (r *SceneRef) Load(g *Game) error {
|
||||
f, err := g.AssetFS.Open(r.Path)
|
||||
func (r *SceneRef) Load(assets fs.FS) error {
|
||||
f, err := assets.Open(r.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -15,7 +15,6 @@ func init() {
|
|||
// One component must be the designated root component - usually a
|
||||
// scene of some kind.
|
||||
type Game struct {
|
||||
AssetFS fs.FS
|
||||
ScreenWidth int
|
||||
ScreenHeight int
|
||||
Root DrawUpdater // typically a *Scene or SceneRef though
|
||||
|
@ -90,13 +89,13 @@ func Walk(c interface{}, v func(interface{}) error) error {
|
|||
|
||||
// Load calls Load on all Loaders reachable via Scan (using Walk).
|
||||
// It stops on the first error.
|
||||
func (g *Game) Load() error {
|
||||
func (g *Game) Load(assets fs.FS) error {
|
||||
return Walk(g.Root, func(c interface{}) error {
|
||||
l, ok := c.(Loader)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return l.Load(g)
|
||||
return l.Load(assets)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package engine
|
|||
|
||||
import (
|
||||
"image"
|
||||
"io/fs"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
@ -39,7 +40,7 @@ type Identifier interface {
|
|||
// Loader components get the chance to load themselves. This happens
|
||||
// before preparation.
|
||||
type Loader interface {
|
||||
Load(game *Game) error
|
||||
Load(fs.FS) error
|
||||
}
|
||||
|
||||
// ParallaxScaler components have a scaling factor. This is used for
|
||||
|
|
3
main.go
3
main.go
|
@ -102,7 +102,6 @@ func main() {
|
|||
}
|
||||
|
||||
game := &engine.Game{
|
||||
AssetFS: assets,
|
||||
ScreenHeight: 240,
|
||||
ScreenWidth: 320,
|
||||
Root: &engine.Scene{
|
||||
|
@ -119,7 +118,7 @@ func main() {
|
|||
},
|
||||
},
|
||||
}
|
||||
if err := game.Load(); err != nil {
|
||||
if err := game.Load(assets); err != nil {
|
||||
log.Fatalf("Loading error: %v", err)
|
||||
}
|
||||
game.Prepare()
|
||||
|
|
Loading…
Reference in a new issue