Files
ordoor/internal/conv/object.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

54 lines
1010 B
Go

package conv
import (
"github.com/hajimehoshi/ebiten"
"code.ur.gs/lupine/ordoor/internal/data"
)
// Important conversions:
//
// * Width & height now stored using int
// * Colour data is now 32-bit rather than using a palette
type Sprite struct {
Width int
Height int
Image *ebiten.Image
}
type Object struct {
Name string
Sprites []*Sprite
}
func MapByName(objects []*Object) map[string]*Object {
out := make(map[string]*Object, len(objects))
for _, obj := range objects {
out[obj.Name] = obj
}
return out
}
func ConvertObject(rawObj *data.Object, name string) (*Object, error) {
out := &Object{
Name: name,
Sprites: make([]*Sprite, len(rawObj.Sprites)),
}
for i, rawSpr := range rawObj.Sprites {
w := int(rawSpr.Width)
h := int(rawSpr.Height)
ebitenImage, err := ebiten.NewImageFromImage(rawSpr.ToImage(), ebiten.FilterDefault)
if err != nil {
return nil, err
}
out.Sprites[i] = &Sprite{Width: w, Height: h, Image: ebitenImage}
}
return out, nil
}