Skip to content

Commit

Permalink
Merge pull request #55 from Over-Run/glfw-fix
Browse files Browse the repository at this point in the history
[GLFW] Fix JVM crash of error callback
  • Loading branch information
squid233 committed Jul 31, 2024
2 parents 9277e69 + 176dcd4 commit 7440e5e
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 93 deletions.
73 changes: 40 additions & 33 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
/.idea/workspace.xml
/.idea/usage.statistics.xml
/nbproject/private/
/bin
/wiki
.DS_Store
*.bat
*.conf
*.dll
*.dylib
*.jar
*.jpg
*.mhr
*.obj
*.ogg
*.sh
*.so
*.ttf
*.wav
*.zip
touch.txt
.run/
run/

.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

# Ignore Gradle GUI config
gradle-app.setting
### IntelliJ IDEA ###
.idea/
#.idea/modules.xml
#.idea/jarRepositories.xml
#.idea/compiler.xml
#.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar
!gradlew.bat
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

# Cache of project
.gradletasknamecache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
# gradle/wrapper/gradle-wrapper.properties
### VS Code ###
.vscode/

.idea/
out/
### Mac OS ###
.DS_Store

run/
natives/
.kotlin/
4 changes: 3 additions & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
plugins { `kotlin-dsl` }
plugins {
kotlin("jvm") version "2.0.0"
}

repositories { mavenCentral() }
12 changes: 4 additions & 8 deletions buildSrc/src/main/kotlin/myproject.java-conventions.gradle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,17 @@ enum class NativePlatform(
WIN_64("windows", "x64", classifier = "windows", nativeLibPrefix = "", nativeLibSuffix = ".dll"),
WIN_ARM64("windows", "arm64", nativeLibPrefix = "", nativeLibSuffix = ".dll");

companion object {
val ALL = values()
}

val classifier = "natives-$classifier"
}

enum class NativeBinding(
val bindingName: String,
val basename: String,
vararg val platforms: NativePlatform
val platforms: List<NativePlatform>
) {
GLFW("glfw", "glfw", *NativePlatform.ALL),
NFD("nfd", "nfd", *NativePlatform.ALL),
STB("stb", "stb", *NativePlatform.ALL)
GLFW("glfw", "glfw", NativePlatform.entries),
NFD("nfd", "nfd", NativePlatform.entries),
STB("stb", "stb", NativePlatform.entries)
}

