From fecebfd39809f862e098805219b15c50588bb81a Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sun, 30 Aug 2026 22:41:25 +0530 Subject: Development setup + toggling recording --- src/main/kotlin/TemplateLiveProgram.kt | 22 --------- src/main/kotlin/TemplateProgram.kt | 31 ------------- src/main/kotlin/org/openrndr/Preload.kt | 14 ++++++ .../kotlin/phenax/extensions/HeadlessRecording.kt | 53 ++++++++++++++++++++++ src/main/kotlin/phenax/utils/program.kt | 9 ++++ src/main/kotlin/play/PlayShowProgram.kt | 42 +++++++++++++++++ 6 files changed, 118 insertions(+), 53 deletions(-) delete mode 100644 src/main/kotlin/TemplateLiveProgram.kt delete mode 100644 src/main/kotlin/TemplateProgram.kt create mode 100644 src/main/kotlin/org/openrndr/Preload.kt create mode 100644 src/main/kotlin/phenax/extensions/HeadlessRecording.kt create mode 100644 src/main/kotlin/phenax/utils/program.kt create mode 100644 src/main/kotlin/play/PlayShowProgram.kt (limited to 'src') diff --git a/src/main/kotlin/TemplateLiveProgram.kt b/src/main/kotlin/TemplateLiveProgram.kt deleted file mode 100644 index 314bc54..0000000 --- a/src/main/kotlin/TemplateLiveProgram.kt +++ /dev/null @@ -1,22 +0,0 @@ -import org.openrndr.application -import org.openrndr.color.ColorRGBa -import org.openrndr.extra.olive.oliveProgram - -/** - * This is a template for a live program. - * - * It uses oliveProgram {} instead of program {}. All code inside the - * oliveProgram {} can be changed while the program is running. - */ - -fun main() = application { - configure { - width = 800 - height = 800 - } - oliveProgram { - extend { - drawer.clear(ColorRGBa.PINK) - } - } -} \ No newline at end of file diff --git a/src/main/kotlin/TemplateProgram.kt b/src/main/kotlin/TemplateProgram.kt deleted file mode 100644 index b8e2afe..0000000 --- a/src/main/kotlin/TemplateProgram.kt +++ /dev/null @@ -1,31 +0,0 @@ -import org.openrndr.application -import org.openrndr.color.ColorRGBa -import org.openrndr.draw.loadFont -import org.openrndr.draw.loadImage -import org.openrndr.extra.color.colormatrix.tint -import kotlin.math.cos -import kotlin.math.sin - -fun main() = application { - configure { - width = 768 - height = 576 - } - - program { - val image = loadImage("data/images/pm5544.png") - val font = loadFont("data/fonts/default.otf", 64.0) - - extend { - drawer.drawStyle.colorMatrix = tint(ColorRGBa.WHITE.shade(0.2)) - drawer.image(image) - - drawer.fill = ColorRGBa.PINK - drawer.circle(cos(seconds) * width / 2.0 + width / 2.0, sin(0.5 * seconds) * height / 2.0 + height / 2.0, 140.0) - - drawer.fontMap = font - drawer.fill = ColorRGBa.WHITE - drawer.text("OPENRNDR", width / 2.0, height / 2.0) - } - } -} diff --git a/src/main/kotlin/org/openrndr/Preload.kt b/src/main/kotlin/org/openrndr/Preload.kt new file mode 100644 index 0000000..375294a --- /dev/null +++ b/src/main/kotlin/org/openrndr/Preload.kt @@ -0,0 +1,14 @@ +package org.openrndr + +import org.openrndr.draw.* +import phenax.extensions.HeadlessRecorder +import phenax.utils.recordingEnabled + +class Preload : ApplicationPreload() { + override fun onProgramSetup(program: Program) { + println("BACKEND(${program.name}): ${program.application::class.qualifiedName}") + if (recordingEnabled()) { + program.extend(HeadlessRecorder()) + } + } +} diff --git a/src/main/kotlin/phenax/extensions/HeadlessRecording.kt b/src/main/kotlin/phenax/extensions/HeadlessRecording.kt new file mode 100644 index 0000000..78cde2c --- /dev/null +++ b/src/main/kotlin/phenax/extensions/HeadlessRecording.kt @@ -0,0 +1,53 @@ +package phenax.extensions + +import org.openrndr.* +import org.openrndr.draw.* +import org.openrndr.ffmpeg.VideoWriter +import phenax.utils.recordDuration + +class HeadlessRecorder : Extension { + override var enabled: Boolean = true + + var videoTarget: RenderTarget? = null + var videoWriter: VideoWriter? = null + var maxFrameCount: Int? = null + + override fun setup(program: Program) { + val width = program.application.configuration.width + val height = program.application.configuration.height + program.width = width + program.height = height + videoWriter = + VideoWriter().apply { + frameRate = 60 + size(width, height) + output("video/${program.name}.mp4") + start() + } + videoTarget = + renderTarget(width, height) { + colorBuffer() + depthBuffer() + } + maxFrameCount = recordDuration() + program.clock = { program.frameCount / (videoWriter?.frameRate?.toDouble() ?: 0.0) } + } + + override fun beforeDraw(drawer: Drawer, program: Program) { + videoTarget?.bind() + program.backgroundColor?.let { drawer.clear(it) } + } + + override fun afterDraw(drawer: Drawer, program: Program) { + val target = videoTarget ?: return + val writer = videoWriter ?: return + + target.unbind() + writer.frame(target.colorBuffer(0)) + + if (program.frameCount >= (maxFrameCount ?: 60 * 10)) { + writer.stop() + program.application.exit() + } + } +} diff --git a/src/main/kotlin/phenax/utils/program.kt b/src/main/kotlin/phenax/utils/program.kt new file mode 100644 index 0000000..2d3e04b --- /dev/null +++ b/src/main/kotlin/phenax/utils/program.kt @@ -0,0 +1,9 @@ +package phenax.utils + +fun recordDuration(durationSeconds: Int) { + System.setProperty("phenax.record.frameCount", (60 * durationSeconds).toString()) +} + +fun recordDuration(): Int? = System.getProperty("phenax.record.frameCount")?.toIntOrNull() + +fun recordingEnabled(): Boolean = System.getenv("RECORD") != null diff --git a/src/main/kotlin/play/PlayShowProgram.kt b/src/main/kotlin/play/PlayShowProgram.kt new file mode 100644 index 0000000..016378f --- /dev/null +++ b/src/main/kotlin/play/PlayShowProgram.kt @@ -0,0 +1,42 @@ +package play + +import kotlin.math.cos +import kotlin.math.sin +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.draw.loadFont +import org.openrndr.draw.loadImage +import org.openrndr.extra.color.colormatrix.tint +import org.openrndr.extra.olive.oliveProgram +import phenax.utils.* + +fun main() = application { + recordDuration(10) + + configure { + width = 640 + height = 480 + windowResizable = false + } + + oliveProgram { + val image = loadImage("data/images/pm5544.png") + val font = loadFont("data/fonts/default.otf", 64.0) + + extend { + drawer.drawStyle.colorMatrix = tint(ColorRGBa.WHITE.shade(0.2)) + drawer.image(image) + + drawer.fill = ColorRGBa.PINK + drawer.circle( + cos(seconds) * width / 2.0 + width / 2.0, + sin(0.5 * seconds) * height / 2.0 + height / 2.0, + 140.0 + ) + + drawer.fontMap = font + drawer.fill = ColorRGBa.WHITE + drawer.text("OPENRNDR", width / 2.0, height / 2.0) + } + } +} -- cgit v1.3.1