https://kotlinlang.org logo
#compose-desktop
Title
# compose-desktop
c

Christopher Porto

07/10/2022, 3:35 AM
Is it possible to use LWJGL 3 AWT within Compose? My current attempt results in
No context is current or a function that is not available in the current context was called. The JVM will abort execution.
I'm sure this is due to some threading done underneath and was wondering if there was a way around it. I'll post code in thread. I'm not tied to using LWJGL or AWT, I simply wish to utilize OpenGL within Compose 🙂
Copy code
@Composable
@Preview
fun App() {
    MaterialTheme {
        Column(Modifier.fillMaxSize(), Arrangement.spacedBy(5.dp)) {
            Button(onClick = {}) {
                Text("Hello")
            }
            LwjglExample()
        }
    }
}

@Composable
@Preview
fun LwjglExample() {
    SwingPanel(
        background = Color.Black,
        modifier = Modifier.size(200.dp, 200.dp),
        factory = {
            val data = GLData().apply {
                majorVersion = 3
                minorVersion = 3
                profile = GLData.Profile.CORE
                samples = 4
            }
            object : AWTGLCanvas(data) {
                override fun initGL() {
                    createCapabilities()
                    glClearColor(0.3f, 0.4f, 0.5f, 1F)
                }

                override fun paintGL() {
                    val w = 200
                    val h = 200 // TODO: Remove magic numbers
                    val aspect = w.toFloat() / h
                    val now = System.currentTimeMillis() * 0.001
                    val width = abs(sin(now * 0.3)).toFloat()
                    glClear(GL_COLOR_BUFFER_BIT)
                    glViewport(0, 0, w, h)
                    glBegin(GL_QUADS)
                    glColor3f(0.4f, 0.6f, 0.8f)
                    glVertex2f(-0.75f * width / aspect, 0.0f)
                    glVertex2f(0f, -0.75f)
                    glVertex2f(+0.75f * width / aspect, 0f)
                    glVertex2f(0f, +0.75f)
                    glEnd()
                    swapBuffers()
                }
            }
        },
        update = {
            val renderLoop: Runnable = object : Runnable {
                override fun run() {
                    if (!it.isValid) return
                    it.render()
                    SwingUtilities.invokeLater(this)
                }
            }
            SwingUtilities.invokeLater(renderLoop)
        }
    )
}
m

Michael Paus

07/10/2022, 7:05 AM
👀 1
c

Christopher Porto

07/10/2022, 11:45 AM
Hmm that's integrating Compose into LWJGL, kind of wanted the opposite, to integrate LWGJL into Compose if that makes sense, that way I could utilize the layout advantages of Compose instead of manually handling that myself. If there's not another option though I'll go with it, thanks :]
85 Views