feat: added parallelity for Update and Event method handling.
This commit is contained in:
parent
067a127721
commit
a3d7939d0e
1 changed files with 26 additions and 9 deletions
17
engine.go
17
engine.go
|
@ -7,6 +7,7 @@ import (
|
|||
//"fmt"
|
||||
"time"
|
||||
"slices"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// The type represents order of drawing.
|
||||
|
@ -202,6 +203,7 @@ func (e *Engine) AbsCursorPosition() Vector {
|
|||
}
|
||||
|
||||
func (e *engine) Update() error {
|
||||
var wg sync.WaitGroup
|
||||
eng := (*Engine)(e)
|
||||
|
||||
e.dt = time.Since(e.lastTime).Seconds()
|
||||
|
@ -210,10 +212,16 @@ func (e *engine) Update() error {
|
|||
if !ok {
|
||||
continue
|
||||
}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
updater.Update(&Context{
|
||||
Engine: eng,
|
||||
})
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
e.prevKeys = e.keys
|
||||
e.keys = inpututil.
|
||||
AppendPressedKeys(e.keys[:0])
|
||||
|
@ -270,17 +278,26 @@ func (e *engine) Update() error {
|
|||
})
|
||||
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() {
|
||||
eventer, ok := object.(Eventer)
|
||||
if ok {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for _, event := range events {
|
||||
eventer.Event(&Context{
|
||||
Engine: eng,
|
||||
Event: event,
|
||||
})
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
e.lastTime = time.Now()
|
||||
return nil
|
||||
|
|
Loading…
Reference in a new issue