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

@@ -52,6 +52,11 @@ func main() {
log.Fatalf("Failed to load map %v: %v", *gameMap, err) log.Fatalf("Failed to load map %v: %v", *gameMap, err)
} }
// Eager load sprites
if err := area.LoadSprites(); err != nil {
log.Fatal("Eager-loading sprites failed: %v", err)
}
state := state{ state := state{
zoom: 1.0, zoom: 1.0,
origin: image.Point{0, 3000}, // FIXME: haxxx origin: image.Point{0, 3000}, // FIXME: haxxx
@@ -144,18 +149,21 @@ func (e *env) Draw(screen *ebiten.Image) error {
return false return false
}) })
counter := map[string]int{}
for _, pt := range toDraw { for _, pt := range toDraw {
for z := 0; z <= e.state.zIdx; z++ { for z := 0; z <= e.state.zIdx; z++ {
if err := e.renderCell(pt.X, pt.Y, z, screen); err != nil { if err := e.renderCell(pt.X, pt.Y, z, screen, counter); err != nil {
return err return err
} }
} }
} }
//log.Printf("%#+v", counter)
return nil return nil
} }
func (e *env) renderCell(x, y, z int, screen *ebiten.Image) error { func (e *env) renderCell(x, y, z int, screen *ebiten.Image, counter map[string]int) error {
sprites, err := e.area.SpritesForCell(x, y, z) sprites, err := e.area.SpritesForCell(x, y, z)
if err != nil { if err != nil {
return err return err
@@ -167,10 +175,6 @@ func (e *env) renderCell(x, y, z int, screen *ebiten.Image) error {
pix := cellToPix(image.Pt(x, y)) pix := cellToPix(image.Pt(x, y))
iso.Translate(float64(pix.X), float64(pix.Y)) iso.Translate(float64(pix.X), float64(pix.Y))
if e.step%30 == 0 {
// log.Printf("x=%v y=%v z=%v", pix.X-e.state.origin.X, pix.Y-e.state.origin.Y, z)
}
// Taking the Z index away *seems* to draw the object in the correct place. // Taking the Z index away *seems* to draw the object in the correct place.
// FIXME: There are some artifacts, investigate more // FIXME: There are some artifacts, investigate more
iso.Translate(0.0, -float64(z*48.0)) // offset for Z index iso.Translate(0.0, -float64(z*48.0)) // offset for Z index
@@ -178,7 +182,13 @@ func (e *env) renderCell(x, y, z int, screen *ebiten.Image) error {
// TODO: iso.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor // TODO: iso.Scale(e.state.zoom, e.state.zoom) // apply current zoom factor
for _, spr := range sprites { for _, spr := range sprites {
// if _, ok := counter[spr.ID]; !ok {
// counter[spr.ID] = 0
// }
// counter[spr.ID] = counter[spr.ID] + 1
iso.Translate(float64(spr.XOffset), float64(spr.YOffset)) iso.Translate(float64(spr.XOffset), float64(spr.YOffset))
if err := screen.DrawImage(spr.Image, &ebiten.DrawImageOptions{GeoM: iso}); err != nil { if err := screen.DrawImage(spr.Image, &ebiten.DrawImageOptions{GeoM: iso}); err != nil {
return err return err
} }

View File

@@ -62,6 +62,21 @@ func (a *AssetStore) Map(name string) (*Map, error) {
return m, nil 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. // SpritesForCell returns the sprites needed to correctly render this cell.
// They should be rendered from first to last to get the correct ordering // They should be rendered from first to last to get the correct ordering
func (m *Map) SpritesForCell(x, y, z int) ([]*Sprite, error) { func (m *Map) SpritesForCell(x, y, z int) ([]*Sprite, error) {

View File

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