feat: added parallelity for Update and Event method handling.

This commit is contained in:
Andrey Parhomenko 2023-12-24 22:33:01 +03:00
parent 067a127721
commit a3d7939d0e

View file

@ -7,6 +7,7 @@ import (
//"fmt" //"fmt"
"time" "time"
"slices" "slices"
"sync"
) )
// The type represents order of drawing. // The type represents order of drawing.
@ -202,6 +203,7 @@ func (e *Engine) AbsCursorPosition() Vector {
} }
func (e *engine) Update() error { func (e *engine) Update() error {
var wg sync.WaitGroup
eng := (*Engine)(e) eng := (*Engine)(e)
e.dt = time.Since(e.lastTime).Seconds() e.dt = time.Since(e.lastTime).Seconds()
@ -210,10 +212,16 @@ func (e *engine) Update() error {
if !ok { if !ok {
continue continue
} }
wg.Add(1)
go func() {
updater.Update(&Context{ updater.Update(&Context{
Engine: eng, Engine: eng,
}) })
wg.Done()
}()
} }
wg.Wait()
e.prevKeys = e.keys e.prevKeys = e.keys
e.keys = inpututil. e.keys = inpututil.
AppendPressedKeys(e.keys[:0]) AppendPressedKeys(e.keys[:0])
@ -270,17 +278,26 @@ func (e *engine) Update() error {
}) })
eng.cursorPos = realPos eng.cursorPos = realPos
} }
// Providing the events to the objects.
// Maybe should think of the better way,
// but for it is simple enough.
for object := range e.Objects.KeyChan() { for object := range e.Objects.KeyChan() {
eventer, ok := object.(Eventer) eventer, ok := object.(Eventer)
if ok { if ok {
wg.Add(1)
go func() {
defer wg.Done()
for _, event := range events { for _, event := range events {
eventer.Event(&Context{ eventer.Event(&Context{
Engine: eng, Engine: eng,
Event: event, Event: event,
}) })
} }
}()
} }
} }
wg.Wait()
e.lastTime = time.Now() e.lastTime = time.Now()
return nil return nil