From a3d7939d0eb854b0f5b73fc3efad04b797be261c Mon Sep 17 00:00:00 2001 From: surdeus Date: Sun, 24 Dec 2023 22:33:01 +0300 Subject: [PATCH] feat: added parallelity for Update and Event method handling. --- engine.go | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/engine.go b/engine.go index 48d4a41..06560d4 100644 --- a/engine.go +++ b/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 } - updater.Update(&Context{ - Engine: eng, - }) + 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 { - for _, event := range events { - eventer.Event(&Context{ - Engine: eng, - Event: event, - }) - } + 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