pass assets FS into Load - cleaner?

This commit is contained in:
Josh Deprez 2021-08-23 10:09:49 +10:00
parent eb2929f6cc
commit 7e76032ee2
4 changed files with 10 additions and 10 deletions

View file

@ -4,6 +4,7 @@ import (
"compress/gzip" "compress/gzip"
"encoding/gob" "encoding/gob"
"image" "image"
"io/fs"
"log" "log"
"github.com/hajimehoshi/ebiten/v2" "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. // Load loads the image. Load is required before Image returns.
// Loading the same path multiple times uses a cache to return // Loading the same path multiple times uses a cache to return
// the same image. // the same image.
func (r *ImageRef) Load(g *Game) error { func (r *ImageRef) Load(assets fs.FS) error {
// Fast path load from cache // Fast path load from cache
r.image = imageCache[r.Path] r.image = imageCache[r.Path]
if r.image != nil { if r.image != nil {
return nil return nil
} }
// Slow path // Slow path
f, err := g.AssetFS.Open(r.Path) f, err := assets.Open(r.Path)
if err != nil { if err != nil {
return err return err
} }
@ -106,8 +107,8 @@ type SceneRef struct {
} }
// Load loads the scene from the file. // Load loads the scene from the file.
func (r *SceneRef) Load(g *Game) error { func (r *SceneRef) Load(assets fs.FS) error {
f, err := g.AssetFS.Open(r.Path) f, err := assets.Open(r.Path)
if err != nil { if err != nil {
return err return err
} }

View file

@ -15,7 +15,6 @@ func init() {
// One component must be the designated root component - usually a // One component must be the designated root component - usually a
// scene of some kind. // scene of some kind.
type Game struct { type Game struct {
AssetFS fs.FS
ScreenWidth int ScreenWidth int
ScreenHeight int ScreenHeight int
Root DrawUpdater // typically a *Scene or SceneRef though 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). // Load calls Load on all Loaders reachable via Scan (using Walk).
// It stops on the first error. // 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 { return Walk(g.Root, func(c interface{}) error {
l, ok := c.(Loader) l, ok := c.(Loader)
if !ok { if !ok {
return nil return nil
} }
return l.Load(g) return l.Load(assets)
}) })
} }

View file

@ -2,6 +2,7 @@ package engine
import ( import (
"image" "image"
"io/fs"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
) )
@ -39,7 +40,7 @@ type Identifier interface {
// Loader components get the chance to load themselves. This happens // Loader components get the chance to load themselves. This happens
// before preparation. // before preparation.
type Loader interface { type Loader interface {
Load(game *Game) error Load(fs.FS) error
} }
// ParallaxScaler components have a scaling factor. This is used for // ParallaxScaler components have a scaling factor. This is used for

View file

@ -102,7 +102,6 @@ func main() {
} }
game := &engine.Game{ game := &engine.Game{
AssetFS: assets,
ScreenHeight: 240, ScreenHeight: 240,
ScreenWidth: 320, ScreenWidth: 320,
Root: &engine.Scene{ 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) log.Fatalf("Loading error: %v", err)
} }
game.Prepare() game.Prepare()