Eager load used sprites

This commit is contained in:
2020-03-21 10:59:07 +00:00
parent 7a8e9dbd97
commit 8b02f534f1
3 changed files with 35 additions and 7 deletions

View File

@@ -62,6 +62,21 @@ func (a *AssetStore) Map(name string) (*Map, error) {
return m, nil
}
func (m *Map) LoadSprites() error {
// Eager load the sprites we use
for x := m.Rect.Min.X; x <= m.Rect.Max.X; x++ {
for y := m.Rect.Min.Y; y <= m.Rect.Max.Y; y++ {
for z := 0; z < maps.MaxHeight; z++ {
if _, err := m.SpritesForCell(x, y, z); err != nil {
return err
}
}
}
}
return nil
}
// SpritesForCell returns the sprites needed to correctly render this cell.
// They should be rendered from first to last to get the correct ordering
func (m *Map) SpritesForCell(x, y, z int) ([]*Sprite, error) {

View File

@@ -1,6 +1,7 @@
package assetstore
import (
"fmt"
"log"
"github.com/hajimehoshi/ebiten"
@@ -18,6 +19,7 @@ type Object struct {
type Sprite struct {
obj *Object
ID string
XOffset int
YOffset int
Width int
@@ -50,8 +52,8 @@ func (a *AssetStore) Object(name string) (*Object, error) {
sprites: make([]*Sprite, int(raw.NumSprites)),
raw: raw,
}
a.objs[name] = obj
a.objs[name] = obj
return obj, nil
}
@@ -75,6 +77,7 @@ func (o *Object) Sprite(idx int) (*Sprite, error) {
}
sprite := &Sprite{
ID: fmt.Sprintf("%v:%v", o.raw.Name, idx),
obj: o,
Width: int(raw.Width),
Height: int(raw.Width),