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.
This commit is contained in:
50
internal/assetstore/set.go
Normal file
50
internal/assetstore/set.go
Normal file
@@ -0,0 +1,50 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user