Files
ordoor/internal/assetstore/set.go
Nick Thomas 5fccf97f4b Lazily load sprite image data
This cuts memory use significantly, since many sprites in an object are
never used. We can get savings over time by evicting sprites when they
go out of scope, but that's, well, out of scope.

To achieve this, I introduce an assetstore package that is in charge of
loading things from the filesystem. This also allows some lingering
case-sensitivity issues to be handled cleanly.

I'd hoped that creating fewer ebiten.Image instances would help CPU
usage, but that doesn't seem to be the case.
2020-03-19 22:24:21 +00:00

51 lines
753 B
Go

package assetstore
import (
"errors"
"code.ur.gs/lupine/ordoor/internal/sets"
)
var (
ErrOutOfBounds = errors.New("Out of bounds")
)
type Set struct {
assets *AssetStore
raw *sets.MapSet
}
func (s *Set) Object(idx int) (*Object, error) {
if idx < 0 || idx >= len(s.raw.Palette) {
return nil, ErrOutOfBounds
}
return s.assets.Object(s.raw.Palette[idx])
}
func (a *AssetStore) Set(name string) (*Set, error) {
name = canonical(name)
if set, ok := a.sets[name]; ok {
return set, nil
}
filename, err := a.lookup(name, "set", "Sets")
if err != nil {
return nil, err
}
raw, err := sets.LoadSet(filename)
if err != nil {
return nil, err
}
set := &Set{
assets: a,
raw: raw,
}
a.sets[name] = set
return set, nil
}