Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jetpack Compose example #172

Open
DevSrSouza opened this issue Nov 7, 2021 · 6 comments
Open

Jetpack Compose example #172

DevSrSouza opened this issue Nov 7, 2021 · 6 comments

Comments

@DevSrSouza
Copy link

DevSrSouza commented Nov 7, 2021

I saw that Compose-jb now was an experimental example for LWJGL/GLFW using ComposeScene.

Is possible, with this new API from Compose, be able to use with JWM? If so, would be awesome to see a code sample.

@DevSrSouza DevSrSouza changed the title Jetpack Compose sample Jetpack Compose example Nov 7, 2021
@tonsky
Copy link
Collaborator

tonsky commented Nov 8, 2021

Should be possible, yes. Do you want to make a PR with an example?

@DevSrSouza
Copy link
Author

DevSrSouza commented Nov 8, 2021

I don't know JWM enough to do that, I think.

@tonsky
Copy link
Collaborator

tonsky commented Nov 8, 2021

Great places to start is https://github.com/HumbleUI/JWM/blob/main/docs/Getting%20Started.md and https://github.com/HumbleUI/JWM/blob/main/examples/dashboard/java/Example.java. Let me know if you have any questions. I’d do it myself but I am not sure how long it might be delayed because of conflicting priorities

@DevSrSouza
Copy link
Author

cc @smallshen

@GavinRay97
Copy link

GavinRay97 commented Jan 22, 2022

Had a look at that example l, am pretty sure you could integrate it easily.

You need access to just 2 things for this to work:

  • OpenGL Surface
  • Window handle

Here is what it should look like:
(You would need to sort out the remaining glfw() calls because JWM manages these internally I believe)

fun main() {
    var width = 640
    var height = 480

    // Initialize JWM OpenGL
    // Then:
    var surface = // get JWM GL Surface
    val windowHandle = //get JWM GL Window Handle

    val glfwDispatcher = GlfwCoroutineDispatcher() // a custom coroutine dispatcher, in which Compose will run
    glfwSetWindowCloseCallback(windowHandle) { glfwDispatcher.stop() }

    lateinit var composeScene: ComposeScene

    fun render() {
       // You might need to change this call, not sure what the proper code is to get the canvas from the surface
        surface.canvas.clear(Color.WHITE)
        composeScene.constraints = Constraints(maxWidth = width, maxHeight = height)
        composeScene.render(surface.canvas, System.nanoTime())

        context.flush()
        glfwSwapBuffers(windowHandle)
    }

    val frameDispatcher = FrameDispatcher(glfwDispatcher) { render() }

    val density = Density(glfwGetWindowContentScale(windowHandle))
    composeScene = ComposeScene(glfwDispatcher, density, invalidate = frameDispatcher::scheduleFrame)

    glfwSetWindowSizeCallback(windowHandle) { _, windowWidth, windowHeight ->
        width = windowWidth
        height = windowHeight
        surface.close()
        surface = createSurface(width, height, context)

        glfwSwapInterval(0)
        render()
        glfwSwapInterval(1)
    }

    composeScene.subscribeToGLFWEvents(windowHandle)
    composeScene.setContent { App() }
    glfwShowWindow(windowHandle)

    glfwDispatcher.runLoop()

    composeScene.close()
    glfwDestroyWindow(windowHandle)

    exitProcess(0)
}

@smallshen
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants