feat: implemented buffering with goroutines. (not fast, damn sorry)

This commit is contained in:
Andrey Parhomenko 2024-01-08 13:08:52 +03:00
parent 79c9da4ae1
commit de316bd79c
2 changed files with 53 additions and 8 deletions

View file

@ -77,7 +77,7 @@ func (t *Tri) Update(c *Context) {
}
tt := *t
tt.Spawned = true
tt.Visible = false
tt.Visible = true
tt.Disconnect()
if c.Spawn(&tt) == nil {
counter++

View file

@ -11,7 +11,7 @@ import (
)
const (
LayerBufSize = 0
LayerBufSize = 100
)
type GraphicsLibrary = ebiten.GraphicsLibrary
@ -115,6 +115,12 @@ func NewEngine(
ret.handleEvents = make(EventChan)
ret.Objects = &Objects{}
ret.buttons = MouseButtonMap{}
siz := ret.RealWinSize()
w, h := int(siz.X), int(siz.Y)
for i:=0 ; i<len(ret.bufs) ; i++ {
ret.bufs[i] = NewImage(w, h)
}
return ret
}
@ -295,7 +301,8 @@ func (e *engine) Update() error {
return nil
}
func (e *engine) Draw(i *ebiten.Image) {
func (e *engine) Draw(image *ebiten.Image) {
var wg sync.WaitGroup
e.dt = time.Since(e.lastTime).Seconds()
eng := (*Engine)(e)
m := map[Layer][]Drawer{}
@ -317,15 +324,53 @@ func (e *engine) Draw(i *ebiten.Image) {
// Drawing layers.
layers := maps.NewSparse[Layer, []Drawer](nil, m)
c := &Context{Engine: eng, typ: drawContext, Image: i}
for layer := range layers.Chan() {
ln := layers.Size()
pageN := ln / LayerBufSize
mod := ln % LayerBufSize
chn := layers.Chan()
opts := &ebiten.DrawImageOptions{GeoM: Matrix{}}
for n := 0 ; n<pageN ; n++ {
for i:=0 ; i<LayerBufSize ; i++ {
layer := <-chn
buf := e.bufs[i]
c := &Context{Engine: eng, typ: drawContext, Image: buf}
wg.Add(1)
go func() {
for _, drawer := range layer {
drawer.Draw(c)
}
wg.Done()
}()
}
wg.Wait()
for _, buf := range e.bufs {
image.DrawImage(buf, opts)
buf.Clear()
}
}
// Empty the buff to generate it again.
for i:=0 ; i<mod ; i++ {
layer := <-chn
buf := e.bufs[i]
c := &Context{Engine: eng, typ: drawContext, Image: buf}
wg.Add(1)
go func() {
for _, drawer := range layer {
drawer.Draw(c)
}
wg.Done()
}()
}
wg.Wait()
for i:=0 ; i<mod ; i++ {
buf := e.bufs[i]
image.DrawImage(buf, opts)
buf.Clear()
}
// Empty the camera buffer to generate it again.
eng.Camera.buffered = false
e.lastTime = time.Now()
}