-
-
Save edwinRNDR/3615d5eeac6250fe29b352caf1fbe5f3 to your computer and use it in GitHub Desktop.
Coroutines experiments in OPENRNDR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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