Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coro block bug #479

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,18 @@ func (p *eventSink) asyncCall(start bool, data interface{}, doSth func(*eventSin

func (p *eventSink) syncCall(data interface{}, doSth func(*eventSink)) {
var wg sync.WaitGroup
// calc the wait count
temp := p
for temp != nil {
if temp.cond == nil || temp.cond(data) {
wg.Add(1)
}
temp = temp.prev
}

// start create tasks
for p != nil {
if p.cond == nil || p.cond(data) {
wg.Add(1)
copy := p
gco.CreateAndStart(false, p.pthis, func(coroutine.Thread) int {
defer wg.Done()
Expand All @@ -76,7 +85,8 @@ func (p *eventSink) syncCall(data interface{}, doSth func(*eventSink)) {
}
p = p.prev
}
engine.WaitToDo(wg.Wait)
// wait for all tasks to start executing
engine.WaitGroup(&wg)
}

func (p *eventSink) call(wait bool, data interface{}, doSth func(*eventSink)) {
Expand Down
19 changes: 1 addition & 18 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,20 +704,9 @@ func (p *Game) handleEvent(event event) {
}

func (p *Game) fireEvent(ev event) {
select {
case p.events <- ev:
default:
log.Println("Event buffer is full. Skip event:", ev)
}
p.handleEvent(ev)
}

func (p *Game) eventLoop(me coroutine.Thread) int {
for {
var ev event
engine.WaitForChan(p.events, &ev)
p.handleEvent(ev)
}
}
func (p *Game) logicLoop(me coroutine.Thread) int {
for {
tempItems := p.getTempShapes()
Expand Down Expand Up @@ -758,16 +747,10 @@ func (p *Game) inputEventLoop(me coroutine.Thread) int {
}

func (p *Game) initEventLoop() {
gco.Create(nil, p.eventLoop)
gco.Create(nil, p.inputEventLoop)
gco.Create(nil, p.logicLoop)
}

func init() {
gco = coroutine.New()
engine.SetCoroutines(gco)
}

var (
gco *coroutine.Coroutines
)
Expand Down
79 changes: 45 additions & 34 deletions gdspx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ import (
"math"
"strings"

"github.com/goplus/spx/internal/coroutine"
"github.com/goplus/spx/internal/define"
"github.com/goplus/spx/internal/engine"
"github.com/goplus/spx/internal/enginewrap"
"github.com/goplus/spx/internal/time"

"github.com/realdream-ai/mathf"
)
Expand All @@ -47,18 +50,20 @@ var (

func (p *Game) OnEngineStart() {
cachedBounds_ = make(map[string]mathf.Rect2)
onStart := func() {
gamer := p.gamer_
gco = coroutine.New()
engine.SetCoroutines(gco)

engine.CreateCoroAndWait(p, func() {
initInput()
gamer := p.gamer_
if me, ok := gamer.(interface{ MainEntry() }); ok {
me.MainEntry()
}
if !p.isRunned {
Gopt_Game_Run(gamer, "assets")
}
engine.OnGameStarted()
}
go onStart()
define.HasInit = true
})
}

func (p *Game) OnEngineDestroy() {
Expand All @@ -68,38 +73,43 @@ func (p *Game) OnEngineUpdate(delta float64) {
if !p.isRunned {
return
}
// all these functions is called in main thread
p.syncUpdateInput()
p.syncUpdateCamera()
p.syncUpdateLogic()
engine.CreateCoroAndWait(p, func() {
// update input
pos := inputMgr.GetMousePos()
wpos := engine.ScreenToWorld(pos)
p.mousePos = wpos

// update camera
isOn, pos := p.Camera.getFollowPos()
if isOn {
cameraMgr.SetPosition(pos)
}

// update logic
p.startFlag.Do(func() {
p.fireEvent(&eventStart{})
})
})
}
func (p *Game) OnEngineRender(delta float64) {
if !p.isRunned {
return
}
p.syncUpdateProxy()
p.syncUpdatePhysic()
}

func (p *Game) syncUpdateLogic() error {
p.startFlag.Do(func() {
p.fireEvent(&eventStart{})
engine.CreateCoroAndWait(p, func() {
done := make(chan bool, 1)
t0 := time.RealTimeSinceStart()
engine.WaitMainThread(func() {
p.syncUpdateProxy()
t1 := time.RealTimeSinceStart()
p.syncUpdatePhysic()
t2 := time.RealTimeSinceStart()
if t2-t0 > 0.02 {
println(time.Frame(), fmt.Sprintf("== OnEngineRender total%fms proxy %f physic %f ", t2-t0, t1-t0, t2-t1))
}
done <- true
})
<-done
})

return nil
}

func (p *Game) syncUpdateCamera() {
isOn, pos := p.Camera.getFollowPos()
if isOn {
engine.SyncSetCameraPosition(pos)
}
}

func (p *Game) syncUpdateInput() {
pos := engine.SyncGetMousePos()
wpos := engine.SyncScreenToWorld(pos)
p.mousePos = wpos
}

func (sprite *SpriteImpl) syncCheckInitProxy() {
Expand All @@ -125,7 +135,7 @@ func (sprite *SpriteImpl) updateProxyTransform(isSync bool) {

func (p *Game) syncUpdateProxy() {
count := 0
items := p.getItems()
items := p.getTempShapes()
for _, item := range items {
sprite, ok := item.(*SpriteImpl)
if ok {
Expand All @@ -145,14 +155,15 @@ func (p *Game) syncUpdateProxy() {
}

// unbind syncSprite
for _, item := range p.destroyItems {
desItems := p.destroyItems
p.destroyItems = nil
for _, item := range desItems {
sprite, ok := item.(*SpriteImpl)
if ok && sprite.syncSprite != nil {
sprite.syncSprite.Destroy()
sprite.syncSprite = nil
}
}
p.destroyItems = nil
}

func checkUpdateCostume(p *baseObj) {
Expand Down
Loading