Skip to content

Instantly share code, notes, and snippets.

@edwinRNDR
Last active November 3, 2018 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwinRNDR/3615d5eeac6250fe29b352caf1fbe5f3 to your computer and use it in GitHub Desktop.
Save edwinRNDR/3615d5eeac6250fe29b352caf1fbe5f3 to your computer and use it in GitHub Desktop.
Coroutines experiments in OPENRNDR
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.runBlocking
import org.openrndr.Program
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.configuration
import org.openrndr.draw.ColorBuffer
import org.openrndr.draw.renderTarget
import org.openrndr.internal.Driver
import org.openrndr.launch
class Coroutines001 : Program() {
var drawFunc = {}
override fun setup() {
val drawThread = Driver.driver.createDrawThread()
val result = Channel<ColorBuffer>()
val go = Channel<Unit>()
launch(drawThread.dispatcher) {
val rt = renderTarget(512, 512) {
colorBuffer()
}
while (true) {
runBlocking {
go.receive()
}
drawThread.drawer.withTarget(rt) {
background(ColorRGBa(Math.random(), Math.random(), Math.random()))
}
Driver.driver.finish()
result.send(rt.colorBuffer(0))
}
}
runBlocking {
go.send(Unit)
}
drawFunc = {
if (!result.isEmpty) {
runBlocking {
drawer.image(result.receive())
Driver.driver.finish()
go.send(Unit)
}
}
}
}
override fun draw() {
drawFunc()
}
}
fun main() = application(Coroutines001(), configuration { })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment