From 7e76032ee28b69914f7e388781acbb491e8d72bc Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Mon, 23 Aug 2021 10:09:49 +1000 Subject: [PATCH] pass assets FS into Load - cleaner? --- engine/asset.go | 9 +++++---- engine/game.go | 5 ++--- engine/interface.go | 3 ++- main.go | 3 +-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/engine/asset.go b/engine/asset.go index 4a2bfb8..63c14e9 100644 --- a/engine/asset.go +++ b/engine/asset.go @@ -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 } diff --git a/engine/game.go b/engine/game.go index b99b21d..2f122cd 100644 --- a/engine/game.go +++ b/engine/game.go @@ -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) }) } diff --git a/engine/interface.go b/engine/interface.go index 4326948..bc1e557 100644 --- a/engine/interface.go +++ b/engine/interface.go @@ -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 diff --git a/main.go b/main.go index 42b70df..80f4228 100644 --- a/main.go +++ b/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()