aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/phenax
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/phenax')
-rw-r--r--src/main/kotlin/phenax/utils/program.kt63
1 files changed, 60 insertions, 3 deletions
diff --git a/src/main/kotlin/phenax/utils/program.kt b/src/main/kotlin/phenax/utils/program.kt
index 2d3e04b..6ff5379 100644
--- a/src/main/kotlin/phenax/utils/program.kt
+++ b/src/main/kotlin/phenax/utils/program.kt
@@ -1,9 +1,66 @@
package phenax.utils
-fun recordDuration(durationSeconds: Int) {
- System.setProperty("phenax.record.frameCount", (60 * durationSeconds).toString())
+import java.io.File
+import kotlin.properties.ReadOnlyProperty
+import org.openrndr.ApplicationBuilder
+import org.openrndr.Program
+import org.openrndr.draw.*
+import org.openrndr.extra.filewatcher.watchingFile
+import org.openrndr.extra.olive.oliveProgram as oliveProgramOriginal
+
+fun Int.seconds(): Int = this * 1000
+
+fun Int.minutes(): Int = this.seconds() * 60
+
+fun recordDuration(millis: Int) {
+ System.setProperty("phenax.record.frameCount", (60 * millis / 1000).toInt().toString())
}
fun recordDuration(): Int? = System.getProperty("phenax.record.frameCount")?.toIntOrNull()
-fun recordingEnabled(): Boolean = System.getenv("RECORD") != null
+val recordingEnabled: Boolean by lazy { System.getenv("RECORD") != null }
+
+// fun classDir(): String =
+// "src/main/kotlin/${stackRootClassName(sanitize =
+// true).split(".").dropLast(1).joinToString("/")}";
+
+// fun localComputeShader(name: String): ComputeShader =
+// ComputeShader.fromCode(File("src/main/resources/shaders/${name}.cs").readText(), name)
+
+// Have to keep the same name (oliveProgram) to get update on save to work
+fun ApplicationBuilder.oliveProgram(init: Program.() -> Unit) {
+ if (recordingEnabled) {
+ program(init)
+ } else {
+ // TODO: Make watching shader files work
+ oliveProgramOriginal(init = init)
+ }
+}
+
+fun Program.liveComputeShader(name: String): ReadOnlyProperty<Any?, ComputeShader> {
+ val shaderCode by
+ watchingFile(File("src/main/resources/shaders/${name}.cs")) { file ->
+ println("change")
+ file.readText()
+ }
+
+ var cs: ComputeShader = ComputeShader.fromCode(shaderCode, name)
+
+ if (!recordingEnabled) {
+ var lastCompiledCode: String = shaderCode
+
+ program.extend {
+ if (shaderCode != lastCompiledCode) {
+ cs.destroy()
+ try {
+ cs = ComputeShader.fromCode(shaderCode, name)
+ } catch (e: Exception) {
+ println("Shader compile failed: ${e.message}")
+ }
+ lastCompiledCode = shaderCode
+ }
+ }
+ }
+
+ return ReadOnlyProperty { _, _ -> cs }
+}