enum class Artifact(
Expand Down
5 changes: 3 additions & 2 deletions generators/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

/*
Expand All @@ -16,7 +17,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
* copies or substantial portions of the Software.
*/

plugins { embeddedKotlin("jvm") }
plugins { kotlin("jvm") version "2.0.0" }

allprojects {
apply(plugin = "org.jetbrains.kotlin.jvm")
Expand All @@ -28,7 +29,7 @@ allprojects {
repositories { mavenCentral() }

tasks.withType<KotlinCompile> {
kotlinOptions { jvmTarget = kotlinTargetJdkVersion }
compilerOptions { jvmTarget.set(JvmTarget.fromTarget(kotlinTargetJdkVersion)) }
}

tasks.withType<JavaCompile> {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ jdkEnablePreview=true
#jdkEarlyAccessDoc=jdk22
kotlinTargetJdkVersion=21

overrunMarshalVersion=0.1.0-alpha.26-jdk22
overrunMarshalVersion=0.1.0-alpha.28-jdk22
overrunPlatformVersion=1.0.0
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
7 changes: 5 additions & 2 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
Expand Down Expand Up @@ -55,7 +57,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down Expand Up @@ -84,7 +86,8 @@ done
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down
2 changes: 2 additions & 0 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down
6 changes: 3 additions & 3 deletions modules/overrungl.glfw/src/main/java/overrungl/glfw/GLFW.java
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ default Tuple2.OfObjInt<String> getError() {
*/
@Skip
default MemorySegment setErrorCallback(@Nullable GLFWErrorFun callback) {
return nsetErrorCallback(callback != null ? callback.stub(Arena.ofAuto()) : MemorySegment.NULL);
return nsetErrorCallback(callback != null ? callback.stub(Arena.global()) : MemorySegment.NULL);
}

/**
Expand Down Expand Up @@ -1907,7 +1907,7 @@ default Pair.OfFloat getMonitorContentScale(MemorySegment monitor) {
*/
@Skip
default MemorySegment setMonitorCallback(@Nullable GLFWMonitorFun callback) {
return nsetMonitorCallback(callback != null ? callback.stub(Arena.ofAuto()) : MemorySegment.NULL);
return nsetMonitorCallback(callback != null ? callback.stub(Arena.global()) : MemorySegment.NULL);
}

/**
Expand Down Expand Up @@ -4944,7 +4944,7 @@ default byte[] getJoystickHats(int jid) {
*/
@Skip
default MemorySegment setJoystickCallback(@Nullable GLFWJoystickFun callback) {
return nsetJoystickCallback(callback != null ? callback.stub(Arena.ofAuto()) : MemorySegment.NULL);
return nsetJoystickCallback(callback != null ? callback.stub(Arena.global()) : MemorySegment.NULL);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,31 @@ private void init() {
}

private void loop() {
var states = new GLFWGamepadState[GLFW.JOYSTICK_LAST + 1];
for (int i = 0; i < states.length; i++) {
states[i] = GLFWGamepadState.OF.of(Arena.ofAuto());
}
while (!glfw.windowShouldClose(window)) {
for (int i = 0; i <= GLFW.JOYSTICK_LAST; i++) {
if (glfw.joystickPresent(i)) {
if (glfw.joystickIsGamepad(i)) {
var state = states[i];
if (glfw.getGamepadState(i, state)) {
System.out.println(STR."""
Get gamepad state for [jid=\{i},name=\{glfw.getGamepadName(i)}] successful:
Buttons: [A(Cross)=\{state.button(GLFW.GAMEPAD_BUTTON_A)}, B(Circle)=\{state.button(GLFW.GAMEPAD_BUTTON_B)}, X(Square)=\{state.button(GLFW.GAMEPAD_BUTTON_X)}, Y(Triangle)=\{state.button(GLFW.GAMEPAD_BUTTON_Y)},
Left bumper=\{state.button(GLFW.GAMEPAD_BUTTON_LEFT_BUMPER)}, Right bumper=\{state.button(GLFW.GAMEPAD_BUTTON_RIGHT_BUMPER)}, Back=\{state.button(GLFW.GAMEPAD_BUTTON_BACK)}, Start=\{state.button(GLFW.GAMEPAD_BUTTON_START)},
Guide=\{state.button(GLFW.GAMEPAD_BUTTON_GUIDE)}, Left thumb=\{state.button(GLFW.GAMEPAD_BUTTON_LEFT_THUMB)}, Right thumb=\{state.button(GLFW.GAMEPAD_BUTTON_RIGHT_THUMB)},
DPAD(up=\{state.button(GLFW.GAMEPAD_BUTTON_DPAD_UP)}, right=\{state.button(GLFW.GAMEPAD_BUTTON_DPAD_RIGHT)}, down=\{state.button(GLFW.GAMEPAD_BUTTON_DPAD_DOWN)}, left=\{state.button(GLFW.GAMEPAD_BUTTON_DPAD_LEFT)})],
Axis: [Left(x=\{state.axe(GLFW.GAMEPAD_AXIS_LEFT_X)}, y=\{state.axe(GLFW.GAMEPAD_AXIS_LEFT_Y)}), Right(x=\{state.axe(GLFW.GAMEPAD_AXIS_RIGHT_X)}, y=\{state.axe(GLFW.GAMEPAD_AXIS_RIGHT_Y)}), Trigger(left=\{state.axe(GLFW.GAMEPAD_AXIS_LEFT_TRIGGER)}, right\{state.axe(GLFW.GAMEPAD_AXIS_RIGHT_TRIGGER)})]
""");
try (Arena arena = Arena.ofConfined()) {
var states = new GLFWGamepadState[GLFW.JOYSTICK_LAST + 1];
for (int i = 0; i < states.length; i++) {
states[i] = GLFWGamepadState.OF.of(arena);
}
while (!glfw.windowShouldClose(window)) {
for (int i = 0; i <= GLFW.JOYSTICK_LAST; i++) {
if (glfw.joystickPresent(i)) {
if (glfw.joystickIsGamepad(i)) {
var state = states[i];
if (glfw.getGamepadState(i, state)) {
System.out.println(STR."""
Get gamepad state for [jid=\{i},name=\{glfw.getGamepadName(i)}] successful:
Buttons: [A(Cross)=\{state.button(GLFW.GAMEPAD_BUTTON_A)}, B(Circle)=\{state.button(GLFW.GAMEPAD_BUTTON_B)}, X(Square)=\{state.button(GLFW.GAMEPAD_BUTTON_X)}, Y(Triangle)=\{state.button(GLFW.GAMEPAD_BUTTON_Y)},
Left bumper=\{state.button(GLFW.GAMEPAD_BUTTON_LEFT_BUMPER)}, Right bumper=\{state.button(GLFW.GAMEPAD_BUTTON_RIGHT_BUMPER)}, Back=\{state.button(GLFW.GAMEPAD_BUTTON_BACK)}, Start=\{state.button(GLFW.GAMEPAD_BUTTON_START)},
Guide=\{state.button(GLFW.GAMEPAD_BUTTON_GUIDE)}, Left thumb=\{state.button(GLFW.GAMEPAD_BUTTON_LEFT_THUMB)}, Right thumb=\{state.button(GLFW.GAMEPAD_BUTTON_RIGHT_THUMB)},
DPAD(up=\{state.button(GLFW.GAMEPAD_BUTTON_DPAD_UP)}, right=\{state.button(GLFW.GAMEPAD_BUTTON_DPAD_RIGHT)}, down=\{state.button(GLFW.GAMEPAD_BUTTON_DPAD_DOWN)}, left=\{state.button(GLFW.GAMEPAD_BUTTON_DPAD_LEFT)})],
Axis: [Left(x=\{state.axe(GLFW.GAMEPAD_AXIS_LEFT_X)}, y=\{state.axe(GLFW.GAMEPAD_AXIS_LEFT_Y)}), Right(x=\{state.axe(GLFW.GAMEPAD_AXIS_RIGHT_X)}, y=\{state.axe(GLFW.GAMEPAD_AXIS_RIGHT_Y)}), Trigger(left=\{state.axe(GLFW.GAMEPAD_AXIS_LEFT_TRIGGER)}, right\{state.axe(GLFW.GAMEPAD_AXIS_RIGHT_TRIGGER)})]
""");
}
}
}
}
glfw.waitEventsTimeout(3);
}
glfw.waitEventsTimeout(3);
}
}

Expand Down
48 changes: 25 additions & 23 deletions modules/samples/src/main/java/overrungl/demo/opengl/GL33Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,38 +222,40 @@ void main() {
}

private void loop() {
var matrix = new Matrix4f();
var pRotationMat = Matrixn.allocate(Arena.ofAuto(), matrix);
try (var arena = Arena.ofConfined()) {
var matrix = new Matrix4f();
var pRotationMat = Matrixn.allocate(arena, matrix);

var timer = Timer.ofGetter(20, glfw::getTime);
var timer = Timer.ofGetter(20, glfw::getTime);

while (!glfw.windowShouldClose(window)) {
timer.advanceTime();
timer.performTicks(null);
while (!glfw.windowShouldClose(window)) {
timer.advanceTime();
timer.performTicks(null);

gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);
gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);

// Draw triangle
gl.useProgram(program);
// Draw triangle
gl.useProgram(program);

// rotate 90deg per second
matrix.rotateZ((float) Math.toRadians(90 * timer.deltaTime()));
Matrixn.put(matrix, pRotationMat);
// rotate 90deg per second
matrix.rotateZ((float) Math.toRadians(90 * timer.deltaTime()));
Matrixn.put(matrix, pRotationMat);

gl.uniformMatrix4fv(rotationMat, 1, false, pRotationMat);
gl.bindVertexArray(vao);
gl.drawElementsInstanced(GL.TRIANGLES, 6, GL.UNSIGNED_BYTE, MemorySegment.NULL, INSTANCE_COUNT);
gl.bindVertexArray(0);
gl.useProgram(0);
gl.uniformMatrix4fv(rotationMat, 1, false, pRotationMat);
gl.bindVertexArray(vao);
gl.drawElementsInstanced(GL.TRIANGLES, 6, GL.UNSIGNED_BYTE, MemorySegment.NULL, INSTANCE_COUNT);
gl.bindVertexArray(0);
gl.useProgram(0);

glfw.swapBuffers(window);
glfw.swapBuffers(window);

glfw.pollEvents();
glfw.pollEvents();

// using lambda gets higher FPS ??
timer.calcFPS(fps -> {
glfw.setWindowTitle(window, STR."\{WND_TITLE} FPS: \{fps}");
});
// using lambda gets higher FPS ??
timer.calcFPS(fps -> {
glfw.setWindowTitle(window, STR."\{WND_TITLE} FPS: \{fps}");
});
}
}
}

Expand Down

0 comments on commit 7440e5e

Please sign in to comment.