From d89073bac8affc5b0d90ea7fe4078442d6aa4d3e Mon Sep 17 00:00:00 2001 From: Ruslan Mustakov Date: Fri, 29 Dec 2017 11:50:56 +0700 Subject: [PATCH] Do not depend on asyncdispatch Instead, allow to register frame callbacks. --- godot.nimble | 2 +- godot/nim/godotnim.nim | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/godot.nimble b/godot.nimble index 75c518a..b2d68a7 100644 --- a/godot.nimble +++ b/godot.nimble @@ -1,4 +1,4 @@ -version = "0.6.11" +version = "0.7.0" author = "Xored Software, Inc." description = "Godot Engine bindings" license = "MIT" diff --git a/godot/nim/godotnim.nim b/godot/nim/godotnim.nim index 0eccb6b..5a346c6 100644 --- a/godot/nim/godotnim.nim +++ b/godot/nim/godotnim.nim @@ -1,6 +1,6 @@ # Copyright 2017 Xored Software, Inc. -import tables, typetraits, macros, asyncdispatch +import tables, typetraits, macros import gdnativeapi import core.godotcoretypes import core.vector2, core.rect2, @@ -811,15 +811,30 @@ proc godot_gdnative_terminate(options: ptr GDNativeTerminateOptions) {. deallocHeap(runFinalizers = not options[].inEditor, allowGcAfterwards = false) const nimGcStepLengthUs {.intdefine.} = 2000 + +var idleCallbacks {.threadvar.}: seq[proc () {.closure.}] +idleCallbacks = newSeq[proc () {.closure.}]() + +var isMainThread {.threadvar.}: bool +isMainThread = true + +proc registerFrameCallback*(cb: proc () {.closure.}) = + # Registers a callback to be called on the main thread at the end + # of each frame. + if not isMainThread: + const err = cstring"registerFrameIdleCallback is called from non-main thread. Ignoring." + godotPrintError(err, cstring"registerFrameCallback", + cstring"godotnim.nim", 0) + else: + idleCallbacks.add(cb) + proc godot_nativescript_frame() {.cdecl, exportc, dynlib.} = var stackBottom {.volatile.}: pointer {.emit: """ setStackBottom((void*)(&`stackBottom`)); """.} - for _ in 1..10: - if not asyncdispatch.hasPendingOperations(): - break - poll(0) + for cb in idleCallbacks: + cb() GC_step(nimGcStepLengthUs, true, 0) when not defined(release):