Use pool instead of slices.

This commit is contained in:
Andrey Parhomenko 2023-04-28 18:03:26 +03:00
parent 4f42968d2d
commit dd704c9820
3 changed files with 19 additions and 12 deletions

2
go.mod
View file

@ -8,7 +8,7 @@ require (
github.com/hajimehoshi/ebiten/v2 v2.4.16 // indirect
github.com/hajimehoshi/file2byteslice v1.0.0 // indirect
github.com/jezek/xgb v1.1.0 // indirect
github.com/surdeus/godat v0.0.0-20230217130825-eddf2c345cb7 // indirect
github.com/surdeus/godat v0.0.0-20230428145139-f51a8ab74bc8 // indirect
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb // indirect
golang.org/x/exp/shiny v0.0.0-20230213192124-5e25df0256eb // indirect
golang.org/x/image v0.5.0 // indirect

4
go.sum
View file

@ -23,6 +23,10 @@ github.com/jfreymuth/vorbis v1.0.2/go.mod h1:DoftRo4AznKnShRl1GxiTFCseHr4zR9BN3T
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
github.com/surdeus/godat v0.0.0-20230217130825-eddf2c345cb7 h1:Fmoxhb42mS6BEmLF2spRiYlzes+S1VrEw0PnbR1ktUM=
github.com/surdeus/godat v0.0.0-20230217130825-eddf2c345cb7/go.mod h1:tQGz8oe6Qig5yjYobaW1O8paXGGhzdukg8nT2bpvfes=
github.com/surdeus/godat v0.0.0-20230428142527-575c83ef5202 h1:CK7py+xOj/nuqGsBR+5eyYmSGODfmZuFQXW3eulGCT8=
github.com/surdeus/godat v0.0.0-20230428142527-575c83ef5202/go.mod h1:tQGz8oe6Qig5yjYobaW1O8paXGGhzdukg8nT2bpvfes=
github.com/surdeus/godat v0.0.0-20230428145139-f51a8ab74bc8 h1:yvpqrITnk2IP/MT6P2DfOgut9FWyeR8gcXpNRJoMPqg=
github.com/surdeus/godat v0.0.0-20230428145139-f51a8ab74bc8/go.mod h1:tQGz8oe6Qig5yjYobaW1O8paXGGhzdukg8nT2bpvfes=
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

View file

@ -4,6 +4,7 @@ import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/surdeus/godat/src/sparsex"
"github.com/surdeus/godat/src/poolx"
//"fmt"
"time"
)
@ -19,8 +20,8 @@ type WindowConfig struct {
type Engine struct {
wcfg *WindowConfig
layers *sparsex.Sparse[Layer, *[]Drawer]
behavers []Behaver
layers *sparsex.Sparse[Layer, *poolx.Pool[Drawer]]
behavers *poolx.Pool[Behaver]
lastTime time.Time
dt Float
camera *Camera
@ -48,7 +49,7 @@ func New(
wcfg: cfg,
layers: sparsex.New[
Layer,
*[]Drawer,
*poolx.Pool[Drawer],
](true),
camera: &Camera{
Object: &Object{
@ -57,6 +58,7 @@ func New(
},
},
},
behavers: poolx.New[Behaver](),
}
}
@ -78,19 +80,20 @@ func (e *Engine) Add(l Layer, b any) {
func (e *Engine) AddDrawer(l Layer, d Drawer) {
g, ok := e.layers.Get(l)
if !ok {
layer := poolx.New[Drawer]()
e.layers.Set(
l,
&[]Drawer{d},
layer,
)
layer.Append(d)
} else {
set := append(*g, d)
*g = set
g.Append(d)
}
}
func (e *Engine) AddBehaver(b Behaver) {
e.behavers = append(e.behavers, b)
e.behavers.Append(b)
}
func (e *engine) Update() error {
@ -101,8 +104,8 @@ func (e *engine) Update() error {
AppendPressedKeys(e.keys[:0])
e.dt = time.Since(e.lastTime).Seconds()
for _, v := range eng.behavers {
err = v.Update(eng)
for p := range eng.behavers.Range() {
err = p.V.Update(eng)
if err != nil {
return err
}
@ -115,8 +118,8 @@ func (e *engine) Update() error {
func (e *engine) Draw(i *ebiten.Image) {
eng := (*Engine)(e)
for p := range e.layers.Vals() {
for _, d := range *p.V {
d.Draw(eng, i)
for pj := range p.V.Range() {
pj.V.Draw(eng, i)
}
}
}