Skip to content

Commit

Permalink
Full refactoring, LWJGL support
Browse files Browse the repository at this point in the history
  • Loading branch information
husker-dev committed Dec 17, 2021
1 parent a49078a commit a8a1a36
Show file tree
Hide file tree
Showing 36 changed files with 1,095 additions and 335 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/modules/jogl/openglfx.jogl.test.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 21 additions & 27 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.5.21'
id 'org.jetbrains.kotlin.jvm' version '1.6.10'
}

group 'com.husker'
version '1.1'

repositories {
mavenCentral()
allprojects {
version = "2.0"
}

sourceSets {
main {
resources {
srcDirs "src/main/resources"
}
}
}
group 'com.husker.openglfx'

jar{
java {
exclude 'com/husker/test/**'
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'maven'

dependencies {
implementation fileTree("libs")
sourceCompatibility = 1.8
targetCompatibility = 1.8

implementation "org.jetbrains.kotlin:kotlin-stdlib"
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}

implementation "org.openjfx:javafx-base:16:win"
implementation "org.openjfx:javafx-controls:16:win"
implementation "org.openjfx:javafx-graphics:16:win"
implementation "org.openjfx:javafx-swing:16:win"
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}

implementation 'org.jogamp.jogl:jogl-all-main:2.3.2'
implementation 'org.jogamp.gluegen:gluegen-rt-main:2.3.2'
artifacts {
archives sourcesJar
archives javadocJar
}
}
19 changes: 19 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
}

group 'com.husker'
version '2.0'

repositories {
mavenCentral()
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"

implementation "org.openjfx:javafx-base:16:win"
implementation "org.openjfx:javafx-controls:16:win"
implementation "org.openjfx:javafx-graphics:16:win"
implementation "org.openjfx:javafx-swing:16:win"
}
10 changes: 10 additions & 0 deletions core/src/main/kotlin/com/husker/openglfx/FXGLEventListener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.husker.openglfx


interface FXGLEventListener {

fun display()
fun reshape(width: Float, height: Float)
fun init()
fun dispose()
}
11 changes: 11 additions & 0 deletions core/src/main/kotlin/com/husker/openglfx/FXGLInitializer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.husker.openglfx

abstract class FXGLInitializer {

abstract val name: String
abstract val supportsDirect: Boolean
abstract val supportsUniversal: Boolean

abstract fun createDirect(): OpenGLCanvas
abstract fun createUniversal(): OpenGLCanvas
}
135 changes: 135 additions & 0 deletions core/src/main/kotlin/com/husker/openglfx/OpenGLCanvas.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package com.husker.openglfx


import com.husker.openglfx.utils.FXUtils
import com.husker.openglfx.utils.RegionAccessorObject
import com.husker.openglfx.utils.RegionAccessorOverrider

import com.sun.javafx.sg.prism.NGRegion
import com.sun.prism.Graphics
import javafx.scene.layout.Pane

enum class DirectDrawPolicy {
NEVER,
IF_AVAILABLE,
ALWAYS
}

abstract class OpenGLCanvas: Pane() {

companion object{
init{
RegionAccessorOverrider.overwrite(object: RegionAccessorObject<OpenGLCanvas>(){
override fun doCreatePeer(node: OpenGLCanvas) = NGOpenGLCanvas(node)
})
}

@JvmOverloads
@JvmStatic
fun create(
initializer: FXGLInitializer,
directDrawPolicy: DirectDrawPolicy = DirectDrawPolicy.NEVER
): OpenGLCanvas {
val isES2 = FXUtils.pipelineName == "es2"
return when(directDrawPolicy) {
DirectDrawPolicy.NEVER -> {
if(!initializer.supportsUniversal)
throw UnsupportedOperationException("${initializer.name} doesn't support universal rendering")
initializer.createUniversal()
}
DirectDrawPolicy.ALWAYS -> {
if(!initializer.supportsDirect)
throw UnsupportedOperationException("${initializer.name} doesn't support direct rendering")
if(!isES2)
throw UnsupportedOperationException("Direct rendering only supports ES2 JavaFX pipeline (current: ${FXUtils.pipelineName})")
initializer.createDirect()
}
DirectDrawPolicy.IF_AVAILABLE -> {
if(initializer.supportsDirect && isES2) initializer.createDirect()
else initializer.createUniversal()
}
}
}
}

private var onInit: Runnable? = null
private var onRender: Runnable? = null
private var onReshape: Runnable? = null
private var onDispose: Runnable? = null

private var initialized = false

protected val dpi: Double
get() = scene.window.outputScaleX

protected val scaledWidth: Double
get() = width * dpi

protected val scaledHeight: Double
get() = height * dpi

fun onNGRender(listener: Runnable){
onRender = listener
}

fun onReshape(listener: Runnable){
onReshape = listener
}

fun onInitialize(listener: Runnable){
onInit = listener
initialized = false
}

fun onDispose(listener: Runnable){
onDispose = listener
}

protected abstract fun onNGRender(g: Graphics)
protected abstract fun repaint()

protected open fun fireRenderEvent() {
checkInitialization()
onRender?.run()
}

protected open fun fireReshapeEvent() {
checkInitialization()
onReshape?.run()
}

protected open fun fireInitEvent() = checkInitialization()

protected open fun fireDisposeEvent() {
checkInitialization()
onDispose?.run()
}

private fun checkInitialization(){
if(!initialized){
initialized = true
onInit?.run()
}
}

private class NGOpenGLCanvas(val canvas: OpenGLCanvas): NGRegion() {

init{
/*
NodeUtils.onWindowReady(canvas){
object: AnimationTimer(){
override fun handle(p: Long) {
//NodeHelper.markDirty(canvas, DirtyBits.NODE_GEOMETRY)
}
}.start()
}
*/
}

override fun renderContent(g: Graphics) {
canvas.onNGRender(g)
super.renderContent(g)
}
}
}
26 changes: 26 additions & 0 deletions core/src/main/kotlin/com/husker/openglfx/utils/FXUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.husker.openglfx.utils


import com.sun.prism.GraphicsPipeline
import javafx.scene.Node
import javafx.scene.Scene

class FXUtils {
companion object {

val pipelineName: String
get() = GraphicsPipeline.getPipeline().javaClass.canonicalName.split(".")[3]

fun onWindowReady(node: Node, listener: () -> Unit){
if(node.scene != null)
processScene(node.scene, listener)
else node.sceneProperty().addListener { _, _, _ -> processScene(node.scene, listener) }
}

private fun processScene(scene: Scene, listener: () -> Unit){
if(scene.window != null)
listener.invoke()
else scene.windowProperty().addListener { _, _, _ -> listener.invoke() }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.husker.openglfx.utils

import kotlin.concurrent.thread

class LifetimeLoopThread(private val lifetime: Long, private var runnable: Runnable) {

private var threadInstance = Thread()
Expand All @@ -8,13 +10,12 @@ class LifetimeLoopThread(private val lifetime: Long, private var runnable: Runna
fun startRequest(){
startTime = System.currentTimeMillis()
if(!threadInstance.isAlive){
threadInstance = Thread{
threadInstance = thread(isDaemon = true){
while(System.currentTimeMillis() - startTime < lifetime){
Thread.sleep(1)
runnable.run()
}
}
threadInstance.start()
}
}
}
Loading

0 comments on commit a8a1a36

Please sign in to comment.